blob: 221a2b245690504a291034e4974eb496a5942f93 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
13my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080014my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070015
Joe Perches000d1cc12011-07-25 17:13:25 -070016my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $quiet = 0;
21my $tree = 1;
22my $chk_signoff = 1;
23my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070024my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $file = 0;
28my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070029my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $summary = 1;
31my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080032my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070033my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070034my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080035my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Joe Perches34456862013-07-03 15:05:34 -070038my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070039my %use_type = ();
40my @use = ();
41my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070042my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070043my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080045my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070046my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070048my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070049my $spelling_file = "$D/spelling.txt";
Hannes Eder77f5b102009-09-21 17:04:37 -070050
51sub help {
52 my ($exitcode) = @_;
53
54 print << "EOM";
55Usage: $P [OPTION]... [FILE]...
56Version: $V
57
58Options:
59 -q, --quiet quiet
60 --no-tree run without a kernel tree
61 --no-signoff do not check for 'Signed-off-by' line
62 --patch treat FILE as patchfile (default)
63 --emacs emacs compile window format
64 --terse one line per report
65 -f, --file treat FILE as regular source file
66 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070067 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070068 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080069 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070070 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070071 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070072 --root=PATH PATH to the kernel tree root
73 --no-summary suppress the per-file summary
74 --mailback only produce a report in case of warnings/errors
75 --summary-file include the filename in summary
76 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
77 'values', 'possible', 'type', and 'attr' (default
78 is all off)
79 --test-only=WORD report only warnings/errors containing WORD
80 literally
Joe Perches3705ce52013-07-03 15:05:31 -070081 --fix EXPERIMENTAL - may create horrible results
82 If correctable single-line errors exist, create
83 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
84 with potential errors corrected to the preferred
85 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080086 --fix-inplace EXPERIMENTAL - may create horrible results
87 Is the same as --fix, but overwrites the input
88 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070089 --ignore-perl-version override checking of perl version. expect
90 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070091 -h, --help, --version display this help and exit
92
93When FILE is - read standard input.
94EOM
95
96 exit($exitcode);
97}
98
Joe Perches000d1cc12011-07-25 17:13:25 -070099my $conf = which_conf($configuration_file);
100if (-f $conf) {
101 my @conf_args;
102 open(my $conffile, '<', "$conf")
103 or warn "$P: Can't find a readable $configuration_file file $!\n";
104
105 while (<$conffile>) {
106 my $line = $_;
107
108 $line =~ s/\s*\n?$//g;
109 $line =~ s/^\s*//g;
110 $line =~ s/\s+/ /g;
111
112 next if ($line =~ m/^\s*#/);
113 next if ($line =~ m/^\s*$/);
114
115 my @words = split(" ", $line);
116 foreach my $word (@words) {
117 last if ($word =~ m/^#/);
118 push (@conf_args, $word);
119 }
120 }
121 close($conffile);
122 unshift(@ARGV, @conf_args) if @conf_args;
123}
124
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700125GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700126 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700127 'tree!' => \$tree,
128 'signoff!' => \$chk_signoff,
129 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700130 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800131 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700132 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700133 'subjective!' => \$check,
134 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700135 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700136 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700137 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800138 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700139 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700140 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800141 'summary!' => \$summary,
142 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800143 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700144 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800145 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700146 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800147 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700148 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700149 'h|help' => \$help,
150 'version' => \$help
151) or help(1);
152
153help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700154
Joe Perches9624b8d2014-01-23 15:54:44 -0800155$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700156$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800157
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700158my $exit = 0;
159
Dave Hansend62a2012013-09-11 14:23:56 -0700160if ($^V && $^V lt $minimum_perl_version) {
161 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
162 if (!$ignore_perl_version) {
163 exit(1);
164 }
165}
166
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700167if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700168 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700169 exit(1);
170}
171
Joe Perches91bfe482013-09-11 14:23:59 -0700172sub hash_save_array_words {
173 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700174
Joe Perches91bfe482013-09-11 14:23:59 -0700175 my @array = split(/,/, join(',', @$arrayRef));
176 foreach my $word (@array) {
177 $word =~ s/\s*\n?$//g;
178 $word =~ s/^\s*//g;
179 $word =~ s/\s+/ /g;
180 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700181
Joe Perches91bfe482013-09-11 14:23:59 -0700182 next if ($word =~ m/^\s*#/);
183 next if ($word =~ m/^\s*$/);
184
185 $hashRef->{$word}++;
186 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700187}
188
Joe Perches91bfe482013-09-11 14:23:59 -0700189sub hash_show_words {
190 my ($hashRef, $prefix) = @_;
191
Joe Perches58cb3cf2013-09-11 14:24:04 -0700192 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700193 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700194 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700195 print " $word";
196 }
197 print "\n\n";
198 }
199}
200
201hash_save_array_words(\%ignore_type, \@ignore);
202hash_save_array_words(\%use_type, \@use);
203
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800204my $dbg_values = 0;
205my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700206my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700207my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800208for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800209 ## no critic
210 eval "\${dbg_$key} = '$debug{$key}';";
211 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800212}
213
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700214my $rpt_cleaners = 0;
215
Andy Whitcroft8905a672007-11-28 16:21:06 -0800216if ($terse) {
217 $emacs = 1;
218 $quiet++;
219}
220
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700221if ($tree) {
222 if (defined $root) {
223 if (!top_of_kernel_tree($root)) {
224 die "$P: $root: --root does not point at a valid tree\n";
225 }
226 } else {
227 if (top_of_kernel_tree('.')) {
228 $root = '.';
229 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
230 top_of_kernel_tree($1)) {
231 $root = $1;
232 }
233 }
234
235 if (!defined $root) {
236 print "Must be run from the top-level dir. of a kernel tree\n";
237 exit(2);
238 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700239}
240
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700241my $emitted_corrupt = 0;
242
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700243our $Ident = qr{
244 [A-Za-z_][A-Za-z\d_]*
245 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
246 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700247our $Storage = qr{extern|static|asmlinkage};
248our $Sparse = qr{
249 __user|
250 __kernel|
251 __force|
252 __iomem|
253 __must_check|
254 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800255 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700256 __ref|
257 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700258 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800259our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
260our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
261our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
262our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
263our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700264
Wolfram Sang52131292010-03-05 13:43:51 -0800265# Notes to $Attribute:
266# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700267our $Attribute = qr{
268 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700269 __percpu|
270 __nocast|
271 __safe|
272 __bitwise__|
273 __packed__|
274 __packed2__|
275 __naked|
276 __maybe_unused|
277 __always_unused|
278 __noreturn|
279 __used|
280 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800281 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700282 __noclone|
283 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700284 __read_mostly|
285 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700286 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700287 ____cacheline_aligned|
288 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800289 ____cacheline_internodealigned_in_smp|
290 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700291 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700292our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700293our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
295our $Lval = qr{$Ident(?:$Member)*};
296
Joe Perches95e2c602013-07-03 15:05:20 -0700297our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
298our $Binary = qr{(?i)0b[01]+$Int_type?};
299our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
300our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700301our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800302our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800303our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
304our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
305our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800306our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700307our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800308our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700309our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700310our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700311our $Operators = qr{
312 <=|>=|==|!=|
313 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700314 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700315 }x;
316
Joe Perches91cb5192014-04-03 14:49:32 -0700317our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
318
Andy Whitcroft8905a672007-11-28 16:21:06 -0800319our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700320our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700321our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800322our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700323our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800324our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700325our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800326
Joe Perches15662b32011-10-31 17:13:12 -0700327our $NON_ASCII_UTF8 = qr{
328 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700329 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
330 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
331 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
332 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
333 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
334 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
335}x;
336
Joe Perches15662b32011-10-31 17:13:12 -0700337our $UTF8 = qr{
338 [\x09\x0A\x0D\x20-\x7E] # ASCII
339 | $NON_ASCII_UTF8
340}x;
341
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700342our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700343 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700344 atomic_t
345)};
346
Joe Perches691e6692010-03-05 13:43:51 -0800347our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700348 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700349 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700350 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700351 panic|
Joe Perches06668722013-11-12 15:10:07 -0800352 MODULE_[A-Z_]+|
353 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800354)};
355
Joe Perches20112472011-07-25 17:13:23 -0700356our $signature_tags = qr{(?xi:
357 Signed-off-by:|
358 Acked-by:|
359 Tested-by:|
360 Reviewed-by:|
361 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700362 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700363 To:|
364 Cc:
365)};
366
Joe Perches18130872014-08-06 16:11:22 -0700367our @typeListMisordered = (
368 qr{char\s+(?:un)?signed},
369 qr{int\s+(?:(?:un)?signed\s+)?short\s},
370 qr{int\s+short(?:\s+(?:un)?signed)},
371 qr{short\s+int(?:\s+(?:un)?signed)},
372 qr{(?:un)?signed\s+int\s+short},
373 qr{short\s+(?:un)?signed},
374 qr{long\s+int\s+(?:un)?signed},
375 qr{int\s+long\s+(?:un)?signed},
376 qr{long\s+(?:un)?signed\s+int},
377 qr{int\s+(?:un)?signed\s+long},
378 qr{int\s+(?:un)?signed},
379 qr{int\s+long\s+long\s+(?:un)?signed},
380 qr{long\s+long\s+int\s+(?:un)?signed},
381 qr{long\s+long\s+(?:un)?signed\s+int},
382 qr{long\s+long\s+(?:un)?signed},
383 qr{long\s+(?:un)?signed},
384);
385
Andy Whitcroft8905a672007-11-28 16:21:06 -0800386our @typeList = (
387 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700388 qr{(?:(?:un)?signed\s+)?char},
389 qr{(?:(?:un)?signed\s+)?short\s+int},
390 qr{(?:(?:un)?signed\s+)?short},
391 qr{(?:(?:un)?signed\s+)?int},
392 qr{(?:(?:un)?signed\s+)?long\s+int},
393 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
394 qr{(?:(?:un)?signed\s+)?long\s+long},
395 qr{(?:(?:un)?signed\s+)?long},
396 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800397 qr{float},
398 qr{double},
399 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800400 qr{struct\s+$Ident},
401 qr{union\s+$Ident},
402 qr{enum\s+$Ident},
403 qr{${Ident}_t},
404 qr{${Ident}_handler},
405 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700406 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800407);
Joe Perches8716de32013-09-11 14:24:05 -0700408our @typeListWithAttr = (
409 @typeList,
410 qr{struct\s+$InitAttribute\s+$Ident},
411 qr{union\s+$InitAttribute\s+$Ident},
412);
413
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700414our @modifierList = (
415 qr{fastcall},
416);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800417
Joe Perches24358802014-04-03 14:49:13 -0700418our @mode_permission_funcs = (
419 ["module_param", 3],
420 ["module_param_(?:array|named|string)", 4],
421 ["module_param_array_named", 5],
422 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
423 ["proc_create(?:_data|)", 2],
424 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
425);
426
Joe Perches515a2352014-04-03 14:49:24 -0700427#Create a search pattern for all these functions to speed up a loop below
428our $mode_perms_search = "";
429foreach my $entry (@mode_permission_funcs) {
430 $mode_perms_search .= '|' if ($mode_perms_search ne "");
431 $mode_perms_search .= $entry->[0];
432}
433
Wolfram Sang7840a942010-08-09 17:20:57 -0700434our $allowed_asm_includes = qr{(?x:
435 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700436 memory|
437 time|
438 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700439)};
440# memory.h: ARM has a custom one
441
Kees Cook66b47b42014-10-13 15:51:57 -0700442# Load common spelling mistakes and build regular expression list.
443my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700444my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700445
Joe Perches36061e32014-12-10 15:51:43 -0800446if (open(my $spelling, '<', $spelling_file)) {
447 my @spelling_list;
448 while (<$spelling>) {
449 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700450
Joe Perches36061e32014-12-10 15:51:43 -0800451 $line =~ s/\s*\n?$//g;
452 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700453
Joe Perches36061e32014-12-10 15:51:43 -0800454 next if ($line =~ m/^\s*#/);
455 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700456
Joe Perches36061e32014-12-10 15:51:43 -0800457 my ($suspect, $fix) = split(/\|\|/, $line);
458
459 push(@spelling_list, $suspect);
460 $spelling_fix{$suspect} = $fix;
461 }
462 close($spelling);
463 $misspellings = join("|", @spelling_list);
464} else {
465 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700466}
Kees Cook66b47b42014-10-13 15:51:57 -0700467
Andy Whitcroft8905a672007-11-28 16:21:06 -0800468sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700469 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
470 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700471 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700472 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700473 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800474 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700475 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800476 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800477 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700478 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700479 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800480 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700481 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800482 }x;
Joe Perches18130872014-08-06 16:11:22 -0700483 $NonptrTypeMisordered = qr{
484 (?:$Modifier\s+|const\s+)*
485 (?:
486 (?:${Misordered}\b)
487 )
488 (?:\s+$Modifier|\s+const)*
489 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700490 $NonptrTypeWithAttr = qr{
491 (?:$Modifier\s+|const\s+)*
492 (?:
493 (?:typeof|__typeof__)\s*\([^\)]*\)|
494 (?:$typeTypedefs\b)|
495 (?:${allWithAttr}\b)
496 )
497 (?:\s+$Modifier|\s+const)*
498 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800499 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700500 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700501 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700502 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800503 }x;
Joe Perches18130872014-08-06 16:11:22 -0700504 $TypeMisordered = qr{
505 $NonptrTypeMisordered
506 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
507 (?:\s+$Inline|\s+$Modifier)*
508 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700509 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700510 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800511}
512build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700513
Joe Perches7d2367a2011-07-25 17:13:22 -0700514our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700515
516# Using $balanced_parens, $LvalOrFunc, or $FuncArg
517# requires at least perl version v5.10.0
518# Any use must be runtime checked with $^V
519
520our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700521our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800522our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700523
Joe Perchesf8422302014-08-06 16:11:31 -0700524our $declaration_macros = qr{(?x:
525 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
526 (?:$Storage\s+)?LIST_HEAD\s*\(|
527 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
528)};
529
Joe Perches7d2367a2011-07-25 17:13:22 -0700530sub deparenthesize {
531 my ($string) = @_;
532 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700533
534 while ($string =~ /^\s*\(.*\)\s*$/) {
535 $string =~ s@^\s*\(\s*@@;
536 $string =~ s@\s*\)\s*$@@;
537 }
538
Joe Perches7d2367a2011-07-25 17:13:22 -0700539 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700540
Joe Perches7d2367a2011-07-25 17:13:22 -0700541 return $string;
542}
543
Joe Perches34456862013-07-03 15:05:34 -0700544sub seed_camelcase_file {
545 my ($file) = @_;
546
547 return if (!(-f $file));
548
549 local $/;
550
551 open(my $include_file, '<', "$file")
552 or warn "$P: Can't read '$file' $!\n";
553 my $text = <$include_file>;
554 close($include_file);
555
556 my @lines = split('\n', $text);
557
558 foreach my $line (@lines) {
559 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
560 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
561 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800562 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
563 $camelcase{$1} = 1;
564 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700565 $camelcase{$1} = 1;
566 }
567 }
568}
569
570my $camelcase_seeded = 0;
571sub seed_camelcase_includes {
572 return if ($camelcase_seeded);
573
574 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700575 my $camelcase_cache = "";
576 my @include_files = ();
577
578 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700579
Richard Genoud3645e322014-02-10 14:25:32 -0800580 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700581 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
582 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700583 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700584 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700585 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700586 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700587 @include_files = split('\n', $files);
588 foreach my $file (@include_files) {
589 my $date = POSIX::strftime("%Y%m%d%H%M",
590 localtime((stat $file)[9]));
591 $last_mod_date = $date if ($last_mod_date < $date);
592 }
593 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700594 }
Joe Perchesc707a812013-07-08 16:00:43 -0700595
596 if ($camelcase_cache ne "" && -f $camelcase_cache) {
597 open(my $camelcase_file, '<', "$camelcase_cache")
598 or warn "$P: Can't read '$camelcase_cache' $!\n";
599 while (<$camelcase_file>) {
600 chomp;
601 $camelcase{$_} = 1;
602 }
603 close($camelcase_file);
604
605 return;
606 }
607
Richard Genoud3645e322014-02-10 14:25:32 -0800608 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700609 $files = `git ls-files "include/*.h"`;
610 @include_files = split('\n', $files);
611 }
612
Joe Perches34456862013-07-03 15:05:34 -0700613 foreach my $file (@include_files) {
614 seed_camelcase_file($file);
615 }
Joe Perches351b2a12013-07-03 15:05:36 -0700616
Joe Perchesc707a812013-07-08 16:00:43 -0700617 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700618 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700619 open(my $camelcase_file, '>', "$camelcase_cache")
620 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700621 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
622 print $camelcase_file ("$_\n");
623 }
624 close($camelcase_file);
625 }
Joe Perches34456862013-07-03 15:05:34 -0700626}
627
Joe Perchesd311cd42014-08-06 16:10:57 -0700628sub git_commit_info {
629 my ($commit, $id, $desc) = @_;
630
631 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
632
633 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
634 $output =~ s/^\s*//gm;
635 my @lines = split("\n", $output);
636
637 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
638# Maybe one day convert this block of bash into something that returns
639# all matching commit ids, but it's very slow...
640#
641# echo "checking commits $1..."
642# git rev-list --remotes | grep -i "^$1" |
643# while read line ; do
644# git log --format='%H %s' -1 $line |
645# echo "commit $(cut -c 1-12,41-)"
646# done
647 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
648 } else {
649 $id = substr($lines[0], 0, 12);
650 $desc = substr($lines[0], 41);
651 }
652
653 return ($id, $desc);
654}
655
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700656$chk_signoff = 0 if ($file);
657
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700658my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800659my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700660my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700661my @fixed_inserted = ();
662my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700663my $fixlinenr = -1;
664
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800665my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700666for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800667 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700668 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800669 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700670 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800671 } elsif ($filename eq '-') {
672 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700673 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800674 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700675 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700676 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800677 if ($filename eq '-') {
678 $vname = 'Your patch';
679 } else {
680 $vname = $filename;
681 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800682 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700683 chomp;
684 push(@rawlines, $_);
685 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800686 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800687 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700688 $exit = 1;
689 }
690 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800691 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700692 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700693 @fixed_inserted = ();
694 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700695 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700696}
697
698exit($exit);
699
700sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700701 my ($root) = @_;
702
703 my @tree_check = (
704 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
705 "README", "Documentation", "arch", "include", "drivers",
706 "fs", "init", "ipc", "kernel", "lib", "scripts",
707 );
708
709 foreach my $check (@tree_check) {
710 if (! -e $root . '/' . $check) {
711 return 0;
712 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700713 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700714 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700715}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700716
Joe Perches20112472011-07-25 17:13:23 -0700717sub parse_email {
718 my ($formatted_email) = @_;
719
720 my $name = "";
721 my $address = "";
722 my $comment = "";
723
724 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
725 $name = $1;
726 $address = $2;
727 $comment = $3 if defined $3;
728 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
729 $address = $1;
730 $comment = $2 if defined $2;
731 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
732 $address = $1;
733 $comment = $2 if defined $2;
734 $formatted_email =~ s/$address.*$//;
735 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700736 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700737 $name =~ s/^\"|\"$//g;
738 # If there's a name left after stripping spaces and
739 # leading quotes, and the address doesn't have both
740 # leading and trailing angle brackets, the address
741 # is invalid. ie:
742 # "joe smith joe@smith.com" bad
743 # "joe smith <joe@smith.com" bad
744 if ($name ne "" && $address !~ /^<[^>]+>$/) {
745 $name = "";
746 $address = "";
747 $comment = "";
748 }
749 }
750
Joe Perches3705ce52013-07-03 15:05:31 -0700751 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700752 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700753 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700754 $address =~ s/^\<|\>$//g;
755
756 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
757 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
758 $name = "\"$name\"";
759 }
760
761 return ($name, $address, $comment);
762}
763
764sub format_email {
765 my ($name, $address) = @_;
766
767 my $formatted_email;
768
Joe Perches3705ce52013-07-03 15:05:31 -0700769 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700770 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700771 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700772
773 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
774 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
775 $name = "\"$name\"";
776 }
777
778 if ("$name" eq "") {
779 $formatted_email = "$address";
780 } else {
781 $formatted_email = "$name <$address>";
782 }
783
784 return $formatted_email;
785}
786
Joe Perchesd311cd42014-08-06 16:10:57 -0700787sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700788 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700789
Joe Perchesbd474ca2014-08-06 16:11:10 -0700790 foreach my $path (split(/:/, $ENV{PATH})) {
791 if (-e "$path/$bin") {
792 return "$path/$bin";
793 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700794 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700795
Joe Perchesbd474ca2014-08-06 16:11:10 -0700796 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700797}
798
Joe Perches000d1cc12011-07-25 17:13:25 -0700799sub which_conf {
800 my ($conf) = @_;
801
802 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
803 if (-e "$path/$conf") {
804 return "$path/$conf";
805 }
806 }
807
808 return "";
809}
810
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700811sub expand_tabs {
812 my ($str) = @_;
813
814 my $res = '';
815 my $n = 0;
816 for my $c (split(//, $str)) {
817 if ($c eq "\t") {
818 $res .= ' ';
819 $n++;
820 for (; ($n % 8) != 0; $n++) {
821 $res .= ' ';
822 }
823 next;
824 }
825 $res .= $c;
826 $n++;
827 }
828
829 return $res;
830}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700831sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700832 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700833 return $res;
834}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700835
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700836sub line_stats {
837 my ($line) = @_;
838
839 # Drop the diff line leader and expand tabs
840 $line =~ s/^.//;
841 $line = expand_tabs($line);
842
843 # Pick the indent from the front of the line.
844 my ($white) = ($line =~ /^(\s*)/);
845
846 return (length($line), length($white));
847}
848
Andy Whitcroft773647a2008-03-28 14:15:58 -0700849my $sanitise_quote = '';
850
851sub sanitise_line_reset {
852 my ($in_comment) = @_;
853
854 if ($in_comment) {
855 $sanitise_quote = '*/';
856 } else {
857 $sanitise_quote = '';
858 }
859}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700860sub sanitise_line {
861 my ($line) = @_;
862
863 my $res = '';
864 my $l = '';
865
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800866 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700867 my $off = 0;
868 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700869
Andy Whitcroft773647a2008-03-28 14:15:58 -0700870 # Always copy over the diff marker.
871 $res = substr($line, 0, 1);
872
873 for ($off = 1; $off < length($line); $off++) {
874 $c = substr($line, $off, 1);
875
876 # Comments we are wacking completly including the begin
877 # and end, all to $;.
878 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
879 $sanitise_quote = '*/';
880
881 substr($res, $off, 2, "$;$;");
882 $off++;
883 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800884 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700885 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700886 $sanitise_quote = '';
887 substr($res, $off, 2, "$;$;");
888 $off++;
889 next;
890 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700891 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
892 $sanitise_quote = '//';
893
894 substr($res, $off, 2, $sanitise_quote);
895 $off++;
896 next;
897 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700898
899 # A \ in a string means ignore the next character.
900 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
901 $c eq "\\") {
902 substr($res, $off, 2, 'XX');
903 $off++;
904 next;
905 }
906 # Regular quotes.
907 if ($c eq "'" || $c eq '"') {
908 if ($sanitise_quote eq '') {
909 $sanitise_quote = $c;
910
911 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700912 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700913 } elsif ($sanitise_quote eq $c) {
914 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700915 }
916 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700917
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800918 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700919 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
920 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700921 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
922 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700923 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
924 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700925 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700926 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700927 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800928 }
929
Daniel Walker113f04a2009-09-21 17:04:35 -0700930 if ($sanitise_quote eq '//') {
931 $sanitise_quote = '';
932 }
933
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800934 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700935 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800936 my $clean = 'X' x length($1);
937 $res =~ s@\<.*\>@<$clean>@;
938
939 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700940 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800941 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700942 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943 }
944
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700945 return $res;
946}
947
Joe Perchesa6962d72013-04-29 16:18:13 -0700948sub get_quoted_string {
949 my ($line, $rawline) = @_;
950
Joe Perches5e4f6ba2014-12-10 15:52:05 -0800951 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -0700952 return substr($rawline, $-[0], $+[0] - $-[0]);
953}
954
Andy Whitcroft8905a672007-11-28 16:21:06 -0800955sub ctx_statement_block {
956 my ($linenr, $remain, $off) = @_;
957 my $line = $linenr - 1;
958 my $blk = '';
959 my $soff = $off;
960 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700961 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800962
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800963 my $loff = 0;
964
Andy Whitcroft8905a672007-11-28 16:21:06 -0800965 my $type = '';
966 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800967 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800968 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800969 my $c;
970 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800971
972 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800973 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800974 @stack = (['', 0]) if ($#stack == -1);
975
Andy Whitcroft773647a2008-03-28 14:15:58 -0700976 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800977 # If we are about to drop off the end, pull in more
978 # context.
979 if ($off >= $len) {
980 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700981 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800982 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800983 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800984 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800985 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800986 $len = length($blk);
987 $line++;
988 last;
989 }
990 # Bail if there is no further context.
991 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800992 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800993 last;
994 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800995 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
996 $level++;
997 $type = '#';
998 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800999 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001000 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001001 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001002 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001003
Andy Whitcroft773647a2008-03-28 14:15:58 -07001004 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001005
1006 # Handle nested #if/#else.
1007 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1008 push(@stack, [ $type, $level ]);
1009 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1010 ($type, $level) = @{$stack[$#stack - 1]};
1011 } elsif ($remainder =~ /^#\s*endif\b/) {
1012 ($type, $level) = @{pop(@stack)};
1013 }
1014
Andy Whitcroft8905a672007-11-28 16:21:06 -08001015 # Statement ends at the ';' or a close '}' at the
1016 # outermost level.
1017 if ($level == 0 && $c eq ';') {
1018 last;
1019 }
1020
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001021 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001022 if ($level == 0 && $coff_set == 0 &&
1023 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1024 $remainder =~ /^(else)(?:\s|{)/ &&
1025 $remainder !~ /^else\s+if\b/) {
1026 $coff = $off + length($1) - 1;
1027 $coff_set = 1;
1028 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1029 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001030 }
1031
Andy Whitcroft8905a672007-11-28 16:21:06 -08001032 if (($type eq '' || $type eq '(') && $c eq '(') {
1033 $level++;
1034 $type = '(';
1035 }
1036 if ($type eq '(' && $c eq ')') {
1037 $level--;
1038 $type = ($level != 0)? '(' : '';
1039
1040 if ($level == 0 && $coff < $soff) {
1041 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001042 $coff_set = 1;
1043 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001044 }
1045 }
1046 if (($type eq '' || $type eq '{') && $c eq '{') {
1047 $level++;
1048 $type = '{';
1049 }
1050 if ($type eq '{' && $c eq '}') {
1051 $level--;
1052 $type = ($level != 0)? '{' : '';
1053
1054 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001055 if (substr($blk, $off + 1, 1) eq ';') {
1056 $off++;
1057 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001058 last;
1059 }
1060 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001061 # Preprocessor commands end at the newline unless escaped.
1062 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1063 $level--;
1064 $type = '';
1065 $off++;
1066 last;
1067 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001068 $off++;
1069 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001070 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001071 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001072 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001073 $line++;
1074 $remain--;
1075 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001076
1077 my $statement = substr($blk, $soff, $off - $soff + 1);
1078 my $condition = substr($blk, $soff, $coff - $soff + 1);
1079
1080 #warn "STATEMENT<$statement>\n";
1081 #warn "CONDITION<$condition>\n";
1082
Andy Whitcroft773647a2008-03-28 14:15:58 -07001083 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001084
1085 return ($statement, $condition,
1086 $line, $remain + 1, $off - $loff + 1, $level);
1087}
1088
Andy Whitcroftcf655042008-03-04 14:28:20 -08001089sub statement_lines {
1090 my ($stmt) = @_;
1091
1092 # Strip the diff line prefixes and rip blank lines at start and end.
1093 $stmt =~ s/(^|\n)./$1/g;
1094 $stmt =~ s/^\s*//;
1095 $stmt =~ s/\s*$//;
1096
1097 my @stmt_lines = ($stmt =~ /\n/g);
1098
1099 return $#stmt_lines + 2;
1100}
1101
1102sub statement_rawlines {
1103 my ($stmt) = @_;
1104
1105 my @stmt_lines = ($stmt =~ /\n/g);
1106
1107 return $#stmt_lines + 2;
1108}
1109
1110sub statement_block_size {
1111 my ($stmt) = @_;
1112
1113 $stmt =~ s/(^|\n)./$1/g;
1114 $stmt =~ s/^\s*{//;
1115 $stmt =~ s/}\s*$//;
1116 $stmt =~ s/^\s*//;
1117 $stmt =~ s/\s*$//;
1118
1119 my @stmt_lines = ($stmt =~ /\n/g);
1120 my @stmt_statements = ($stmt =~ /;/g);
1121
1122 my $stmt_lines = $#stmt_lines + 2;
1123 my $stmt_statements = $#stmt_statements + 1;
1124
1125 if ($stmt_lines > $stmt_statements) {
1126 return $stmt_lines;
1127 } else {
1128 return $stmt_statements;
1129 }
1130}
1131
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001132sub ctx_statement_full {
1133 my ($linenr, $remain, $off) = @_;
1134 my ($statement, $condition, $level);
1135
1136 my (@chunks);
1137
Andy Whitcroftcf655042008-03-04 14:28:20 -08001138 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001139 ($statement, $condition, $linenr, $remain, $off, $level) =
1140 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001141 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001142 push(@chunks, [ $condition, $statement ]);
1143 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1144 return ($level, $linenr, @chunks);
1145 }
1146
1147 # Pull in the following conditional/block pairs and see if they
1148 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001149 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001150 ($statement, $condition, $linenr, $remain, $off, $level) =
1151 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001152 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001153 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001154 #print "C: push\n";
1155 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001156 }
1157
1158 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001159}
1160
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001161sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001162 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001163 my $line;
1164 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001165 my $blk = '';
1166 my @o;
1167 my @c;
1168 my @res = ();
1169
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001170 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001171 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001172 for ($line = $start; $remain > 0; $line++) {
1173 next if ($rawlines[$line] =~ /^-/);
1174 $remain--;
1175
1176 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001177
1178 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001179 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001180 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001181 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001182 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001183 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001184 $level = pop(@stack);
1185 }
1186
Andy Whitcroft01464f32010-10-26 14:23:19 -07001187 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001188 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1189 if ($off > 0) {
1190 $off--;
1191 next;
1192 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001193
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001194 if ($c eq $close && $level > 0) {
1195 $level--;
1196 last if ($level == 0);
1197 } elsif ($c eq $open) {
1198 $level++;
1199 }
1200 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001201
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001202 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001203 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001204 }
1205
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001206 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001207 }
1208
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001209 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001210}
1211sub ctx_block_outer {
1212 my ($linenr, $remain) = @_;
1213
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001214 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1215 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001216}
1217sub ctx_block {
1218 my ($linenr, $remain) = @_;
1219
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001220 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1221 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001222}
1223sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001224 my ($linenr, $remain, $off) = @_;
1225
1226 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1227 return @r;
1228}
1229sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001230 my ($linenr, $remain) = @_;
1231
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001232 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001233}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001234sub ctx_statement_level {
1235 my ($linenr, $remain, $off) = @_;
1236
1237 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1238}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001239
1240sub ctx_locate_comment {
1241 my ($first_line, $end_line) = @_;
1242
1243 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001244 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001245 return $current_comment if (defined $current_comment);
1246
1247 # Look through the context and try and figure out if there is a
1248 # comment.
1249 my $in_comment = 0;
1250 $current_comment = '';
1251 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001252 my $line = $rawlines[$linenr - 1];
1253 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001254 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1255 $in_comment = 1;
1256 }
1257 if ($line =~ m@/\*@) {
1258 $in_comment = 1;
1259 }
1260 if (!$in_comment && $current_comment ne '') {
1261 $current_comment = '';
1262 }
1263 $current_comment .= $line . "\n" if ($in_comment);
1264 if ($line =~ m@\*/@) {
1265 $in_comment = 0;
1266 }
1267 }
1268
1269 chomp($current_comment);
1270 return($current_comment);
1271}
1272sub ctx_has_comment {
1273 my ($first_line, $end_line) = @_;
1274 my $cmt = ctx_locate_comment($first_line, $end_line);
1275
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001276 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001277 ##print "CMMT: $cmt\n";
1278
1279 return ($cmt ne '');
1280}
1281
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001282sub raw_line {
1283 my ($linenr, $cnt) = @_;
1284
1285 my $offset = $linenr - 1;
1286 $cnt++;
1287
1288 my $line;
1289 while ($cnt) {
1290 $line = $rawlines[$offset++];
1291 next if (defined($line) && $line =~ /^-/);
1292 $cnt--;
1293 }
1294
1295 return $line;
1296}
1297
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001298sub cat_vet {
1299 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001300 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001301
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001302 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001303 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1304 $res .= $1;
1305 if ($2 ne '') {
1306 $coded = sprintf("^%c", unpack('C', $2) + 64);
1307 $res .= $coded;
1308 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001309 }
1310 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001311
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001312 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001313}
1314
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001315my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001316my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001317my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001318my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001319
1320sub annotate_reset {
1321 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001322 $av_pending = '_';
1323 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001324 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001325}
1326
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001327sub annotate_values {
1328 my ($stream, $type) = @_;
1329
1330 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001331 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001332 my $cur = $stream;
1333
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001334 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001335
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001336 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001337 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001338 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001339 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001340 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001341 print "WS($1)\n" if ($dbg_values > 1);
1342 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001343 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001344 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001345 }
1346
Florian Micklerc023e4732011-01-12 16:59:58 -08001347 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001348 print "CAST($1)\n" if ($dbg_values > 1);
1349 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001350 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001351
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001352 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001353 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001354 $type = 'T';
1355
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001356 } elsif ($cur =~ /^($Modifier)\s*/) {
1357 print "MODIFIER($1)\n" if ($dbg_values > 1);
1358 $type = 'T';
1359
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001360 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001361 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001362 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001363 push(@av_paren_type, $type);
1364 if ($2 ne '') {
1365 $av_pending = 'N';
1366 }
1367 $type = 'E';
1368
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001369 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001370 print "UNDEF($1)\n" if ($dbg_values > 1);
1371 $av_preprocessor = 1;
1372 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001373
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001374 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001375 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001376 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001377
1378 push(@av_paren_type, $type);
1379 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001380 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001381
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001382 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001383 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1384 $av_preprocessor = 1;
1385
1386 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1387
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001388 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001389
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001390 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001391 print "PRE_END($1)\n" if ($dbg_values > 1);
1392
1393 $av_preprocessor = 1;
1394
1395 # Assume all arms of the conditional end as this
1396 # one does, and continue as if the #endif was not here.
1397 pop(@av_paren_type);
1398 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001399 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001400
1401 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001402 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001403
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001404 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1405 print "ATTR($1)\n" if ($dbg_values > 1);
1406 $av_pending = $type;
1407 $type = 'N';
1408
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001409 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001410 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001411 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001412 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001413 }
1414 $type = 'N';
1415
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001416 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001417 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001418 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001419 $type = 'N';
1420
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001421 } elsif ($cur =~/^(case)/o) {
1422 print "CASE($1)\n" if ($dbg_values > 1);
1423 $av_pend_colon = 'C';
1424 $type = 'N';
1425
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001426 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001427 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001428 $type = 'N';
1429
1430 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001431 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001432 push(@av_paren_type, $av_pending);
1433 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001434 $type = 'N';
1435
1436 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001437 my $new_type = pop(@av_paren_type);
1438 if ($new_type ne '_') {
1439 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001440 print "PAREN('$1') -> $type\n"
1441 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001442 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001443 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001444 }
1445
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001446 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001447 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001448 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001449 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001450
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001451 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1452 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001453 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001454 } elsif ($type eq 'E') {
1455 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001456 }
1457 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1458 $type = 'V';
1459
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001460 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001461 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001462 $type = 'V';
1463
1464 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001465 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001466 $type = 'N';
1467
Andy Whitcroftcf655042008-03-04 14:28:20 -08001468 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001469 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001470 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001471 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001472
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001473 } elsif ($cur =~/^(,)/) {
1474 print "COMMA($1)\n" if ($dbg_values > 1);
1475 $type = 'C';
1476
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001477 } elsif ($cur =~ /^(\?)/o) {
1478 print "QUESTION($1)\n" if ($dbg_values > 1);
1479 $type = 'N';
1480
1481 } elsif ($cur =~ /^(:)/o) {
1482 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1483
1484 substr($var, length($res), 1, $av_pend_colon);
1485 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1486 $type = 'E';
1487 } else {
1488 $type = 'N';
1489 }
1490 $av_pend_colon = 'O';
1491
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001492 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001493 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001494 $type = 'N';
1495
Andy Whitcroft0d413862008-10-15 22:02:16 -07001496 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001497 my $variant;
1498
1499 print "OPV($1)\n" if ($dbg_values > 1);
1500 if ($type eq 'V') {
1501 $variant = 'B';
1502 } else {
1503 $variant = 'U';
1504 }
1505
1506 substr($var, length($res), 1, $variant);
1507 $type = 'N';
1508
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001509 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001510 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001511 if ($1 ne '++' && $1 ne '--') {
1512 $type = 'N';
1513 }
1514
1515 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001516 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001517 }
1518 if (defined $1) {
1519 $cur = substr($cur, length($1));
1520 $res .= $type x length($1);
1521 }
1522 }
1523
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001524 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001525}
1526
Andy Whitcroft8905a672007-11-28 16:21:06 -08001527sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001528 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001529 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001530 ^(?:
1531 $Modifier|
1532 $Storage|
1533 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001534 DEFINE_\S+
1535 )$|
1536 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001537 goto|
1538 return|
1539 case|
1540 else|
1541 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001542 do|
1543 \#|
1544 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001545 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001546 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001547 )}x;
1548 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1549 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001550 # Check for modifiers.
1551 $possible =~ s/\s*$Storage\s*//g;
1552 $possible =~ s/\s*$Sparse\s*//g;
1553 if ($possible =~ /^\s*$/) {
1554
1555 } elsif ($possible =~ /\s/) {
1556 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001557 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001558 if ($modifier !~ $notPermitted) {
1559 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1560 push(@modifierList, $modifier);
1561 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001562 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001563
1564 } else {
1565 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1566 push(@typeList, $possible);
1567 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001568 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001569 } else {
1570 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001571 }
1572}
1573
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001574my $prefix = '';
1575
Joe Perches000d1cc12011-07-25 17:13:25 -07001576sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001577 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001578
Joe Perchescbec18a2014-04-03 14:49:19 -07001579 return defined $use_type{$type} if (scalar keys %use_type > 0);
1580
1581 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001582}
1583
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001584sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001585 my ($level, $type, $msg) = @_;
1586
1587 if (!show_type($type) ||
1588 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001589 return 0;
1590 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001591 my $line;
1592 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001593 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001594 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001595 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001596 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001597 $line = (split('\n', $line))[0] . "\n" if ($terse);
1598
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001599 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001600
1601 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001602}
Joe Perchescbec18a2014-04-03 14:49:19 -07001603
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001604sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001605 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001606}
Joe Perches000d1cc12011-07-25 17:13:25 -07001607
Joe Perchesd752fcc2014-08-06 16:11:05 -07001608sub fixup_current_range {
1609 my ($lineRef, $offset, $length) = @_;
1610
1611 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1612 my $o = $1;
1613 my $l = $2;
1614 my $no = $o + $offset;
1615 my $nl = $l + $length;
1616 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1617 }
1618}
1619
1620sub fix_inserted_deleted_lines {
1621 my ($linesRef, $insertedRef, $deletedRef) = @_;
1622
1623 my $range_last_linenr = 0;
1624 my $delta_offset = 0;
1625
1626 my $old_linenr = 0;
1627 my $new_linenr = 0;
1628
1629 my $next_insert = 0;
1630 my $next_delete = 0;
1631
1632 my @lines = ();
1633
1634 my $inserted = @{$insertedRef}[$next_insert++];
1635 my $deleted = @{$deletedRef}[$next_delete++];
1636
1637 foreach my $old_line (@{$linesRef}) {
1638 my $save_line = 1;
1639 my $line = $old_line; #don't modify the array
1640 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1641 $delta_offset = 0;
1642 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1643 $range_last_linenr = $new_linenr;
1644 fixup_current_range(\$line, $delta_offset, 0);
1645 }
1646
1647 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1648 $deleted = @{$deletedRef}[$next_delete++];
1649 $save_line = 0;
1650 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1651 }
1652
1653 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1654 push(@lines, ${$inserted}{'LINE'});
1655 $inserted = @{$insertedRef}[$next_insert++];
1656 $new_linenr++;
1657 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1658 }
1659
1660 if ($save_line) {
1661 push(@lines, $line);
1662 $new_linenr++;
1663 }
1664
1665 $old_linenr++;
1666 }
1667
1668 return @lines;
1669}
1670
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001671sub fix_insert_line {
1672 my ($linenr, $line) = @_;
1673
1674 my $inserted = {
1675 LINENR => $linenr,
1676 LINE => $line,
1677 };
1678 push(@fixed_inserted, $inserted);
1679}
1680
1681sub fix_delete_line {
1682 my ($linenr, $line) = @_;
1683
1684 my $deleted = {
1685 LINENR => $linenr,
1686 LINE => $line,
1687 };
1688
1689 push(@fixed_deleted, $deleted);
1690}
1691
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001692sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001693 my ($type, $msg) = @_;
1694
1695 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001696 our $clean = 0;
1697 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001698 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001699 }
Joe Perches3705ce52013-07-03 15:05:31 -07001700 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001701}
1702sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001703 my ($type, $msg) = @_;
1704
1705 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001706 our $clean = 0;
1707 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001708 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001709 }
Joe Perches3705ce52013-07-03 15:05:31 -07001710 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001711}
1712sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001713 my ($type, $msg) = @_;
1714
1715 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001716 our $clean = 0;
1717 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001718 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001719 }
Joe Perches3705ce52013-07-03 15:05:31 -07001720 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001721}
1722
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001723sub check_absolute_file {
1724 my ($absolute, $herecurr) = @_;
1725 my $file = $absolute;
1726
1727 ##print "absolute<$absolute>\n";
1728
1729 # See if any suffix of this path is a path within the tree.
1730 while ($file =~ s@^[^/]*/@@) {
1731 if (-f "$root/$file") {
1732 ##print "file<$file>\n";
1733 last;
1734 }
1735 }
1736 if (! -f _) {
1737 return 0;
1738 }
1739
1740 # It is, so see if the prefix is acceptable.
1741 my $prefix = $absolute;
1742 substr($prefix, -length($file)) = '';
1743
1744 ##print "prefix<$prefix>\n";
1745 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001746 WARN("USE_RELATIVE_PATH",
1747 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001748 }
1749}
1750
Joe Perches3705ce52013-07-03 15:05:31 -07001751sub trim {
1752 my ($string) = @_;
1753
Joe Perchesb34c6482013-09-11 14:24:01 -07001754 $string =~ s/^\s+|\s+$//g;
1755
1756 return $string;
1757}
1758
1759sub ltrim {
1760 my ($string) = @_;
1761
1762 $string =~ s/^\s+//;
1763
1764 return $string;
1765}
1766
1767sub rtrim {
1768 my ($string) = @_;
1769
1770 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001771
1772 return $string;
1773}
1774
Joe Perches52ea8502013-11-12 15:10:09 -08001775sub string_find_replace {
1776 my ($string, $find, $replace) = @_;
1777
1778 $string =~ s/$find/$replace/g;
1779
1780 return $string;
1781}
1782
Joe Perches3705ce52013-07-03 15:05:31 -07001783sub tabify {
1784 my ($leading) = @_;
1785
1786 my $source_indent = 8;
1787 my $max_spaces_before_tab = $source_indent - 1;
1788 my $spaces_to_tab = " " x $source_indent;
1789
1790 #convert leading spaces to tabs
1791 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1792 #Remove spaces before a tab
1793 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1794
1795 return "$leading";
1796}
1797
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001798sub pos_last_openparen {
1799 my ($line) = @_;
1800
1801 my $pos = 0;
1802
1803 my $opens = $line =~ tr/\(/\(/;
1804 my $closes = $line =~ tr/\)/\)/;
1805
1806 my $last_openparen = 0;
1807
1808 if (($opens == 0) || ($closes >= $opens)) {
1809 return -1;
1810 }
1811
1812 my $len = length($line);
1813
1814 for ($pos = 0; $pos < $len; $pos++) {
1815 my $string = substr($line, $pos);
1816 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1817 $pos += length($1) - 1;
1818 } elsif (substr($line, $pos, 1) eq '(') {
1819 $last_openparen = $pos;
1820 } elsif (index($string, '(') == -1) {
1821 last;
1822 }
1823 }
1824
Joe Perches91cb5192014-04-03 14:49:32 -07001825 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001826}
1827
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001828sub process {
1829 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001830
1831 my $linenr=0;
1832 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001833 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001834 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001835 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001836
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001837 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001838 my $indent;
1839 my $previndent=0;
1840 my $stashindent=0;
1841
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001842 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001843 my $signoff = 0;
1844 my $is_patch = 0;
1845
Joe Perches29ee1b02014-08-06 16:10:35 -07001846 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001847 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001848 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001849 my $non_utf8_charset = 0;
1850
Joe Perches365dd4e2014-08-06 16:10:42 -07001851 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001852 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001853
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001854 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001855 our $cnt_lines = 0;
1856 our $cnt_error = 0;
1857 our $cnt_warn = 0;
1858 our $cnt_chk = 0;
1859
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001860 # Trace the real file/line as we go.
1861 my $realfile = '';
1862 my $realline = 0;
1863 my $realcnt = 0;
1864 my $here = '';
1865 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001866 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001867 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001868 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001869
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001870 my $prev_values = 'E';
1871
1872 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001873 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001874 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001875 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001876 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001877
Joe Perches7e51f192013-09-11 14:23:57 -07001878 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001879
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001880 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001881 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001882 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001883 my @setup_docs = ();
1884 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001885
Joe Perchesd8b07712013-11-12 15:10:06 -08001886 my $camelcase_file_seeded = 0;
1887
Andy Whitcroft773647a2008-03-28 14:15:58 -07001888 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001889 my $line;
1890 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001891 $linenr++;
1892 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001893
Joe Perches3705ce52013-07-03 15:05:31 -07001894 push(@fixed, $rawline) if ($fix);
1895
Andy Whitcroft773647a2008-03-28 14:15:58 -07001896 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001897 $setup_docs = 0;
1898 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1899 $setup_docs = 1;
1900 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001901 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001902 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001903 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1904 $realline=$1-1;
1905 if (defined $2) {
1906 $realcnt=$3+1;
1907 } else {
1908 $realcnt=1+1;
1909 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001910 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001911
1912 # Guestimate if this is a continuing comment. Run
1913 # the context looking for a comment "edge". If this
1914 # edge is a close comment then we must be in a comment
1915 # at context start.
1916 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001917 my $cnt = $realcnt;
1918 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1919 next if (defined $rawlines[$ln - 1] &&
1920 $rawlines[$ln - 1] =~ /^-/);
1921 $cnt--;
1922 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001923 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001924 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1925 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1926 ($edge) = $1;
1927 last;
1928 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001929 }
1930 if (defined $edge && $edge eq '*/') {
1931 $in_comment = 1;
1932 }
1933
1934 # Guestimate if this is a continuing comment. If this
1935 # is the start of a diff block and this line starts
1936 # ' *' then it is very likely a comment.
1937 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001938 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001939 {
1940 $in_comment = 1;
1941 }
1942
1943 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1944 sanitise_line_reset($in_comment);
1945
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001946 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001947 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001948 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001949 $line = sanitise_line($rawline);
1950 }
1951 push(@lines, $line);
1952
1953 if ($realcnt > 1) {
1954 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1955 } else {
1956 $realcnt = 0;
1957 }
1958
1959 #print "==>$rawline\n";
1960 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001961
1962 if ($setup_docs && $line =~ /^\+/) {
1963 push(@setup_docs, $line);
1964 }
1965 }
1966
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001967 $prefix = '';
1968
Andy Whitcroft773647a2008-03-28 14:15:58 -07001969 $realcnt = 0;
1970 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001971 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001972 foreach my $line (@lines) {
1973 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001974 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001975 my $sline = $line; #copy of $line
1976 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001977
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001978 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001979
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001980#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001981 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001982 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001983 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001984 $realline=$1-1;
1985 if (defined $2) {
1986 $realcnt=$3+1;
1987 } else {
1988 $realcnt=1+1;
1989 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001990 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001991 $prev_values = 'E';
1992
Andy Whitcroft773647a2008-03-28 14:15:58 -07001993 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001994 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001995 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001996 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001997 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001998
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001999# track the line number as we move through the hunk, note that
2000# new versions of GNU diff omit the leading space on completely
2001# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002002 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002003 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002004 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002005
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002006 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002007 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002008
2009 # Track the previous line.
2010 ($prevline, $stashline) = ($stashline, $line);
2011 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002012 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2013
Andy Whitcroft773647a2008-03-28 14:15:58 -07002014 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002015
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002016 } elsif ($realcnt == 1) {
2017 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002018 }
2019
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002020 my $hunk_line = ($realcnt != 0);
2021
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002022#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023 $prefix = "$filename:$realline: " if ($emacs && $file);
2024 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2025
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002026 $here = "#$linenr: " if (!$file);
2027 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002028
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002029 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002030 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002031 if ($line =~ /^diff --git.*?(\S+)$/) {
2032 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002033 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002034 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002035 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002036 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002037 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002038 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002039 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002040
2041 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002042 if (!$file && $tree && $p1_prefix ne '' &&
2043 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002044 WARN("PATCH_PREFIX",
2045 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002046 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002047
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002048 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002049 ERROR("MODIFIED_INCLUDE_ASM",
2050 "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 -07002051 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002052 $found_file = 1;
2053 }
2054
2055 if ($found_file) {
2056 if ($realfile =~ m@^(drivers/net/|net/)@) {
2057 $check = 1;
2058 } else {
2059 $check = $check_orig;
2060 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002061 next;
2062 }
2063
Randy Dunlap389834b2007-06-08 13:47:03 -07002064 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002065
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002066 my $hereline = "$here\n$rawline\n";
2067 my $herecurr = "$here\n$rawline\n";
2068 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002069
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002070 $cnt_lines++ if ($realcnt != 0);
2071
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002072# Check for incorrect file permissions
2073 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2074 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002075 if ($realfile !~ m@scripts/@ &&
2076 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002077 ERROR("EXECUTE_PERMISSIONS",
2078 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002079 }
2080 }
2081
Joe Perches20112472011-07-25 17:13:23 -07002082# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002083 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002084 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002085 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002086 }
2087
Joe Perchese0d975b2014-12-10 15:51:49 -08002088# Check if MAINTAINERS is being updated. If so, there's probably no need to
2089# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2090 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2091 $reported_maintainer_file = 1;
2092 }
2093
Joe Perches20112472011-07-25 17:13:23 -07002094# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002095 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002096 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002097 my $space_before = $1;
2098 my $sign_off = $2;
2099 my $space_after = $3;
2100 my $email = $4;
2101 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2102
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002103 if ($sign_off !~ /$signature_tags/) {
2104 WARN("BAD_SIGN_OFF",
2105 "Non-standard signature: $sign_off\n" . $herecurr);
2106 }
Joe Perches20112472011-07-25 17:13:23 -07002107 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002108 if (WARN("BAD_SIGN_OFF",
2109 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2110 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002111 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002112 "$ucfirst_sign_off $email";
2113 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002114 }
Joe Perches20112472011-07-25 17:13:23 -07002115 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002116 if (WARN("BAD_SIGN_OFF",
2117 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2118 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002119 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002120 "$ucfirst_sign_off $email";
2121 }
2122
Joe Perches20112472011-07-25 17:13:23 -07002123 }
2124 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002125 if (WARN("BAD_SIGN_OFF",
2126 "Use a single space after $ucfirst_sign_off\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 }
Joe Perches20112472011-07-25 17:13:23 -07002131 }
2132
2133 my ($email_name, $email_address, $comment) = parse_email($email);
2134 my $suggested_email = format_email(($email_name, $email_address));
2135 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002136 ERROR("BAD_SIGN_OFF",
2137 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002138 } else {
2139 my $dequoted = $suggested_email;
2140 $dequoted =~ s/^"//;
2141 $dequoted =~ s/" </ </;
2142 # Don't force email to have quotes
2143 # Allow just an angle bracketed address
2144 if ("$dequoted$comment" ne $email &&
2145 "<$email_address>$comment" ne $email &&
2146 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002147 WARN("BAD_SIGN_OFF",
2148 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002149 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002150 }
Joe Perches7e51f192013-09-11 14:23:57 -07002151
2152# Check for duplicate signatures
2153 my $sig_nospace = $line;
2154 $sig_nospace =~ s/\s//g;
2155 $sig_nospace = lc($sig_nospace);
2156 if (defined $signatures{$sig_nospace}) {
2157 WARN("BAD_SIGN_OFF",
2158 "Duplicate signature\n" . $herecurr);
2159 } else {
2160 $signatures{$sig_nospace} = 1;
2161 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002162 }
2163
Joe Perches9b3189eb2014-06-04 16:12:10 -07002164# Check for old stable address
2165 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2166 ERROR("STABLE_ADDRESS",
2167 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2168 }
2169
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002170# Check for unwanted Gerrit info
2171 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2172 ERROR("GERRIT_CHANGE_ID",
2173 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2174 }
2175
Joe Perchesd311cd42014-08-06 16:10:57 -07002176# Check for improperly formed commit descriptions
2177 if ($in_commit_log &&
2178 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
Joe Perches66881732014-09-09 14:50:55 -07002179 !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2180 ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2181 defined $rawlines[$linenr] &&
2182 $rawlines[$linenr] =~ /^\s*\("/))) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002183 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2184 my $init_char = $1;
2185 my $orig_commit = lc($2);
2186 my $id = '01234567890ab';
2187 my $desc = 'commit description';
2188 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2189 ERROR("GIT_COMMIT_ID",
Joe Perches3f6316b42014-08-29 15:18:26 -07002190 "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
Joe Perchesd311cd42014-08-06 16:10:57 -07002191 }
2192
Joe Perches13f19372014-08-06 16:10:59 -07002193# Check for added, moved or deleted files
2194 if (!$reported_maintainer_file && !$in_commit_log &&
2195 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2196 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2197 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2198 (defined($1) || defined($2))))) {
2199 $reported_maintainer_file = 1;
2200 WARN("FILE_PATH_CHANGES",
2201 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2202 }
2203
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002204# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002205 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002206 ERROR("CORRUPTED_PATCH",
2207 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002208 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002209 }
2210
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002211# Check for absolute kernel paths.
2212 if ($tree) {
2213 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2214 my $file = $1;
2215
2216 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2217 check_absolute_file($1, $herecurr)) {
2218 #
2219 } else {
2220 check_absolute_file($file, $herecurr);
2221 }
2222 }
2223 }
2224
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002225# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2226 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002227 $rawline !~ m/^$UTF8*$/) {
2228 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2229
2230 my $blank = copy_spacing($rawline);
2231 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2232 my $hereptr = "$hereline$ptr\n";
2233
Joe Perches34d99212011-07-25 17:13:26 -07002234 CHK("INVALID_UTF8",
2235 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002236 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002237
Joe Perches15662b32011-10-31 17:13:12 -07002238# Check if it's the start of a commit log
2239# (not a header line and we haven't seen the patch filename)
2240 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002241 !($rawline =~ /^\s+\S/ ||
2242 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002243 $in_header_lines = 0;
2244 $in_commit_log = 1;
2245 }
2246
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002247# Check if there is UTF-8 in a commit log when a mail header has explicitly
2248# declined it, i.e defined some charset where it is missing.
2249 if ($in_header_lines &&
2250 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2251 $1 !~ /utf-8/i) {
2252 $non_utf8_charset = 1;
2253 }
2254
2255 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002256 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002257 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002258 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2259 }
2260
Kees Cook66b47b42014-10-13 15:51:57 -07002261# Check for various typo / spelling mistakes
Joe Perches36061e32014-12-10 15:51:43 -08002262 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
Kees Cook66b47b42014-10-13 15:51:57 -07002263 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2264 my $typo = $1;
2265 my $typo_fix = $spelling_fix{lc($typo)};
2266 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2267 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2268 my $msg_type = \&WARN;
2269 $msg_type = \&CHK if ($file);
2270 if (&{$msg_type}("TYPO_SPELLING",
2271 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2272 $fix) {
2273 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2274 }
2275 }
2276 }
2277
Andy Whitcroft306708542008-10-15 22:02:28 -07002278# ignore non-hunk lines and lines being removed
2279 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002280
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002281#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002282 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002283 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002284 if (ERROR("DOS_LINE_ENDINGS",
2285 "DOS line endings\n" . $herevet) &&
2286 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002287 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002288 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002289 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2290 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002291 if (ERROR("TRAILING_WHITESPACE",
2292 "trailing whitespace\n" . $herevet) &&
2293 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002294 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002295 }
2296
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002297 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002298 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002299
Josh Triplett4783f892013-11-12 15:10:12 -08002300# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002301 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002302 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2303 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002304 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2305 my $msg_type = \&ERROR;
2306 $msg_type = \&CHK if ($file);
2307 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002308 "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 -08002309 }
2310
Andi Kleen33549572010-05-24 14:33:29 -07002311# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002312# Only applies when adding the entry originally, after that we do not have
2313# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002314 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002315 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002316 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002317 my $cnt = $realcnt;
2318 my $ln = $linenr + 1;
2319 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002320 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002321 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002322 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002323 $f = $lines[$ln - 1];
2324 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2325 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002326
2327 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002328 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002329
Joe Perches8d73e0e2014-08-06 16:10:46 -07002330 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002331 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002332 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002333 $length = -1;
2334 }
2335
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002336 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002337 $f =~ s/#.*//;
2338 $f =~ s/^\s+//;
2339 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002340 if ($f =~ /^\s*config\s/) {
2341 $is_end = 1;
2342 last;
2343 }
Andi Kleen33549572010-05-24 14:33:29 -07002344 $length++;
2345 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002346 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2347 WARN("CONFIG_DESCRIPTION",
2348 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2349 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002350 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002351 }
2352
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002353# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2354 if ($realfile =~ /Kconfig/ &&
2355 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2356 WARN("CONFIG_EXPERIMENTAL",
2357 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2358 }
2359
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002360 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2361 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2362 my $flag = $1;
2363 my $replacement = {
2364 'EXTRA_AFLAGS' => 'asflags-y',
2365 'EXTRA_CFLAGS' => 'ccflags-y',
2366 'EXTRA_CPPFLAGS' => 'cppflags-y',
2367 'EXTRA_LDFLAGS' => 'ldflags-y',
2368 };
2369
2370 WARN("DEPRECATED_VARIABLE",
2371 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2372 }
2373
Rob Herringbff5da42014-01-23 15:54:51 -08002374# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002375 if (defined $root &&
2376 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2377 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2378
Rob Herringbff5da42014-01-23 15:54:51 -08002379 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2380
Florian Vaussardcc933192014-04-03 14:49:27 -07002381 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2382 my $vp_file = $dt_path . "vendor-prefixes.txt";
2383
Rob Herringbff5da42014-01-23 15:54:51 -08002384 foreach my $compat (@compats) {
2385 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002386 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2387 my $compat3 = $compat;
2388 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2389 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002390 if ( $? >> 8 ) {
2391 WARN("UNDOCUMENTED_DT_STRING",
2392 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2393 }
2394
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002395 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2396 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002397 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002398 if ( $? >> 8 ) {
2399 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002400 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002401 }
2402 }
2403 }
2404
Andy Whitcroft5368df202008-10-15 22:02:27 -07002405# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002406 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002407
Joe Perches6cd7f382012-12-17 16:01:54 -08002408#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002409 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002410 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002411 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002412 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002413 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002414 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002415 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002416 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002417 }
2418
Andy Whitcroft8905a672007-11-28 16:21:06 -08002419# check for adding lines without a newline.
2420 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002421 WARN("MISSING_EOF_NEWLINE",
2422 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002423 }
2424
Mike Frysinger42e41c52009-09-21 17:04:40 -07002425# Blackfin: use hi/lo macros
2426 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2427 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2428 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002429 ERROR("LO_MACRO",
2430 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002431 }
2432 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2433 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002434 ERROR("HI_MACRO",
2435 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002436 }
2437 }
2438
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002439# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002440 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002441
2442# at the beginning of a line any tabs must come first and anything
2443# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002444 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2445 $rawline =~ /^\+\s* \s*/) {
2446 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002447 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002448 if (ERROR("CODE_INDENT",
2449 "code indent should use tabs where possible\n" . $herevet) &&
2450 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002451 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002452 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002453 }
2454
Alberto Panizzo08e44362010-03-05 13:43:54 -08002455# check for space before tabs.
2456 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2457 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002458 if (WARN("SPACE_BEFORE_TAB",
2459 "please, no space before tabs\n" . $herevet) &&
2460 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002461 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002462 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002463 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002464 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002465 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002466 }
2467
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002468# check for && or || at the start of a line
2469 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2470 CHK("LOGICAL_CONTINUATIONS",
2471 "Logical continuations should be on the previous line\n" . $hereprev);
2472 }
2473
2474# check multi-line statement indentation matches previous line
2475 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002476 $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 -07002477 $prevline =~ /^\+(\t*)(.*)$/;
2478 my $oldindent = $1;
2479 my $rest = $2;
2480
2481 my $pos = pos_last_openparen($rest);
2482 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002483 $line =~ /^(\+| )([ \t]*)/;
2484 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002485
2486 my $goodtabindent = $oldindent .
2487 "\t" x ($pos / 8) .
2488 " " x ($pos % 8);
2489 my $goodspaceindent = $oldindent . " " x $pos;
2490
2491 if ($newindent ne $goodtabindent &&
2492 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002493
2494 if (CHK("PARENTHESIS_ALIGNMENT",
2495 "Alignment should match open parenthesis\n" . $hereprev) &&
2496 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002497 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002498 s/^\+[ \t]*/\+$goodtabindent/;
2499 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002500 }
2501 }
2502 }
2503
Joe Perches04941aa2014-12-10 15:51:37 -08002504 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
2505 (!defined($1) || $1 !~ /sizeof\s*/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002506 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002507 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002508 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002509 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002510 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002511 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002512 }
2513
Joe Perches05880602012-10-04 17:13:35 -07002514 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002515 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002516 $rawline =~ /^\+[ \t]*\*/ &&
2517 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002518 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2519 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2520 }
2521
2522 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002523 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2524 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002525 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002526 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2527 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2528 "networking block comments start with * on subsequent lines\n" . $hereprev);
2529 }
2530
2531 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002532 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2533 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2534 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2535 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002536 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2537 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2538 }
2539
Joe Perches7f619192014-08-06 16:10:39 -07002540# check for missing blank lines after struct/union declarations
2541# with exceptions for various attributes and macros
2542 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2543 $line =~ /^\+/ &&
2544 !($line =~ /^\+\s*$/ ||
2545 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2546 $line =~ /^\+\s*MODULE_/i ||
2547 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2548 $line =~ /^\+[a-z_]*init/ ||
2549 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2550 $line =~ /^\+\s*DECLARE/ ||
2551 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002552 if (CHK("LINE_SPACING",
2553 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2554 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002555 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002556 }
Joe Perches7f619192014-08-06 16:10:39 -07002557 }
2558
Joe Perches365dd4e2014-08-06 16:10:42 -07002559# check for multiple consecutive blank lines
2560 if ($prevline =~ /^[\+ ]\s*$/ &&
2561 $line =~ /^\+\s*$/ &&
2562 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002563 if (CHK("LINE_SPACING",
2564 "Please don't use multiple blank lines\n" . $hereprev) &&
2565 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002566 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002567 }
2568
Joe Perches365dd4e2014-08-06 16:10:42 -07002569 $last_blank_line = $linenr;
2570 }
2571
Joe Perches3b617e32014-04-03 14:49:28 -07002572# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002573 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2574 # actual declarations
2575 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002576 # function pointer declarations
2577 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002578 # foo bar; where foo is some local typedef or #define
2579 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2580 # known declaration macros
2581 $prevline =~ /^\+\s+$declaration_macros/) &&
2582 # for "else if" which can look like "$Ident $Ident"
2583 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2584 # other possible extensions of declaration lines
2585 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2586 # not starting a section or a macro "\" extended line
2587 $prevline =~ /(?:\{\s*|\\)$/) &&
2588 # looks like a declaration
2589 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002590 # function pointer declarations
2591 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002592 # foo bar; where foo is some local typedef or #define
2593 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2594 # known declaration macros
2595 $sline =~ /^\+\s+$declaration_macros/ ||
2596 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002597 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002598 # start or end of block or continuation of declaration
2599 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2600 # bitfield continuation
2601 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2602 # other possible extensions of declaration lines
2603 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2604 # indentation of previous and current line are the same
2605 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002606 if (WARN("LINE_SPACING",
2607 "Missing a blank line after declarations\n" . $hereprev) &&
2608 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002609 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002610 }
Joe Perches3b617e32014-04-03 14:49:28 -07002611 }
2612
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002613# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002614# Exceptions:
2615# 1) within comments
2616# 2) indented preprocessor commands
2617# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002618 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002619 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002620 if (WARN("LEADING_SPACE",
2621 "please, no spaces at the start of a line\n" . $herevet) &&
2622 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002623 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002624 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002625 }
2626
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002627# check we are in a valid C source file if not then ignore this hunk
2628 next if ($realfile !~ /\.(h|c)$/);
2629
Joe Perches032a4c02014-08-06 16:10:29 -07002630# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002631# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002632# if the previous line is a break or return and is indented 1 tab more...
2633 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2634 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002635 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2636 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2637 defined $lines[$linenr] &&
2638 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002639 WARN("UNNECESSARY_ELSE",
2640 "else is not generally useful after a break or return\n" . $hereprev);
2641 }
2642 }
2643
Joe Perchesc00df192014-08-06 16:11:01 -07002644# check indentation of a line with a break;
2645# if the previous line is a goto or return and is indented the same # of tabs
2646 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2647 my $tabs = $1;
2648 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2649 WARN("UNNECESSARY_BREAK",
2650 "break is not useful after a goto or return\n" . $hereprev);
2651 }
2652 }
2653
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002654# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2655 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2656 WARN("CONFIG_EXPERIMENTAL",
2657 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2658 }
2659
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002660# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002661 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002662 WARN("CVS_KEYWORD",
2663 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002664 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002665
Mike Frysinger42e41c52009-09-21 17:04:40 -07002666# Blackfin: don't use __builtin_bfin_[cs]sync
2667 if ($line =~ /__builtin_bfin_csync/) {
2668 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002669 ERROR("CSYNC",
2670 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002671 }
2672 if ($line =~ /__builtin_bfin_ssync/) {
2673 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002674 ERROR("SSYNC",
2675 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002676 }
2677
Joe Perches56e77d72013-02-21 16:44:14 -08002678# check for old HOTPLUG __dev<foo> section markings
2679 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2680 WARN("HOTPLUG_SECTION",
2681 "Using $1 is unnecessary\n" . $herecurr);
2682 }
2683
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002684# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002685 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2686 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002687#print "LINE<$line>\n";
2688 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002689 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002690 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002691 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002692 $stat =~ s/\n./\n /g;
2693 $cond =~ s/\n./\n /g;
2694
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002695#print "linenr<$linenr> <$stat>\n";
2696 # If this statement has no statement boundaries within
2697 # it there is no point in retrying a statement scan
2698 # until we hit end of it.
2699 my $frag = $stat; $frag =~ s/;+\s*$//;
2700 if ($frag !~ /(?:{|;)/) {
2701#print "skip<$line_nr_next>\n";
2702 $suppress_statement = $line_nr_next;
2703 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002704
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002705 # Find the real next line.
2706 $realline_next = $line_nr_next;
2707 if (defined $realline_next &&
2708 (!defined $lines[$realline_next - 1] ||
2709 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2710 $realline_next++;
2711 }
2712
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002713 my $s = $stat;
2714 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002715
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002716 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002717 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002718
2719 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002720 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002721
Andy Whitcroft463f2862009-09-21 17:04:34 -07002722 } elsif ($s =~ /^.\s*else\b/s) {
2723
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002724 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002725 } 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 -07002726 my $type = $1;
2727 $type =~ s/\s+/ /g;
2728 possible($type, "A:" . $s);
2729
Andy Whitcroft8905a672007-11-28 16:21:06 -08002730 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002731 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002732 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002733 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002734
2735 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002736 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002737 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002738 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002739
2740 # Check for any sort of function declaration.
2741 # int foo(something bar, other baz);
2742 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002743 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 -08002744 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002745
Andy Whitcroftcf655042008-03-04 14:28:20 -08002746 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002747 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002748 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002749
Andy Whitcroft8905a672007-11-28 16:21:06 -08002750 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002751 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002752
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002753 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002754 }
2755 }
2756 }
2757
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002758 }
2759
Andy Whitcroft653d4872007-06-23 17:16:34 -07002760#
2761# Checks which may be anchored in the context.
2762#
2763
2764# Check for switch () and associated case and default
2765# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002766 if ($line=~/\bswitch\s*\(.*\)/) {
2767 my $err = '';
2768 my $sep = '';
2769 my @ctx = ctx_block_outer($linenr, $realcnt);
2770 shift(@ctx);
2771 for my $ctx (@ctx) {
2772 my ($clen, $cindent) = line_stats($ctx);
2773 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2774 $indent != $cindent) {
2775 $err .= "$sep$ctx\n";
2776 $sep = '';
2777 } else {
2778 $sep = "[...]\n";
2779 }
2780 }
2781 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002782 ERROR("SWITCH_CASE_INDENT_LEVEL",
2783 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002784 }
2785 }
2786
2787# if/while/etc brace do not go on next line, unless defining a do while loop,
2788# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002789 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 -07002790 my $pre_ctx = "$1$2";
2791
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002792 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002793
2794 if ($line =~ /^\+\t{6,}/) {
2795 WARN("DEEP_INDENTATION",
2796 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2797 }
2798
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002799 my $ctx_cnt = $realcnt - $#ctx - 1;
2800 my $ctx = join("\n", @ctx);
2801
Andy Whitcroft548596d2008-07-23 21:29:01 -07002802 my $ctx_ln = $linenr;
2803 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002804
Andy Whitcroft548596d2008-07-23 21:29:01 -07002805 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2806 defined $lines[$ctx_ln - 1] &&
2807 $lines[$ctx_ln - 1] =~ /^-/)) {
2808 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2809 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002810 $ctx_ln++;
2811 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002812
Andy Whitcroft53210162008-07-23 21:29:03 -07002813 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2814 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002815
Joe Perchesd752fcc2014-08-06 16:11:05 -07002816 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002817 ERROR("OPEN_BRACE",
2818 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002819 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002820 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002821 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2822 $ctx =~ /\)\s*\;\s*$/ &&
2823 defined $lines[$ctx_ln - 1])
2824 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002825 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2826 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002827 WARN("TRAILING_SEMICOLON",
2828 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002829 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002830 }
2831 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002832 }
2833
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002834# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002835 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 -08002836 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2837 ctx_statement_block($linenr, $realcnt, 0)
2838 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002839 my ($s, $c) = ($stat, $cond);
2840
2841 substr($s, 0, length($c), '');
2842
2843 # Make sure we remove the line prefixes as we have
2844 # none on the first line, and are going to readd them
2845 # where necessary.
2846 $s =~ s/\n./\n/gs;
2847
2848 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002849 my @newlines = ($c =~ /\n/gs);
2850 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002851
2852 # We want to check the first line inside the block
2853 # starting at the end of the conditional, so remove:
2854 # 1) any blank line termination
2855 # 2) any opening brace { on end of the line
2856 # 3) any do (...) {
2857 my $continuation = 0;
2858 my $check = 0;
2859 $s =~ s/^.*\bdo\b//;
2860 $s =~ s/^\s*{//;
2861 if ($s =~ s/^\s*\\//) {
2862 $continuation = 1;
2863 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002864 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002865 $check = 1;
2866 $cond_lines++;
2867 }
2868
2869 # Also ignore a loop construct at the end of a
2870 # preprocessor statement.
2871 if (($prevline =~ /^.\s*#\s*define\s/ ||
2872 $prevline =~ /\\\s*$/) && $continuation == 0) {
2873 $check = 0;
2874 }
2875
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002876 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002877 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002878 while ($cond_ptr != $cond_lines) {
2879 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002880
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002881 # If we see an #else/#elif then the code
2882 # is not linear.
2883 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2884 $check = 0;
2885 }
2886
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002887 # Ignore:
2888 # 1) blank lines, they should be at 0,
2889 # 2) preprocessor lines, and
2890 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002891 if ($continuation ||
2892 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002893 $s =~ /^\s*#\s*?/ ||
2894 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002895 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002896 if ($s =~ s/^.*?\n//) {
2897 $cond_lines++;
2898 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002899 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002900 }
2901
2902 my (undef, $sindent) = line_stats("+" . $s);
2903 my $stat_real = raw_line($linenr, $cond_lines);
2904
2905 # Check if either of these lines are modified, else
2906 # this is not this patch's fault.
2907 if (!defined($stat_real) ||
2908 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2909 $check = 0;
2910 }
2911 if (defined($stat_real) && $cond_lines > 1) {
2912 $stat_real = "[...]\n$stat_real";
2913 }
2914
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002915 #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 -07002916
2917 if ($check && (($sindent % 8) != 0 ||
2918 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002919 WARN("SUSPECT_CODE_INDENT",
2920 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002921 }
2922 }
2923
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002924 # Track the 'values' across context and added lines.
2925 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002926 my ($curr_values, $curr_vars) =
2927 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002928 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002929 if ($dbg_values) {
2930 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002931 print "$linenr > .$outline\n";
2932 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002933 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002934 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002935 $prev_values = substr($curr_values, -1);
2936
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002937#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002938 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002939
Andy Whitcroft653d4872007-06-23 17:16:34 -07002940# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002941 if ($dbg_type) {
2942 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002943 ERROR("TEST_TYPE",
2944 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002945 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002946 ERROR("TEST_NOT_TYPE",
2947 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002948 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002949 next;
2950 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002951# TEST: allow direct testing of the attribute matcher.
2952 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002953 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002954 ERROR("TEST_ATTR",
2955 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002956 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002957 ERROR("TEST_NOT_ATTR",
2958 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002959 }
2960 next;
2961 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002962
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002963# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002964 if ($line =~ /^.\s*{/ &&
2965 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002966 if (ERROR("OPEN_BRACE",
2967 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002968 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2969 fix_delete_line($fixlinenr - 1, $prevrawline);
2970 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002971 my $fixedline = $prevrawline;
2972 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002973 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002974 $fixedline = $line;
2975 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002976 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002977 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002978 }
2979
Andy Whitcroft653d4872007-06-23 17:16:34 -07002980#
2981# Checks which are anchored on the added line.
2982#
2983
2984# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002985 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002986 my $path = $1;
2987 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002988 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002989 "malformed #include filename\n" . $herecurr);
2990 }
2991 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2992 ERROR("UAPI_INCLUDE",
2993 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002994 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002995 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002996
2997# no C99 // comments
2998 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002999 if (ERROR("C99_COMMENTS",
3000 "do not use C99 // comments\n" . $herecurr) &&
3001 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003002 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003003 if ($line =~ /\/\/(.*)$/) {
3004 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003005 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003006 }
3007 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003008 }
3009 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003010 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003011 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003012
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003013# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3014# the whole statement.
3015#print "APW <$lines[$realline_next - 1]>\n";
3016 if (defined $realline_next &&
3017 exists $lines[$realline_next - 1] &&
3018 !defined $suppress_export{$realline_next} &&
3019 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3020 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003021 # Handle definitions which produce identifiers with
3022 # a prefix:
3023 # XXX(foo);
3024 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003025 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003026 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003027 $name =~ /^${Ident}_$2/) {
3028#print "FOO C name<$name>\n";
3029 $suppress_export{$realline_next} = 1;
3030
3031 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003032 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003033 ^.DEFINE_$Ident\(\Q$name\E\)|
3034 ^.DECLARE_$Ident\(\Q$name\E\)|
3035 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003036 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3037 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003038 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003039#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3040 $suppress_export{$realline_next} = 2;
3041 } else {
3042 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003043 }
3044 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003045 if (!defined $suppress_export{$linenr} &&
3046 $prevline =~ /^.\s*$/ &&
3047 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3048 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3049#print "FOO B <$lines[$linenr - 1]>\n";
3050 $suppress_export{$linenr} = 2;
3051 }
3052 if (defined $suppress_export{$linenr} &&
3053 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003054 WARN("EXPORT_SYMBOL",
3055 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003056 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003057
Joe Eloff5150bda2010-08-09 17:21:00 -07003058# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003059 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3060 if (ERROR("GLOBAL_INITIALISERS",
3061 "do not initialise globals to 0 or NULL\n" .
3062 $herecurr) &&
3063 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003064 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003065 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003066 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003067# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003068 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3069 if (ERROR("INITIALISED_STATIC",
3070 "do not initialise statics to 0 or NULL\n" .
3071 $herecurr) &&
3072 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003073 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003074 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003075 }
3076
Joe Perches18130872014-08-06 16:11:22 -07003077# check for misordered declarations of char/short/int/long with signed/unsigned
3078 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3079 my $tmp = trim($1);
3080 WARN("MISORDERED_TYPE",
3081 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3082 }
3083
Joe Perchescb710ec2010-10-26 14:23:20 -07003084# check for static const char * arrays.
3085 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003086 WARN("STATIC_CONST_CHAR_ARRAY",
3087 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003088 $herecurr);
3089 }
3090
3091# check for static char foo[] = "bar" declarations.
3092 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003093 WARN("STATIC_CONST_CHAR_ARRAY",
3094 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003095 $herecurr);
3096 }
3097
Joe Perches9b0fa602014-04-03 14:49:18 -07003098# check for non-global char *foo[] = {"bar", ...} declarations.
3099 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3100 WARN("STATIC_CONST_CHAR_ARRAY",
3101 "char * array declaration might be better as static const\n" .
3102 $herecurr);
3103 }
3104
Joe Perchesb36190c2014-01-27 17:07:18 -08003105# check for function declarations without arguments like "int foo()"
3106 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3107 if (ERROR("FUNCTION_WITHOUT_ARGS",
3108 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3109 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003110 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003111 }
3112 }
3113
Joe Perches92e112f2013-12-13 11:36:22 -07003114# check for uses of DEFINE_PCI_DEVICE_TABLE
3115 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3116 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3117 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3118 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003119 $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 -07003120 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003121 }
3122
Andy Whitcroft653d4872007-06-23 17:16:34 -07003123# check for new typedefs, only function parameters and sparse annotations
3124# make sense.
3125 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003126 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003127 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003128 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003129 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003130 WARN("NEW_TYPEDEFS",
3131 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003132 }
3133
3134# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003135 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003136 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3137 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003138 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003139
Andy Whitcroft65863862009-01-06 14:41:21 -08003140 # Should start with a space.
3141 $to =~ s/^(\S)/ $1/;
3142 # Should not end with a space.
3143 $to =~ s/\s+$//;
3144 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003145 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003146 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003147
Joe Perches3705ce52013-07-03 15:05:31 -07003148## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003149 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003150 if (ERROR("POINTER_LOCATION",
3151 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3152 $fix) {
3153 my $sub_from = $ident;
3154 my $sub_to = $ident;
3155 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003156 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003157 s@\Q$sub_from\E@$sub_to@;
3158 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003159 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003160 }
3161 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3162 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003163 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003164
Andy Whitcroft65863862009-01-06 14:41:21 -08003165 # Should start with a space.
3166 $to =~ s/^(\S)/ $1/;
3167 # Should not end with a space.
3168 $to =~ s/\s+$//;
3169 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003170 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003171 }
3172 # Modifiers should have spaces.
3173 $to =~ s/(\b$Modifier$)/$1 /;
3174
Joe Perches3705ce52013-07-03 15:05:31 -07003175## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003176 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003177 if (ERROR("POINTER_LOCATION",
3178 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3179 $fix) {
3180
3181 my $sub_from = $match;
3182 my $sub_to = $match;
3183 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003184 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003185 s@\Q$sub_from\E@$sub_to@;
3186 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003187 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003188 }
3189
3190# # no BUG() or BUG_ON()
3191# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3192# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3193# print "$herecurr";
3194# $clean = 0;
3195# }
3196
Andy Whitcroft8905a672007-11-28 16:21:06 -08003197 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003198 WARN("LINUX_VERSION_CODE",
3199 "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 -08003200 }
3201
Joe Perches17441222011-06-15 15:08:17 -07003202# check for uses of printk_ratelimit
3203 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003204 WARN("PRINTK_RATELIMITED",
3205"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003206 }
3207
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003208# printk should use KERN_* levels. Note that follow on printk's on the
3209# same line do not need a level, so we use the current block context
3210# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003211# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003212# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003213 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003214 my $ok = 0;
3215 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3216 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003217 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003218 # with "\n" ignore it, else it is to blame
3219 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3220 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3221 $ok = 1;
3222 }
3223 last;
3224 }
3225 }
3226 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003227 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3228 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003229 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003230 }
3231
Joe Perches243f3802012-05-31 16:26:09 -07003232 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3233 my $orig = $1;
3234 my $level = lc($orig);
3235 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003236 my $level2 = $level;
3237 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003238 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003239 "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 -07003240 }
3241
3242 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003243 if (WARN("PREFER_PR_LEVEL",
3244 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3245 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003246 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003247 s/\bpr_warning\b/pr_warn/;
3248 }
Joe Perches243f3802012-05-31 16:26:09 -07003249 }
3250
Joe Perchesdc139312013-02-21 16:44:13 -08003251 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3252 my $orig = $1;
3253 my $level = lc($orig);
3254 $level = "warn" if ($level eq "warning");
3255 $level = "dbg" if ($level eq "debug");
3256 WARN("PREFER_DEV_LEVEL",
3257 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3258 }
3259
Andy Whitcroft653d4872007-06-23 17:16:34 -07003260# function brace can't be on same line, except for #defines of do while,
3261# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003262 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003263 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003264 if (ERROR("OPEN_BRACE",
3265 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3266 $fix) {
3267 fix_delete_line($fixlinenr, $rawline);
3268 my $fixed_line = $rawline;
3269 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3270 my $line1 = $1;
3271 my $line2 = $2;
3272 fix_insert_line($fixlinenr, ltrim($line1));
3273 fix_insert_line($fixlinenr, "\+{");
3274 if ($line2 !~ /^\s*$/) {
3275 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3276 }
3277 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003278 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003279
Andy Whitcroft8905a672007-11-28 16:21:06 -08003280# open braces for enum, union and struct go on the same line.
3281 if ($line =~ /^.\s*{/ &&
3282 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003283 if (ERROR("OPEN_BRACE",
3284 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3285 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3286 fix_delete_line($fixlinenr - 1, $prevrawline);
3287 fix_delete_line($fixlinenr, $rawline);
3288 my $fixedline = rtrim($prevrawline) . " {";
3289 fix_insert_line($fixlinenr, $fixedline);
3290 $fixedline = $rawline;
3291 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3292 if ($fixedline !~ /^\+\s*$/) {
3293 fix_insert_line($fixlinenr, $fixedline);
3294 }
3295 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003296 }
3297
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003298# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003299 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3300 if (WARN("SPACING",
3301 "missing space after $1 definition\n" . $herecurr) &&
3302 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003303 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003304 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3305 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003306 }
3307
Joe Perches31070b52014-01-23 15:54:49 -08003308# Function pointer declarations
3309# check spacing between type, funcptr, and args
3310# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003311 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003312 my $declare = $1;
3313 my $pre_pointer_space = $2;
3314 my $post_pointer_space = $3;
3315 my $funcname = $4;
3316 my $post_funcname_space = $5;
3317 my $pre_args_space = $6;
3318
Joe Perches91f72e92014-04-03 14:49:12 -07003319# the $Declare variable will capture all spaces after the type
3320# so check it for a missing trailing missing space but pointer return types
3321# don't need a space so don't warn for those.
3322 my $post_declare_space = "";
3323 if ($declare =~ /(\s+)$/) {
3324 $post_declare_space = $1;
3325 $declare = rtrim($declare);
3326 }
3327 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003328 WARN("SPACING",
3329 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003330 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003331 }
3332
3333# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003334# This test is not currently implemented because these declarations are
3335# equivalent to
3336# int foo(int bar, ...)
3337# and this is form shouldn't/doesn't generate a checkpatch warning.
3338#
3339# elsif ($declare =~ /\s{2,}$/) {
3340# WARN("SPACING",
3341# "Multiple spaces after return type\n" . $herecurr);
3342# }
Joe Perches31070b52014-01-23 15:54:49 -08003343
3344# unnecessary space "type ( *funcptr)(args...)"
3345 if (defined $pre_pointer_space &&
3346 $pre_pointer_space =~ /^\s/) {
3347 WARN("SPACING",
3348 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3349 }
3350
3351# unnecessary space "type (* funcptr)(args...)"
3352 if (defined $post_pointer_space &&
3353 $post_pointer_space =~ /^\s/) {
3354 WARN("SPACING",
3355 "Unnecessary space before function pointer name\n" . $herecurr);
3356 }
3357
3358# unnecessary space "type (*funcptr )(args...)"
3359 if (defined $post_funcname_space &&
3360 $post_funcname_space =~ /^\s/) {
3361 WARN("SPACING",
3362 "Unnecessary space after function pointer name\n" . $herecurr);
3363 }
3364
3365# unnecessary space "type (*funcptr) (args...)"
3366 if (defined $pre_args_space &&
3367 $pre_args_space =~ /^\s/) {
3368 WARN("SPACING",
3369 "Unnecessary space before function pointer arguments\n" . $herecurr);
3370 }
3371
3372 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003373 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003374 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003375 }
3376 }
3377
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003378# check for spacing round square brackets; allowed:
3379# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003380# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3381# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003382 while ($line =~ /(.*?\s)\[/g) {
3383 my ($where, $prefix) = ($-[1], $1);
3384 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003385 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003386 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003387 if (ERROR("BRACKET_SPACE",
3388 "space prohibited before open square bracket '['\n" . $herecurr) &&
3389 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003390 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003391 s/^(\+.*?)\s+\[/$1\[/;
3392 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003393 }
3394 }
3395
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003396# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003397 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003398 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003399 my $ctx_before = substr($line, 0, $-[1]);
3400 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003401
3402 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003403 if ($name =~ /^(?:
3404 if|for|while|switch|return|case|
3405 volatile|__volatile__|
3406 __attribute__|format|__extension__|
3407 asm|__asm__)$/x)
3408 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003409 # cpp #define statements have non-optional spaces, ie
3410 # if there is a space between the name and the open
3411 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003412 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003413
3414 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003415 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003416
3417 # If this whole things ends with a type its most
3418 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003419 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003420
3421 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003422 if (WARN("SPACING",
3423 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3424 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003425 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003426 s/\b$name\s+\(/$name\(/;
3427 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003428 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003429 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003430
Andy Whitcroft653d4872007-06-23 17:16:34 -07003431# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003432 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003433 my $fixed_line = "";
3434 my $line_fixed = 0;
3435
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003436 my $ops = qr{
3437 <<=|>>=|<=|>=|==|!=|
3438 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3439 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003440 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003441 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003442 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003443 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003444
3445## print("element count: <" . $#elements . ">\n");
3446## foreach my $el (@elements) {
3447## print("el: <$el>\n");
3448## }
3449
3450 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003451 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003452
Joe Perches3705ce52013-07-03 15:05:31 -07003453 foreach my $el (@elements) {
3454 push(@fix_elements, substr($rawline, $off, length($el)));
3455 $off += length($el);
3456 }
3457
3458 $off = 0;
3459
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003460 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003461 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003462
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003463 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003464
3465 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3466
3467## print("n: <$n> good: <$good>\n");
3468
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003469 $off += length($elements[$n]);
3470
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003471 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003472 my $ca = substr($opline, 0, $off);
3473 my $cc = '';
3474 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3475 $cc = substr($opline, $off + length($elements[$n + 1]));
3476 }
3477 my $cb = "$ca$;$cc";
3478
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003479 my $a = '';
3480 $a = 'V' if ($elements[$n] ne '');
3481 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003482 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003483 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3484 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003485 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003486
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003487 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003488
3489 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003490 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003491 $c = 'V' if ($elements[$n + 2] ne '');
3492 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003493 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003494 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3495 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003496 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003497 } else {
3498 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003499 }
3500
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003501 my $ctx = "${a}x${c}";
3502
3503 my $at = "(ctx:$ctx)";
3504
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003505 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003506 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003507
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003508 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003509 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003510
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003511 # Get the full operator variant.
3512 my $opv = $op . substr($curr_vars, $off, 1);
3513
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003514 # Ignore operators passed as parameters.
3515 if ($op_type ne 'V' &&
3516 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3517
Andy Whitcroftcf655042008-03-04 14:28:20 -08003518# # Ignore comments
3519# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003520
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003521 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003522 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003523 if ($ctx !~ /.x[WEBC]/ &&
3524 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003525 if (ERROR("SPACING",
3526 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003527 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003528 $line_fixed = 1;
3529 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003530 }
3531
3532 # // is a comment
3533 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003534
Joe Perchesb00e4812014-04-03 14:49:33 -07003535 # : when part of a bitfield
3536 } elsif ($opv eq ':B') {
3537 # skip the bitfield test for now
3538
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003539 # No spaces for:
3540 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003541 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003542 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003543 if (ERROR("SPACING",
3544 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003545 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003546 if (defined $fix_elements[$n + 2]) {
3547 $fix_elements[$n + 2] =~ s/^\s+//;
3548 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003549 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003550 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003551 }
3552
Joe Perches23810972014-12-10 15:51:32 -08003553 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003554 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003555 my $rtrim_before = 0;
3556 my $space_after = 0;
3557 if ($ctx =~ /Wx./) {
3558 if (ERROR("SPACING",
3559 "space prohibited before that '$op' $at\n" . $hereptr)) {
3560 $line_fixed = 1;
3561 $rtrim_before = 1;
3562 }
3563 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003564 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003565 if (ERROR("SPACING",
3566 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003567 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003568 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003569 $space_after = 1;
3570 }
3571 }
3572 if ($rtrim_before || $space_after) {
3573 if ($rtrim_before) {
3574 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3575 } else {
3576 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3577 }
3578 if ($space_after) {
3579 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003580 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003581 }
3582
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003583 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003584 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003585 #warn "'*' is part of type\n";
3586
3587 # unary operators should have a space before and
3588 # none after. May be left adjacent to another
3589 # unary operator, or a cast
3590 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003591 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003592 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003593 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003594 if (ERROR("SPACING",
3595 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003596 if ($n != $last_after + 2) {
3597 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3598 $line_fixed = 1;
3599 }
Joe Perches3705ce52013-07-03 15:05:31 -07003600 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003601 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003602 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003603 # A unary '*' may be const
3604
3605 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003606 if (ERROR("SPACING",
3607 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003608 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003609 if (defined $fix_elements[$n + 2]) {
3610 $fix_elements[$n + 2] =~ s/^\s+//;
3611 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003612 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003613 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003614 }
3615
3616 # unary ++ and unary -- are allowed no space on one side.
3617 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003618 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003619 if (ERROR("SPACING",
3620 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003621 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003622 $line_fixed = 1;
3623 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003624 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003625 if ($ctx =~ /Wx[BE]/ ||
3626 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003627 if (ERROR("SPACING",
3628 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003629 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003630 $line_fixed = 1;
3631 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003632 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003633 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003634 if (ERROR("SPACING",
3635 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003636 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003637 if (defined $fix_elements[$n + 2]) {
3638 $fix_elements[$n + 2] =~ s/^\s+//;
3639 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003640 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003641 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003642 }
3643
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003644 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003645 } elsif ($op eq '<<' or $op eq '>>' or
3646 $op eq '&' or $op eq '^' or $op eq '|' or
3647 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003648 $op eq '*' or $op eq '/' or
3649 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003650 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003651 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003652 if (ERROR("SPACING",
3653 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003654 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3655 if (defined $fix_elements[$n + 2]) {
3656 $fix_elements[$n + 2] =~ s/^\s+//;
3657 }
Joe Perches3705ce52013-07-03 15:05:31 -07003658 $line_fixed = 1;
3659 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003660 }
3661
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003662 # A colon needs no spaces before when it is
3663 # terminating a case value or a label.
3664 } elsif ($opv eq ':C' || $opv eq ':L') {
3665 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003666 if (ERROR("SPACING",
3667 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003668 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003669 $line_fixed = 1;
3670 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003671 }
3672
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003673 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003674 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003675 my $ok = 0;
3676
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003677 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003678 if (($op eq '<' &&
3679 $cc =~ /^\S+\@\S+>/) ||
3680 ($op eq '>' &&
3681 $ca =~ /<\S+\@\S+$/))
3682 {
3683 $ok = 1;
3684 }
3685
Joe Perches84731622013-11-12 15:10:05 -08003686 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003687 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003688 my $msg_type = \&ERROR;
3689 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3690
3691 if (&{$msg_type}("SPACING",
3692 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003693 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3694 if (defined $fix_elements[$n + 2]) {
3695 $fix_elements[$n + 2] =~ s/^\s+//;
3696 }
Joe Perches3705ce52013-07-03 15:05:31 -07003697 $line_fixed = 1;
3698 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003699 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003700 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003701 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003702
3703## print("n: <$n> GOOD: <$good>\n");
3704
3705 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003706 }
Joe Perches3705ce52013-07-03 15:05:31 -07003707
3708 if (($#elements % 2) == 0) {
3709 $fixed_line = $fixed_line . $fix_elements[$#elements];
3710 }
3711
Joe Perches194f66f2014-08-06 16:11:03 -07003712 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3713 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003714 }
3715
3716
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003717 }
3718
Joe Perches786b6322013-07-03 15:05:32 -07003719# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003720 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003721 if (WARN("SPACING",
3722 "space prohibited before semicolon\n" . $herecurr) &&
3723 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003724 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003725 s/^(\+.*\S)\s+;/$1;/;
3726 }
3727 }
3728
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003729# check for multiple assignments
3730 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003731 CHK("MULTIPLE_ASSIGNMENTS",
3732 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003733 }
3734
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003735## # check for multiple declarations, allowing for a function declaration
3736## # continuation.
3737## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3738## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3739##
3740## # Remove any bracketed sections to ensure we do not
3741## # falsly report the parameters of functions.
3742## my $ln = $line;
3743## while ($ln =~ s/\([^\(\)]*\)//g) {
3744## }
3745## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003746## WARN("MULTIPLE_DECLARATION",
3747## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003748## }
3749## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003750
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003751#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003752 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3753 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003754 if (ERROR("SPACING",
3755 "space required before the open brace '{'\n" . $herecurr) &&
3756 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003757 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003758 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003759 }
3760
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003761## # check for blank lines before declarations
3762## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3763## $prevrawline =~ /^.\s*$/) {
3764## WARN("SPACING",
3765## "No blank lines before declarations\n" . $hereprev);
3766## }
3767##
3768
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003769# closing brace should have a space following it when it has anything
3770# on the line
3771 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003772 if (ERROR("SPACING",
3773 "space required after that close brace '}'\n" . $herecurr) &&
3774 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003775 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003776 s/}((?!(?:,|;|\)))\S)/} $1/;
3777 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003778 }
3779
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003780# check spacing on square brackets
3781 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003782 if (ERROR("SPACING",
3783 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3784 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003785 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003786 s/\[\s+/\[/;
3787 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003788 }
3789 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003790 if (ERROR("SPACING",
3791 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003793 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003794 s/\s+\]/\]/;
3795 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003796 }
3797
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003798# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003799 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3800 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003801 if (ERROR("SPACING",
3802 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3803 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003804 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003805 s/\(\s+/\(/;
3806 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003807 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003808 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003809 $line !~ /for\s*\(.*;\s+\)/ &&
3810 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003811 if (ERROR("SPACING",
3812 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3813 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003814 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003815 s/\s+\)/\)/;
3816 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003817 }
3818
Joe Perchese2826fd2014-08-06 16:10:48 -07003819# check unnecessary parentheses around addressof/dereference single $Lvals
3820# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3821
3822 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003823 my $var = $1;
3824 if (CHK("UNNECESSARY_PARENTHESES",
3825 "Unnecessary parentheses around $var\n" . $herecurr) &&
3826 $fix) {
3827 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3828 }
3829 }
3830
3831# check for unnecessary parentheses around function pointer uses
3832# ie: (foo->bar)(); should be foo->bar();
3833# but not "if (foo->bar) (" to avoid some false positives
3834 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3835 my $var = $2;
3836 if (CHK("UNNECESSARY_PARENTHESES",
3837 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3838 $fix) {
3839 my $var2 = deparenthesize($var);
3840 $var2 =~ s/\s//g;
3841 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3842 }
3843 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003844
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003845#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003846 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003847 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003848 if (WARN("INDENTED_LABEL",
3849 "labels should not be indented\n" . $herecurr) &&
3850 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003851 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003852 s/^(.)\s+/$1/;
3853 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003854 }
3855
Joe Perches5b9553a2014-04-03 14:49:21 -07003856# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003857 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003858 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003859 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003860 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3861 my $value = $1;
3862 $value = deparenthesize($value);
3863 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3864 ERROR("RETURN_PARENTHESES",
3865 "return is not a function, parentheses are not required\n" . $herecurr);
3866 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003867 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003868 ERROR("SPACING",
3869 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003870 }
3871 }
Joe Perches507e5142013-11-12 15:10:13 -08003872
Joe Perchesb43ae212014-06-23 13:22:07 -07003873# unnecessary return in a void function
3874# at end-of-function, with the previous line a single leading tab, then return;
3875# and the line before that not a goto label target like "out:"
3876 if ($sline =~ /^[ \+]}\s*$/ &&
3877 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3878 $linenr >= 3 &&
3879 $lines[$linenr - 3] =~ /^[ +]/ &&
3880 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003881 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003882 "void function return statements are not generally useful\n" . $hereprev);
3883 }
Joe Perches9819cf22014-06-04 16:12:09 -07003884
Joe Perches189248d2014-01-23 15:54:47 -08003885# if statements using unnecessary parentheses - ie: if ((foo == bar))
3886 if ($^V && $^V ge 5.10.0 &&
3887 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3888 my $openparens = $1;
3889 my $count = $openparens =~ tr@\(@\(@;
3890 my $msg = "";
3891 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3892 my $comp = $4; #Not $1 because of $LvalOrFunc
3893 $msg = " - maybe == should be = ?" if ($comp eq "==");
3894 WARN("UNNECESSARY_PARENTHESES",
3895 "Unnecessary parentheses$msg\n" . $herecurr);
3896 }
3897 }
3898
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003899# Return of what appears to be an errno should normally be -'ve
3900 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3901 my $name = $1;
3902 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003903 WARN("USE_NEGATIVE_ERRNO",
3904 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003905 }
3906 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003907
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003908# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003909 if ($line =~ /\b(if|while|for|switch)\(/) {
3910 if (ERROR("SPACING",
3911 "space required before the open parenthesis '('\n" . $herecurr) &&
3912 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003913 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003914 s/\b(if|while|for|switch)\(/$1 \(/;
3915 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003916 }
3917
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003918# Check for illegal assignment in if conditional -- and check for trailing
3919# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003920 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003921 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3922 ctx_statement_block($linenr, $realcnt, 0)
3923 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003924 my ($stat_next) = ctx_statement_block($line_nr_next,
3925 $remain_next, $off_next);
3926 $stat_next =~ s/\n./\n /g;
3927 ##print "stat<$stat> stat_next<$stat_next>\n";
3928
3929 if ($stat_next =~ /^\s*while\b/) {
3930 # If the statement carries leading newlines,
3931 # then count those as offsets.
3932 my ($whitespace) =
3933 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3934 my $offset =
3935 statement_rawlines($whitespace) - 1;
3936
3937 $suppress_whiletrailers{$line_nr_next +
3938 $offset} = 1;
3939 }
3940 }
3941 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003942 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003943 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003944 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003945
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003946 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003947 ERROR("ASSIGN_IN_IF",
3948 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003949 }
3950
3951 # Find out what is on the end of the line after the
3952 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003953 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003954 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003955 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003956 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3957 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003958 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003959 # Find out how long the conditional actually is.
3960 my @newlines = ($c =~ /\n/gs);
3961 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003962 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003963
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003964 $stat_real = raw_line($linenr, $cond_lines)
3965 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003966 if (defined($stat_real) && $cond_lines > 1) {
3967 $stat_real = "[...]\n$stat_real";
3968 }
3969
Joe Perches000d1cc12011-07-25 17:13:25 -07003970 ERROR("TRAILING_STATEMENTS",
3971 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003972 }
3973 }
3974
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003975# Check for bitwise tests written as boolean
3976 if ($line =~ /
3977 (?:
3978 (?:\[|\(|\&\&|\|\|)
3979 \s*0[xX][0-9]+\s*
3980 (?:\&\&|\|\|)
3981 |
3982 (?:\&\&|\|\|)
3983 \s*0[xX][0-9]+\s*
3984 (?:\&\&|\|\||\)|\])
3985 )/x)
3986 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003987 WARN("HEXADECIMAL_BOOLEAN_TEST",
3988 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003989 }
3990
Andy Whitcroft8905a672007-11-28 16:21:06 -08003991# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003992 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3993 my $s = $1;
3994 $s =~ s/$;//g; # Remove any comments
3995 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003996 ERROR("TRAILING_STATEMENTS",
3997 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003998 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003999 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004000# if should not continue a brace
4001 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004002 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004003 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004004 $herecurr);
4005 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004006# case and default should not have general statements after them
4007 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4008 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004009 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004010 \s*return\s+
4011 )/xg)
4012 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004013 ERROR("TRAILING_STATEMENTS",
4014 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004015 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004016
4017 # Check for }<nl>else {, these must be at the same
4018 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004019 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4020 $previndent == $indent) {
4021 if (ERROR("ELSE_AFTER_BRACE",
4022 "else should follow close brace '}'\n" . $hereprev) &&
4023 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4024 fix_delete_line($fixlinenr - 1, $prevrawline);
4025 fix_delete_line($fixlinenr, $rawline);
4026 my $fixedline = $prevrawline;
4027 $fixedline =~ s/}\s*$//;
4028 if ($fixedline !~ /^\+\s*$/) {
4029 fix_insert_line($fixlinenr, $fixedline);
4030 }
4031 $fixedline = $rawline;
4032 $fixedline =~ s/^(.\s*)else/$1} else/;
4033 fix_insert_line($fixlinenr, $fixedline);
4034 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004035 }
4036
Joe Perches8b8856f2014-08-06 16:11:14 -07004037 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4038 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004039 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4040
4041 # Find out what is on the end of the line after the
4042 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004043 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004044 $s =~ s/\n.*//g;
4045
4046 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004047 if (ERROR("WHILE_AFTER_BRACE",
4048 "while should follow close brace '}'\n" . $hereprev) &&
4049 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4050 fix_delete_line($fixlinenr - 1, $prevrawline);
4051 fix_delete_line($fixlinenr, $rawline);
4052 my $fixedline = $prevrawline;
4053 my $trailing = $rawline;
4054 $trailing =~ s/^\+//;
4055 $trailing = trim($trailing);
4056 $fixedline =~ s/}\s*$/} $trailing/;
4057 fix_insert_line($fixlinenr, $fixedline);
4058 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004059 }
4060 }
4061
Joe Perches95e2c602013-07-03 15:05:20 -07004062#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004063 while ($line =~ m{($Constant|$Lval)}g) {
4064 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004065
4066#gcc binary extension
4067 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004068 if (WARN("GCC_BINARY_CONSTANT",
4069 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4070 $fix) {
4071 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004072 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004073 s/\b$var\b/$hexval/;
4074 }
Joe Perches95e2c602013-07-03 15:05:20 -07004075 }
4076
4077#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004078 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004079 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004080#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004081 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004082#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004083 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4084#Ignore some three character SI units explicitly, like MiB and KHz
4085 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004086 while ($var =~ m{($Ident)}g) {
4087 my $word = $1;
4088 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004089 if ($check) {
4090 seed_camelcase_includes();
4091 if (!$file && !$camelcase_file_seeded) {
4092 seed_camelcase_file($realfile);
4093 $camelcase_file_seeded = 1;
4094 }
4095 }
Joe Perches7e781f62013-09-11 14:23:55 -07004096 if (!defined $camelcase{$word}) {
4097 $camelcase{$word} = 1;
4098 CHK("CAMELCASE",
4099 "Avoid CamelCase: <$word>\n" . $herecurr);
4100 }
Joe Perches34456862013-07-03 15:05:34 -07004101 }
Joe Perches323c1262012-12-17 16:02:07 -08004102 }
4103 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004104
4105#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004106 if ($line =~ /\#\s*define.*\\\s+$/) {
4107 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4108 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4109 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004110 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004111 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004112 }
4113
Andy Whitcroft653d4872007-06-23 17:16:34 -07004114#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004115 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004116 my $file = "$1.h";
4117 my $checkfile = "include/linux/$file";
4118 if (-f "$root/$checkfile" &&
4119 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004120 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004121 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004122 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004123 CHK("ARCH_INCLUDE_LINUX",
4124 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004125 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004126 WARN("INCLUDE_LINUX",
4127 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004128 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004129 }
4130 }
4131
Andy Whitcroft653d4872007-06-23 17:16:34 -07004132# multi-statement macros should be enclosed in a do while loop, grab the
4133# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004134# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004135 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4136 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004137 my $ln = $linenr;
4138 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004139 my ($off, $dstat, $dcond, $rest);
4140 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004141 my $has_flow_statement = 0;
4142 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004143 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004144 ctx_statement_block($linenr, $realcnt, 0);
4145 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004146 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004147 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004148
Joe Perches08a28432014-10-13 15:51:55 -07004149 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4150 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4151
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004152 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004153 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004154 $dstat =~ s/\\\n.//g;
4155 $dstat =~ s/^\s*//s;
4156 $dstat =~ s/\s*$//s;
4157
4158 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004159 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4160 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004161 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004162 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004163 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004164
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004165 # Flatten any obvious string concatentation.
4166 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4167 $dstat =~ s/$Ident\s*("X*")/$1/)
4168 {
4169 }
4170
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004171 my $exceptions = qr{
4172 $Declare|
4173 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004174 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004175 DECLARE_PER_CPU|
4176 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004177 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004178 union|
4179 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004180 \.$Ident\s*=\s*|
4181 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004182 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004183 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004184 if ($dstat ne '' &&
4185 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4186 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004187 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004188 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004189 $dstat !~ /$exceptions/ &&
4190 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004191 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004192 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004193 $dstat !~ /^for\s*$Constant$/ && # for (...)
4194 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4195 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004196 $dstat !~ /^\({/ && # ({...
4197 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004198 {
4199 $ctx =~ s/\n*$//;
4200 my $herectx = $here . "\n";
4201 my $cnt = statement_rawlines($ctx);
4202
4203 for (my $n = 0; $n < $cnt; $n++) {
4204 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004205 }
4206
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004207 if ($dstat =~ /;/) {
4208 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4209 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4210 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004211 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004212 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004213 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004214 }
Joe Perches5023d342012-12-17 16:01:47 -08004215
Joe Perches08a28432014-10-13 15:51:55 -07004216# check for macros with flow control, but without ## concatenation
4217# ## concatenation is commonly a macro that defines a function so ignore those
4218 if ($has_flow_statement && !$has_arg_concat) {
4219 my $herectx = $here . "\n";
4220 my $cnt = statement_rawlines($ctx);
4221
4222 for (my $n = 0; $n < $cnt; $n++) {
4223 $herectx .= raw_line($linenr, $n) . "\n";
4224 }
4225 WARN("MACRO_WITH_FLOW_CONTROL",
4226 "Macros with flow control statements should be avoided\n" . "$herectx");
4227 }
4228
Joe Perches481eb482012-12-17 16:01:56 -08004229# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004230
4231 } else {
4232 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004233 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4234 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004235 $line =~ /^\+.*\\$/) {
4236 WARN("LINE_CONTINUATIONS",
4237 "Avoid unnecessary line continuations\n" . $herecurr);
4238 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004239 }
4240
Joe Perchesb13edf72012-07-30 14:41:24 -07004241# do {} while (0) macro tests:
4242# single-statement macros do not need to be enclosed in do while (0) loop,
4243# macro should not end with a semicolon
4244 if ($^V && $^V ge 5.10.0 &&
4245 $realfile !~ m@/vmlinux.lds.h$@ &&
4246 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4247 my $ln = $linenr;
4248 my $cnt = $realcnt;
4249 my ($off, $dstat, $dcond, $rest);
4250 my $ctx = '';
4251 ($dstat, $dcond, $ln, $cnt, $off) =
4252 ctx_statement_block($linenr, $realcnt, 0);
4253 $ctx = $dstat;
4254
4255 $dstat =~ s/\\\n.//g;
4256
4257 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4258 my $stmts = $2;
4259 my $semis = $3;
4260
4261 $ctx =~ s/\n*$//;
4262 my $cnt = statement_rawlines($ctx);
4263 my $herectx = $here . "\n";
4264
4265 for (my $n = 0; $n < $cnt; $n++) {
4266 $herectx .= raw_line($linenr, $n) . "\n";
4267 }
4268
Joe Perchesac8e97f2012-08-21 16:15:53 -07004269 if (($stmts =~ tr/;/;/) == 1 &&
4270 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004271 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4272 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4273 }
4274 if (defined $semis && $semis ne "") {
4275 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4276 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4277 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004278 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4279 $ctx =~ s/\n*$//;
4280 my $cnt = statement_rawlines($ctx);
4281 my $herectx = $here . "\n";
4282
4283 for (my $n = 0; $n < $cnt; $n++) {
4284 $herectx .= raw_line($linenr, $n) . "\n";
4285 }
4286
4287 WARN("TRAILING_SEMICOLON",
4288 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004289 }
4290 }
4291
Mike Frysinger080ba922009-01-06 14:41:25 -08004292# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4293# all assignments may have only one of the following with an assignment:
4294# .
4295# ALIGN(...)
4296# VMLINUX_SYMBOL(...)
4297 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004298 WARN("MISSING_VMLINUX_SYMBOL",
4299 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004300 }
4301
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004302# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004303 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4304 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004305 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004306 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004307 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4308 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004309 my @allowed = ();
4310 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004311 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004312 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004313 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004314 for my $chunk (@chunks) {
4315 my ($cond, $block) = @{$chunk};
4316
Andy Whitcroft773647a2008-03-28 14:15:58 -07004317 # If the condition carries leading newlines, then count those as offsets.
4318 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4319 my $offset = statement_rawlines($whitespace) - 1;
4320
Joe Perchesaad4f612012-03-23 15:02:19 -07004321 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004322 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4323
4324 # We have looked at and allowed this specific line.
4325 $suppress_ifbraces{$ln + $offset} = 1;
4326
4327 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004328 $ln += statement_rawlines($block) - 1;
4329
Andy Whitcroft773647a2008-03-28 14:15:58 -07004330 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004331
4332 $seen++ if ($block =~ /^\s*{/);
4333
Joe Perchesaad4f612012-03-23 15:02:19 -07004334 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004335 if (statement_lines($cond) > 1) {
4336 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004337 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004338 }
4339 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004340 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004341 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004342 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004343 if (statement_block_size($block) > 1) {
4344 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004345 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004346 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004347 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004348 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004349 if ($seen) {
4350 my $sum_allowed = 0;
4351 foreach (@allowed) {
4352 $sum_allowed += $_;
4353 }
4354 if ($sum_allowed == 0) {
4355 WARN("BRACES",
4356 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4357 } elsif ($sum_allowed != $allow &&
4358 $seen != $allow) {
4359 CHK("BRACES",
4360 "braces {} should be used on all arms of this statement\n" . $herectx);
4361 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004362 }
4363 }
4364 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004365 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004366 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004367 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004368
Andy Whitcroftcf655042008-03-04 14:28:20 -08004369 # Check the pre-context.
4370 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4371 #print "APW: ALLOWED: pre<$1>\n";
4372 $allowed = 1;
4373 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004374
4375 my ($level, $endln, @chunks) =
4376 ctx_statement_full($linenr, $realcnt, $-[0]);
4377
Andy Whitcroftcf655042008-03-04 14:28:20 -08004378 # Check the condition.
4379 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004380 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004381 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004382 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004383 }
4384 if (statement_lines($cond) > 1) {
4385 #print "APW: ALLOWED: cond<$cond>\n";
4386 $allowed = 1;
4387 }
4388 if ($block =~/\b(?:if|for|while)\b/) {
4389 #print "APW: ALLOWED: block<$block>\n";
4390 $allowed = 1;
4391 }
4392 if (statement_block_size($block) > 1) {
4393 #print "APW: ALLOWED: lines block<$block>\n";
4394 $allowed = 1;
4395 }
4396 # Check the post-context.
4397 if (defined $chunks[1]) {
4398 my ($cond, $block) = @{$chunks[1]};
4399 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004400 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004401 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004402 if ($block =~ /^\s*\{/) {
4403 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4404 $allowed = 1;
4405 }
4406 }
4407 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004408 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004409 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004410
Andy Whitcroftf0556632008-10-15 22:02:23 -07004411 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004412 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004413 }
4414
Joe Perches000d1cc12011-07-25 17:13:25 -07004415 WARN("BRACES",
4416 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004417 }
4418 }
4419
Joe Perches0979ae62012-12-17 16:01:59 -08004420# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004421 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004422 CHK("BRACES",
4423 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4424 }
Joe Perches77b9a532013-07-03 15:05:29 -07004425 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004426 CHK("BRACES",
4427 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4428 }
4429
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004430# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004431 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4432 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004433 WARN("VOLATILE",
4434 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004435 }
4436
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004437# Check for user-visible strings broken across lines, which breaks the ability
4438# to grep for the string. Make exceptions when the previous string ends in a
4439# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4440# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4441 if ($line =~ /^\+\s*"[X\t]*"/ &&
4442 $prevline =~ /"\s*$/ &&
4443 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4444 if (WARN("SPLIT_STRING",
4445 "quoted string split across lines\n" . $hereprev) &&
4446 $fix &&
4447 $prevrawline =~ /^\+.*"\s*$/ &&
4448 $last_coalesced_string_linenr != $linenr - 1) {
4449 my $extracted_string = get_quoted_string($line, $rawline);
4450 my $comma_close = "";
4451 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4452 $comma_close = $1;
4453 }
4454
4455 fix_delete_line($fixlinenr - 1, $prevrawline);
4456 fix_delete_line($fixlinenr, $rawline);
4457 my $fixedline = $prevrawline;
4458 $fixedline =~ s/"\s*$//;
4459 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4460 fix_insert_line($fixlinenr - 1, $fixedline);
4461 $fixedline = $rawline;
4462 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4463 if ($fixedline !~ /\+\s*$/) {
4464 fix_insert_line($fixlinenr, $fixedline);
4465 }
4466 $last_coalesced_string_linenr = $linenr;
4467 }
4468 }
4469
4470# check for missing a space in a string concatenation
4471 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4472 WARN('MISSING_SPACE',
4473 "break quoted strings at a space character\n" . $hereprev);
4474 }
4475
4476# check for spaces before a quoted newline
4477 if ($rawline =~ /^.*\".*\s\\n/) {
4478 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4479 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4480 $fix) {
4481 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4482 }
4483
4484 }
4485
Joe Perchesf17dba42014-10-13 15:51:51 -07004486# concatenated string without spaces between elements
4487 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4488 CHK("CONCATENATED_STRING",
4489 "Concatenated strings should use spaces between elements\n" . $herecurr);
4490 }
4491
Joe Perches90ad30e2014-12-10 15:51:59 -08004492# uncoalesced string fragments
4493 if ($line =~ /"X*"\s*"/) {
4494 WARN("STRING_FRAGMENTS",
4495 "Consecutive strings are generally better as a single string\n" . $herecurr);
4496 }
4497
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004498# check for %L{u,d,i} in strings
4499 my $string;
4500 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4501 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4502 $string =~ s/%%/__/g;
4503 if ($string =~ /(?<!%)%L[udi]/) {
4504 WARN("PRINTF_L",
4505 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4506 last;
4507 }
4508 }
4509
4510# check for line continuations in quoted strings with odd counts of "
4511 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4512 WARN("LINE_CONTINUATIONS",
4513 "Avoid line continuations in quoted strings\n" . $herecurr);
4514 }
4515
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004516# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004517 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004518 CHK("REDUNDANT_CODE",
4519 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004520 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004521 }
4522
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004523# check for needless "if (<foo>) fn(<foo>)" uses
4524 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4525 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4526 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4527 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004528 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004529 }
4530 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004531
Joe Perchesebfdc402014-08-06 16:10:27 -07004532# check for unnecessary "Out of Memory" messages
4533 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4534 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4535 (defined $1 || defined $3) &&
4536 $linenr > 3) {
4537 my $testval = $2;
4538 my $testline = $lines[$linenr - 3];
4539
4540 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4541# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4542
4543 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4544 WARN("OOM_MESSAGE",
4545 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4546 }
4547 }
4548
Joe Perchesf78d98f2014-10-13 15:52:01 -07004549# check for logging functions with KERN_<LEVEL>
4550 if ($line !~ /printk\s*\(/ &&
4551 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4552 my $level = $1;
4553 if (WARN("UNNECESSARY_KERN_LEVEL",
4554 "Possible unnecessary $level\n" . $herecurr) &&
4555 $fix) {
4556 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4557 }
4558 }
4559
Joe Perchesabb08a52014-12-10 15:51:46 -08004560# check for mask then right shift without a parentheses
4561 if ($^V && $^V ge 5.10.0 &&
4562 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4563 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4564 WARN("MASK_THEN_SHIFT",
4565 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4566 }
4567
Joe Perchesb75ac612014-12-10 15:52:02 -08004568# check for pointer comparisons to NULL
4569 if ($^V && $^V ge 5.10.0) {
4570 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4571 my $val = $1;
4572 my $equal = "!";
4573 $equal = "" if ($4 eq "!=");
4574 if (CHK("COMPARISON_TO_NULL",
4575 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4576 $fix) {
4577 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4578 }
4579 }
4580 }
4581
Joe Perches8716de32013-09-11 14:24:05 -07004582# check for bad placement of section $InitAttribute (e.g.: __initdata)
4583 if ($line =~ /(\b$InitAttribute\b)/) {
4584 my $attr = $1;
4585 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4586 my $ptr = $1;
4587 my $var = $2;
4588 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4589 ERROR("MISPLACED_INIT",
4590 "$attr should be placed after $var\n" . $herecurr)) ||
4591 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4592 WARN("MISPLACED_INIT",
4593 "$attr should be placed after $var\n" . $herecurr))) &&
4594 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004595 $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 -07004596 }
4597 }
4598 }
4599
Joe Perchese970b8842013-11-12 15:10:10 -08004600# check for $InitAttributeData (ie: __initdata) with const
4601 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4602 my $attr = $1;
4603 $attr =~ /($InitAttributePrefix)(.*)/;
4604 my $attr_prefix = $1;
4605 my $attr_type = $2;
4606 if (ERROR("INIT_ATTRIBUTE",
4607 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4608 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004609 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004610 s/$InitAttributeData/${attr_prefix}initconst/;
4611 }
4612 }
4613
4614# check for $InitAttributeConst (ie: __initconst) without const
4615 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4616 my $attr = $1;
4617 if (ERROR("INIT_ATTRIBUTE",
4618 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4619 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004620 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004621 /(^\+\s*(?:static\s+))/;
4622 $lead = rtrim($1);
4623 $lead = "$lead " if ($lead !~ /^\+$/);
4624 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004625 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004626 }
4627 }
4628
Joe Perchesfbdb8132014-04-03 14:49:14 -07004629# don't use __constant_<foo> functions outside of include/uapi/
4630 if ($realfile !~ m@^include/uapi/@ &&
4631 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4632 my $constant_func = $1;
4633 my $func = $constant_func;
4634 $func =~ s/^__constant_//;
4635 if (WARN("CONSTANT_CONVERSION",
4636 "$constant_func should be $func\n" . $herecurr) &&
4637 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004638 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004639 }
4640 }
4641
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004642# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004643 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004644 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004645 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004646 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004647 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004648 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4649 }
4650 if ($delay > 2000) {
4651 WARN("LONG_UDELAY",
4652 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004653 }
4654 }
4655
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004656# warn about unexpectedly long msleep's
4657 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4658 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004659 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004660 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004661 }
4662 }
4663
Joe Perches36ec1932013-07-03 15:05:25 -07004664# check for comparisons of jiffies
4665 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4666 WARN("JIFFIES_COMPARISON",
4667 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4668 }
4669
Joe Perches9d7a34a2013-07-03 15:05:26 -07004670# check for comparisons of get_jiffies_64()
4671 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4672 WARN("JIFFIES_COMPARISON",
4673 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4674 }
4675
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004676# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004677# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004678# print "#ifdef in C files should be avoided\n";
4679# print "$herecurr";
4680# $clean = 0;
4681# }
4682
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004683# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004684 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004685 if (ERROR("SPACING",
4686 "exactly one space required after that #$1\n" . $herecurr) &&
4687 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004688 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004689 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4690 }
4691
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004692 }
4693
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004694# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004695 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4696 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004697 my $which = $1;
4698 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004699 CHK("UNCOMMENTED_DEFINITION",
4700 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004701 }
4702 }
4703# check for memory barriers without a comment.
4704 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4705 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004706 WARN("MEMORY_BARRIER",
4707 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004708 }
4709 }
4710# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004711 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004712 CHK("ARCH_DEFINES",
4713 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004714 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004715
Tobias Klauserd4977c72010-05-24 14:33:30 -07004716# Check that the storage class is at the beginning of a declaration
4717 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004718 WARN("STORAGE_CLASS",
4719 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004720 }
4721
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004722# check the location of the inline attribute, that it is between
4723# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004724 if ($line =~ /\b$Type\s+$Inline\b/ ||
4725 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004726 ERROR("INLINE_LOCATION",
4727 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004728 }
4729
Andy Whitcroft8905a672007-11-28 16:21:06 -08004730# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004731 if ($realfile !~ m@\binclude/uapi/@ &&
4732 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004733 if (WARN("INLINE",
4734 "plain inline is preferred over $1\n" . $herecurr) &&
4735 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004736 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004737
4738 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004739 }
4740
Joe Perches3d130fd2011-01-12 17:00:00 -08004741# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004742 if ($realfile !~ m@\binclude/uapi/@ &&
4743 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004744 WARN("PREFER_PACKED",
4745 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004746 }
4747
Joe Perches39b7e282011-07-25 17:13:24 -07004748# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004749 if ($realfile !~ m@\binclude/uapi/@ &&
4750 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004751 WARN("PREFER_ALIGNED",
4752 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004753 }
4754
Joe Perches5f14d3b2012-01-10 15:09:52 -08004755# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004756 if ($realfile !~ m@\binclude/uapi/@ &&
4757 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004758 if (WARN("PREFER_PRINTF",
4759 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4760 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004761 $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 -07004762
4763 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004764 }
4765
Joe Perches6061d942012-03-23 15:02:16 -07004766# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004767 if ($realfile !~ m@\binclude/uapi/@ &&
4768 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004769 if (WARN("PREFER_SCANF",
4770 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4771 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004772 $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 -07004773 }
Joe Perches6061d942012-03-23 15:02:16 -07004774 }
4775
Joe Perches619a9082014-12-10 15:51:35 -08004776# Check for __attribute__ weak, or __weak declarations (may have link issues)
4777 if ($^V && $^V ge 5.10.0 &&
4778 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4779 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4780 $line =~ /\b__weak\b/)) {
4781 ERROR("WEAK_DECLARATION",
4782 "Using weak declarations can have unintended link defects\n" . $herecurr);
4783 }
4784
Joe Perches8f53a9b2010-03-05 13:43:48 -08004785# check for sizeof(&)
4786 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004787 WARN("SIZEOF_ADDRESS",
4788 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004789 }
4790
Joe Perches66c80b62012-07-30 14:41:22 -07004791# check for sizeof without parenthesis
4792 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004793 if (WARN("SIZEOF_PARENTHESIS",
4794 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4795 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004796 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004797 }
Joe Perches66c80b62012-07-30 14:41:22 -07004798 }
4799
Joe Perches88982fe2012-12-17 16:02:00 -08004800# check for struct spinlock declarations
4801 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4802 WARN("USE_SPINLOCK_T",
4803 "struct spinlock should be spinlock_t\n" . $herecurr);
4804 }
4805
Joe Perchesa6962d72013-04-29 16:18:13 -07004806# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004807 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004808 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004809 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004810 if (WARN("PREFER_SEQ_PUTS",
4811 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4812 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004813 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004814 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004815 }
4816 }
4817
Andy Whitcroft554e1652012-01-10 15:09:57 -08004818# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004819 if ($^V && $^V ge 5.10.0 &&
4820 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004821 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004822
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004823 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004824 my $ms_val = $7;
4825 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004826
Andy Whitcroft554e1652012-01-10 15:09:57 -08004827 if ($ms_size =~ /^(0x|)0$/i) {
4828 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004829 "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 -08004830 } elsif ($ms_size =~ /^(0x|)1$/i) {
4831 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004832 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4833 }
4834 }
4835
Joe Perches98a9bba2014-01-23 15:54:52 -08004836# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4837 if ($^V && $^V ge 5.10.0 &&
4838 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4839 if (WARN("PREFER_ETHER_ADDR_COPY",
4840 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4841 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004842 $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 -08004843 }
4844 }
4845
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004846# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004847 if ($^V && $^V ge 5.10.0 &&
4848 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004849 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004850 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004851 my $call = $1;
4852 my $cast1 = deparenthesize($2);
4853 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004854 my $cast2 = deparenthesize($7);
4855 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004856 my $cast;
4857
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004858 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004859 $cast = "$cast1 or $cast2";
4860 } elsif ($cast1 ne "") {
4861 $cast = $cast1;
4862 } else {
4863 $cast = $cast2;
4864 }
4865 WARN("MINMAX",
4866 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004867 }
4868 }
4869
Joe Perches4a273192012-07-30 14:41:20 -07004870# check usleep_range arguments
4871 if ($^V && $^V ge 5.10.0 &&
4872 defined $stat &&
4873 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4874 my $min = $1;
4875 my $max = $7;
4876 if ($min eq $max) {
4877 WARN("USLEEP_RANGE",
4878 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4879 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4880 $min > $max) {
4881 WARN("USLEEP_RANGE",
4882 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4883 }
4884 }
4885
Joe Perches823b7942013-11-12 15:10:15 -08004886# check for naked sscanf
4887 if ($^V && $^V ge 5.10.0 &&
4888 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004889 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004890 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4891 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4892 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4893 my $lc = $stat =~ tr@\n@@;
4894 $lc = $lc + $linenr;
4895 my $stat_real = raw_line($linenr, 0);
4896 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4897 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4898 }
4899 WARN("NAKED_SSCANF",
4900 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4901 }
4902
Joe Perchesafc819a2014-06-04 16:12:08 -07004903# check for simple sscanf that should be kstrto<foo>
4904 if ($^V && $^V ge 5.10.0 &&
4905 defined $stat &&
4906 $line =~ /\bsscanf\b/) {
4907 my $lc = $stat =~ tr@\n@@;
4908 $lc = $lc + $linenr;
4909 my $stat_real = raw_line($linenr, 0);
4910 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4911 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4912 }
4913 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4914 my $format = $6;
4915 my $count = $format =~ tr@%@%@;
4916 if ($count == 1 &&
4917 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4918 WARN("SSCANF_TO_KSTRTO",
4919 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4920 }
4921 }
4922 }
4923
Joe Perches70dc8a42013-09-11 14:23:58 -07004924# check for new externs in .h files.
4925 if ($realfile =~ /\.h$/ &&
4926 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004927 if (CHK("AVOID_EXTERNS",
4928 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004929 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004930 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004931 }
4932 }
4933
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004934# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004935 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004936 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004937 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004938 my $function_name = $1;
4939 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004940
4941 my $s = $stat;
4942 if (defined $cond) {
4943 substr($s, 0, length($cond), '');
4944 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004945 if ($s =~ /^\s*;/ &&
4946 $function_name ne 'uninitialized_var')
4947 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004948 WARN("AVOID_EXTERNS",
4949 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004950 }
4951
4952 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004953 WARN("FUNCTION_ARGUMENTS",
4954 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004955 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004956
4957 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4958 $stat =~ /^.\s*extern\s+/)
4959 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004960 WARN("AVOID_EXTERNS",
4961 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004962 }
4963
4964# checks for new __setup's
4965 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4966 my $name = $1;
4967
4968 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004969 CHK("UNDOCUMENTED_SETUP",
4970 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004971 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004972 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004973
4974# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004975 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004976 WARN("UNNECESSARY_CASTS",
4977 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004978 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004979
Joe Perchesa640d252013-07-03 15:05:21 -07004980# alloc style
4981# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4982 if ($^V && $^V ge 5.10.0 &&
4983 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4984 CHK("ALLOC_SIZEOF_STRUCT",
4985 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4986 }
4987
Joe Perches60a55362014-06-04 16:12:07 -07004988# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4989 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004990 $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 -07004991 my $oldfunc = $3;
4992 my $a1 = $4;
4993 my $a2 = $10;
4994 my $newfunc = "kmalloc_array";
4995 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004996 my $r1 = $a1;
4997 my $r2 = $a2;
4998 if ($a1 =~ /^sizeof\s*\S/) {
4999 $r1 = $a2;
5000 $r2 = $a1;
5001 }
5002 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5003 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005004 if (WARN("ALLOC_WITH_MULTIPLY",
5005 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5006 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005007 $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 -07005008
5009 }
5010 }
5011 }
5012
Joe Perches972fdea2013-04-29 16:18:12 -07005013# check for krealloc arg reuse
5014 if ($^V && $^V ge 5.10.0 &&
5015 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5016 WARN("KREALLOC_ARG_REUSE",
5017 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5018 }
5019
Joe Perches5ce59ae2013-02-21 16:44:18 -08005020# check for alloc argument mismatch
5021 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5022 WARN("ALLOC_ARRAY_ARGS",
5023 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5024 }
5025
Joe Perchescaf2a542011-01-12 16:59:56 -08005026# check for multiple semicolons
5027 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005028 if (WARN("ONE_SEMICOLON",
5029 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5030 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005031 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005032 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005033 }
5034
Joe Perches0ab90192014-12-10 15:51:57 -08005035# check for #defines like: 1 << <digit> that could be BIT(digit)
5036 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5037 my $ull = "";
5038 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5039 if (CHK("BIT_MACRO",
5040 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5041 $fix) {
5042 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5043 }
5044 }
5045
Joe Perchese81f2392014-08-06 16:11:25 -07005046# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005047 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5048 my $has_break = 0;
5049 my $has_statement = 0;
5050 my $count = 0;
5051 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005052 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005053 $prevline--;
5054 my $rline = $rawlines[$prevline - 1];
5055 my $fline = $lines[$prevline - 1];
5056 last if ($fline =~ /^\@\@/);
5057 next if ($fline =~ /^\-/);
5058 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5059 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5060 next if ($fline =~ /^.[\s$;]*$/);
5061 $has_statement = 1;
5062 $count++;
5063 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5064 }
5065 if (!$has_break && $has_statement) {
5066 WARN("MISSING_BREAK",
5067 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5068 }
5069 }
5070
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005071# check for switch/default statements without a break;
5072 if ($^V && $^V ge 5.10.0 &&
5073 defined $stat &&
5074 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5075 my $ctx = '';
5076 my $herectx = $here . "\n";
5077 my $cnt = statement_rawlines($stat);
5078 for (my $n = 0; $n < $cnt; $n++) {
5079 $herectx .= raw_line($linenr, $n) . "\n";
5080 }
5081 WARN("DEFAULT_NO_BREAK",
5082 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005083 }
5084
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005085# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005086 if ($line =~ /\b__FUNCTION__\b/) {
5087 if (WARN("USE_FUNC",
5088 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5089 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005090 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005091 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005092 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005093
Joe Perches62ec8182015-02-13 14:38:18 -08005094# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5095 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5096 ERROR("DATE_TIME",
5097 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5098 }
5099
Joe Perches2c924882012-03-23 15:02:20 -07005100# check for use of yield()
5101 if ($line =~ /\byield\s*\(\s*\)/) {
5102 WARN("YIELD",
5103 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5104 }
5105
Joe Perches179f8f42013-07-03 15:05:30 -07005106# check for comparisons against true and false
5107 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5108 my $lead = $1;
5109 my $arg = $2;
5110 my $test = $3;
5111 my $otype = $4;
5112 my $trail = $5;
5113 my $op = "!";
5114
5115 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5116
5117 my $type = lc($otype);
5118 if ($type =~ /^(?:true|false)$/) {
5119 if (("$test" eq "==" && "$type" eq "true") ||
5120 ("$test" eq "!=" && "$type" eq "false")) {
5121 $op = "";
5122 }
5123
5124 CHK("BOOL_COMPARISON",
5125 "Using comparison to $otype is error prone\n" . $herecurr);
5126
5127## maybe suggesting a correct construct would better
5128## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5129
5130 }
5131 }
5132
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005133# check for semaphores initialized locked
5134 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005135 WARN("CONSIDER_COMPLETION",
5136 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005137 }
Joe Perches6712d852012-03-23 15:02:20 -07005138
Joe Perches67d0a072011-10-31 17:13:10 -07005139# recommend kstrto* over simple_strto* and strict_strto*
5140 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005141 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005142 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005143 }
Joe Perches6712d852012-03-23 15:02:20 -07005144
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005145# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005146 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005147 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005148 "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 -07005149 }
Joe Perches6712d852012-03-23 15:02:20 -07005150
Emese Revfy79404842010-03-05 13:43:53 -08005151# check for various ops structs, ensure they are const.
5152 my $struct_ops = qr{acpi_dock_ops|
5153 address_space_operations|
5154 backlight_ops|
5155 block_device_operations|
5156 dentry_operations|
5157 dev_pm_ops|
5158 dma_map_ops|
5159 extent_io_ops|
5160 file_lock_operations|
5161 file_operations|
5162 hv_ops|
5163 ide_dma_ops|
5164 intel_dvo_dev_ops|
5165 item_operations|
5166 iwl_ops|
5167 kgdb_arch|
5168 kgdb_io|
5169 kset_uevent_ops|
5170 lock_manager_operations|
5171 microcode_ops|
5172 mtrr_ops|
5173 neigh_ops|
5174 nlmsvc_binding|
5175 pci_raw_ops|
5176 pipe_buf_operations|
5177 platform_hibernation_ops|
5178 platform_suspend_ops|
5179 proto_ops|
5180 rpc_pipe_ops|
5181 seq_operations|
5182 snd_ac97_build_ops|
5183 soc_pcmcia_socket_ops|
5184 stacktrace_ops|
5185 sysfs_ops|
5186 tty_operations|
5187 usb_mon_operations|
5188 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005189 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08005190 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005191 WARN("CONST_STRUCT",
5192 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005193 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005194 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005195
5196# use of NR_CPUS is usually wrong
5197# ignore definitions of NR_CPUS and usage to define arrays as likely right
5198 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005199 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5200 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005201 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5202 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5203 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005204 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005205 WARN("NR_CPUS",
5206 "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 -07005207 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005208
Joe Perches52ea8502013-11-12 15:10:09 -08005209# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5210 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5211 ERROR("DEFINE_ARCH_HAS",
5212 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5213 }
5214
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005215# whine mightly about in_atomic
5216 if ($line =~ /\bin_atomic\s*\(/) {
5217 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005218 ERROR("IN_ATOMIC",
5219 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005220 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005221 WARN("IN_ATOMIC",
5222 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005223 }
5224 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005225
5226# check for lockdep_set_novalidate_class
5227 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5228 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5229 if ($realfile !~ m@^kernel/lockdep@ &&
5230 $realfile !~ m@^include/linux/lockdep@ &&
5231 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005232 ERROR("LOCKDEP",
5233 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005234 }
5235 }
Dave Jones88f88312011-01-12 16:59:59 -08005236
5237 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5238 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005239 WARN("EXPORTED_WORLD_WRITABLE",
5240 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005241 }
Joe Perches24358802014-04-03 14:49:13 -07005242
Joe Perches515a2352014-04-03 14:49:24 -07005243# Mode permission misuses where it seems decimal should be octal
5244# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5245 if ($^V && $^V ge 5.10.0 &&
5246 $line =~ /$mode_perms_search/) {
5247 foreach my $entry (@mode_permission_funcs) {
5248 my $func = $entry->[0];
5249 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005250
Joe Perches515a2352014-04-03 14:49:24 -07005251 my $skip_args = "";
5252 if ($arg_pos > 1) {
5253 $arg_pos--;
5254 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5255 }
5256 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5257 if ($line =~ /$test/) {
5258 my $val = $1;
5259 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005260
Joe Perches515a2352014-04-03 14:49:24 -07005261 if ($val !~ /^0$/ &&
5262 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5263 length($val) ne 4)) {
5264 ERROR("NON_OCTAL_PERMISSIONS",
5265 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005266 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5267 ERROR("EXPORTED_WORLD_WRITABLE",
5268 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005269 }
Joe Perches24358802014-04-03 14:49:13 -07005270 }
5271 }
5272 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005273 }
5274
5275 # If we have no input at all, then there is nothing to report on
5276 # so just keep quiet.
5277 if ($#rawlines == -1) {
5278 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005279 }
5280
Andy Whitcroft8905a672007-11-28 16:21:06 -08005281 # In mailback mode only produce a report in the negative, for
5282 # things that appear to be patches.
5283 if ($mailback && ($clean == 1 || !$is_patch)) {
5284 exit(0);
5285 }
5286
5287 # This is not a patch, and we are are in 'no-patch' mode so
5288 # just keep quiet.
5289 if (!$chk_patch && !$is_patch) {
5290 exit(0);
5291 }
5292
5293 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005294 ERROR("NOT_UNIFIED_DIFF",
5295 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005296 }
5297 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005298 ERROR("MISSING_SIGN_OFF",
5299 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005300 }
5301
Andy Whitcroft8905a672007-11-28 16:21:06 -08005302 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005303 if ($summary && !($clean == 1 && $quiet == 1)) {
5304 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005305 print "total: $cnt_error errors, $cnt_warn warnings, " .
5306 (($check)? "$cnt_chk checks, " : "") .
5307 "$cnt_lines lines checked\n";
5308 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005309 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005310
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005311 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005312
5313 if ($^V lt 5.10.0) {
5314 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5315 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5316 }
5317
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005318 # If there were whitespace errors which cleanpatch can fix
5319 # then suggest that.
5320 if ($rpt_cleaners) {
5321 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5322 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005323 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005324 }
5325 }
5326
Joe Perches91bfe482013-09-11 14:23:59 -07005327 hash_show_words(\%use_type, "Used");
5328 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005329
Joe Perchesd752fcc2014-08-06 16:11:05 -07005330 if ($clean == 0 && $fix &&
5331 ("@rawlines" ne "@fixed" ||
5332 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005333 my $newfile = $filename;
5334 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005335 my $linecount = 0;
5336 my $f;
5337
Joe Perchesd752fcc2014-08-06 16:11:05 -07005338 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5339
Joe Perches3705ce52013-07-03 15:05:31 -07005340 open($f, '>', $newfile)
5341 or die "$P: Can't open $newfile for write\n";
5342 foreach my $fixed_line (@fixed) {
5343 $linecount++;
5344 if ($file) {
5345 if ($linecount > 3) {
5346 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005347 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005348 }
5349 } else {
5350 print $f $fixed_line . "\n";
5351 }
5352 }
5353 close($f);
5354
5355 if (!$quiet) {
5356 print << "EOM";
5357Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5358
5359Do _NOT_ trust the results written to this file.
5360Do _NOT_ submit these changes without inspecting them for correctness.
5361
5362This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5363No warranties, expressed or implied...
5364
5365EOM
5366 }
5367 }
5368
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005369 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005370 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005371 }
5372 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005373 print << "EOM";
5374$vname has style problems, please review.
5375
5376If any of these errors are false positives, please report
5377them to the maintainer, see CHECKPATCH in MAINTAINERS.
5378EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005379 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005380
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005381 return $clean;
5382}