blob: 2b77d96db73554a7cd297e72d64688530596fce7 [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 Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy 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 Perches2ac73b42014-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 Perches2ac73b42014-06-04 16:12:05 -0700156$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800157
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700158my $exit = 0;
159
Dave Hansend62a2012013-09-11 14:23:56 -0700160if ($^V && $^V lt $minimum_perl_version) {
161 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
162 if (!$ignore_perl_version) {
163 exit(1);
164 }
165}
166
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700167if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700168 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700169 exit(1);
170}
171
Joe Perches91bfe482013-09-11 14:23:59 -0700172sub hash_save_array_words {
173 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700174
Joe Perches91bfe482013-09-11 14:23:59 -0700175 my @array = split(/,/, join(',', @$arrayRef));
176 foreach my $word (@array) {
177 $word =~ s/\s*\n?$//g;
178 $word =~ s/^\s*//g;
179 $word =~ s/\s+/ /g;
180 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700181
Joe Perches91bfe482013-09-11 14:23:59 -0700182 next if ($word =~ m/^\s*#/);
183 next if ($word =~ m/^\s*$/);
184
185 $hashRef->{$word}++;
186 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700187}
188
Joe Perches91bfe482013-09-11 14:23:59 -0700189sub hash_show_words {
190 my ($hashRef, $prefix) = @_;
191
Joe Perches58cb3cf2013-09-11 14:24:04 -0700192 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700193 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700194 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700195 print " $word";
196 }
197 print "\n\n";
198 }
199}
200
201hash_save_array_words(\%ignore_type, \@ignore);
202hash_save_array_words(\%use_type, \@use);
203
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800204my $dbg_values = 0;
205my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700206my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700207my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800208for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800209 ## no critic
210 eval "\${dbg_$key} = '$debug{$key}';";
211 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800212}
213
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700214my $rpt_cleaners = 0;
215
Andy Whitcroft8905a672007-11-28 16:21:06 -0800216if ($terse) {
217 $emacs = 1;
218 $quiet++;
219}
220
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700221if ($tree) {
222 if (defined $root) {
223 if (!top_of_kernel_tree($root)) {
224 die "$P: $root: --root does not point at a valid tree\n";
225 }
226 } else {
227 if (top_of_kernel_tree('.')) {
228 $root = '.';
229 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
230 top_of_kernel_tree($1)) {
231 $root = $1;
232 }
233 }
234
235 if (!defined $root) {
236 print "Must be run from the top-level dir. of a kernel tree\n";
237 exit(2);
238 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700239}
240
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700241my $emitted_corrupt = 0;
242
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700243our $Ident = qr{
244 [A-Za-z_][A-Za-z\d_]*
245 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
246 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700247our $Storage = qr{extern|static|asmlinkage};
248our $Sparse = qr{
249 __user|
250 __kernel|
251 __force|
252 __iomem|
253 __must_check|
254 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800255 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700256 __ref|
257 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700258 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800259our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
260our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
261our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
262our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
263our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700264
Wolfram Sang52131292010-03-05 13:43:51 -0800265# Notes to $Attribute:
266# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700267our $Attribute = qr{
268 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700269 __percpu|
270 __nocast|
271 __safe|
272 __bitwise__|
273 __packed__|
274 __packed2__|
275 __naked|
276 __maybe_unused|
277 __always_unused|
278 __noreturn|
279 __used|
280 __cold|
281 __noclone|
282 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700283 __read_mostly|
284 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700285 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700286 ____cacheline_aligned|
287 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800288 ____cacheline_internodealigned_in_smp|
289 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700291our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700292our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700293our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
294our $Lval = qr{$Ident(?:$Member)*};
295
Joe Perches95e2c602013-07-03 15:05:20 -0700296our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
297our $Binary = qr{(?i)0b[01]+$Int_type?};
298our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
299our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700300our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800301our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800302our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
303our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
304our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800305our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700306our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800307our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700308our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700309our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700310our $Operators = qr{
311 <=|>=|==|!=|
312 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700313 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700314 }x;
315
Joe Perches91cb5192014-04-03 14:49:32 -0700316our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
317
Andy Whitcroft8905a672007-11-28 16:21:06 -0800318our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700319our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700320our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800321our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700322our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800323our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700324our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800325
Joe Perches15662b32011-10-31 17:13:12 -0700326our $NON_ASCII_UTF8 = qr{
327 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700328 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
329 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
330 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
331 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
332 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
333 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
334}x;
335
Joe Perches15662b32011-10-31 17:13:12 -0700336our $UTF8 = qr{
337 [\x09\x0A\x0D\x20-\x7E] # ASCII
338 | $NON_ASCII_UTF8
339}x;
340
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700341our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700342 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700343 atomic_t
344)};
345
Joe Perches691e6692010-03-05 13:43:51 -0800346our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700347 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700348 (?:[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 -0700349 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700350 panic|
Joe Perches06668722013-11-12 15:10:07 -0800351 MODULE_[A-Z_]+|
352 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800353)};
354
Joe Perches20112472011-07-25 17:13:23 -0700355our $signature_tags = qr{(?xi:
356 Signed-off-by:|
357 Acked-by:|
358 Tested-by:|
359 Reviewed-by:|
360 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700361 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700362 To:|
363 Cc:
364)};
365
Joe Perches18130872014-08-06 16:11:22 -0700366our @typeListMisordered = (
367 qr{char\s+(?:un)?signed},
368 qr{int\s+(?:(?:un)?signed\s+)?short\s},
369 qr{int\s+short(?:\s+(?:un)?signed)},
370 qr{short\s+int(?:\s+(?:un)?signed)},
371 qr{(?:un)?signed\s+int\s+short},
372 qr{short\s+(?:un)?signed},
373 qr{long\s+int\s+(?:un)?signed},
374 qr{int\s+long\s+(?:un)?signed},
375 qr{long\s+(?:un)?signed\s+int},
376 qr{int\s+(?:un)?signed\s+long},
377 qr{int\s+(?:un)?signed},
378 qr{int\s+long\s+long\s+(?:un)?signed},
379 qr{long\s+long\s+int\s+(?:un)?signed},
380 qr{long\s+long\s+(?:un)?signed\s+int},
381 qr{long\s+long\s+(?:un)?signed},
382 qr{long\s+(?:un)?signed},
383);
384
Andy Whitcroft8905a672007-11-28 16:21:06 -0800385our @typeList = (
386 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700387 qr{(?:(?:un)?signed\s+)?char},
388 qr{(?:(?:un)?signed\s+)?short\s+int},
389 qr{(?:(?:un)?signed\s+)?short},
390 qr{(?:(?:un)?signed\s+)?int},
391 qr{(?:(?:un)?signed\s+)?long\s+int},
392 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
393 qr{(?:(?:un)?signed\s+)?long\s+long},
394 qr{(?:(?:un)?signed\s+)?long},
395 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800396 qr{float},
397 qr{double},
398 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399 qr{struct\s+$Ident},
400 qr{union\s+$Ident},
401 qr{enum\s+$Ident},
402 qr{${Ident}_t},
403 qr{${Ident}_handler},
404 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700405 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800406);
Joe Perches8716de32013-09-11 14:24:05 -0700407our @typeListWithAttr = (
408 @typeList,
409 qr{struct\s+$InitAttribute\s+$Ident},
410 qr{union\s+$InitAttribute\s+$Ident},
411);
412
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700413our @modifierList = (
414 qr{fastcall},
415);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800416
Joe Perches24358802014-04-03 14:49:13 -0700417our @mode_permission_funcs = (
418 ["module_param", 3],
419 ["module_param_(?:array|named|string)", 4],
420 ["module_param_array_named", 5],
421 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
422 ["proc_create(?:_data|)", 2],
423 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
424);
425
Joe Perches515a2352014-04-03 14:49:24 -0700426#Create a search pattern for all these functions to speed up a loop below
427our $mode_perms_search = "";
428foreach my $entry (@mode_permission_funcs) {
429 $mode_perms_search .= '|' if ($mode_perms_search ne "");
430 $mode_perms_search .= $entry->[0];
431}
432
Wolfram Sang7840a942010-08-09 17:20:57 -0700433our $allowed_asm_includes = qr{(?x:
434 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700435 memory|
436 time|
437 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700438)};
439# memory.h: ARM has a custom one
440
Kees Cook66b47b42014-10-13 15:51:57 -0700441# Load common spelling mistakes and build regular expression list.
442my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700443my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700444
Joe Perches36061e32014-12-10 15:51:43 -0800445if (open(my $spelling, '<', $spelling_file)) {
446 my @spelling_list;
447 while (<$spelling>) {
448 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700449
Joe Perches36061e32014-12-10 15:51:43 -0800450 $line =~ s/\s*\n?$//g;
451 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700452
Joe Perches36061e32014-12-10 15:51:43 -0800453 next if ($line =~ m/^\s*#/);
454 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700455
Joe Perches36061e32014-12-10 15:51:43 -0800456 my ($suspect, $fix) = split(/\|\|/, $line);
457
458 push(@spelling_list, $suspect);
459 $spelling_fix{$suspect} = $fix;
460 }
461 close($spelling);
462 $misspellings = join("|", @spelling_list);
463} else {
464 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700465}
Kees Cook66b47b42014-10-13 15:51:57 -0700466
Andy Whitcroft8905a672007-11-28 16:21:06 -0800467sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700468 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
469 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700470 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700471 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700472 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800473 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700474 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800475 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800476 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700477 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700478 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800479 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700480 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800481 }x;
Joe Perches18130872014-08-06 16:11:22 -0700482 $NonptrTypeMisordered = qr{
483 (?:$Modifier\s+|const\s+)*
484 (?:
485 (?:${Misordered}\b)
486 )
487 (?:\s+$Modifier|\s+const)*
488 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700489 $NonptrTypeWithAttr = qr{
490 (?:$Modifier\s+|const\s+)*
491 (?:
492 (?:typeof|__typeof__)\s*\([^\)]*\)|
493 (?:$typeTypedefs\b)|
494 (?:${allWithAttr}\b)
495 )
496 (?:\s+$Modifier|\s+const)*
497 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700499 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700500 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700501 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800502 }x;
Joe Perches18130872014-08-06 16:11:22 -0700503 $TypeMisordered = qr{
504 $NonptrTypeMisordered
505 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
506 (?:\s+$Inline|\s+$Modifier)*
507 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700508 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700509 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800510}
511build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700512
Joe Perches7d2367a2011-07-25 17:13:22 -0700513our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700514
515# Using $balanced_parens, $LvalOrFunc, or $FuncArg
516# requires at least perl version v5.10.0
517# Any use must be runtime checked with $^V
518
519our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700520our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800521our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700522
Joe Perchesf8422302014-08-06 16:11:31 -0700523our $declaration_macros = qr{(?x:
524 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
525 (?:$Storage\s+)?LIST_HEAD\s*\(|
526 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
527)};
528
Joe Perches7d2367a2011-07-25 17:13:22 -0700529sub deparenthesize {
530 my ($string) = @_;
531 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700532
533 while ($string =~ /^\s*\(.*\)\s*$/) {
534 $string =~ s@^\s*\(\s*@@;
535 $string =~ s@\s*\)\s*$@@;
536 }
537
Joe Perches7d2367a2011-07-25 17:13:22 -0700538 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700539
Joe Perches7d2367a2011-07-25 17:13:22 -0700540 return $string;
541}
542
Joe Perches34456862013-07-03 15:05:34 -0700543sub seed_camelcase_file {
544 my ($file) = @_;
545
546 return if (!(-f $file));
547
548 local $/;
549
550 open(my $include_file, '<', "$file")
551 or warn "$P: Can't read '$file' $!\n";
552 my $text = <$include_file>;
553 close($include_file);
554
555 my @lines = split('\n', $text);
556
557 foreach my $line (@lines) {
558 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
559 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
560 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800561 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
562 $camelcase{$1} = 1;
563 } 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 -0700564 $camelcase{$1} = 1;
565 }
566 }
567}
568
569my $camelcase_seeded = 0;
570sub seed_camelcase_includes {
571 return if ($camelcase_seeded);
572
573 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700574 my $camelcase_cache = "";
575 my @include_files = ();
576
577 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700578
Richard Genoud3645e322014-02-10 14:25:32 -0800579 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700580 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
581 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700582 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700583 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700584 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700585 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700586 @include_files = split('\n', $files);
587 foreach my $file (@include_files) {
588 my $date = POSIX::strftime("%Y%m%d%H%M",
589 localtime((stat $file)[9]));
590 $last_mod_date = $date if ($last_mod_date < $date);
591 }
592 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700593 }
Joe Perchesc707a812013-07-08 16:00:43 -0700594
595 if ($camelcase_cache ne "" && -f $camelcase_cache) {
596 open(my $camelcase_file, '<', "$camelcase_cache")
597 or warn "$P: Can't read '$camelcase_cache' $!\n";
598 while (<$camelcase_file>) {
599 chomp;
600 $camelcase{$_} = 1;
601 }
602 close($camelcase_file);
603
604 return;
605 }
606
Richard Genoud3645e322014-02-10 14:25:32 -0800607 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700608 $files = `git ls-files "include/*.h"`;
609 @include_files = split('\n', $files);
610 }
611
Joe Perches34456862013-07-03 15:05:34 -0700612 foreach my $file (@include_files) {
613 seed_camelcase_file($file);
614 }
Joe Perches351b2a12013-07-03 15:05:36 -0700615
Joe Perchesc707a812013-07-08 16:00:43 -0700616 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700617 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700618 open(my $camelcase_file, '>', "$camelcase_cache")
619 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700620 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
621 print $camelcase_file ("$_\n");
622 }
623 close($camelcase_file);
624 }
Joe Perches34456862013-07-03 15:05:34 -0700625}
626
Joe Perchesd311cd42014-08-06 16:10:57 -0700627sub git_commit_info {
628 my ($commit, $id, $desc) = @_;
629
630 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
631
632 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
633 $output =~ s/^\s*//gm;
634 my @lines = split("\n", $output);
635
636 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
637# Maybe one day convert this block of bash into something that returns
638# all matching commit ids, but it's very slow...
639#
640# echo "checking commits $1..."
641# git rev-list --remotes | grep -i "^$1" |
642# while read line ; do
643# git log --format='%H %s' -1 $line |
644# echo "commit $(cut -c 1-12,41-)"
645# done
646 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
647 } else {
648 $id = substr($lines[0], 0, 12);
649 $desc = substr($lines[0], 41);
650 }
651
652 return ($id, $desc);
653}
654
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700655$chk_signoff = 0 if ($file);
656
Andy Whitcroft00df3442007-06-08 13:47:06 -0700657my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800658my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700659my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700660my @fixed_inserted = ();
661my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700662my $fixlinenr = -1;
663
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800664my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700665for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800666 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700667 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800668 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700669 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800670 } elsif ($filename eq '-') {
671 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700672 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800673 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700674 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700675 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800676 if ($filename eq '-') {
677 $vname = 'Your patch';
678 } else {
679 $vname = $filename;
680 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800681 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700682 chomp;
683 push(@rawlines, $_);
684 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800685 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800686 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700687 $exit = 1;
688 }
689 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800690 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700691 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700692 @fixed_inserted = ();
693 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700694 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700695}
696
697exit($exit);
698
699sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700700 my ($root) = @_;
701
702 my @tree_check = (
703 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
704 "README", "Documentation", "arch", "include", "drivers",
705 "fs", "init", "ipc", "kernel", "lib", "scripts",
706 );
707
708 foreach my $check (@tree_check) {
709 if (! -e $root . '/' . $check) {
710 return 0;
711 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700712 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700713 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700714}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700715
Joe Perches20112472011-07-25 17:13:23 -0700716sub parse_email {
717 my ($formatted_email) = @_;
718
719 my $name = "";
720 my $address = "";
721 my $comment = "";
722
723 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
724 $name = $1;
725 $address = $2;
726 $comment = $3 if defined $3;
727 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
728 $address = $1;
729 $comment = $2 if defined $2;
730 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
731 $address = $1;
732 $comment = $2 if defined $2;
733 $formatted_email =~ s/$address.*$//;
734 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700735 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700736 $name =~ s/^\"|\"$//g;
737 # If there's a name left after stripping spaces and
738 # leading quotes, and the address doesn't have both
739 # leading and trailing angle brackets, the address
740 # is invalid. ie:
741 # "joe smith joe@smith.com" bad
742 # "joe smith <joe@smith.com" bad
743 if ($name ne "" && $address !~ /^<[^>]+>$/) {
744 $name = "";
745 $address = "";
746 $comment = "";
747 }
748 }
749
Joe Perches3705ce52013-07-03 15:05:31 -0700750 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700751 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700752 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700753 $address =~ s/^\<|\>$//g;
754
755 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
756 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
757 $name = "\"$name\"";
758 }
759
760 return ($name, $address, $comment);
761}
762
763sub format_email {
764 my ($name, $address) = @_;
765
766 my $formatted_email;
767
Joe Perches3705ce52013-07-03 15:05:31 -0700768 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700769 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700770 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700771
772 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
773 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
774 $name = "\"$name\"";
775 }
776
777 if ("$name" eq "") {
778 $formatted_email = "$address";
779 } else {
780 $formatted_email = "$name <$address>";
781 }
782
783 return $formatted_email;
784}
785
Joe Perchesd311cd42014-08-06 16:10:57 -0700786sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700787 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700788
Joe Perchesbd474ca2014-08-06 16:11:10 -0700789 foreach my $path (split(/:/, $ENV{PATH})) {
790 if (-e "$path/$bin") {
791 return "$path/$bin";
792 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700793 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700794
Joe Perchesbd474ca2014-08-06 16:11:10 -0700795 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700796}
797
Joe Perches000d1cc12011-07-25 17:13:25 -0700798sub which_conf {
799 my ($conf) = @_;
800
801 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
802 if (-e "$path/$conf") {
803 return "$path/$conf";
804 }
805 }
806
807 return "";
808}
809
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700810sub expand_tabs {
811 my ($str) = @_;
812
813 my $res = '';
814 my $n = 0;
815 for my $c (split(//, $str)) {
816 if ($c eq "\t") {
817 $res .= ' ';
818 $n++;
819 for (; ($n % 8) != 0; $n++) {
820 $res .= ' ';
821 }
822 next;
823 }
824 $res .= $c;
825 $n++;
826 }
827
828 return $res;
829}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700830sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700831 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700832 return $res;
833}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700834
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700835sub line_stats {
836 my ($line) = @_;
837
838 # Drop the diff line leader and expand tabs
839 $line =~ s/^.//;
840 $line = expand_tabs($line);
841
842 # Pick the indent from the front of the line.
843 my ($white) = ($line =~ /^(\s*)/);
844
845 return (length($line), length($white));
846}
847
Andy Whitcroft773647a2008-03-28 14:15:58 -0700848my $sanitise_quote = '';
849
850sub sanitise_line_reset {
851 my ($in_comment) = @_;
852
853 if ($in_comment) {
854 $sanitise_quote = '*/';
855 } else {
856 $sanitise_quote = '';
857 }
858}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700859sub sanitise_line {
860 my ($line) = @_;
861
862 my $res = '';
863 my $l = '';
864
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800865 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700866 my $off = 0;
867 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700868
Andy Whitcroft773647a2008-03-28 14:15:58 -0700869 # Always copy over the diff marker.
870 $res = substr($line, 0, 1);
871
872 for ($off = 1; $off < length($line); $off++) {
873 $c = substr($line, $off, 1);
874
875 # Comments we are wacking completly including the begin
876 # and end, all to $;.
877 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
878 $sanitise_quote = '*/';
879
880 substr($res, $off, 2, "$;$;");
881 $off++;
882 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800883 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700884 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700885 $sanitise_quote = '';
886 substr($res, $off, 2, "$;$;");
887 $off++;
888 next;
889 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700890 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
891 $sanitise_quote = '//';
892
893 substr($res, $off, 2, $sanitise_quote);
894 $off++;
895 next;
896 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700897
898 # A \ in a string means ignore the next character.
899 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
900 $c eq "\\") {
901 substr($res, $off, 2, 'XX');
902 $off++;
903 next;
904 }
905 # Regular quotes.
906 if ($c eq "'" || $c eq '"') {
907 if ($sanitise_quote eq '') {
908 $sanitise_quote = $c;
909
910 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700911 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700912 } elsif ($sanitise_quote eq $c) {
913 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700914 }
915 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700916
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800917 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700918 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
919 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700920 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
921 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700922 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
923 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700924 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700925 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700926 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800927 }
928
Daniel Walker113f04a2009-09-21 17:04:35 -0700929 if ($sanitise_quote eq '//') {
930 $sanitise_quote = '';
931 }
932
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800933 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700934 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800935 my $clean = 'X' x length($1);
936 $res =~ s@\<.*\>@<$clean>@;
937
938 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700939 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800940 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700941 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800942 }
943
Andy Whitcroft00df3442007-06-08 13:47:06 -0700944 return $res;
945}
946
Joe Perchesa6962d72013-04-29 16:18:13 -0700947sub get_quoted_string {
948 my ($line, $rawline) = @_;
949
Joe Perches5e4f6ba2014-12-10 15:52:05 -0800950 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -0700951 return substr($rawline, $-[0], $+[0] - $-[0]);
952}
953
Andy Whitcroft8905a672007-11-28 16:21:06 -0800954sub ctx_statement_block {
955 my ($linenr, $remain, $off) = @_;
956 my $line = $linenr - 1;
957 my $blk = '';
958 my $soff = $off;
959 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700960 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800961
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800962 my $loff = 0;
963
Andy Whitcroft8905a672007-11-28 16:21:06 -0800964 my $type = '';
965 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800966 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800967 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800968 my $c;
969 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800970
971 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800972 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800973 @stack = (['', 0]) if ($#stack == -1);
974
Andy Whitcroft773647a2008-03-28 14:15:58 -0700975 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800976 # If we are about to drop off the end, pull in more
977 # context.
978 if ($off >= $len) {
979 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700980 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800981 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800982 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800983 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800984 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800985 $len = length($blk);
986 $line++;
987 last;
988 }
989 # Bail if there is no further context.
990 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800991 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800992 last;
993 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800994 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
995 $level++;
996 $type = '#';
997 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800998 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800999 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001000 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001001 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001002
Andy Whitcroft773647a2008-03-28 14:15:58 -07001003 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001004
1005 # Handle nested #if/#else.
1006 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1007 push(@stack, [ $type, $level ]);
1008 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1009 ($type, $level) = @{$stack[$#stack - 1]};
1010 } elsif ($remainder =~ /^#\s*endif\b/) {
1011 ($type, $level) = @{pop(@stack)};
1012 }
1013
Andy Whitcroft8905a672007-11-28 16:21:06 -08001014 # Statement ends at the ';' or a close '}' at the
1015 # outermost level.
1016 if ($level == 0 && $c eq ';') {
1017 last;
1018 }
1019
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001020 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001021 if ($level == 0 && $coff_set == 0 &&
1022 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1023 $remainder =~ /^(else)(?:\s|{)/ &&
1024 $remainder !~ /^else\s+if\b/) {
1025 $coff = $off + length($1) - 1;
1026 $coff_set = 1;
1027 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1028 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001029 }
1030
Andy Whitcroft8905a672007-11-28 16:21:06 -08001031 if (($type eq '' || $type eq '(') && $c eq '(') {
1032 $level++;
1033 $type = '(';
1034 }
1035 if ($type eq '(' && $c eq ')') {
1036 $level--;
1037 $type = ($level != 0)? '(' : '';
1038
1039 if ($level == 0 && $coff < $soff) {
1040 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001041 $coff_set = 1;
1042 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001043 }
1044 }
1045 if (($type eq '' || $type eq '{') && $c eq '{') {
1046 $level++;
1047 $type = '{';
1048 }
1049 if ($type eq '{' && $c eq '}') {
1050 $level--;
1051 $type = ($level != 0)? '{' : '';
1052
1053 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001054 if (substr($blk, $off + 1, 1) eq ';') {
1055 $off++;
1056 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001057 last;
1058 }
1059 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001060 # Preprocessor commands end at the newline unless escaped.
1061 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1062 $level--;
1063 $type = '';
1064 $off++;
1065 last;
1066 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001067 $off++;
1068 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001069 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001070 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001071 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001072 $line++;
1073 $remain--;
1074 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001075
1076 my $statement = substr($blk, $soff, $off - $soff + 1);
1077 my $condition = substr($blk, $soff, $coff - $soff + 1);
1078
1079 #warn "STATEMENT<$statement>\n";
1080 #warn "CONDITION<$condition>\n";
1081
Andy Whitcroft773647a2008-03-28 14:15:58 -07001082 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001083
1084 return ($statement, $condition,
1085 $line, $remain + 1, $off - $loff + 1, $level);
1086}
1087
Andy Whitcroftcf655042008-03-04 14:28:20 -08001088sub statement_lines {
1089 my ($stmt) = @_;
1090
1091 # Strip the diff line prefixes and rip blank lines at start and end.
1092 $stmt =~ s/(^|\n)./$1/g;
1093 $stmt =~ s/^\s*//;
1094 $stmt =~ s/\s*$//;
1095
1096 my @stmt_lines = ($stmt =~ /\n/g);
1097
1098 return $#stmt_lines + 2;
1099}
1100
1101sub statement_rawlines {
1102 my ($stmt) = @_;
1103
1104 my @stmt_lines = ($stmt =~ /\n/g);
1105
1106 return $#stmt_lines + 2;
1107}
1108
1109sub statement_block_size {
1110 my ($stmt) = @_;
1111
1112 $stmt =~ s/(^|\n)./$1/g;
1113 $stmt =~ s/^\s*{//;
1114 $stmt =~ s/}\s*$//;
1115 $stmt =~ s/^\s*//;
1116 $stmt =~ s/\s*$//;
1117
1118 my @stmt_lines = ($stmt =~ /\n/g);
1119 my @stmt_statements = ($stmt =~ /;/g);
1120
1121 my $stmt_lines = $#stmt_lines + 2;
1122 my $stmt_statements = $#stmt_statements + 1;
1123
1124 if ($stmt_lines > $stmt_statements) {
1125 return $stmt_lines;
1126 } else {
1127 return $stmt_statements;
1128 }
1129}
1130
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001131sub ctx_statement_full {
1132 my ($linenr, $remain, $off) = @_;
1133 my ($statement, $condition, $level);
1134
1135 my (@chunks);
1136
Andy Whitcroftcf655042008-03-04 14:28:20 -08001137 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001138 ($statement, $condition, $linenr, $remain, $off, $level) =
1139 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001140 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001141 push(@chunks, [ $condition, $statement ]);
1142 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1143 return ($level, $linenr, @chunks);
1144 }
1145
1146 # Pull in the following conditional/block pairs and see if they
1147 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001148 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001149 ($statement, $condition, $linenr, $remain, $off, $level) =
1150 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001151 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001152 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001153 #print "C: push\n";
1154 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001155 }
1156
1157 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001158}
1159
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001160sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001161 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001162 my $line;
1163 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001164 my $blk = '';
1165 my @o;
1166 my @c;
1167 my @res = ();
1168
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001169 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001170 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001171 for ($line = $start; $remain > 0; $line++) {
1172 next if ($rawlines[$line] =~ /^-/);
1173 $remain--;
1174
1175 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001176
1177 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001178 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001179 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001180 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001181 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001182 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001183 $level = pop(@stack);
1184 }
1185
Andy Whitcroft01464f32010-10-26 14:23:19 -07001186 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001187 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1188 if ($off > 0) {
1189 $off--;
1190 next;
1191 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001192
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001193 if ($c eq $close && $level > 0) {
1194 $level--;
1195 last if ($level == 0);
1196 } elsif ($c eq $open) {
1197 $level++;
1198 }
1199 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001200
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001201 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001202 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001203 }
1204
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001205 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001206 }
1207
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001208 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001209}
1210sub ctx_block_outer {
1211 my ($linenr, $remain) = @_;
1212
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001213 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1214 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001215}
1216sub ctx_block {
1217 my ($linenr, $remain) = @_;
1218
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001219 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1220 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001221}
1222sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001223 my ($linenr, $remain, $off) = @_;
1224
1225 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1226 return @r;
1227}
1228sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001229 my ($linenr, $remain) = @_;
1230
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001231 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001232}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001233sub ctx_statement_level {
1234 my ($linenr, $remain, $off) = @_;
1235
1236 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1237}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001238
1239sub ctx_locate_comment {
1240 my ($first_line, $end_line) = @_;
1241
1242 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001243 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001244 return $current_comment if (defined $current_comment);
1245
1246 # Look through the context and try and figure out if there is a
1247 # comment.
1248 my $in_comment = 0;
1249 $current_comment = '';
1250 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001251 my $line = $rawlines[$linenr - 1];
1252 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001253 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1254 $in_comment = 1;
1255 }
1256 if ($line =~ m@/\*@) {
1257 $in_comment = 1;
1258 }
1259 if (!$in_comment && $current_comment ne '') {
1260 $current_comment = '';
1261 }
1262 $current_comment .= $line . "\n" if ($in_comment);
1263 if ($line =~ m@\*/@) {
1264 $in_comment = 0;
1265 }
1266 }
1267
1268 chomp($current_comment);
1269 return($current_comment);
1270}
1271sub ctx_has_comment {
1272 my ($first_line, $end_line) = @_;
1273 my $cmt = ctx_locate_comment($first_line, $end_line);
1274
Andy Whitcroft00df3442007-06-08 13:47:06 -07001275 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001276 ##print "CMMT: $cmt\n";
1277
1278 return ($cmt ne '');
1279}
1280
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001281sub raw_line {
1282 my ($linenr, $cnt) = @_;
1283
1284 my $offset = $linenr - 1;
1285 $cnt++;
1286
1287 my $line;
1288 while ($cnt) {
1289 $line = $rawlines[$offset++];
1290 next if (defined($line) && $line =~ /^-/);
1291 $cnt--;
1292 }
1293
1294 return $line;
1295}
1296
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001297sub cat_vet {
1298 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001299 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001300
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001301 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001302 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1303 $res .= $1;
1304 if ($2 ne '') {
1305 $coded = sprintf("^%c", unpack('C', $2) + 64);
1306 $res .= $coded;
1307 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001308 }
1309 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001310
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001311 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001312}
1313
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001314my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001315my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001316my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001317my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001318
1319sub annotate_reset {
1320 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001321 $av_pending = '_';
1322 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001323 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001324}
1325
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001326sub annotate_values {
1327 my ($stream, $type) = @_;
1328
1329 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001330 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001331 my $cur = $stream;
1332
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001333 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001334
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001335 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001336 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001337 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001338 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001339 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001340 print "WS($1)\n" if ($dbg_values > 1);
1341 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001342 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001343 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001344 }
1345
Florian Micklerc023e4732011-01-12 16:59:58 -08001346 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001347 print "CAST($1)\n" if ($dbg_values > 1);
1348 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001349 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001350
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001351 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001352 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001353 $type = 'T';
1354
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001355 } elsif ($cur =~ /^($Modifier)\s*/) {
1356 print "MODIFIER($1)\n" if ($dbg_values > 1);
1357 $type = 'T';
1358
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001359 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001360 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001361 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001362 push(@av_paren_type, $type);
1363 if ($2 ne '') {
1364 $av_pending = 'N';
1365 }
1366 $type = 'E';
1367
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001368 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001369 print "UNDEF($1)\n" if ($dbg_values > 1);
1370 $av_preprocessor = 1;
1371 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001372
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001373 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001374 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001375 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001376
1377 push(@av_paren_type, $type);
1378 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001379 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001380
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001381 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001382 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1383 $av_preprocessor = 1;
1384
1385 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1386
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001387 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001388
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001389 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001390 print "PRE_END($1)\n" if ($dbg_values > 1);
1391
1392 $av_preprocessor = 1;
1393
1394 # Assume all arms of the conditional end as this
1395 # one does, and continue as if the #endif was not here.
1396 pop(@av_paren_type);
1397 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001398 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001399
1400 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001401 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001402
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001403 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1404 print "ATTR($1)\n" if ($dbg_values > 1);
1405 $av_pending = $type;
1406 $type = 'N';
1407
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001408 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001410 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001411 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001412 }
1413 $type = 'N';
1414
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001415 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001416 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001417 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001418 $type = 'N';
1419
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001420 } elsif ($cur =~/^(case)/o) {
1421 print "CASE($1)\n" if ($dbg_values > 1);
1422 $av_pend_colon = 'C';
1423 $type = 'N';
1424
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001425 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001426 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001427 $type = 'N';
1428
1429 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001430 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001431 push(@av_paren_type, $av_pending);
1432 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001433 $type = 'N';
1434
1435 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 my $new_type = pop(@av_paren_type);
1437 if ($new_type ne '_') {
1438 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001439 print "PAREN('$1') -> $type\n"
1440 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001441 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001442 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001443 }
1444
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001445 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001446 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001447 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001448 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001449
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001450 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1451 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001452 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001453 } elsif ($type eq 'E') {
1454 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001455 }
1456 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1457 $type = 'V';
1458
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001459 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001460 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001461 $type = 'V';
1462
1463 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001464 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001465 $type = 'N';
1466
Andy Whitcroftcf655042008-03-04 14:28:20 -08001467 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001468 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001469 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001470 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001471
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001472 } elsif ($cur =~/^(,)/) {
1473 print "COMMA($1)\n" if ($dbg_values > 1);
1474 $type = 'C';
1475
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001476 } elsif ($cur =~ /^(\?)/o) {
1477 print "QUESTION($1)\n" if ($dbg_values > 1);
1478 $type = 'N';
1479
1480 } elsif ($cur =~ /^(:)/o) {
1481 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1482
1483 substr($var, length($res), 1, $av_pend_colon);
1484 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1485 $type = 'E';
1486 } else {
1487 $type = 'N';
1488 }
1489 $av_pend_colon = 'O';
1490
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001491 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001492 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001493 $type = 'N';
1494
Andy Whitcroft0d413862008-10-15 22:02:16 -07001495 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001496 my $variant;
1497
1498 print "OPV($1)\n" if ($dbg_values > 1);
1499 if ($type eq 'V') {
1500 $variant = 'B';
1501 } else {
1502 $variant = 'U';
1503 }
1504
1505 substr($var, length($res), 1, $variant);
1506 $type = 'N';
1507
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001508 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001509 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001510 if ($1 ne '++' && $1 ne '--') {
1511 $type = 'N';
1512 }
1513
1514 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001515 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001516 }
1517 if (defined $1) {
1518 $cur = substr($cur, length($1));
1519 $res .= $type x length($1);
1520 }
1521 }
1522
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001523 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001524}
1525
Andy Whitcroft8905a672007-11-28 16:21:06 -08001526sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001527 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001528 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001529 ^(?:
1530 $Modifier|
1531 $Storage|
1532 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001533 DEFINE_\S+
1534 )$|
1535 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001536 goto|
1537 return|
1538 case|
1539 else|
1540 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001541 do|
1542 \#|
1543 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001544 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001545 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001546 )}x;
1547 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1548 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001549 # Check for modifiers.
1550 $possible =~ s/\s*$Storage\s*//g;
1551 $possible =~ s/\s*$Sparse\s*//g;
1552 if ($possible =~ /^\s*$/) {
1553
1554 } elsif ($possible =~ /\s/) {
1555 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001556 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001557 if ($modifier !~ $notPermitted) {
1558 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1559 push(@modifierList, $modifier);
1560 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001561 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001562
1563 } else {
1564 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1565 push(@typeList, $possible);
1566 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001567 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001568 } else {
1569 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001570 }
1571}
1572
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001573my $prefix = '';
1574
Joe Perches000d1cc12011-07-25 17:13:25 -07001575sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001576 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001577
Joe Perchescbec18a2014-04-03 14:49:19 -07001578 return defined $use_type{$type} if (scalar keys %use_type > 0);
1579
1580 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001581}
1582
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001583sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001584 my ($level, $type, $msg) = @_;
1585
1586 if (!show_type($type) ||
1587 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001588 return 0;
1589 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001590 my $line;
1591 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001592 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001593 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001594 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001595 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001596 $line = (split('\n', $line))[0] . "\n" if ($terse);
1597
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001598 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001599
1600 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001601}
Joe Perchescbec18a2014-04-03 14:49:19 -07001602
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001603sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001604 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001605}
Joe Perches000d1cc12011-07-25 17:13:25 -07001606
Joe Perchesd752fcc2014-08-06 16:11:05 -07001607sub fixup_current_range {
1608 my ($lineRef, $offset, $length) = @_;
1609
1610 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1611 my $o = $1;
1612 my $l = $2;
1613 my $no = $o + $offset;
1614 my $nl = $l + $length;
1615 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1616 }
1617}
1618
1619sub fix_inserted_deleted_lines {
1620 my ($linesRef, $insertedRef, $deletedRef) = @_;
1621
1622 my $range_last_linenr = 0;
1623 my $delta_offset = 0;
1624
1625 my $old_linenr = 0;
1626 my $new_linenr = 0;
1627
1628 my $next_insert = 0;
1629 my $next_delete = 0;
1630
1631 my @lines = ();
1632
1633 my $inserted = @{$insertedRef}[$next_insert++];
1634 my $deleted = @{$deletedRef}[$next_delete++];
1635
1636 foreach my $old_line (@{$linesRef}) {
1637 my $save_line = 1;
1638 my $line = $old_line; #don't modify the array
1639 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1640 $delta_offset = 0;
1641 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1642 $range_last_linenr = $new_linenr;
1643 fixup_current_range(\$line, $delta_offset, 0);
1644 }
1645
1646 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1647 $deleted = @{$deletedRef}[$next_delete++];
1648 $save_line = 0;
1649 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1650 }
1651
1652 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1653 push(@lines, ${$inserted}{'LINE'});
1654 $inserted = @{$insertedRef}[$next_insert++];
1655 $new_linenr++;
1656 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1657 }
1658
1659 if ($save_line) {
1660 push(@lines, $line);
1661 $new_linenr++;
1662 }
1663
1664 $old_linenr++;
1665 }
1666
1667 return @lines;
1668}
1669
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001670sub fix_insert_line {
1671 my ($linenr, $line) = @_;
1672
1673 my $inserted = {
1674 LINENR => $linenr,
1675 LINE => $line,
1676 };
1677 push(@fixed_inserted, $inserted);
1678}
1679
1680sub fix_delete_line {
1681 my ($linenr, $line) = @_;
1682
1683 my $deleted = {
1684 LINENR => $linenr,
1685 LINE => $line,
1686 };
1687
1688 push(@fixed_deleted, $deleted);
1689}
1690
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001691sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001692 my ($type, $msg) = @_;
1693
1694 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001695 our $clean = 0;
1696 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001697 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001698 }
Joe Perches3705ce52013-07-03 15:05:31 -07001699 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001700}
1701sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001702 my ($type, $msg) = @_;
1703
1704 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001705 our $clean = 0;
1706 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001707 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001708 }
Joe Perches3705ce52013-07-03 15:05:31 -07001709 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001710}
1711sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001712 my ($type, $msg) = @_;
1713
1714 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001715 our $clean = 0;
1716 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001717 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718 }
Joe Perches3705ce52013-07-03 15:05:31 -07001719 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001720}
1721
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001722sub check_absolute_file {
1723 my ($absolute, $herecurr) = @_;
1724 my $file = $absolute;
1725
1726 ##print "absolute<$absolute>\n";
1727
1728 # See if any suffix of this path is a path within the tree.
1729 while ($file =~ s@^[^/]*/@@) {
1730 if (-f "$root/$file") {
1731 ##print "file<$file>\n";
1732 last;
1733 }
1734 }
1735 if (! -f _) {
1736 return 0;
1737 }
1738
1739 # It is, so see if the prefix is acceptable.
1740 my $prefix = $absolute;
1741 substr($prefix, -length($file)) = '';
1742
1743 ##print "prefix<$prefix>\n";
1744 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001745 WARN("USE_RELATIVE_PATH",
1746 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001747 }
1748}
1749
Joe Perches3705ce52013-07-03 15:05:31 -07001750sub trim {
1751 my ($string) = @_;
1752
Joe Perchesb34c6482013-09-11 14:24:01 -07001753 $string =~ s/^\s+|\s+$//g;
1754
1755 return $string;
1756}
1757
1758sub ltrim {
1759 my ($string) = @_;
1760
1761 $string =~ s/^\s+//;
1762
1763 return $string;
1764}
1765
1766sub rtrim {
1767 my ($string) = @_;
1768
1769 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001770
1771 return $string;
1772}
1773
Joe Perches52ea8502013-11-12 15:10:09 -08001774sub string_find_replace {
1775 my ($string, $find, $replace) = @_;
1776
1777 $string =~ s/$find/$replace/g;
1778
1779 return $string;
1780}
1781
Joe Perches3705ce52013-07-03 15:05:31 -07001782sub tabify {
1783 my ($leading) = @_;
1784
1785 my $source_indent = 8;
1786 my $max_spaces_before_tab = $source_indent - 1;
1787 my $spaces_to_tab = " " x $source_indent;
1788
1789 #convert leading spaces to tabs
1790 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1791 #Remove spaces before a tab
1792 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1793
1794 return "$leading";
1795}
1796
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001797sub pos_last_openparen {
1798 my ($line) = @_;
1799
1800 my $pos = 0;
1801
1802 my $opens = $line =~ tr/\(/\(/;
1803 my $closes = $line =~ tr/\)/\)/;
1804
1805 my $last_openparen = 0;
1806
1807 if (($opens == 0) || ($closes >= $opens)) {
1808 return -1;
1809 }
1810
1811 my $len = length($line);
1812
1813 for ($pos = 0; $pos < $len; $pos++) {
1814 my $string = substr($line, $pos);
1815 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1816 $pos += length($1) - 1;
1817 } elsif (substr($line, $pos, 1) eq '(') {
1818 $last_openparen = $pos;
1819 } elsif (index($string, '(') == -1) {
1820 last;
1821 }
1822 }
1823
Joe Perches91cb5192014-04-03 14:49:32 -07001824 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001825}
1826
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001827sub process {
1828 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001829
1830 my $linenr=0;
1831 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001832 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001833 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001834 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001835
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001836 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001837 my $indent;
1838 my $previndent=0;
1839 my $stashindent=0;
1840
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001841 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001842 my $signoff = 0;
1843 my $is_patch = 0;
1844
Joe Perches29ee1b02014-08-06 16:10:35 -07001845 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001846 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001847 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001848 my $non_utf8_charset = 0;
1849
Joe Perches365dd4e2014-08-06 16:10:42 -07001850 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001851 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001852
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001853 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001854 our $cnt_lines = 0;
1855 our $cnt_error = 0;
1856 our $cnt_warn = 0;
1857 our $cnt_chk = 0;
1858
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001859 # Trace the real file/line as we go.
1860 my $realfile = '';
1861 my $realline = 0;
1862 my $realcnt = 0;
1863 my $here = '';
1864 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001865 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001866 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001867 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001868
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001869 my $prev_values = 'E';
1870
1871 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001872 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001873 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001874 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001875 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001876
Joe Perches7e51f192013-09-11 14:23:57 -07001877 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001878
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001879 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001880 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001881 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001882 my @setup_docs = ();
1883 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001884
Joe Perchesd8b07712013-11-12 15:10:06 -08001885 my $camelcase_file_seeded = 0;
1886
Andy Whitcroft773647a2008-03-28 14:15:58 -07001887 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001888 my $line;
1889 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001890 $linenr++;
1891 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001892
Joe Perches3705ce52013-07-03 15:05:31 -07001893 push(@fixed, $rawline) if ($fix);
1894
Andy Whitcroft773647a2008-03-28 14:15:58 -07001895 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001896 $setup_docs = 0;
1897 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1898 $setup_docs = 1;
1899 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001900 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001901 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001902 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1903 $realline=$1-1;
1904 if (defined $2) {
1905 $realcnt=$3+1;
1906 } else {
1907 $realcnt=1+1;
1908 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001909 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001910
1911 # Guestimate if this is a continuing comment. Run
1912 # the context looking for a comment "edge". If this
1913 # edge is a close comment then we must be in a comment
1914 # at context start.
1915 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001916 my $cnt = $realcnt;
1917 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1918 next if (defined $rawlines[$ln - 1] &&
1919 $rawlines[$ln - 1] =~ /^-/);
1920 $cnt--;
1921 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001922 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001923 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1924 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1925 ($edge) = $1;
1926 last;
1927 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001928 }
1929 if (defined $edge && $edge eq '*/') {
1930 $in_comment = 1;
1931 }
1932
1933 # Guestimate if this is a continuing comment. If this
1934 # is the start of a diff block and this line starts
1935 # ' *' then it is very likely a comment.
1936 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001937 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001938 {
1939 $in_comment = 1;
1940 }
1941
1942 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1943 sanitise_line_reset($in_comment);
1944
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001945 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001946 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001947 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001948 $line = sanitise_line($rawline);
1949 }
1950 push(@lines, $line);
1951
1952 if ($realcnt > 1) {
1953 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1954 } else {
1955 $realcnt = 0;
1956 }
1957
1958 #print "==>$rawline\n";
1959 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001960
1961 if ($setup_docs && $line =~ /^\+/) {
1962 push(@setup_docs, $line);
1963 }
1964 }
1965
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001966 $prefix = '';
1967
Andy Whitcroft773647a2008-03-28 14:15:58 -07001968 $realcnt = 0;
1969 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001970 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001971 foreach my $line (@lines) {
1972 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001973 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001974 my $sline = $line; #copy of $line
1975 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001976
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001977 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001978
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001979#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001980 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001981 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001982 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001983 $realline=$1-1;
1984 if (defined $2) {
1985 $realcnt=$3+1;
1986 } else {
1987 $realcnt=1+1;
1988 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001989 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001990 $prev_values = 'E';
1991
Andy Whitcroft773647a2008-03-28 14:15:58 -07001992 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001993 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001994 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001995 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001996 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001997
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001998# track the line number as we move through the hunk, note that
1999# new versions of GNU diff omit the leading space on completely
2000# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002001 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002002 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002003 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002004
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002005 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002006 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002007
2008 # Track the previous line.
2009 ($prevline, $stashline) = ($stashline, $line);
2010 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002011 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2012
Andy Whitcroft773647a2008-03-28 14:15:58 -07002013 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002014
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002015 } elsif ($realcnt == 1) {
2016 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002017 }
2018
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002019 my $hunk_line = ($realcnt != 0);
2020
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002021#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002022 $prefix = "$filename:$realline: " if ($emacs && $file);
2023 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2024
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002025 $here = "#$linenr: " if (!$file);
2026 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002027
Joe Perches2ac73b42014-06-04 16:12:05 -07002028 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002029 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002030 if ($line =~ /^diff --git.*?(\S+)$/) {
2031 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002032 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002033 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002034 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002035 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002036 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002037 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002038 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002039
2040 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002041 if (!$file && $tree && $p1_prefix ne '' &&
2042 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002043 WARN("PATCH_PREFIX",
2044 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002045 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002046
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002047 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002048 ERROR("MODIFIED_INCLUDE_ASM",
2049 "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 -07002050 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002051 $found_file = 1;
2052 }
2053
2054 if ($found_file) {
2055 if ($realfile =~ m@^(drivers/net/|net/)@) {
2056 $check = 1;
2057 } else {
2058 $check = $check_orig;
2059 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002060 next;
2061 }
2062
Randy Dunlap389834b2007-06-08 13:47:03 -07002063 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002064
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002065 my $hereline = "$here\n$rawline\n";
2066 my $herecurr = "$here\n$rawline\n";
2067 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002068
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002069 $cnt_lines++ if ($realcnt != 0);
2070
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002071# Check for incorrect file permissions
2072 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2073 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002074 if ($realfile !~ m@scripts/@ &&
2075 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002076 ERROR("EXECUTE_PERMISSIONS",
2077 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002078 }
2079 }
2080
Joe Perches20112472011-07-25 17:13:23 -07002081# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002082 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002083 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002084 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002085 }
2086
Joe Perchese0d975b2014-12-10 15:51:49 -08002087# Check if MAINTAINERS is being updated. If so, there's probably no need to
2088# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2089 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2090 $reported_maintainer_file = 1;
2091 }
2092
Joe Perches20112472011-07-25 17:13:23 -07002093# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002094 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002095 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002096 my $space_before = $1;
2097 my $sign_off = $2;
2098 my $space_after = $3;
2099 my $email = $4;
2100 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2101
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002102 if ($sign_off !~ /$signature_tags/) {
2103 WARN("BAD_SIGN_OFF",
2104 "Non-standard signature: $sign_off\n" . $herecurr);
2105 }
Joe Perches20112472011-07-25 17:13:23 -07002106 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002107 if (WARN("BAD_SIGN_OFF",
2108 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2109 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002110 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002111 "$ucfirst_sign_off $email";
2112 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002113 }
Joe Perches20112472011-07-25 17:13:23 -07002114 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002115 if (WARN("BAD_SIGN_OFF",
2116 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2117 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002118 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002119 "$ucfirst_sign_off $email";
2120 }
2121
Joe Perches20112472011-07-25 17:13:23 -07002122 }
2123 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002124 if (WARN("BAD_SIGN_OFF",
2125 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2126 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002127 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002128 "$ucfirst_sign_off $email";
2129 }
Joe Perches20112472011-07-25 17:13:23 -07002130 }
2131
2132 my ($email_name, $email_address, $comment) = parse_email($email);
2133 my $suggested_email = format_email(($email_name, $email_address));
2134 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002135 ERROR("BAD_SIGN_OFF",
2136 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002137 } else {
2138 my $dequoted = $suggested_email;
2139 $dequoted =~ s/^"//;
2140 $dequoted =~ s/" </ </;
2141 # Don't force email to have quotes
2142 # Allow just an angle bracketed address
2143 if ("$dequoted$comment" ne $email &&
2144 "<$email_address>$comment" ne $email &&
2145 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002146 WARN("BAD_SIGN_OFF",
2147 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002148 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002149 }
Joe Perches7e51f192013-09-11 14:23:57 -07002150
2151# Check for duplicate signatures
2152 my $sig_nospace = $line;
2153 $sig_nospace =~ s/\s//g;
2154 $sig_nospace = lc($sig_nospace);
2155 if (defined $signatures{$sig_nospace}) {
2156 WARN("BAD_SIGN_OFF",
2157 "Duplicate signature\n" . $herecurr);
2158 } else {
2159 $signatures{$sig_nospace} = 1;
2160 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002161 }
2162
Joe Perches9b3189e2014-06-04 16:12:10 -07002163# Check for old stable address
2164 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2165 ERROR("STABLE_ADDRESS",
2166 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2167 }
2168
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002169# Check for unwanted Gerrit info
2170 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2171 ERROR("GERRIT_CHANGE_ID",
2172 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2173 }
2174
Joe Perchesd311cd42014-08-06 16:10:57 -07002175# Check for improperly formed commit descriptions
2176 if ($in_commit_log &&
2177 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
Joe Perches66881732014-09-09 14:50:55 -07002178 !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2179 ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2180 defined $rawlines[$linenr] &&
2181 $rawlines[$linenr] =~ /^\s*\("/))) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002182 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2183 my $init_char = $1;
2184 my $orig_commit = lc($2);
2185 my $id = '01234567890ab';
2186 my $desc = 'commit description';
2187 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2188 ERROR("GIT_COMMIT_ID",
Joe Perches3f6316b42014-08-29 15:18:26 -07002189 "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 -07002190 }
2191
Joe Perches13f19372014-08-06 16:10:59 -07002192# Check for added, moved or deleted files
2193 if (!$reported_maintainer_file && !$in_commit_log &&
2194 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2195 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2196 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2197 (defined($1) || defined($2))))) {
2198 $reported_maintainer_file = 1;
2199 WARN("FILE_PATH_CHANGES",
2200 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2201 }
2202
Andy Whitcroft00df3442007-06-08 13:47:06 -07002203# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002204 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002205 ERROR("CORRUPTED_PATCH",
2206 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002207 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002208 }
2209
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002210# Check for absolute kernel paths.
2211 if ($tree) {
2212 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2213 my $file = $1;
2214
2215 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2216 check_absolute_file($1, $herecurr)) {
2217 #
2218 } else {
2219 check_absolute_file($file, $herecurr);
2220 }
2221 }
2222 }
2223
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002224# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2225 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002226 $rawline !~ m/^$UTF8*$/) {
2227 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2228
2229 my $blank = copy_spacing($rawline);
2230 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2231 my $hereptr = "$hereline$ptr\n";
2232
Joe Perches34d99212011-07-25 17:13:26 -07002233 CHK("INVALID_UTF8",
2234 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002235 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002236
Joe Perches15662b32011-10-31 17:13:12 -07002237# Check if it's the start of a commit log
2238# (not a header line and we haven't seen the patch filename)
2239 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002240 !($rawline =~ /^\s+\S/ ||
2241 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002242 $in_header_lines = 0;
2243 $in_commit_log = 1;
2244 }
2245
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002246# Check if there is UTF-8 in a commit log when a mail header has explicitly
2247# declined it, i.e defined some charset where it is missing.
2248 if ($in_header_lines &&
2249 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2250 $1 !~ /utf-8/i) {
2251 $non_utf8_charset = 1;
2252 }
2253
2254 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002255 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002256 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002257 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2258 }
2259
Kees Cook66b47b42014-10-13 15:51:57 -07002260# Check for various typo / spelling mistakes
Joe Perches36061e32014-12-10 15:51:43 -08002261 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
Kees Cook66b47b42014-10-13 15:51:57 -07002262 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2263 my $typo = $1;
2264 my $typo_fix = $spelling_fix{lc($typo)};
2265 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2266 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2267 my $msg_type = \&WARN;
2268 $msg_type = \&CHK if ($file);
2269 if (&{$msg_type}("TYPO_SPELLING",
2270 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2271 $fix) {
2272 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2273 }
2274 }
2275 }
2276
Andy Whitcroft306708542008-10-15 22:02:28 -07002277# ignore non-hunk lines and lines being removed
2278 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002279
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002280#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002281 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002282 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002283 if (ERROR("DOS_LINE_ENDINGS",
2284 "DOS line endings\n" . $herevet) &&
2285 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002286 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002287 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002288 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2289 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002290 if (ERROR("TRAILING_WHITESPACE",
2291 "trailing whitespace\n" . $herevet) &&
2292 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002293 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002294 }
2295
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002296 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002297 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002298
Josh Triplett4783f892013-11-12 15:10:12 -08002299# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002300 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002301 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2302 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002303 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2304 my $msg_type = \&ERROR;
2305 $msg_type = \&CHK if ($file);
2306 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002307 "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 -08002308 }
2309
Andi Kleen33549572010-05-24 14:33:29 -07002310# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002311# Only applies when adding the entry originally, after that we do not have
2312# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002313 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002314 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002315 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002316 my $cnt = $realcnt;
2317 my $ln = $linenr + 1;
2318 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002319 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002320 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002321 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002322 $f = $lines[$ln - 1];
2323 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2324 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002325
2326 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002327 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002328
Joe Perches8d73e0e2014-08-06 16:10:46 -07002329 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002330 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002331 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002332 $length = -1;
2333 }
2334
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002335 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002336 $f =~ s/#.*//;
2337 $f =~ s/^\s+//;
2338 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002339 if ($f =~ /^\s*config\s/) {
2340 $is_end = 1;
2341 last;
2342 }
Andi Kleen33549572010-05-24 14:33:29 -07002343 $length++;
2344 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002345 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2346 WARN("CONFIG_DESCRIPTION",
2347 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2348 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002349 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002350 }
2351
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002352# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2353 if ($realfile =~ /Kconfig/ &&
2354 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2355 WARN("CONFIG_EXPERIMENTAL",
2356 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2357 }
2358
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002359 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2360 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2361 my $flag = $1;
2362 my $replacement = {
2363 'EXTRA_AFLAGS' => 'asflags-y',
2364 'EXTRA_CFLAGS' => 'ccflags-y',
2365 'EXTRA_CPPFLAGS' => 'cppflags-y',
2366 'EXTRA_LDFLAGS' => 'ldflags-y',
2367 };
2368
2369 WARN("DEPRECATED_VARIABLE",
2370 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2371 }
2372
Rob Herringbff5da42014-01-23 15:54:51 -08002373# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002374 if (defined $root &&
2375 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2376 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2377
Rob Herringbff5da42014-01-23 15:54:51 -08002378 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2379
Florian Vaussardcc933192014-04-03 14:49:27 -07002380 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2381 my $vp_file = $dt_path . "vendor-prefixes.txt";
2382
Rob Herringbff5da42014-01-23 15:54:51 -08002383 foreach my $compat (@compats) {
2384 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002385 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2386 my $compat3 = $compat;
2387 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2388 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002389 if ( $? >> 8 ) {
2390 WARN("UNDOCUMENTED_DT_STRING",
2391 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2392 }
2393
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002394 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2395 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002396 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002397 if ( $? >> 8 ) {
2398 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002399 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002400 }
2401 }
2402 }
2403
Andy Whitcroft5368df202008-10-15 22:02:27 -07002404# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002405 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002406
Joe Perches6cd7f382012-12-17 16:01:54 -08002407#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002408 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002409 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002410 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002411 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002412 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002413 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002414 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002415 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002416 }
2417
Andy Whitcroft8905a672007-11-28 16:21:06 -08002418# check for adding lines without a newline.
2419 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002420 WARN("MISSING_EOF_NEWLINE",
2421 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002422 }
2423
Mike Frysinger42e41c52009-09-21 17:04:40 -07002424# Blackfin: use hi/lo macros
2425 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2426 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2427 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002428 ERROR("LO_MACRO",
2429 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002430 }
2431 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2432 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002433 ERROR("HI_MACRO",
2434 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002435 }
2436 }
2437
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002438# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002439 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002440
2441# at the beginning of a line any tabs must come first and anything
2442# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002443 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2444 $rawline =~ /^\+\s* \s*/) {
2445 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002446 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002447 if (ERROR("CODE_INDENT",
2448 "code indent should use tabs where possible\n" . $herevet) &&
2449 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002450 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002451 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002452 }
2453
Alberto Panizzo08e44362010-03-05 13:43:54 -08002454# check for space before tabs.
2455 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2456 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002457 if (WARN("SPACE_BEFORE_TAB",
2458 "please, no space before tabs\n" . $herevet) &&
2459 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002460 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002461 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002462 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002463 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002464 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002465 }
2466
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002467# check for && or || at the start of a line
2468 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2469 CHK("LOGICAL_CONTINUATIONS",
2470 "Logical continuations should be on the previous line\n" . $hereprev);
2471 }
2472
2473# check multi-line statement indentation matches previous line
2474 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002475 $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 -07002476 $prevline =~ /^\+(\t*)(.*)$/;
2477 my $oldindent = $1;
2478 my $rest = $2;
2479
2480 my $pos = pos_last_openparen($rest);
2481 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002482 $line =~ /^(\+| )([ \t]*)/;
2483 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002484
2485 my $goodtabindent = $oldindent .
2486 "\t" x ($pos / 8) .
2487 " " x ($pos % 8);
2488 my $goodspaceindent = $oldindent . " " x $pos;
2489
2490 if ($newindent ne $goodtabindent &&
2491 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002492
2493 if (CHK("PARENTHESIS_ALIGNMENT",
2494 "Alignment should match open parenthesis\n" . $hereprev) &&
2495 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002496 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002497 s/^\+[ \t]*/\+$goodtabindent/;
2498 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002499 }
2500 }
2501 }
2502
Joe Perches04941aa2014-12-10 15:51:37 -08002503 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
2504 (!defined($1) || $1 !~ /sizeof\s*/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002505 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002506 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002507 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002508 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002509 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002510 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002511 }
2512
Joe Perches05880602012-10-04 17:13:35 -07002513 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002514 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002515 $rawline =~ /^\+[ \t]*\*/ &&
2516 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002517 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2518 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2519 }
2520
2521 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002522 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2523 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002524 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002525 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2526 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2527 "networking block comments start with * on subsequent lines\n" . $hereprev);
2528 }
2529
2530 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002531 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2532 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2533 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2534 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002535 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2536 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2537 }
2538
Joe Perches7f619192014-08-06 16:10:39 -07002539# check for missing blank lines after struct/union declarations
2540# with exceptions for various attributes and macros
2541 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2542 $line =~ /^\+/ &&
2543 !($line =~ /^\+\s*$/ ||
2544 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2545 $line =~ /^\+\s*MODULE_/i ||
2546 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2547 $line =~ /^\+[a-z_]*init/ ||
2548 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2549 $line =~ /^\+\s*DECLARE/ ||
2550 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002551 if (CHK("LINE_SPACING",
2552 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2553 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002554 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002555 }
Joe Perches7f619192014-08-06 16:10:39 -07002556 }
2557
Joe Perches365dd4e2014-08-06 16:10:42 -07002558# check for multiple consecutive blank lines
2559 if ($prevline =~ /^[\+ ]\s*$/ &&
2560 $line =~ /^\+\s*$/ &&
2561 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002562 if (CHK("LINE_SPACING",
2563 "Please don't use multiple blank lines\n" . $hereprev) &&
2564 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002565 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002566 }
2567
Joe Perches365dd4e2014-08-06 16:10:42 -07002568 $last_blank_line = $linenr;
2569 }
2570
Joe Perches3b617e32014-04-03 14:49:28 -07002571# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002572 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2573 # actual declarations
2574 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002575 # function pointer declarations
2576 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002577 # foo bar; where foo is some local typedef or #define
2578 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2579 # known declaration macros
2580 $prevline =~ /^\+\s+$declaration_macros/) &&
2581 # for "else if" which can look like "$Ident $Ident"
2582 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2583 # other possible extensions of declaration lines
2584 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2585 # not starting a section or a macro "\" extended line
2586 $prevline =~ /(?:\{\s*|\\)$/) &&
2587 # looks like a declaration
2588 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002589 # function pointer declarations
2590 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002591 # foo bar; where foo is some local typedef or #define
2592 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2593 # known declaration macros
2594 $sline =~ /^\+\s+$declaration_macros/ ||
2595 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002596 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002597 # start or end of block or continuation of declaration
2598 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2599 # bitfield continuation
2600 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2601 # other possible extensions of declaration lines
2602 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2603 # indentation of previous and current line are the same
2604 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002605 if (WARN("LINE_SPACING",
2606 "Missing a blank line after declarations\n" . $hereprev) &&
2607 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002608 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002609 }
Joe Perches3b617e32014-04-03 14:49:28 -07002610 }
2611
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002612# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002613# Exceptions:
2614# 1) within comments
2615# 2) indented preprocessor commands
2616# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002617 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002618 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002619 if (WARN("LEADING_SPACE",
2620 "please, no spaces at the start of a line\n" . $herevet) &&
2621 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002622 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002623 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002624 }
2625
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002626# check we are in a valid C source file if not then ignore this hunk
2627 next if ($realfile !~ /\.(h|c)$/);
2628
Joe Perches032a4c02014-08-06 16:10:29 -07002629# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002630# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002631# if the previous line is a break or return and is indented 1 tab more...
2632 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2633 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002634 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2635 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2636 defined $lines[$linenr] &&
2637 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002638 WARN("UNNECESSARY_ELSE",
2639 "else is not generally useful after a break or return\n" . $hereprev);
2640 }
2641 }
2642
Joe Perchesc00df192014-08-06 16:11:01 -07002643# check indentation of a line with a break;
2644# if the previous line is a goto or return and is indented the same # of tabs
2645 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2646 my $tabs = $1;
2647 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2648 WARN("UNNECESSARY_BREAK",
2649 "break is not useful after a goto or return\n" . $hereprev);
2650 }
2651 }
2652
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002653# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2654 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2655 WARN("CONFIG_EXPERIMENTAL",
2656 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2657 }
2658
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002659# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002660 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002661 WARN("CVS_KEYWORD",
2662 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002663 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002664
Mike Frysinger42e41c52009-09-21 17:04:40 -07002665# Blackfin: don't use __builtin_bfin_[cs]sync
2666 if ($line =~ /__builtin_bfin_csync/) {
2667 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002668 ERROR("CSYNC",
2669 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002670 }
2671 if ($line =~ /__builtin_bfin_ssync/) {
2672 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002673 ERROR("SSYNC",
2674 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002675 }
2676
Joe Perches56e77d72013-02-21 16:44:14 -08002677# check for old HOTPLUG __dev<foo> section markings
2678 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2679 WARN("HOTPLUG_SECTION",
2680 "Using $1 is unnecessary\n" . $herecurr);
2681 }
2682
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002683# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002684 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2685 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002686#print "LINE<$line>\n";
2687 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002688 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002689 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002690 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002691 $stat =~ s/\n./\n /g;
2692 $cond =~ s/\n./\n /g;
2693
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002694#print "linenr<$linenr> <$stat>\n";
2695 # If this statement has no statement boundaries within
2696 # it there is no point in retrying a statement scan
2697 # until we hit end of it.
2698 my $frag = $stat; $frag =~ s/;+\s*$//;
2699 if ($frag !~ /(?:{|;)/) {
2700#print "skip<$line_nr_next>\n";
2701 $suppress_statement = $line_nr_next;
2702 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002703
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002704 # Find the real next line.
2705 $realline_next = $line_nr_next;
2706 if (defined $realline_next &&
2707 (!defined $lines[$realline_next - 1] ||
2708 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2709 $realline_next++;
2710 }
2711
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002712 my $s = $stat;
2713 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002714
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002715 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002716 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002717
2718 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002719 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002720
Andy Whitcroft463f2862009-09-21 17:04:34 -07002721 } elsif ($s =~ /^.\s*else\b/s) {
2722
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002723 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002724 } 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 -07002725 my $type = $1;
2726 $type =~ s/\s+/ /g;
2727 possible($type, "A:" . $s);
2728
Andy Whitcroft8905a672007-11-28 16:21:06 -08002729 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002730 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002731 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002732 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002733
2734 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002735 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002736 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002737 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002738
2739 # Check for any sort of function declaration.
2740 # int foo(something bar, other baz);
2741 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002742 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 -08002743 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002744
Andy Whitcroftcf655042008-03-04 14:28:20 -08002745 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002746 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002747 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002748
Andy Whitcroft8905a672007-11-28 16:21:06 -08002749 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002750 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002751
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002752 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002753 }
2754 }
2755 }
2756
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002757 }
2758
Andy Whitcroft653d4872007-06-23 17:16:34 -07002759#
2760# Checks which may be anchored in the context.
2761#
2762
2763# Check for switch () and associated case and default
2764# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002765 if ($line=~/\bswitch\s*\(.*\)/) {
2766 my $err = '';
2767 my $sep = '';
2768 my @ctx = ctx_block_outer($linenr, $realcnt);
2769 shift(@ctx);
2770 for my $ctx (@ctx) {
2771 my ($clen, $cindent) = line_stats($ctx);
2772 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2773 $indent != $cindent) {
2774 $err .= "$sep$ctx\n";
2775 $sep = '';
2776 } else {
2777 $sep = "[...]\n";
2778 }
2779 }
2780 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002781 ERROR("SWITCH_CASE_INDENT_LEVEL",
2782 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002783 }
2784 }
2785
2786# if/while/etc brace do not go on next line, unless defining a do while loop,
2787# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002788 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 -07002789 my $pre_ctx = "$1$2";
2790
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002791 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002792
2793 if ($line =~ /^\+\t{6,}/) {
2794 WARN("DEEP_INDENTATION",
2795 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2796 }
2797
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002798 my $ctx_cnt = $realcnt - $#ctx - 1;
2799 my $ctx = join("\n", @ctx);
2800
Andy Whitcroft548596d2008-07-23 21:29:01 -07002801 my $ctx_ln = $linenr;
2802 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002803
Andy Whitcroft548596d2008-07-23 21:29:01 -07002804 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2805 defined $lines[$ctx_ln - 1] &&
2806 $lines[$ctx_ln - 1] =~ /^-/)) {
2807 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2808 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002809 $ctx_ln++;
2810 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002811
Andy Whitcroft53210162008-07-23 21:29:03 -07002812 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2813 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002814
Joe Perchesd752fcc2014-08-06 16:11:05 -07002815 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002816 ERROR("OPEN_BRACE",
2817 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002818 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002819 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002820 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2821 $ctx =~ /\)\s*\;\s*$/ &&
2822 defined $lines[$ctx_ln - 1])
2823 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002824 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2825 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002826 WARN("TRAILING_SEMICOLON",
2827 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002828 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002829 }
2830 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002831 }
2832
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002833# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002834 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 -08002835 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2836 ctx_statement_block($linenr, $realcnt, 0)
2837 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002838 my ($s, $c) = ($stat, $cond);
2839
2840 substr($s, 0, length($c), '');
2841
2842 # Make sure we remove the line prefixes as we have
2843 # none on the first line, and are going to readd them
2844 # where necessary.
2845 $s =~ s/\n./\n/gs;
2846
2847 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002848 my @newlines = ($c =~ /\n/gs);
2849 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002850
2851 # We want to check the first line inside the block
2852 # starting at the end of the conditional, so remove:
2853 # 1) any blank line termination
2854 # 2) any opening brace { on end of the line
2855 # 3) any do (...) {
2856 my $continuation = 0;
2857 my $check = 0;
2858 $s =~ s/^.*\bdo\b//;
2859 $s =~ s/^\s*{//;
2860 if ($s =~ s/^\s*\\//) {
2861 $continuation = 1;
2862 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002863 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002864 $check = 1;
2865 $cond_lines++;
2866 }
2867
2868 # Also ignore a loop construct at the end of a
2869 # preprocessor statement.
2870 if (($prevline =~ /^.\s*#\s*define\s/ ||
2871 $prevline =~ /\\\s*$/) && $continuation == 0) {
2872 $check = 0;
2873 }
2874
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002875 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002876 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002877 while ($cond_ptr != $cond_lines) {
2878 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002879
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002880 # If we see an #else/#elif then the code
2881 # is not linear.
2882 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2883 $check = 0;
2884 }
2885
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002886 # Ignore:
2887 # 1) blank lines, they should be at 0,
2888 # 2) preprocessor lines, and
2889 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002890 if ($continuation ||
2891 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002892 $s =~ /^\s*#\s*?/ ||
2893 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002894 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002895 if ($s =~ s/^.*?\n//) {
2896 $cond_lines++;
2897 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002898 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002899 }
2900
2901 my (undef, $sindent) = line_stats("+" . $s);
2902 my $stat_real = raw_line($linenr, $cond_lines);
2903
2904 # Check if either of these lines are modified, else
2905 # this is not this patch's fault.
2906 if (!defined($stat_real) ||
2907 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2908 $check = 0;
2909 }
2910 if (defined($stat_real) && $cond_lines > 1) {
2911 $stat_real = "[...]\n$stat_real";
2912 }
2913
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002914 #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 -07002915
2916 if ($check && (($sindent % 8) != 0 ||
2917 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002918 WARN("SUSPECT_CODE_INDENT",
2919 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002920 }
2921 }
2922
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002923 # Track the 'values' across context and added lines.
2924 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002925 my ($curr_values, $curr_vars) =
2926 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002927 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002928 if ($dbg_values) {
2929 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002930 print "$linenr > .$outline\n";
2931 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002932 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002933 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002934 $prev_values = substr($curr_values, -1);
2935
Andy Whitcroft00df3442007-06-08 13:47:06 -07002936#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002937 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002938
Andy Whitcroft653d4872007-06-23 17:16:34 -07002939# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002940 if ($dbg_type) {
2941 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002942 ERROR("TEST_TYPE",
2943 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002944 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002945 ERROR("TEST_NOT_TYPE",
2946 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002947 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002948 next;
2949 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002950# TEST: allow direct testing of the attribute matcher.
2951 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002952 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002953 ERROR("TEST_ATTR",
2954 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002955 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002956 ERROR("TEST_NOT_ATTR",
2957 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002958 }
2959 next;
2960 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002961
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002962# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002963 if ($line =~ /^.\s*{/ &&
2964 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002965 if (ERROR("OPEN_BRACE",
2966 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002967 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2968 fix_delete_line($fixlinenr - 1, $prevrawline);
2969 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002970 my $fixedline = $prevrawline;
2971 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002972 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002973 $fixedline = $line;
2974 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002975 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002976 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002977 }
2978
Andy Whitcroft653d4872007-06-23 17:16:34 -07002979#
2980# Checks which are anchored on the added line.
2981#
2982
2983# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002984 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002985 my $path = $1;
2986 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002987 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002988 "malformed #include filename\n" . $herecurr);
2989 }
2990 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2991 ERROR("UAPI_INCLUDE",
2992 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002993 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002994 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002995
2996# no C99 // comments
2997 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002998 if (ERROR("C99_COMMENTS",
2999 "do not use C99 // comments\n" . $herecurr) &&
3000 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003001 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003002 if ($line =~ /\/\/(.*)$/) {
3003 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003004 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003005 }
3006 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003007 }
3008 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003009 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003010 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003011
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003012# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3013# the whole statement.
3014#print "APW <$lines[$realline_next - 1]>\n";
3015 if (defined $realline_next &&
3016 exists $lines[$realline_next - 1] &&
3017 !defined $suppress_export{$realline_next} &&
3018 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3019 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003020 # Handle definitions which produce identifiers with
3021 # a prefix:
3022 # XXX(foo);
3023 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003024 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003025 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003026 $name =~ /^${Ident}_$2/) {
3027#print "FOO C name<$name>\n";
3028 $suppress_export{$realline_next} = 1;
3029
3030 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003031 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003032 ^.DEFINE_$Ident\(\Q$name\E\)|
3033 ^.DECLARE_$Ident\(\Q$name\E\)|
3034 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003035 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3036 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003037 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003038#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3039 $suppress_export{$realline_next} = 2;
3040 } else {
3041 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003042 }
3043 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003044 if (!defined $suppress_export{$linenr} &&
3045 $prevline =~ /^.\s*$/ &&
3046 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3047 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3048#print "FOO B <$lines[$linenr - 1]>\n";
3049 $suppress_export{$linenr} = 2;
3050 }
3051 if (defined $suppress_export{$linenr} &&
3052 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003053 WARN("EXPORT_SYMBOL",
3054 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003055 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003056
Joe Eloff5150bda2010-08-09 17:21:00 -07003057# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003058 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3059 if (ERROR("GLOBAL_INITIALISERS",
3060 "do not initialise globals to 0 or NULL\n" .
3061 $herecurr) &&
3062 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003063 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003064 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003065 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003066# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003067 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3068 if (ERROR("INITIALISED_STATIC",
3069 "do not initialise statics to 0 or NULL\n" .
3070 $herecurr) &&
3071 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003072 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003073 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003074 }
3075
Joe Perches18130872014-08-06 16:11:22 -07003076# check for misordered declarations of char/short/int/long with signed/unsigned
3077 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3078 my $tmp = trim($1);
3079 WARN("MISORDERED_TYPE",
3080 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3081 }
3082
Joe Perchescb710ec2010-10-26 14:23:20 -07003083# check for static const char * arrays.
3084 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003085 WARN("STATIC_CONST_CHAR_ARRAY",
3086 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003087 $herecurr);
3088 }
3089
3090# check for static char foo[] = "bar" declarations.
3091 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003092 WARN("STATIC_CONST_CHAR_ARRAY",
3093 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003094 $herecurr);
3095 }
3096
Joe Perches9b0fa602014-04-03 14:49:18 -07003097# check for non-global char *foo[] = {"bar", ...} declarations.
3098 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3099 WARN("STATIC_CONST_CHAR_ARRAY",
3100 "char * array declaration might be better as static const\n" .
3101 $herecurr);
3102 }
3103
Joe Perchesb36190c2014-01-27 17:07:18 -08003104# check for function declarations without arguments like "int foo()"
3105 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3106 if (ERROR("FUNCTION_WITHOUT_ARGS",
3107 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3108 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003109 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003110 }
3111 }
3112
Joe Perches92e112f2013-12-13 11:36:22 -07003113# check for uses of DEFINE_PCI_DEVICE_TABLE
3114 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3115 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3116 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3117 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003118 $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 -07003119 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003120 }
3121
Andy Whitcroft653d4872007-06-23 17:16:34 -07003122# check for new typedefs, only function parameters and sparse annotations
3123# make sense.
3124 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003125 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003126 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003127 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003128 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003129 WARN("NEW_TYPEDEFS",
3130 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003131 }
3132
3133# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003134 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003135 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3136 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003137 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003138
Andy Whitcroft65863862009-01-06 14:41:21 -08003139 # Should start with a space.
3140 $to =~ s/^(\S)/ $1/;
3141 # Should not end with a space.
3142 $to =~ s/\s+$//;
3143 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003144 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003145 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003146
Joe Perches3705ce52013-07-03 15:05:31 -07003147## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003148 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003149 if (ERROR("POINTER_LOCATION",
3150 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3151 $fix) {
3152 my $sub_from = $ident;
3153 my $sub_to = $ident;
3154 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003155 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003156 s@\Q$sub_from\E@$sub_to@;
3157 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003158 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003159 }
3160 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3161 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003162 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003163
Andy Whitcroft65863862009-01-06 14:41:21 -08003164 # Should start with a space.
3165 $to =~ s/^(\S)/ $1/;
3166 # Should not end with a space.
3167 $to =~ s/\s+$//;
3168 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003169 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003170 }
3171 # Modifiers should have spaces.
3172 $to =~ s/(\b$Modifier$)/$1 /;
3173
Joe Perches3705ce52013-07-03 15:05:31 -07003174## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003175 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003176 if (ERROR("POINTER_LOCATION",
3177 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3178 $fix) {
3179
3180 my $sub_from = $match;
3181 my $sub_to = $match;
3182 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003183 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003184 s@\Q$sub_from\E@$sub_to@;
3185 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003186 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003187 }
3188
3189# # no BUG() or BUG_ON()
3190# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3191# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3192# print "$herecurr";
3193# $clean = 0;
3194# }
3195
Andy Whitcroft8905a672007-11-28 16:21:06 -08003196 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003197 WARN("LINUX_VERSION_CODE",
3198 "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 -08003199 }
3200
Joe Perches17441222011-06-15 15:08:17 -07003201# check for uses of printk_ratelimit
3202 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003203 WARN("PRINTK_RATELIMITED",
3204"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003205 }
3206
Andy Whitcroft00df3442007-06-08 13:47:06 -07003207# printk should use KERN_* levels. Note that follow on printk's on the
3208# same line do not need a level, so we use the current block context
3209# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003210# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003211# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003212 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003213 my $ok = 0;
3214 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3215 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003216 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003217 # with "\n" ignore it, else it is to blame
3218 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3219 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3220 $ok = 1;
3221 }
3222 last;
3223 }
3224 }
3225 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003226 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3227 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003228 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003229 }
3230
Joe Perches243f3802012-05-31 16:26:09 -07003231 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3232 my $orig = $1;
3233 my $level = lc($orig);
3234 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003235 my $level2 = $level;
3236 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003237 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003238 "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 -07003239 }
3240
3241 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003242 if (WARN("PREFER_PR_LEVEL",
3243 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3244 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003245 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003246 s/\bpr_warning\b/pr_warn/;
3247 }
Joe Perches243f3802012-05-31 16:26:09 -07003248 }
3249
Joe Perchesdc139312013-02-21 16:44:13 -08003250 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3251 my $orig = $1;
3252 my $level = lc($orig);
3253 $level = "warn" if ($level eq "warning");
3254 $level = "dbg" if ($level eq "debug");
3255 WARN("PREFER_DEV_LEVEL",
3256 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3257 }
3258
Andy Whitcroft653d4872007-06-23 17:16:34 -07003259# function brace can't be on same line, except for #defines of do while,
3260# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003261 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003262 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003263 if (ERROR("OPEN_BRACE",
3264 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3265 $fix) {
3266 fix_delete_line($fixlinenr, $rawline);
3267 my $fixed_line = $rawline;
3268 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3269 my $line1 = $1;
3270 my $line2 = $2;
3271 fix_insert_line($fixlinenr, ltrim($line1));
3272 fix_insert_line($fixlinenr, "\+{");
3273 if ($line2 !~ /^\s*$/) {
3274 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3275 }
3276 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003277 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003278
Andy Whitcroft8905a672007-11-28 16:21:06 -08003279# open braces for enum, union and struct go on the same line.
3280 if ($line =~ /^.\s*{/ &&
3281 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003282 if (ERROR("OPEN_BRACE",
3283 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3284 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3285 fix_delete_line($fixlinenr - 1, $prevrawline);
3286 fix_delete_line($fixlinenr, $rawline);
3287 my $fixedline = rtrim($prevrawline) . " {";
3288 fix_insert_line($fixlinenr, $fixedline);
3289 $fixedline = $rawline;
3290 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3291 if ($fixedline !~ /^\+\s*$/) {
3292 fix_insert_line($fixlinenr, $fixedline);
3293 }
3294 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003295 }
3296
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003297# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003298 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3299 if (WARN("SPACING",
3300 "missing space after $1 definition\n" . $herecurr) &&
3301 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003302 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003303 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3304 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003305 }
3306
Joe Perches31070b52014-01-23 15:54:49 -08003307# Function pointer declarations
3308# check spacing between type, funcptr, and args
3309# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003310 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003311 my $declare = $1;
3312 my $pre_pointer_space = $2;
3313 my $post_pointer_space = $3;
3314 my $funcname = $4;
3315 my $post_funcname_space = $5;
3316 my $pre_args_space = $6;
3317
Joe Perches91f72e92014-04-03 14:49:12 -07003318# the $Declare variable will capture all spaces after the type
3319# so check it for a missing trailing missing space but pointer return types
3320# don't need a space so don't warn for those.
3321 my $post_declare_space = "";
3322 if ($declare =~ /(\s+)$/) {
3323 $post_declare_space = $1;
3324 $declare = rtrim($declare);
3325 }
3326 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003327 WARN("SPACING",
3328 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003329 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003330 }
3331
3332# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003333# This test is not currently implemented because these declarations are
3334# equivalent to
3335# int foo(int bar, ...)
3336# and this is form shouldn't/doesn't generate a checkpatch warning.
3337#
3338# elsif ($declare =~ /\s{2,}$/) {
3339# WARN("SPACING",
3340# "Multiple spaces after return type\n" . $herecurr);
3341# }
Joe Perches31070b52014-01-23 15:54:49 -08003342
3343# unnecessary space "type ( *funcptr)(args...)"
3344 if (defined $pre_pointer_space &&
3345 $pre_pointer_space =~ /^\s/) {
3346 WARN("SPACING",
3347 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3348 }
3349
3350# unnecessary space "type (* funcptr)(args...)"
3351 if (defined $post_pointer_space &&
3352 $post_pointer_space =~ /^\s/) {
3353 WARN("SPACING",
3354 "Unnecessary space before function pointer name\n" . $herecurr);
3355 }
3356
3357# unnecessary space "type (*funcptr )(args...)"
3358 if (defined $post_funcname_space &&
3359 $post_funcname_space =~ /^\s/) {
3360 WARN("SPACING",
3361 "Unnecessary space after function pointer name\n" . $herecurr);
3362 }
3363
3364# unnecessary space "type (*funcptr) (args...)"
3365 if (defined $pre_args_space &&
3366 $pre_args_space =~ /^\s/) {
3367 WARN("SPACING",
3368 "Unnecessary space before function pointer arguments\n" . $herecurr);
3369 }
3370
3371 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003372 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003373 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003374 }
3375 }
3376
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003377# check for spacing round square brackets; allowed:
3378# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003379# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3380# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003381 while ($line =~ /(.*?\s)\[/g) {
3382 my ($where, $prefix) = ($-[1], $1);
3383 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003384 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003385 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003386 if (ERROR("BRACKET_SPACE",
3387 "space prohibited before open square bracket '['\n" . $herecurr) &&
3388 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003389 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003390 s/^(\+.*?)\s+\[/$1\[/;
3391 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003392 }
3393 }
3394
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003395# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003396 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003397 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003398 my $ctx_before = substr($line, 0, $-[1]);
3399 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003400
3401 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003402 if ($name =~ /^(?:
3403 if|for|while|switch|return|case|
3404 volatile|__volatile__|
3405 __attribute__|format|__extension__|
3406 asm|__asm__)$/x)
3407 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003408 # cpp #define statements have non-optional spaces, ie
3409 # if there is a space between the name and the open
3410 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003411 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003412
3413 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003414 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003415
3416 # If this whole things ends with a type its most
3417 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003418 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003419
3420 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003421 if (WARN("SPACING",
3422 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3423 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003424 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003425 s/\b$name\s+\(/$name\(/;
3426 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003427 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003428 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003429
Andy Whitcroft653d4872007-06-23 17:16:34 -07003430# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003431 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003432 my $fixed_line = "";
3433 my $line_fixed = 0;
3434
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003435 my $ops = qr{
3436 <<=|>>=|<=|>=|==|!=|
3437 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3438 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003439 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003440 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003441 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003442 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003443
3444## print("element count: <" . $#elements . ">\n");
3445## foreach my $el (@elements) {
3446## print("el: <$el>\n");
3447## }
3448
3449 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003450 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003451
Joe Perches3705ce52013-07-03 15:05:31 -07003452 foreach my $el (@elements) {
3453 push(@fix_elements, substr($rawline, $off, length($el)));
3454 $off += length($el);
3455 }
3456
3457 $off = 0;
3458
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003459 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003460 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003461
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003462 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003463
3464 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3465
3466## print("n: <$n> good: <$good>\n");
3467
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003468 $off += length($elements[$n]);
3469
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003470 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003471 my $ca = substr($opline, 0, $off);
3472 my $cc = '';
3473 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3474 $cc = substr($opline, $off + length($elements[$n + 1]));
3475 }
3476 my $cb = "$ca$;$cc";
3477
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003478 my $a = '';
3479 $a = 'V' if ($elements[$n] ne '');
3480 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003481 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003482 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3483 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003484 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003485
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003486 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003487
3488 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003489 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003490 $c = 'V' if ($elements[$n + 2] ne '');
3491 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003492 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003493 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3494 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003495 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003496 } else {
3497 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003498 }
3499
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003500 my $ctx = "${a}x${c}";
3501
3502 my $at = "(ctx:$ctx)";
3503
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003504 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003505 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003506
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003507 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003508 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003509
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003510 # Get the full operator variant.
3511 my $opv = $op . substr($curr_vars, $off, 1);
3512
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003513 # Ignore operators passed as parameters.
3514 if ($op_type ne 'V' &&
3515 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3516
Andy Whitcroftcf655042008-03-04 14:28:20 -08003517# # Ignore comments
3518# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003519
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003520 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003521 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003522 if ($ctx !~ /.x[WEBC]/ &&
3523 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003524 if (ERROR("SPACING",
3525 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003526 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003527 $line_fixed = 1;
3528 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003529 }
3530
3531 # // is a comment
3532 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003533
Joe Perchesb00e4812014-04-03 14:49:33 -07003534 # : when part of a bitfield
3535 } elsif ($opv eq ':B') {
3536 # skip the bitfield test for now
3537
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003538 # No spaces for:
3539 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003540 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003541 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003542 if (ERROR("SPACING",
3543 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003544 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003545 if (defined $fix_elements[$n + 2]) {
3546 $fix_elements[$n + 2] =~ s/^\s+//;
3547 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003548 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003549 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003550 }
3551
Joe Perches23810972014-12-10 15:51:32 -08003552 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003553 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003554 my $rtrim_before = 0;
3555 my $space_after = 0;
3556 if ($ctx =~ /Wx./) {
3557 if (ERROR("SPACING",
3558 "space prohibited before that '$op' $at\n" . $hereptr)) {
3559 $line_fixed = 1;
3560 $rtrim_before = 1;
3561 }
3562 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003563 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003564 if (ERROR("SPACING",
3565 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003566 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003567 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003568 $space_after = 1;
3569 }
3570 }
3571 if ($rtrim_before || $space_after) {
3572 if ($rtrim_before) {
3573 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3574 } else {
3575 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3576 }
3577 if ($space_after) {
3578 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003579 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003580 }
3581
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003582 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003583 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003584 #warn "'*' is part of type\n";
3585
3586 # unary operators should have a space before and
3587 # none after. May be left adjacent to another
3588 # unary operator, or a cast
3589 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003590 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003591 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003592 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003593 if (ERROR("SPACING",
3594 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003595 if ($n != $last_after + 2) {
3596 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3597 $line_fixed = 1;
3598 }
Joe Perches3705ce52013-07-03 15:05:31 -07003599 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003600 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003601 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003602 # A unary '*' may be const
3603
3604 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003605 if (ERROR("SPACING",
3606 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003607 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003608 if (defined $fix_elements[$n + 2]) {
3609 $fix_elements[$n + 2] =~ s/^\s+//;
3610 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003611 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003612 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003613 }
3614
3615 # unary ++ and unary -- are allowed no space on one side.
3616 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003617 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003618 if (ERROR("SPACING",
3619 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003620 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003621 $line_fixed = 1;
3622 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003623 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003624 if ($ctx =~ /Wx[BE]/ ||
3625 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003626 if (ERROR("SPACING",
3627 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003628 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003629 $line_fixed = 1;
3630 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003631 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003632 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003633 if (ERROR("SPACING",
3634 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003635 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003636 if (defined $fix_elements[$n + 2]) {
3637 $fix_elements[$n + 2] =~ s/^\s+//;
3638 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003639 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003640 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003641 }
3642
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003643 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003644 } elsif ($op eq '<<' or $op eq '>>' or
3645 $op eq '&' or $op eq '^' or $op eq '|' or
3646 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003647 $op eq '*' or $op eq '/' or
3648 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003649 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003650 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003651 if (ERROR("SPACING",
3652 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003653 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3654 if (defined $fix_elements[$n + 2]) {
3655 $fix_elements[$n + 2] =~ s/^\s+//;
3656 }
Joe Perches3705ce52013-07-03 15:05:31 -07003657 $line_fixed = 1;
3658 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003659 }
3660
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003661 # A colon needs no spaces before when it is
3662 # terminating a case value or a label.
3663 } elsif ($opv eq ':C' || $opv eq ':L') {
3664 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003665 if (ERROR("SPACING",
3666 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003667 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003668 $line_fixed = 1;
3669 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003670 }
3671
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003672 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003673 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003674 my $ok = 0;
3675
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003676 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003677 if (($op eq '<' &&
3678 $cc =~ /^\S+\@\S+>/) ||
3679 ($op eq '>' &&
3680 $ca =~ /<\S+\@\S+$/))
3681 {
3682 $ok = 1;
3683 }
3684
Joe Perches84731622013-11-12 15:10:05 -08003685 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003686 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003687 my $msg_type = \&ERROR;
3688 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3689
3690 if (&{$msg_type}("SPACING",
3691 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003692 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3693 if (defined $fix_elements[$n + 2]) {
3694 $fix_elements[$n + 2] =~ s/^\s+//;
3695 }
Joe Perches3705ce52013-07-03 15:05:31 -07003696 $line_fixed = 1;
3697 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003698 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003699 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003700 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003701
3702## print("n: <$n> GOOD: <$good>\n");
3703
3704 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003705 }
Joe Perches3705ce52013-07-03 15:05:31 -07003706
3707 if (($#elements % 2) == 0) {
3708 $fixed_line = $fixed_line . $fix_elements[$#elements];
3709 }
3710
Joe Perches194f66f2014-08-06 16:11:03 -07003711 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3712 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003713 }
3714
3715
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003716 }
3717
Joe Perches786b6322013-07-03 15:05:32 -07003718# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003719 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003720 if (WARN("SPACING",
3721 "space prohibited before semicolon\n" . $herecurr) &&
3722 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003723 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003724 s/^(\+.*\S)\s+;/$1;/;
3725 }
3726 }
3727
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003728# check for multiple assignments
3729 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003730 CHK("MULTIPLE_ASSIGNMENTS",
3731 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003732 }
3733
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003734## # check for multiple declarations, allowing for a function declaration
3735## # continuation.
3736## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3737## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3738##
3739## # Remove any bracketed sections to ensure we do not
3740## # falsly report the parameters of functions.
3741## my $ln = $line;
3742## while ($ln =~ s/\([^\(\)]*\)//g) {
3743## }
3744## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003745## WARN("MULTIPLE_DECLARATION",
3746## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003747## }
3748## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003749
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003750#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003751 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3752 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003753 if (ERROR("SPACING",
3754 "space required before the open brace '{'\n" . $herecurr) &&
3755 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003756 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003757 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003758 }
3759
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003760## # check for blank lines before declarations
3761## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3762## $prevrawline =~ /^.\s*$/) {
3763## WARN("SPACING",
3764## "No blank lines before declarations\n" . $hereprev);
3765## }
3766##
3767
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003768# closing brace should have a space following it when it has anything
3769# on the line
3770 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003771 if (ERROR("SPACING",
3772 "space required after that close brace '}'\n" . $herecurr) &&
3773 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003774 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003775 s/}((?!(?:,|;|\)))\S)/} $1/;
3776 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003777 }
3778
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003779# check spacing on square brackets
3780 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003781 if (ERROR("SPACING",
3782 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3783 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003784 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003785 s/\[\s+/\[/;
3786 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003787 }
3788 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003789 if (ERROR("SPACING",
3790 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3791 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003792 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003793 s/\s+\]/\]/;
3794 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003795 }
3796
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003797# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003798 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3799 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003800 if (ERROR("SPACING",
3801 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3802 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003803 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003804 s/\(\s+/\(/;
3805 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003806 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003807 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003808 $line !~ /for\s*\(.*;\s+\)/ &&
3809 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003810 if (ERROR("SPACING",
3811 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3812 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003813 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003814 s/\s+\)/\)/;
3815 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003816 }
3817
Joe Perchese2826fd2014-08-06 16:10:48 -07003818# check unnecessary parentheses around addressof/dereference single $Lvals
3819# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3820
3821 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003822 my $var = $1;
3823 if (CHK("UNNECESSARY_PARENTHESES",
3824 "Unnecessary parentheses around $var\n" . $herecurr) &&
3825 $fix) {
3826 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3827 }
3828 }
3829
3830# check for unnecessary parentheses around function pointer uses
3831# ie: (foo->bar)(); should be foo->bar();
3832# but not "if (foo->bar) (" to avoid some false positives
3833 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3834 my $var = $2;
3835 if (CHK("UNNECESSARY_PARENTHESES",
3836 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3837 $fix) {
3838 my $var2 = deparenthesize($var);
3839 $var2 =~ s/\s//g;
3840 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3841 }
3842 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003843
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003844#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003845 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003846 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003847 if (WARN("INDENTED_LABEL",
3848 "labels should not be indented\n" . $herecurr) &&
3849 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003850 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003851 s/^(.)\s+/$1/;
3852 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003853 }
3854
Joe Perches5b9553a2014-04-03 14:49:21 -07003855# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003856 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003857 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003858 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003859 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3860 my $value = $1;
3861 $value = deparenthesize($value);
3862 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3863 ERROR("RETURN_PARENTHESES",
3864 "return is not a function, parentheses are not required\n" . $herecurr);
3865 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003866 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003867 ERROR("SPACING",
3868 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003869 }
3870 }
Joe Perches507e5142013-11-12 15:10:13 -08003871
Joe Perchesb43ae212014-06-23 13:22:07 -07003872# unnecessary return in a void function
3873# at end-of-function, with the previous line a single leading tab, then return;
3874# and the line before that not a goto label target like "out:"
3875 if ($sline =~ /^[ \+]}\s*$/ &&
3876 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3877 $linenr >= 3 &&
3878 $lines[$linenr - 3] =~ /^[ +]/ &&
3879 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003880 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003881 "void function return statements are not generally useful\n" . $hereprev);
3882 }
Joe Perches9819cf22014-06-04 16:12:09 -07003883
Joe Perches189248d2014-01-23 15:54:47 -08003884# if statements using unnecessary parentheses - ie: if ((foo == bar))
3885 if ($^V && $^V ge 5.10.0 &&
3886 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3887 my $openparens = $1;
3888 my $count = $openparens =~ tr@\(@\(@;
3889 my $msg = "";
3890 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3891 my $comp = $4; #Not $1 because of $LvalOrFunc
3892 $msg = " - maybe == should be = ?" if ($comp eq "==");
3893 WARN("UNNECESSARY_PARENTHESES",
3894 "Unnecessary parentheses$msg\n" . $herecurr);
3895 }
3896 }
3897
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003898# Return of what appears to be an errno should normally be -'ve
3899 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3900 my $name = $1;
3901 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 WARN("USE_NEGATIVE_ERRNO",
3903 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003904 }
3905 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003906
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003907# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003908 if ($line =~ /\b(if|while|for|switch)\(/) {
3909 if (ERROR("SPACING",
3910 "space required before the open parenthesis '('\n" . $herecurr) &&
3911 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003912 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003913 s/\b(if|while|for|switch)\(/$1 \(/;
3914 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003915 }
3916
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003917# Check for illegal assignment in if conditional -- and check for trailing
3918# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003919 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003920 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3921 ctx_statement_block($linenr, $realcnt, 0)
3922 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003923 my ($stat_next) = ctx_statement_block($line_nr_next,
3924 $remain_next, $off_next);
3925 $stat_next =~ s/\n./\n /g;
3926 ##print "stat<$stat> stat_next<$stat_next>\n";
3927
3928 if ($stat_next =~ /^\s*while\b/) {
3929 # If the statement carries leading newlines,
3930 # then count those as offsets.
3931 my ($whitespace) =
3932 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3933 my $offset =
3934 statement_rawlines($whitespace) - 1;
3935
3936 $suppress_whiletrailers{$line_nr_next +
3937 $offset} = 1;
3938 }
3939 }
3940 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003941 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003942 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003943 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003944
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003945 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003946 ERROR("ASSIGN_IN_IF",
3947 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003948 }
3949
3950 # Find out what is on the end of the line after the
3951 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003952 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003953 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003954 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003955 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3956 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003957 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003958 # Find out how long the conditional actually is.
3959 my @newlines = ($c =~ /\n/gs);
3960 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003961 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003962
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003963 $stat_real = raw_line($linenr, $cond_lines)
3964 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003965 if (defined($stat_real) && $cond_lines > 1) {
3966 $stat_real = "[...]\n$stat_real";
3967 }
3968
Joe Perches000d1cc12011-07-25 17:13:25 -07003969 ERROR("TRAILING_STATEMENTS",
3970 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003971 }
3972 }
3973
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003974# Check for bitwise tests written as boolean
3975 if ($line =~ /
3976 (?:
3977 (?:\[|\(|\&\&|\|\|)
3978 \s*0[xX][0-9]+\s*
3979 (?:\&\&|\|\|)
3980 |
3981 (?:\&\&|\|\|)
3982 \s*0[xX][0-9]+\s*
3983 (?:\&\&|\|\||\)|\])
3984 )/x)
3985 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003986 WARN("HEXADECIMAL_BOOLEAN_TEST",
3987 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003988 }
3989
Andy Whitcroft8905a672007-11-28 16:21:06 -08003990# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003991 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3992 my $s = $1;
3993 $s =~ s/$;//g; # Remove any comments
3994 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003995 ERROR("TRAILING_STATEMENTS",
3996 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003997 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003998 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003999# if should not continue a brace
4000 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004001 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004002 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004003 $herecurr);
4004 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004005# case and default should not have general statements after them
4006 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4007 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004008 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004009 \s*return\s+
4010 )/xg)
4011 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004012 ERROR("TRAILING_STATEMENTS",
4013 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004014 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004015
4016 # Check for }<nl>else {, these must be at the same
4017 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004018 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4019 $previndent == $indent) {
4020 if (ERROR("ELSE_AFTER_BRACE",
4021 "else should follow close brace '}'\n" . $hereprev) &&
4022 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4023 fix_delete_line($fixlinenr - 1, $prevrawline);
4024 fix_delete_line($fixlinenr, $rawline);
4025 my $fixedline = $prevrawline;
4026 $fixedline =~ s/}\s*$//;
4027 if ($fixedline !~ /^\+\s*$/) {
4028 fix_insert_line($fixlinenr, $fixedline);
4029 }
4030 $fixedline = $rawline;
4031 $fixedline =~ s/^(.\s*)else/$1} else/;
4032 fix_insert_line($fixlinenr, $fixedline);
4033 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004034 }
4035
Joe Perches8b8856f2014-08-06 16:11:14 -07004036 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4037 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004038 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4039
4040 # Find out what is on the end of the line after the
4041 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004042 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004043 $s =~ s/\n.*//g;
4044
4045 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004046 if (ERROR("WHILE_AFTER_BRACE",
4047 "while should follow close brace '}'\n" . $hereprev) &&
4048 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4049 fix_delete_line($fixlinenr - 1, $prevrawline);
4050 fix_delete_line($fixlinenr, $rawline);
4051 my $fixedline = $prevrawline;
4052 my $trailing = $rawline;
4053 $trailing =~ s/^\+//;
4054 $trailing = trim($trailing);
4055 $fixedline =~ s/}\s*$/} $trailing/;
4056 fix_insert_line($fixlinenr, $fixedline);
4057 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004058 }
4059 }
4060
Joe Perches95e2c602013-07-03 15:05:20 -07004061#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004062 while ($line =~ m{($Constant|$Lval)}g) {
4063 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004064
4065#gcc binary extension
4066 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004067 if (WARN("GCC_BINARY_CONSTANT",
4068 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4069 $fix) {
4070 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004071 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004072 s/\b$var\b/$hexval/;
4073 }
Joe Perches95e2c602013-07-03 15:05:20 -07004074 }
4075
4076#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004077 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004078 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004079#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004080 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004081#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004082 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4083#Ignore some three character SI units explicitly, like MiB and KHz
4084 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004085 while ($var =~ m{($Ident)}g) {
4086 my $word = $1;
4087 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004088 if ($check) {
4089 seed_camelcase_includes();
4090 if (!$file && !$camelcase_file_seeded) {
4091 seed_camelcase_file($realfile);
4092 $camelcase_file_seeded = 1;
4093 }
4094 }
Joe Perches7e781f62013-09-11 14:23:55 -07004095 if (!defined $camelcase{$word}) {
4096 $camelcase{$word} = 1;
4097 CHK("CAMELCASE",
4098 "Avoid CamelCase: <$word>\n" . $herecurr);
4099 }
Joe Perches34456862013-07-03 15:05:34 -07004100 }
Joe Perches323c1262012-12-17 16:02:07 -08004101 }
4102 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004103
4104#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004105 if ($line =~ /\#\s*define.*\\\s+$/) {
4106 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4107 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4108 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004109 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004110 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004111 }
4112
Andy Whitcroft653d4872007-06-23 17:16:34 -07004113#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004114 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004115 my $file = "$1.h";
4116 my $checkfile = "include/linux/$file";
4117 if (-f "$root/$checkfile" &&
4118 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004119 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004120 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004121 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004122 CHK("ARCH_INCLUDE_LINUX",
4123 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004124 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004125 WARN("INCLUDE_LINUX",
4126 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004127 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004128 }
4129 }
4130
Andy Whitcroft653d4872007-06-23 17:16:34 -07004131# multi-statement macros should be enclosed in a do while loop, grab the
4132# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004133# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004134 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4135 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004136 my $ln = $linenr;
4137 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004138 my ($off, $dstat, $dcond, $rest);
4139 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004140 my $has_flow_statement = 0;
4141 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004142 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004143 ctx_statement_block($linenr, $realcnt, 0);
4144 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004145 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004146 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004147
Joe Perches08a28432014-10-13 15:51:55 -07004148 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4149 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4150
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004151 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004152 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004153 $dstat =~ s/\\\n.//g;
4154 $dstat =~ s/^\s*//s;
4155 $dstat =~ s/\s*$//s;
4156
4157 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004158 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4159 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004160 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004161 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004162 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004163
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004164 # Flatten any obvious string concatentation.
4165 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4166 $dstat =~ s/$Ident\s*("X*")/$1/)
4167 {
4168 }
4169
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004170 my $exceptions = qr{
4171 $Declare|
4172 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004173 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004174 DECLARE_PER_CPU|
4175 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004176 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004177 union|
4178 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004179 \.$Ident\s*=\s*|
4180 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004181 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004182 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004183 if ($dstat ne '' &&
4184 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4185 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004186 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004187 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004188 $dstat !~ /$exceptions/ &&
4189 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004190 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004191 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004192 $dstat !~ /^for\s*$Constant$/ && # for (...)
4193 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4194 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004195 $dstat !~ /^\({/ && # ({...
4196 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004197 {
4198 $ctx =~ s/\n*$//;
4199 my $herectx = $here . "\n";
4200 my $cnt = statement_rawlines($ctx);
4201
4202 for (my $n = 0; $n < $cnt; $n++) {
4203 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004204 }
4205
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004206 if ($dstat =~ /;/) {
4207 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4208 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4209 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004210 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004211 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004212 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004213 }
Joe Perches5023d342012-12-17 16:01:47 -08004214
Joe Perches08a28432014-10-13 15:51:55 -07004215# check for macros with flow control, but without ## concatenation
4216# ## concatenation is commonly a macro that defines a function so ignore those
4217 if ($has_flow_statement && !$has_arg_concat) {
4218 my $herectx = $here . "\n";
4219 my $cnt = statement_rawlines($ctx);
4220
4221 for (my $n = 0; $n < $cnt; $n++) {
4222 $herectx .= raw_line($linenr, $n) . "\n";
4223 }
4224 WARN("MACRO_WITH_FLOW_CONTROL",
4225 "Macros with flow control statements should be avoided\n" . "$herectx");
4226 }
4227
Joe Perches481eb482012-12-17 16:01:56 -08004228# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004229
4230 } else {
4231 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004232 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4233 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004234 $line =~ /^\+.*\\$/) {
4235 WARN("LINE_CONTINUATIONS",
4236 "Avoid unnecessary line continuations\n" . $herecurr);
4237 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004238 }
4239
Joe Perchesb13edf72012-07-30 14:41:24 -07004240# do {} while (0) macro tests:
4241# single-statement macros do not need to be enclosed in do while (0) loop,
4242# macro should not end with a semicolon
4243 if ($^V && $^V ge 5.10.0 &&
4244 $realfile !~ m@/vmlinux.lds.h$@ &&
4245 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4246 my $ln = $linenr;
4247 my $cnt = $realcnt;
4248 my ($off, $dstat, $dcond, $rest);
4249 my $ctx = '';
4250 ($dstat, $dcond, $ln, $cnt, $off) =
4251 ctx_statement_block($linenr, $realcnt, 0);
4252 $ctx = $dstat;
4253
4254 $dstat =~ s/\\\n.//g;
4255
4256 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4257 my $stmts = $2;
4258 my $semis = $3;
4259
4260 $ctx =~ s/\n*$//;
4261 my $cnt = statement_rawlines($ctx);
4262 my $herectx = $here . "\n";
4263
4264 for (my $n = 0; $n < $cnt; $n++) {
4265 $herectx .= raw_line($linenr, $n) . "\n";
4266 }
4267
Joe Perchesac8e97f2012-08-21 16:15:53 -07004268 if (($stmts =~ tr/;/;/) == 1 &&
4269 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004270 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4271 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4272 }
4273 if (defined $semis && $semis ne "") {
4274 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4275 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4276 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004277 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4278 $ctx =~ s/\n*$//;
4279 my $cnt = statement_rawlines($ctx);
4280 my $herectx = $here . "\n";
4281
4282 for (my $n = 0; $n < $cnt; $n++) {
4283 $herectx .= raw_line($linenr, $n) . "\n";
4284 }
4285
4286 WARN("TRAILING_SEMICOLON",
4287 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004288 }
4289 }
4290
Mike Frysinger080ba922009-01-06 14:41:25 -08004291# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4292# all assignments may have only one of the following with an assignment:
4293# .
4294# ALIGN(...)
4295# VMLINUX_SYMBOL(...)
4296 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004297 WARN("MISSING_VMLINUX_SYMBOL",
4298 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004299 }
4300
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004301# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004302 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4303 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004304 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004305 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004306 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4307 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004308 my @allowed = ();
4309 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004310 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004311 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004312 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004313 for my $chunk (@chunks) {
4314 my ($cond, $block) = @{$chunk};
4315
Andy Whitcroft773647a2008-03-28 14:15:58 -07004316 # If the condition carries leading newlines, then count those as offsets.
4317 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4318 my $offset = statement_rawlines($whitespace) - 1;
4319
Joe Perchesaad4f612012-03-23 15:02:19 -07004320 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004321 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4322
4323 # We have looked at and allowed this specific line.
4324 $suppress_ifbraces{$ln + $offset} = 1;
4325
4326 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004327 $ln += statement_rawlines($block) - 1;
4328
Andy Whitcroft773647a2008-03-28 14:15:58 -07004329 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004330
4331 $seen++ if ($block =~ /^\s*{/);
4332
Joe Perchesaad4f612012-03-23 15:02:19 -07004333 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004334 if (statement_lines($cond) > 1) {
4335 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004336 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004337 }
4338 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004339 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004340 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004341 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004342 if (statement_block_size($block) > 1) {
4343 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004344 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004345 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004346 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004347 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004348 if ($seen) {
4349 my $sum_allowed = 0;
4350 foreach (@allowed) {
4351 $sum_allowed += $_;
4352 }
4353 if ($sum_allowed == 0) {
4354 WARN("BRACES",
4355 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4356 } elsif ($sum_allowed != $allow &&
4357 $seen != $allow) {
4358 CHK("BRACES",
4359 "braces {} should be used on all arms of this statement\n" . $herectx);
4360 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004361 }
4362 }
4363 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004364 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004365 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004366 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004367
Andy Whitcroftcf655042008-03-04 14:28:20 -08004368 # Check the pre-context.
4369 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4370 #print "APW: ALLOWED: pre<$1>\n";
4371 $allowed = 1;
4372 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004373
4374 my ($level, $endln, @chunks) =
4375 ctx_statement_full($linenr, $realcnt, $-[0]);
4376
Andy Whitcroftcf655042008-03-04 14:28:20 -08004377 # Check the condition.
4378 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004379 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004380 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004381 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004382 }
4383 if (statement_lines($cond) > 1) {
4384 #print "APW: ALLOWED: cond<$cond>\n";
4385 $allowed = 1;
4386 }
4387 if ($block =~/\b(?:if|for|while)\b/) {
4388 #print "APW: ALLOWED: block<$block>\n";
4389 $allowed = 1;
4390 }
4391 if (statement_block_size($block) > 1) {
4392 #print "APW: ALLOWED: lines block<$block>\n";
4393 $allowed = 1;
4394 }
4395 # Check the post-context.
4396 if (defined $chunks[1]) {
4397 my ($cond, $block) = @{$chunks[1]};
4398 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004399 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004400 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004401 if ($block =~ /^\s*\{/) {
4402 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4403 $allowed = 1;
4404 }
4405 }
4406 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004407 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004408 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004409
Andy Whitcroftf0556632008-10-15 22:02:23 -07004410 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004411 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004412 }
4413
Joe Perches000d1cc12011-07-25 17:13:25 -07004414 WARN("BRACES",
4415 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004416 }
4417 }
4418
Joe Perches0979ae62012-12-17 16:01:59 -08004419# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004420 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004421 CHK("BRACES",
4422 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4423 }
Joe Perches77b9a532013-07-03 15:05:29 -07004424 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004425 CHK("BRACES",
4426 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4427 }
4428
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004429# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004430 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4431 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004432 WARN("VOLATILE",
4433 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004434 }
4435
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004436# Check for user-visible strings broken across lines, which breaks the ability
4437# to grep for the string. Make exceptions when the previous string ends in a
4438# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4439# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4440 if ($line =~ /^\+\s*"[X\t]*"/ &&
4441 $prevline =~ /"\s*$/ &&
4442 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4443 if (WARN("SPLIT_STRING",
4444 "quoted string split across lines\n" . $hereprev) &&
4445 $fix &&
4446 $prevrawline =~ /^\+.*"\s*$/ &&
4447 $last_coalesced_string_linenr != $linenr - 1) {
4448 my $extracted_string = get_quoted_string($line, $rawline);
4449 my $comma_close = "";
4450 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4451 $comma_close = $1;
4452 }
4453
4454 fix_delete_line($fixlinenr - 1, $prevrawline);
4455 fix_delete_line($fixlinenr, $rawline);
4456 my $fixedline = $prevrawline;
4457 $fixedline =~ s/"\s*$//;
4458 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4459 fix_insert_line($fixlinenr - 1, $fixedline);
4460 $fixedline = $rawline;
4461 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4462 if ($fixedline !~ /\+\s*$/) {
4463 fix_insert_line($fixlinenr, $fixedline);
4464 }
4465 $last_coalesced_string_linenr = $linenr;
4466 }
4467 }
4468
4469# check for missing a space in a string concatenation
4470 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4471 WARN('MISSING_SPACE',
4472 "break quoted strings at a space character\n" . $hereprev);
4473 }
4474
4475# check for spaces before a quoted newline
4476 if ($rawline =~ /^.*\".*\s\\n/) {
4477 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4478 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4479 $fix) {
4480 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4481 }
4482
4483 }
4484
Joe Perchesf17dba42014-10-13 15:51:51 -07004485# concatenated string without spaces between elements
4486 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4487 CHK("CONCATENATED_STRING",
4488 "Concatenated strings should use spaces between elements\n" . $herecurr);
4489 }
4490
Joe Perches90ad30e2014-12-10 15:51:59 -08004491# uncoalesced string fragments
4492 if ($line =~ /"X*"\s*"/) {
4493 WARN("STRING_FRAGMENTS",
4494 "Consecutive strings are generally better as a single string\n" . $herecurr);
4495 }
4496
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004497# check for %L{u,d,i} in strings
4498 my $string;
4499 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4500 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4501 $string =~ s/%%/__/g;
4502 if ($string =~ /(?<!%)%L[udi]/) {
4503 WARN("PRINTF_L",
4504 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4505 last;
4506 }
4507 }
4508
4509# check for line continuations in quoted strings with odd counts of "
4510 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4511 WARN("LINE_CONTINUATIONS",
4512 "Avoid line continuations in quoted strings\n" . $herecurr);
4513 }
4514
Andy Whitcroft00df3442007-06-08 13:47:06 -07004515# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004516 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004517 CHK("REDUNDANT_CODE",
4518 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004519 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004520 }
4521
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004522# check for needless "if (<foo>) fn(<foo>)" uses
4523 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4524 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4525 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4526 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004527 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004528 }
4529 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004530
Joe Perchesebfdc402014-08-06 16:10:27 -07004531# check for unnecessary "Out of Memory" messages
4532 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4533 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4534 (defined $1 || defined $3) &&
4535 $linenr > 3) {
4536 my $testval = $2;
4537 my $testline = $lines[$linenr - 3];
4538
4539 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4540# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4541
4542 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4543 WARN("OOM_MESSAGE",
4544 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4545 }
4546 }
4547
Joe Perchesf78d98f2014-10-13 15:52:01 -07004548# check for logging functions with KERN_<LEVEL>
4549 if ($line !~ /printk\s*\(/ &&
4550 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4551 my $level = $1;
4552 if (WARN("UNNECESSARY_KERN_LEVEL",
4553 "Possible unnecessary $level\n" . $herecurr) &&
4554 $fix) {
4555 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4556 }
4557 }
4558
Joe Perchesabb08a52014-12-10 15:51:46 -08004559# check for mask then right shift without a parentheses
4560 if ($^V && $^V ge 5.10.0 &&
4561 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4562 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4563 WARN("MASK_THEN_SHIFT",
4564 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4565 }
4566
Joe Perchesb75ac612014-12-10 15:52:02 -08004567# check for pointer comparisons to NULL
4568 if ($^V && $^V ge 5.10.0) {
4569 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4570 my $val = $1;
4571 my $equal = "!";
4572 $equal = "" if ($4 eq "!=");
4573 if (CHK("COMPARISON_TO_NULL",
4574 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4575 $fix) {
4576 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4577 }
4578 }
4579 }
4580
Joe Perches8716de32013-09-11 14:24:05 -07004581# check for bad placement of section $InitAttribute (e.g.: __initdata)
4582 if ($line =~ /(\b$InitAttribute\b)/) {
4583 my $attr = $1;
4584 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4585 my $ptr = $1;
4586 my $var = $2;
4587 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4588 ERROR("MISPLACED_INIT",
4589 "$attr should be placed after $var\n" . $herecurr)) ||
4590 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4591 WARN("MISPLACED_INIT",
4592 "$attr should be placed after $var\n" . $herecurr))) &&
4593 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004594 $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 -07004595 }
4596 }
4597 }
4598
Joe Perchese970b8842013-11-12 15:10:10 -08004599# check for $InitAttributeData (ie: __initdata) with const
4600 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4601 my $attr = $1;
4602 $attr =~ /($InitAttributePrefix)(.*)/;
4603 my $attr_prefix = $1;
4604 my $attr_type = $2;
4605 if (ERROR("INIT_ATTRIBUTE",
4606 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4607 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004608 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004609 s/$InitAttributeData/${attr_prefix}initconst/;
4610 }
4611 }
4612
4613# check for $InitAttributeConst (ie: __initconst) without const
4614 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4615 my $attr = $1;
4616 if (ERROR("INIT_ATTRIBUTE",
4617 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4618 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004619 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004620 /(^\+\s*(?:static\s+))/;
4621 $lead = rtrim($1);
4622 $lead = "$lead " if ($lead !~ /^\+$/);
4623 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004624 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004625 }
4626 }
4627
Joe Perchesfbdb8132014-04-03 14:49:14 -07004628# don't use __constant_<foo> functions outside of include/uapi/
4629 if ($realfile !~ m@^include/uapi/@ &&
4630 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4631 my $constant_func = $1;
4632 my $func = $constant_func;
4633 $func =~ s/^__constant_//;
4634 if (WARN("CONSTANT_CONVERSION",
4635 "$constant_func should be $func\n" . $herecurr) &&
4636 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004637 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004638 }
4639 }
4640
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004641# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004642 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004643 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004644 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004645 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004646 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004647 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4648 }
4649 if ($delay > 2000) {
4650 WARN("LONG_UDELAY",
4651 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004652 }
4653 }
4654
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004655# warn about unexpectedly long msleep's
4656 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4657 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004658 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004659 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004660 }
4661 }
4662
Joe Perches36ec1932013-07-03 15:05:25 -07004663# check for comparisons of jiffies
4664 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4665 WARN("JIFFIES_COMPARISON",
4666 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4667 }
4668
Joe Perches9d7a34a2013-07-03 15:05:26 -07004669# check for comparisons of get_jiffies_64()
4670 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4671 WARN("JIFFIES_COMPARISON",
4672 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4673 }
4674
Andy Whitcroft00df3442007-06-08 13:47:06 -07004675# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004676# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004677# print "#ifdef in C files should be avoided\n";
4678# print "$herecurr";
4679# $clean = 0;
4680# }
4681
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004682# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004683 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004684 if (ERROR("SPACING",
4685 "exactly one space required after that #$1\n" . $herecurr) &&
4686 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004687 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004688 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4689 }
4690
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004691 }
4692
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004693# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004694 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4695 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004696 my $which = $1;
4697 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004698 CHK("UNCOMMENTED_DEFINITION",
4699 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004700 }
4701 }
4702# check for memory barriers without a comment.
4703 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4704 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004705 WARN("MEMORY_BARRIER",
4706 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004707 }
4708 }
4709# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004710 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004711 CHK("ARCH_DEFINES",
4712 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004713 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004714
Tobias Klauserd4977c72010-05-24 14:33:30 -07004715# Check that the storage class is at the beginning of a declaration
4716 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004717 WARN("STORAGE_CLASS",
4718 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004719 }
4720
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004721# check the location of the inline attribute, that it is between
4722# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004723 if ($line =~ /\b$Type\s+$Inline\b/ ||
4724 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004725 ERROR("INLINE_LOCATION",
4726 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004727 }
4728
Andy Whitcroft8905a672007-11-28 16:21:06 -08004729# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004730 if ($realfile !~ m@\binclude/uapi/@ &&
4731 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004732 if (WARN("INLINE",
4733 "plain inline is preferred over $1\n" . $herecurr) &&
4734 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004735 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004736
4737 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004738 }
4739
Joe Perches3d130fd2011-01-12 17:00:00 -08004740# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004741 if ($realfile !~ m@\binclude/uapi/@ &&
4742 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004743 WARN("PREFER_PACKED",
4744 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004745 }
4746
Joe Perches39b7e282011-07-25 17:13:24 -07004747# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004748 if ($realfile !~ m@\binclude/uapi/@ &&
4749 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004750 WARN("PREFER_ALIGNED",
4751 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004752 }
4753
Joe Perches5f14d3b2012-01-10 15:09:52 -08004754# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004755 if ($realfile !~ m@\binclude/uapi/@ &&
4756 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004757 if (WARN("PREFER_PRINTF",
4758 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4759 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004760 $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 -07004761
4762 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004763 }
4764
Joe Perches6061d942012-03-23 15:02:16 -07004765# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004766 if ($realfile !~ m@\binclude/uapi/@ &&
4767 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004768 if (WARN("PREFER_SCANF",
4769 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4770 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004771 $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 -07004772 }
Joe Perches6061d942012-03-23 15:02:16 -07004773 }
4774
Joe Perches619a9082014-12-10 15:51:35 -08004775# Check for __attribute__ weak, or __weak declarations (may have link issues)
4776 if ($^V && $^V ge 5.10.0 &&
4777 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4778 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4779 $line =~ /\b__weak\b/)) {
4780 ERROR("WEAK_DECLARATION",
4781 "Using weak declarations can have unintended link defects\n" . $herecurr);
4782 }
4783
Joe Perches8f53a9b2010-03-05 13:43:48 -08004784# check for sizeof(&)
4785 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004786 WARN("SIZEOF_ADDRESS",
4787 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004788 }
4789
Joe Perches66c80b62012-07-30 14:41:22 -07004790# check for sizeof without parenthesis
4791 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004792 if (WARN("SIZEOF_PARENTHESIS",
4793 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4794 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004795 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004796 }
Joe Perches66c80b62012-07-30 14:41:22 -07004797 }
4798
Joe Perches88982fe2012-12-17 16:02:00 -08004799# check for struct spinlock declarations
4800 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4801 WARN("USE_SPINLOCK_T",
4802 "struct spinlock should be spinlock_t\n" . $herecurr);
4803 }
4804
Joe Perchesa6962d72013-04-29 16:18:13 -07004805# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004806 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004807 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004808 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004809 if (WARN("PREFER_SEQ_PUTS",
4810 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4811 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004812 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004813 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004814 }
4815 }
4816
Andy Whitcroft554e1652012-01-10 15:09:57 -08004817# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004818 if ($^V && $^V ge 5.10.0 &&
4819 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004820 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004821
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004822 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004823 my $ms_val = $7;
4824 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004825
Andy Whitcroft554e1652012-01-10 15:09:57 -08004826 if ($ms_size =~ /^(0x|)0$/i) {
4827 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004828 "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 -08004829 } elsif ($ms_size =~ /^(0x|)1$/i) {
4830 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004831 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4832 }
4833 }
4834
Joe Perches98a9bba2014-01-23 15:54:52 -08004835# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4836 if ($^V && $^V ge 5.10.0 &&
4837 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4838 if (WARN("PREFER_ETHER_ADDR_COPY",
4839 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4840 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004841 $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 -08004842 }
4843 }
4844
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004845# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004846 if ($^V && $^V ge 5.10.0 &&
4847 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004848 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004849 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004850 my $call = $1;
4851 my $cast1 = deparenthesize($2);
4852 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004853 my $cast2 = deparenthesize($7);
4854 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004855 my $cast;
4856
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004857 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004858 $cast = "$cast1 or $cast2";
4859 } elsif ($cast1 ne "") {
4860 $cast = $cast1;
4861 } else {
4862 $cast = $cast2;
4863 }
4864 WARN("MINMAX",
4865 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004866 }
4867 }
4868
Joe Perches4a273192012-07-30 14:41:20 -07004869# check usleep_range arguments
4870 if ($^V && $^V ge 5.10.0 &&
4871 defined $stat &&
4872 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4873 my $min = $1;
4874 my $max = $7;
4875 if ($min eq $max) {
4876 WARN("USLEEP_RANGE",
4877 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4878 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4879 $min > $max) {
4880 WARN("USLEEP_RANGE",
4881 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4882 }
4883 }
4884
Joe Perches823b7942013-11-12 15:10:15 -08004885# check for naked sscanf
4886 if ($^V && $^V ge 5.10.0 &&
4887 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004888 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004889 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4890 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4891 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4892 my $lc = $stat =~ tr@\n@@;
4893 $lc = $lc + $linenr;
4894 my $stat_real = raw_line($linenr, 0);
4895 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4896 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4897 }
4898 WARN("NAKED_SSCANF",
4899 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4900 }
4901
Joe Perchesafc819a2014-06-04 16:12:08 -07004902# check for simple sscanf that should be kstrto<foo>
4903 if ($^V && $^V ge 5.10.0 &&
4904 defined $stat &&
4905 $line =~ /\bsscanf\b/) {
4906 my $lc = $stat =~ tr@\n@@;
4907 $lc = $lc + $linenr;
4908 my $stat_real = raw_line($linenr, 0);
4909 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4910 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4911 }
4912 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4913 my $format = $6;
4914 my $count = $format =~ tr@%@%@;
4915 if ($count == 1 &&
4916 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4917 WARN("SSCANF_TO_KSTRTO",
4918 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4919 }
4920 }
4921 }
4922
Joe Perches70dc8a42013-09-11 14:23:58 -07004923# check for new externs in .h files.
4924 if ($realfile =~ /\.h$/ &&
4925 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004926 if (CHK("AVOID_EXTERNS",
4927 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004928 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004929 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004930 }
4931 }
4932
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004933# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004934 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004935 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004936 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004937 my $function_name = $1;
4938 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004939
4940 my $s = $stat;
4941 if (defined $cond) {
4942 substr($s, 0, length($cond), '');
4943 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004944 if ($s =~ /^\s*;/ &&
4945 $function_name ne 'uninitialized_var')
4946 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004947 WARN("AVOID_EXTERNS",
4948 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004949 }
4950
4951 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004952 WARN("FUNCTION_ARGUMENTS",
4953 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004954 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004955
4956 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4957 $stat =~ /^.\s*extern\s+/)
4958 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004959 WARN("AVOID_EXTERNS",
4960 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004961 }
4962
4963# checks for new __setup's
4964 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4965 my $name = $1;
4966
4967 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004968 CHK("UNDOCUMENTED_SETUP",
4969 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004970 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004971 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004972
4973# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004974 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004975 WARN("UNNECESSARY_CASTS",
4976 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004977 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004978
Joe Perchesa640d252013-07-03 15:05:21 -07004979# alloc style
4980# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4981 if ($^V && $^V ge 5.10.0 &&
4982 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4983 CHK("ALLOC_SIZEOF_STRUCT",
4984 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4985 }
4986
Joe Perches60a55362014-06-04 16:12:07 -07004987# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4988 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004989 $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 -07004990 my $oldfunc = $3;
4991 my $a1 = $4;
4992 my $a2 = $10;
4993 my $newfunc = "kmalloc_array";
4994 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004995 my $r1 = $a1;
4996 my $r2 = $a2;
4997 if ($a1 =~ /^sizeof\s*\S/) {
4998 $r1 = $a2;
4999 $r2 = $a1;
5000 }
5001 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5002 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005003 if (WARN("ALLOC_WITH_MULTIPLY",
5004 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5005 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005006 $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 -07005007
5008 }
5009 }
5010 }
5011
Joe Perches972fdea2013-04-29 16:18:12 -07005012# check for krealloc arg reuse
5013 if ($^V && $^V ge 5.10.0 &&
5014 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5015 WARN("KREALLOC_ARG_REUSE",
5016 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5017 }
5018
Joe Perches5ce59ae2013-02-21 16:44:18 -08005019# check for alloc argument mismatch
5020 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5021 WARN("ALLOC_ARRAY_ARGS",
5022 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5023 }
5024
Joe Perchescaf2a542011-01-12 16:59:56 -08005025# check for multiple semicolons
5026 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005027 if (WARN("ONE_SEMICOLON",
5028 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5029 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005030 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005031 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005032 }
5033
Joe Perches0ab90192014-12-10 15:51:57 -08005034# check for #defines like: 1 << <digit> that could be BIT(digit)
5035 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5036 my $ull = "";
5037 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5038 if (CHK("BIT_MACRO",
5039 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5040 $fix) {
5041 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5042 }
5043 }
5044
Joe Perchese81f2392014-08-06 16:11:25 -07005045# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005046 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5047 my $has_break = 0;
5048 my $has_statement = 0;
5049 my $count = 0;
5050 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005051 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005052 $prevline--;
5053 my $rline = $rawlines[$prevline - 1];
5054 my $fline = $lines[$prevline - 1];
5055 last if ($fline =~ /^\@\@/);
5056 next if ($fline =~ /^\-/);
5057 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5058 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5059 next if ($fline =~ /^.[\s$;]*$/);
5060 $has_statement = 1;
5061 $count++;
5062 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5063 }
5064 if (!$has_break && $has_statement) {
5065 WARN("MISSING_BREAK",
5066 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5067 }
5068 }
5069
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005070# check for switch/default statements without a break;
5071 if ($^V && $^V ge 5.10.0 &&
5072 defined $stat &&
5073 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5074 my $ctx = '';
5075 my $herectx = $here . "\n";
5076 my $cnt = statement_rawlines($stat);
5077 for (my $n = 0; $n < $cnt; $n++) {
5078 $herectx .= raw_line($linenr, $n) . "\n";
5079 }
5080 WARN("DEFAULT_NO_BREAK",
5081 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005082 }
5083
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005084# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005085 if ($line =~ /\b__FUNCTION__\b/) {
5086 if (WARN("USE_FUNC",
5087 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5088 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005089 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005090 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005091 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005092
Joe Perches62ec8182015-02-13 14:38:18 -08005093# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5094 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5095 ERROR("DATE_TIME",
5096 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5097 }
5098
Joe Perches2c924882012-03-23 15:02:20 -07005099# check for use of yield()
5100 if ($line =~ /\byield\s*\(\s*\)/) {
5101 WARN("YIELD",
5102 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5103 }
5104
Joe Perches179f8f42013-07-03 15:05:30 -07005105# check for comparisons against true and false
5106 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5107 my $lead = $1;
5108 my $arg = $2;
5109 my $test = $3;
5110 my $otype = $4;
5111 my $trail = $5;
5112 my $op = "!";
5113
5114 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5115
5116 my $type = lc($otype);
5117 if ($type =~ /^(?:true|false)$/) {
5118 if (("$test" eq "==" && "$type" eq "true") ||
5119 ("$test" eq "!=" && "$type" eq "false")) {
5120 $op = "";
5121 }
5122
5123 CHK("BOOL_COMPARISON",
5124 "Using comparison to $otype is error prone\n" . $herecurr);
5125
5126## maybe suggesting a correct construct would better
5127## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5128
5129 }
5130 }
5131
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005132# check for semaphores initialized locked
5133 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005134 WARN("CONSIDER_COMPLETION",
5135 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005136 }
Joe Perches6712d852012-03-23 15:02:20 -07005137
Joe Perches67d0a072011-10-31 17:13:10 -07005138# recommend kstrto* over simple_strto* and strict_strto*
5139 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005140 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005141 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005142 }
Joe Perches6712d852012-03-23 15:02:20 -07005143
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005144# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005145 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005146 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005147 "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 -07005148 }
Joe Perches6712d852012-03-23 15:02:20 -07005149
Emese Revfy79404842010-03-05 13:43:53 -08005150# check for various ops structs, ensure they are const.
5151 my $struct_ops = qr{acpi_dock_ops|
5152 address_space_operations|
5153 backlight_ops|
5154 block_device_operations|
5155 dentry_operations|
5156 dev_pm_ops|
5157 dma_map_ops|
5158 extent_io_ops|
5159 file_lock_operations|
5160 file_operations|
5161 hv_ops|
5162 ide_dma_ops|
5163 intel_dvo_dev_ops|
5164 item_operations|
5165 iwl_ops|
5166 kgdb_arch|
5167 kgdb_io|
5168 kset_uevent_ops|
5169 lock_manager_operations|
5170 microcode_ops|
5171 mtrr_ops|
5172 neigh_ops|
5173 nlmsvc_binding|
5174 pci_raw_ops|
5175 pipe_buf_operations|
5176 platform_hibernation_ops|
5177 platform_suspend_ops|
5178 proto_ops|
5179 rpc_pipe_ops|
5180 seq_operations|
5181 snd_ac97_build_ops|
5182 soc_pcmcia_socket_ops|
5183 stacktrace_ops|
5184 sysfs_ops|
5185 tty_operations|
5186 usb_mon_operations|
5187 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005188 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08005189 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005190 WARN("CONST_STRUCT",
5191 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005192 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005193 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005194
5195# use of NR_CPUS is usually wrong
5196# ignore definitions of NR_CPUS and usage to define arrays as likely right
5197 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005198 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5199 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005200 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5201 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5202 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005203 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005204 WARN("NR_CPUS",
5205 "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 -07005206 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005207
Joe Perches52ea8502013-11-12 15:10:09 -08005208# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5209 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5210 ERROR("DEFINE_ARCH_HAS",
5211 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5212 }
5213
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005214# whine mightly about in_atomic
5215 if ($line =~ /\bin_atomic\s*\(/) {
5216 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005217 ERROR("IN_ATOMIC",
5218 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005219 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005220 WARN("IN_ATOMIC",
5221 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005222 }
5223 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005224
5225# check for lockdep_set_novalidate_class
5226 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5227 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5228 if ($realfile !~ m@^kernel/lockdep@ &&
5229 $realfile !~ m@^include/linux/lockdep@ &&
5230 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005231 ERROR("LOCKDEP",
5232 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005233 }
5234 }
Dave Jones88f88312011-01-12 16:59:59 -08005235
5236 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5237 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005238 WARN("EXPORTED_WORLD_WRITABLE",
5239 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005240 }
Joe Perches24358802014-04-03 14:49:13 -07005241
Joe Perches515a2352014-04-03 14:49:24 -07005242# Mode permission misuses where it seems decimal should be octal
5243# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5244 if ($^V && $^V ge 5.10.0 &&
5245 $line =~ /$mode_perms_search/) {
5246 foreach my $entry (@mode_permission_funcs) {
5247 my $func = $entry->[0];
5248 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005249
Joe Perches515a2352014-04-03 14:49:24 -07005250 my $skip_args = "";
5251 if ($arg_pos > 1) {
5252 $arg_pos--;
5253 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5254 }
5255 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5256 if ($line =~ /$test/) {
5257 my $val = $1;
5258 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005259
Joe Perches515a2352014-04-03 14:49:24 -07005260 if ($val !~ /^0$/ &&
5261 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5262 length($val) ne 4)) {
5263 ERROR("NON_OCTAL_PERMISSIONS",
5264 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005265 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5266 ERROR("EXPORTED_WORLD_WRITABLE",
5267 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005268 }
Joe Perches24358802014-04-03 14:49:13 -07005269 }
5270 }
5271 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005272 }
5273
5274 # If we have no input at all, then there is nothing to report on
5275 # so just keep quiet.
5276 if ($#rawlines == -1) {
5277 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005278 }
5279
Andy Whitcroft8905a672007-11-28 16:21:06 -08005280 # In mailback mode only produce a report in the negative, for
5281 # things that appear to be patches.
5282 if ($mailback && ($clean == 1 || !$is_patch)) {
5283 exit(0);
5284 }
5285
5286 # This is not a patch, and we are are in 'no-patch' mode so
5287 # just keep quiet.
5288 if (!$chk_patch && !$is_patch) {
5289 exit(0);
5290 }
5291
5292 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005293 ERROR("NOT_UNIFIED_DIFF",
5294 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005295 }
5296 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005297 ERROR("MISSING_SIGN_OFF",
5298 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005299 }
5300
Andy Whitcroft8905a672007-11-28 16:21:06 -08005301 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005302 if ($summary && !($clean == 1 && $quiet == 1)) {
5303 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005304 print "total: $cnt_error errors, $cnt_warn warnings, " .
5305 (($check)? "$cnt_chk checks, " : "") .
5306 "$cnt_lines lines checked\n";
5307 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005308 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005309
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005310 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005311
5312 if ($^V lt 5.10.0) {
5313 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5314 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5315 }
5316
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005317 # If there were whitespace errors which cleanpatch can fix
5318 # then suggest that.
5319 if ($rpt_cleaners) {
5320 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5321 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005322 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005323 }
5324 }
5325
Joe Perches91bfe482013-09-11 14:23:59 -07005326 hash_show_words(\%use_type, "Used");
5327 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005328
Joe Perchesd752fcc2014-08-06 16:11:05 -07005329 if ($clean == 0 && $fix &&
5330 ("@rawlines" ne "@fixed" ||
5331 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005332 my $newfile = $filename;
5333 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005334 my $linecount = 0;
5335 my $f;
5336
Joe Perchesd752fcc2014-08-06 16:11:05 -07005337 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5338
Joe Perches3705ce52013-07-03 15:05:31 -07005339 open($f, '>', $newfile)
5340 or die "$P: Can't open $newfile for write\n";
5341 foreach my $fixed_line (@fixed) {
5342 $linecount++;
5343 if ($file) {
5344 if ($linecount > 3) {
5345 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005346 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005347 }
5348 } else {
5349 print $f $fixed_line . "\n";
5350 }
5351 }
5352 close($f);
5353
5354 if (!$quiet) {
5355 print << "EOM";
5356Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5357
5358Do _NOT_ trust the results written to this file.
5359Do _NOT_ submit these changes without inspecting them for correctness.
5360
5361This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5362No warranties, expressed or implied...
5363
5364EOM
5365 }
5366 }
5367
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005368 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005369 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005370 }
5371 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005372 print << "EOM";
5373$vname has style problems, please review.
5374
5375If any of these errors are false positives, please report
5376them to the maintainer, see CHECKPATCH in MAINTAINERS.
5377EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005378 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005379
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005380 return $clean;
5381}