blob: 9c98100303774cd228ad83f3ff7bfcaeeb7af6fd [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070010
11my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070012$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
Joe Perches000d1cc12011-07-25 17:13:25 -070014my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070015
16use Getopt::Long qw(:config no_auto_abbrev);
17
18my $quiet = 0;
19my $tree = 1;
20my $chk_signoff = 1;
21my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070022my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070023my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080024my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $file = 0;
26my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080027my $summary = 1;
28my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080029my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070030my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070031my $fix = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080033my %debug;
Joe Perches34456862013-07-03 15:05:34 -070034my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070035my %use_type = ();
36my @use = ();
37my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070038my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070039my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070040my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080041my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070042my $ignore_perl_version = 0;
43my $minimum_perl_version = 5.10.0;
Hannes Eder77f5b102009-09-21 17:04:37 -070044
45sub help {
46 my ($exitcode) = @_;
47
48 print << "EOM";
49Usage: $P [OPTION]... [FILE]...
50Version: $V
51
52Options:
53 -q, --quiet quiet
54 --no-tree run without a kernel tree
55 --no-signoff do not check for 'Signed-off-by' line
56 --patch treat FILE as patchfile (default)
57 --emacs emacs compile window format
58 --terse one line per report
59 -f, --file treat FILE as regular source file
60 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070061 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070062 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080063 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070064 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070065 --root=PATH PATH to the kernel tree root
66 --no-summary suppress the per-file summary
67 --mailback only produce a report in case of warnings/errors
68 --summary-file include the filename in summary
69 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
70 'values', 'possible', 'type', and 'attr' (default
71 is all off)
72 --test-only=WORD report only warnings/errors containing WORD
73 literally
Joe Perches3705ce52013-07-03 15:05:31 -070074 --fix EXPERIMENTAL - may create horrible results
75 If correctable single-line errors exist, create
76 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
77 with potential errors corrected to the preferred
78 checkpatch style
Dave Hansend62a2012013-09-11 14:23:56 -070079 --ignore-perl-version override checking of perl version. expect
80 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070081 -h, --help, --version display this help and exit
82
83When FILE is - read standard input.
84EOM
85
86 exit($exitcode);
87}
88
Joe Perches000d1cc12011-07-25 17:13:25 -070089my $conf = which_conf($configuration_file);
90if (-f $conf) {
91 my @conf_args;
92 open(my $conffile, '<', "$conf")
93 or warn "$P: Can't find a readable $configuration_file file $!\n";
94
95 while (<$conffile>) {
96 my $line = $_;
97
98 $line =~ s/\s*\n?$//g;
99 $line =~ s/^\s*//g;
100 $line =~ s/\s+/ /g;
101
102 next if ($line =~ m/^\s*#/);
103 next if ($line =~ m/^\s*$/);
104
105 my @words = split(" ", $line);
106 foreach my $word (@words) {
107 last if ($word =~ m/^#/);
108 push (@conf_args, $word);
109 }
110 }
111 close($conffile);
112 unshift(@ARGV, @conf_args) if @conf_args;
113}
114
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700115GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700116 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700117 'tree!' => \$tree,
118 'signoff!' => \$chk_signoff,
119 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700120 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800121 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700122 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700123 'subjective!' => \$check,
124 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700125 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700126 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700127 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800128 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700129 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800130 'summary!' => \$summary,
131 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800132 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700133 'fix!' => \$fix,
Dave Hansend62a2012013-09-11 14:23:56 -0700134 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800135 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700136 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700137 'h|help' => \$help,
138 'version' => \$help
139) or help(1);
140
141help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700142
143my $exit = 0;
144
Dave Hansend62a2012013-09-11 14:23:56 -0700145if ($^V && $^V lt $minimum_perl_version) {
146 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
147 if (!$ignore_perl_version) {
148 exit(1);
149 }
150}
151
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700152if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700153 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700154 exit(1);
155}
156
Joe Perches91bfe482013-09-11 14:23:59 -0700157sub hash_save_array_words {
158 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700159
Joe Perches91bfe482013-09-11 14:23:59 -0700160 my @array = split(/,/, join(',', @$arrayRef));
161 foreach my $word (@array) {
162 $word =~ s/\s*\n?$//g;
163 $word =~ s/^\s*//g;
164 $word =~ s/\s+/ /g;
165 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700166
Joe Perches91bfe482013-09-11 14:23:59 -0700167 next if ($word =~ m/^\s*#/);
168 next if ($word =~ m/^\s*$/);
169
170 $hashRef->{$word}++;
171 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700172}
173
Joe Perches91bfe482013-09-11 14:23:59 -0700174sub hash_show_words {
175 my ($hashRef, $prefix) = @_;
176
Joe Perches58cb3cf2013-09-11 14:24:04 -0700177 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700178 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700179 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700180 print " $word";
181 }
182 print "\n\n";
183 }
184}
185
186hash_save_array_words(\%ignore_type, \@ignore);
187hash_save_array_words(\%use_type, \@use);
188
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800189my $dbg_values = 0;
190my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700191my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700192my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800193for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800194 ## no critic
195 eval "\${dbg_$key} = '$debug{$key}';";
196 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800197}
198
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700199my $rpt_cleaners = 0;
200
Andy Whitcroft8905a672007-11-28 16:21:06 -0800201if ($terse) {
202 $emacs = 1;
203 $quiet++;
204}
205
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700206if ($tree) {
207 if (defined $root) {
208 if (!top_of_kernel_tree($root)) {
209 die "$P: $root: --root does not point at a valid tree\n";
210 }
211 } else {
212 if (top_of_kernel_tree('.')) {
213 $root = '.';
214 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
215 top_of_kernel_tree($1)) {
216 $root = $1;
217 }
218 }
219
220 if (!defined $root) {
221 print "Must be run from the top-level dir. of a kernel tree\n";
222 exit(2);
223 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700224}
225
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700226my $emitted_corrupt = 0;
227
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700228our $Ident = qr{
229 [A-Za-z_][A-Za-z\d_]*
230 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
231 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700232our $Storage = qr{extern|static|asmlinkage};
233our $Sparse = qr{
234 __user|
235 __kernel|
236 __force|
237 __iomem|
238 __must_check|
239 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800240 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700241 __ref|
242 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700243 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800244our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
245our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
246our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
247our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
248our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700249
Wolfram Sang52131292010-03-05 13:43:51 -0800250# Notes to $Attribute:
251# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700252our $Attribute = qr{
253 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700254 __percpu|
255 __nocast|
256 __safe|
257 __bitwise__|
258 __packed__|
259 __packed2__|
260 __naked|
261 __maybe_unused|
262 __always_unused|
263 __noreturn|
264 __used|
265 __cold|
266 __noclone|
267 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700268 __read_mostly|
269 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700270 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700271 ____cacheline_aligned|
272 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800273 ____cacheline_internodealigned_in_smp|
274 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700275 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700276our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700277our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700278our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
279our $Lval = qr{$Ident(?:$Member)*};
280
Joe Perches95e2c602013-07-03 15:05:20 -0700281our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
282our $Binary = qr{(?i)0b[01]+$Int_type?};
283our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
284our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800285our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
286our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
287our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800288our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700289our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800290our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800291our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700292our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700293our $Operators = qr{
294 <=|>=|==|!=|
295 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700296 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700297 }x;
298
Andy Whitcroft8905a672007-11-28 16:21:06 -0800299our $NonptrType;
Joe Perches8716de32013-09-11 14:24:05 -0700300our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800301our $Type;
302our $Declare;
303
Joe Perches15662b32011-10-31 17:13:12 -0700304our $NON_ASCII_UTF8 = qr{
305 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700306 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
307 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
308 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
309 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
310 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
311 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
312}x;
313
Joe Perches15662b32011-10-31 17:13:12 -0700314our $UTF8 = qr{
315 [\x09\x0A\x0D\x20-\x7E] # ASCII
316 | $NON_ASCII_UTF8
317}x;
318
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700319our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700320 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700321 atomic_t
322)};
323
Joe Perches691e6692010-03-05 13:43:51 -0800324our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700325 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700326 (?:[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 -0700327 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700328 panic|
Joe Perches06668722013-11-12 15:10:07 -0800329 MODULE_[A-Z_]+|
330 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800331)};
332
Joe Perches20112472011-07-25 17:13:23 -0700333our $signature_tags = qr{(?xi:
334 Signed-off-by:|
335 Acked-by:|
336 Tested-by:|
337 Reviewed-by:|
338 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700339 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700340 To:|
341 Cc:
342)};
343
Andy Whitcroft8905a672007-11-28 16:21:06 -0800344our @typeList = (
345 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700346 qr{(?:unsigned\s+)?char},
347 qr{(?:unsigned\s+)?short},
348 qr{(?:unsigned\s+)?int},
349 qr{(?:unsigned\s+)?long},
350 qr{(?:unsigned\s+)?long\s+int},
351 qr{(?:unsigned\s+)?long\s+long},
352 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800353 qr{unsigned},
354 qr{float},
355 qr{double},
356 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800357 qr{struct\s+$Ident},
358 qr{union\s+$Ident},
359 qr{enum\s+$Ident},
360 qr{${Ident}_t},
361 qr{${Ident}_handler},
362 qr{${Ident}_handler_fn},
363);
Joe Perches8716de32013-09-11 14:24:05 -0700364our @typeListWithAttr = (
365 @typeList,
366 qr{struct\s+$InitAttribute\s+$Ident},
367 qr{union\s+$InitAttribute\s+$Ident},
368);
369
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700370our @modifierList = (
371 qr{fastcall},
372);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800373
Wolfram Sang7840a942010-08-09 17:20:57 -0700374our $allowed_asm_includes = qr{(?x:
375 irq|
376 memory
377)};
378# memory.h: ARM has a custom one
379
Andy Whitcroft8905a672007-11-28 16:21:06 -0800380sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700381 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
382 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700383 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700384 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800385 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700386 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800387 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800388 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700389 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700390 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800391 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700392 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800393 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700394 $NonptrTypeWithAttr = qr{
395 (?:$Modifier\s+|const\s+)*
396 (?:
397 (?:typeof|__typeof__)\s*\([^\)]*\)|
398 (?:$typeTypedefs\b)|
399 (?:${allWithAttr}\b)
400 )
401 (?:\s+$Modifier|\s+const)*
402 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700404 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700405 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700406 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800407 }x;
408 $Declare = qr{(?:$Storage\s+)?$Type};
409}
410build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700411
Joe Perches7d2367a2011-07-25 17:13:22 -0700412our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700413
414# Using $balanced_parens, $LvalOrFunc, or $FuncArg
415# requires at least perl version v5.10.0
416# Any use must be runtime checked with $^V
417
418our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
419our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800420our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700421
422sub deparenthesize {
423 my ($string) = @_;
424 return "" if (!defined($string));
425 $string =~ s@^\s*\(\s*@@g;
426 $string =~ s@\s*\)\s*$@@g;
427 $string =~ s@\s+@ @g;
428 return $string;
429}
430
Joe Perches34456862013-07-03 15:05:34 -0700431sub seed_camelcase_file {
432 my ($file) = @_;
433
434 return if (!(-f $file));
435
436 local $/;
437
438 open(my $include_file, '<', "$file")
439 or warn "$P: Can't read '$file' $!\n";
440 my $text = <$include_file>;
441 close($include_file);
442
443 my @lines = split('\n', $text);
444
445 foreach my $line (@lines) {
446 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
447 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
448 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800449 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
450 $camelcase{$1} = 1;
451 } 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 -0700452 $camelcase{$1} = 1;
453 }
454 }
455}
456
457my $camelcase_seeded = 0;
458sub seed_camelcase_includes {
459 return if ($camelcase_seeded);
460
461 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700462 my $camelcase_cache = "";
463 my @include_files = ();
464
465 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700466
Joe Perches34456862013-07-03 15:05:34 -0700467 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700468 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
469 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700470 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700471 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700472 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700473 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700474 @include_files = split('\n', $files);
475 foreach my $file (@include_files) {
476 my $date = POSIX::strftime("%Y%m%d%H%M",
477 localtime((stat $file)[9]));
478 $last_mod_date = $date if ($last_mod_date < $date);
479 }
480 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700481 }
Joe Perchesc707a812013-07-08 16:00:43 -0700482
483 if ($camelcase_cache ne "" && -f $camelcase_cache) {
484 open(my $camelcase_file, '<', "$camelcase_cache")
485 or warn "$P: Can't read '$camelcase_cache' $!\n";
486 while (<$camelcase_file>) {
487 chomp;
488 $camelcase{$_} = 1;
489 }
490 close($camelcase_file);
491
492 return;
493 }
494
495 if (-d ".git") {
496 $files = `git ls-files "include/*.h"`;
497 @include_files = split('\n', $files);
498 }
499
Joe Perches34456862013-07-03 15:05:34 -0700500 foreach my $file (@include_files) {
501 seed_camelcase_file($file);
502 }
Joe Perches351b2a12013-07-03 15:05:36 -0700503
Joe Perchesc707a812013-07-08 16:00:43 -0700504 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700505 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700506 open(my $camelcase_file, '>', "$camelcase_cache")
507 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700508 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
509 print $camelcase_file ("$_\n");
510 }
511 close($camelcase_file);
512 }
Joe Perches34456862013-07-03 15:05:34 -0700513}
514
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700515$chk_signoff = 0 if ($file);
516
Andy Whitcroft00df3442007-06-08 13:47:06 -0700517my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800518my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700519my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800520my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700521for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800522 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700523 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800524 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700525 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800526 } elsif ($filename eq '-') {
527 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700528 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800529 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700530 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700531 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800532 if ($filename eq '-') {
533 $vname = 'Your patch';
534 } else {
535 $vname = $filename;
536 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800537 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700538 chomp;
539 push(@rawlines, $_);
540 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800541 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800542 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700543 $exit = 1;
544 }
545 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800546 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700547 @fixed = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700548}
549
550exit($exit);
551
552sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700553 my ($root) = @_;
554
555 my @tree_check = (
556 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
557 "README", "Documentation", "arch", "include", "drivers",
558 "fs", "init", "ipc", "kernel", "lib", "scripts",
559 );
560
561 foreach my $check (@tree_check) {
562 if (! -e $root . '/' . $check) {
563 return 0;
564 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700565 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700566 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700567}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700568
Joe Perches20112472011-07-25 17:13:23 -0700569sub parse_email {
570 my ($formatted_email) = @_;
571
572 my $name = "";
573 my $address = "";
574 my $comment = "";
575
576 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
577 $name = $1;
578 $address = $2;
579 $comment = $3 if defined $3;
580 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
581 $address = $1;
582 $comment = $2 if defined $2;
583 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
584 $address = $1;
585 $comment = $2 if defined $2;
586 $formatted_email =~ s/$address.*$//;
587 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700588 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700589 $name =~ s/^\"|\"$//g;
590 # If there's a name left after stripping spaces and
591 # leading quotes, and the address doesn't have both
592 # leading and trailing angle brackets, the address
593 # is invalid. ie:
594 # "joe smith joe@smith.com" bad
595 # "joe smith <joe@smith.com" bad
596 if ($name ne "" && $address !~ /^<[^>]+>$/) {
597 $name = "";
598 $address = "";
599 $comment = "";
600 }
601 }
602
Joe Perches3705ce52013-07-03 15:05:31 -0700603 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700604 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700605 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700606 $address =~ s/^\<|\>$//g;
607
608 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
609 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
610 $name = "\"$name\"";
611 }
612
613 return ($name, $address, $comment);
614}
615
616sub format_email {
617 my ($name, $address) = @_;
618
619 my $formatted_email;
620
Joe Perches3705ce52013-07-03 15:05:31 -0700621 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700622 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700623 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700624
625 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
626 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
627 $name = "\"$name\"";
628 }
629
630 if ("$name" eq "") {
631 $formatted_email = "$address";
632 } else {
633 $formatted_email = "$name <$address>";
634 }
635
636 return $formatted_email;
637}
638
Joe Perches000d1cc12011-07-25 17:13:25 -0700639sub which_conf {
640 my ($conf) = @_;
641
642 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
643 if (-e "$path/$conf") {
644 return "$path/$conf";
645 }
646 }
647
648 return "";
649}
650
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700651sub expand_tabs {
652 my ($str) = @_;
653
654 my $res = '';
655 my $n = 0;
656 for my $c (split(//, $str)) {
657 if ($c eq "\t") {
658 $res .= ' ';
659 $n++;
660 for (; ($n % 8) != 0; $n++) {
661 $res .= ' ';
662 }
663 next;
664 }
665 $res .= $c;
666 $n++;
667 }
668
669 return $res;
670}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700671sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700672 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700673 return $res;
674}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700675
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700676sub line_stats {
677 my ($line) = @_;
678
679 # Drop the diff line leader and expand tabs
680 $line =~ s/^.//;
681 $line = expand_tabs($line);
682
683 # Pick the indent from the front of the line.
684 my ($white) = ($line =~ /^(\s*)/);
685
686 return (length($line), length($white));
687}
688
Andy Whitcroft773647a2008-03-28 14:15:58 -0700689my $sanitise_quote = '';
690
691sub sanitise_line_reset {
692 my ($in_comment) = @_;
693
694 if ($in_comment) {
695 $sanitise_quote = '*/';
696 } else {
697 $sanitise_quote = '';
698 }
699}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700700sub sanitise_line {
701 my ($line) = @_;
702
703 my $res = '';
704 my $l = '';
705
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800706 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700707 my $off = 0;
708 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700709
Andy Whitcroft773647a2008-03-28 14:15:58 -0700710 # Always copy over the diff marker.
711 $res = substr($line, 0, 1);
712
713 for ($off = 1; $off < length($line); $off++) {
714 $c = substr($line, $off, 1);
715
716 # Comments we are wacking completly including the begin
717 # and end, all to $;.
718 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
719 $sanitise_quote = '*/';
720
721 substr($res, $off, 2, "$;$;");
722 $off++;
723 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800724 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700725 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700726 $sanitise_quote = '';
727 substr($res, $off, 2, "$;$;");
728 $off++;
729 next;
730 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700731 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
732 $sanitise_quote = '//';
733
734 substr($res, $off, 2, $sanitise_quote);
735 $off++;
736 next;
737 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700738
739 # A \ in a string means ignore the next character.
740 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
741 $c eq "\\") {
742 substr($res, $off, 2, 'XX');
743 $off++;
744 next;
745 }
746 # Regular quotes.
747 if ($c eq "'" || $c eq '"') {
748 if ($sanitise_quote eq '') {
749 $sanitise_quote = $c;
750
751 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700752 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700753 } elsif ($sanitise_quote eq $c) {
754 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700755 }
756 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700757
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800758 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700759 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
760 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700761 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
762 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700763 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
764 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700765 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700766 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700767 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800768 }
769
Daniel Walker113f04a2009-09-21 17:04:35 -0700770 if ($sanitise_quote eq '//') {
771 $sanitise_quote = '';
772 }
773
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800774 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700775 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800776 my $clean = 'X' x length($1);
777 $res =~ s@\<.*\>@<$clean>@;
778
779 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700780 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800781 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700782 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800783 }
784
Andy Whitcroft00df3442007-06-08 13:47:06 -0700785 return $res;
786}
787
Joe Perchesa6962d72013-04-29 16:18:13 -0700788sub get_quoted_string {
789 my ($line, $rawline) = @_;
790
791 return "" if ($line !~ m/(\"[X]+\")/g);
792 return substr($rawline, $-[0], $+[0] - $-[0]);
793}
794
Andy Whitcroft8905a672007-11-28 16:21:06 -0800795sub ctx_statement_block {
796 my ($linenr, $remain, $off) = @_;
797 my $line = $linenr - 1;
798 my $blk = '';
799 my $soff = $off;
800 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700801 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800802
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800803 my $loff = 0;
804
Andy Whitcroft8905a672007-11-28 16:21:06 -0800805 my $type = '';
806 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800807 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800808 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800809 my $c;
810 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800811
812 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800813 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800814 @stack = (['', 0]) if ($#stack == -1);
815
Andy Whitcroft773647a2008-03-28 14:15:58 -0700816 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800817 # If we are about to drop off the end, pull in more
818 # context.
819 if ($off >= $len) {
820 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700821 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800822 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800823 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800824 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800825 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800826 $len = length($blk);
827 $line++;
828 last;
829 }
830 # Bail if there is no further context.
831 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800832 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800833 last;
834 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800835 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
836 $level++;
837 $type = '#';
838 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800839 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800840 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800841 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800842 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800843
Andy Whitcroft773647a2008-03-28 14:15:58 -0700844 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800845
846 # Handle nested #if/#else.
847 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
848 push(@stack, [ $type, $level ]);
849 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
850 ($type, $level) = @{$stack[$#stack - 1]};
851 } elsif ($remainder =~ /^#\s*endif\b/) {
852 ($type, $level) = @{pop(@stack)};
853 }
854
Andy Whitcroft8905a672007-11-28 16:21:06 -0800855 # Statement ends at the ';' or a close '}' at the
856 # outermost level.
857 if ($level == 0 && $c eq ';') {
858 last;
859 }
860
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800861 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700862 if ($level == 0 && $coff_set == 0 &&
863 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
864 $remainder =~ /^(else)(?:\s|{)/ &&
865 $remainder !~ /^else\s+if\b/) {
866 $coff = $off + length($1) - 1;
867 $coff_set = 1;
868 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
869 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800870 }
871
Andy Whitcroft8905a672007-11-28 16:21:06 -0800872 if (($type eq '' || $type eq '(') && $c eq '(') {
873 $level++;
874 $type = '(';
875 }
876 if ($type eq '(' && $c eq ')') {
877 $level--;
878 $type = ($level != 0)? '(' : '';
879
880 if ($level == 0 && $coff < $soff) {
881 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700882 $coff_set = 1;
883 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800884 }
885 }
886 if (($type eq '' || $type eq '{') && $c eq '{') {
887 $level++;
888 $type = '{';
889 }
890 if ($type eq '{' && $c eq '}') {
891 $level--;
892 $type = ($level != 0)? '{' : '';
893
894 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700895 if (substr($blk, $off + 1, 1) eq ';') {
896 $off++;
897 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800898 last;
899 }
900 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800901 # Preprocessor commands end at the newline unless escaped.
902 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
903 $level--;
904 $type = '';
905 $off++;
906 last;
907 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800908 $off++;
909 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700910 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800911 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700912 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800913 $line++;
914 $remain--;
915 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800916
917 my $statement = substr($blk, $soff, $off - $soff + 1);
918 my $condition = substr($blk, $soff, $coff - $soff + 1);
919
920 #warn "STATEMENT<$statement>\n";
921 #warn "CONDITION<$condition>\n";
922
Andy Whitcroft773647a2008-03-28 14:15:58 -0700923 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800924
925 return ($statement, $condition,
926 $line, $remain + 1, $off - $loff + 1, $level);
927}
928
Andy Whitcroftcf655042008-03-04 14:28:20 -0800929sub statement_lines {
930 my ($stmt) = @_;
931
932 # Strip the diff line prefixes and rip blank lines at start and end.
933 $stmt =~ s/(^|\n)./$1/g;
934 $stmt =~ s/^\s*//;
935 $stmt =~ s/\s*$//;
936
937 my @stmt_lines = ($stmt =~ /\n/g);
938
939 return $#stmt_lines + 2;
940}
941
942sub statement_rawlines {
943 my ($stmt) = @_;
944
945 my @stmt_lines = ($stmt =~ /\n/g);
946
947 return $#stmt_lines + 2;
948}
949
950sub statement_block_size {
951 my ($stmt) = @_;
952
953 $stmt =~ s/(^|\n)./$1/g;
954 $stmt =~ s/^\s*{//;
955 $stmt =~ s/}\s*$//;
956 $stmt =~ s/^\s*//;
957 $stmt =~ s/\s*$//;
958
959 my @stmt_lines = ($stmt =~ /\n/g);
960 my @stmt_statements = ($stmt =~ /;/g);
961
962 my $stmt_lines = $#stmt_lines + 2;
963 my $stmt_statements = $#stmt_statements + 1;
964
965 if ($stmt_lines > $stmt_statements) {
966 return $stmt_lines;
967 } else {
968 return $stmt_statements;
969 }
970}
971
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800972sub ctx_statement_full {
973 my ($linenr, $remain, $off) = @_;
974 my ($statement, $condition, $level);
975
976 my (@chunks);
977
Andy Whitcroftcf655042008-03-04 14:28:20 -0800978 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800979 ($statement, $condition, $linenr, $remain, $off, $level) =
980 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700981 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800982 push(@chunks, [ $condition, $statement ]);
983 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
984 return ($level, $linenr, @chunks);
985 }
986
987 # Pull in the following conditional/block pairs and see if they
988 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800989 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800990 ($statement, $condition, $linenr, $remain, $off, $level) =
991 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800992 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700993 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800994 #print "C: push\n";
995 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800996 }
997
998 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800999}
1000
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001001sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001002 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001003 my $line;
1004 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001005 my $blk = '';
1006 my @o;
1007 my @c;
1008 my @res = ();
1009
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001010 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001011 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001012 for ($line = $start; $remain > 0; $line++) {
1013 next if ($rawlines[$line] =~ /^-/);
1014 $remain--;
1015
1016 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001017
1018 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001019 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001020 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001021 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001022 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001023 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001024 $level = pop(@stack);
1025 }
1026
Andy Whitcroft01464f32010-10-26 14:23:19 -07001027 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001028 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1029 if ($off > 0) {
1030 $off--;
1031 next;
1032 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001033
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001034 if ($c eq $close && $level > 0) {
1035 $level--;
1036 last if ($level == 0);
1037 } elsif ($c eq $open) {
1038 $level++;
1039 }
1040 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001041
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001042 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001043 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001044 }
1045
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001046 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001047 }
1048
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001049 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001050}
1051sub ctx_block_outer {
1052 my ($linenr, $remain) = @_;
1053
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001054 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1055 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001056}
1057sub ctx_block {
1058 my ($linenr, $remain) = @_;
1059
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001060 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1061 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001062}
1063sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001064 my ($linenr, $remain, $off) = @_;
1065
1066 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1067 return @r;
1068}
1069sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001070 my ($linenr, $remain) = @_;
1071
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001072 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001073}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001074sub ctx_statement_level {
1075 my ($linenr, $remain, $off) = @_;
1076
1077 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1078}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001079
1080sub ctx_locate_comment {
1081 my ($first_line, $end_line) = @_;
1082
1083 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001084 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001085 return $current_comment if (defined $current_comment);
1086
1087 # Look through the context and try and figure out if there is a
1088 # comment.
1089 my $in_comment = 0;
1090 $current_comment = '';
1091 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001092 my $line = $rawlines[$linenr - 1];
1093 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001094 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1095 $in_comment = 1;
1096 }
1097 if ($line =~ m@/\*@) {
1098 $in_comment = 1;
1099 }
1100 if (!$in_comment && $current_comment ne '') {
1101 $current_comment = '';
1102 }
1103 $current_comment .= $line . "\n" if ($in_comment);
1104 if ($line =~ m@\*/@) {
1105 $in_comment = 0;
1106 }
1107 }
1108
1109 chomp($current_comment);
1110 return($current_comment);
1111}
1112sub ctx_has_comment {
1113 my ($first_line, $end_line) = @_;
1114 my $cmt = ctx_locate_comment($first_line, $end_line);
1115
Andy Whitcroft00df3442007-06-08 13:47:06 -07001116 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001117 ##print "CMMT: $cmt\n";
1118
1119 return ($cmt ne '');
1120}
1121
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001122sub raw_line {
1123 my ($linenr, $cnt) = @_;
1124
1125 my $offset = $linenr - 1;
1126 $cnt++;
1127
1128 my $line;
1129 while ($cnt) {
1130 $line = $rawlines[$offset++];
1131 next if (defined($line) && $line =~ /^-/);
1132 $cnt--;
1133 }
1134
1135 return $line;
1136}
1137
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001138sub cat_vet {
1139 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001140 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001141
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001142 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001143 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1144 $res .= $1;
1145 if ($2 ne '') {
1146 $coded = sprintf("^%c", unpack('C', $2) + 64);
1147 $res .= $coded;
1148 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001149 }
1150 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001151
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001152 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001153}
1154
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001155my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001156my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001157my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001158my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001159
1160sub annotate_reset {
1161 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001162 $av_pending = '_';
1163 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001164 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001165}
1166
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001167sub annotate_values {
1168 my ($stream, $type) = @_;
1169
1170 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001171 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001172 my $cur = $stream;
1173
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001174 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001175
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001176 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001177 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001178 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001179 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001180 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001181 print "WS($1)\n" if ($dbg_values > 1);
1182 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001183 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001184 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001185 }
1186
Florian Micklerc023e4732011-01-12 16:59:58 -08001187 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001188 print "CAST($1)\n" if ($dbg_values > 1);
1189 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001190 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001191
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001192 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001193 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001194 $type = 'T';
1195
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001196 } elsif ($cur =~ /^($Modifier)\s*/) {
1197 print "MODIFIER($1)\n" if ($dbg_values > 1);
1198 $type = 'T';
1199
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001200 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001201 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001202 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001203 push(@av_paren_type, $type);
1204 if ($2 ne '') {
1205 $av_pending = 'N';
1206 }
1207 $type = 'E';
1208
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001209 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001210 print "UNDEF($1)\n" if ($dbg_values > 1);
1211 $av_preprocessor = 1;
1212 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001213
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001214 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001215 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001216 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001217
1218 push(@av_paren_type, $type);
1219 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001220 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001221
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001222 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001223 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1224 $av_preprocessor = 1;
1225
1226 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1227
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001228 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001229
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001230 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001231 print "PRE_END($1)\n" if ($dbg_values > 1);
1232
1233 $av_preprocessor = 1;
1234
1235 # Assume all arms of the conditional end as this
1236 # one does, and continue as if the #endif was not here.
1237 pop(@av_paren_type);
1238 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001239 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001240
1241 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001242 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001243
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001244 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1245 print "ATTR($1)\n" if ($dbg_values > 1);
1246 $av_pending = $type;
1247 $type = 'N';
1248
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001249 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001250 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001251 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001252 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001253 }
1254 $type = 'N';
1255
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001256 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001257 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001258 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001259 $type = 'N';
1260
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001261 } elsif ($cur =~/^(case)/o) {
1262 print "CASE($1)\n" if ($dbg_values > 1);
1263 $av_pend_colon = 'C';
1264 $type = 'N';
1265
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001266 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001267 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001268 $type = 'N';
1269
1270 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001271 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001272 push(@av_paren_type, $av_pending);
1273 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001274 $type = 'N';
1275
1276 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001277 my $new_type = pop(@av_paren_type);
1278 if ($new_type ne '_') {
1279 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001280 print "PAREN('$1') -> $type\n"
1281 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001282 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001283 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001284 }
1285
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001286 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001287 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001288 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001289 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001290
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001291 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1292 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001293 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001294 } elsif ($type eq 'E') {
1295 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001296 }
1297 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1298 $type = 'V';
1299
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001300 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001301 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001302 $type = 'V';
1303
1304 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001305 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001306 $type = 'N';
1307
Andy Whitcroftcf655042008-03-04 14:28:20 -08001308 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001309 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001310 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001311 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001312
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001313 } elsif ($cur =~/^(,)/) {
1314 print "COMMA($1)\n" if ($dbg_values > 1);
1315 $type = 'C';
1316
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001317 } elsif ($cur =~ /^(\?)/o) {
1318 print "QUESTION($1)\n" if ($dbg_values > 1);
1319 $type = 'N';
1320
1321 } elsif ($cur =~ /^(:)/o) {
1322 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1323
1324 substr($var, length($res), 1, $av_pend_colon);
1325 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1326 $type = 'E';
1327 } else {
1328 $type = 'N';
1329 }
1330 $av_pend_colon = 'O';
1331
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001332 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001333 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001334 $type = 'N';
1335
Andy Whitcroft0d413862008-10-15 22:02:16 -07001336 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001337 my $variant;
1338
1339 print "OPV($1)\n" if ($dbg_values > 1);
1340 if ($type eq 'V') {
1341 $variant = 'B';
1342 } else {
1343 $variant = 'U';
1344 }
1345
1346 substr($var, length($res), 1, $variant);
1347 $type = 'N';
1348
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001349 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001350 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001351 if ($1 ne '++' && $1 ne '--') {
1352 $type = 'N';
1353 }
1354
1355 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001356 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001357 }
1358 if (defined $1) {
1359 $cur = substr($cur, length($1));
1360 $res .= $type x length($1);
1361 }
1362 }
1363
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001364 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001365}
1366
Andy Whitcroft8905a672007-11-28 16:21:06 -08001367sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001368 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001369 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001370 ^(?:
1371 $Modifier|
1372 $Storage|
1373 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001374 DEFINE_\S+
1375 )$|
1376 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001377 goto|
1378 return|
1379 case|
1380 else|
1381 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001382 do|
1383 \#|
1384 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001385 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001386 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001387 )}x;
1388 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1389 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001390 # Check for modifiers.
1391 $possible =~ s/\s*$Storage\s*//g;
1392 $possible =~ s/\s*$Sparse\s*//g;
1393 if ($possible =~ /^\s*$/) {
1394
1395 } elsif ($possible =~ /\s/) {
1396 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001397 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001398 if ($modifier !~ $notPermitted) {
1399 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1400 push(@modifierList, $modifier);
1401 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001402 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001403
1404 } else {
1405 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1406 push(@typeList, $possible);
1407 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001408 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001409 } else {
1410 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001411 }
1412}
1413
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001414my $prefix = '';
1415
Joe Perches000d1cc12011-07-25 17:13:25 -07001416sub show_type {
Joe Perches91bfe482013-09-11 14:23:59 -07001417 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1418
1419 return !defined $ignore_type{$_[0]};
Joe Perches000d1cc12011-07-25 17:13:25 -07001420}
1421
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001422sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001423 if (!show_type($_[1]) ||
1424 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001425 return 0;
1426 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001427 my $line;
1428 if ($show_types) {
1429 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1430 } else {
1431 $line = "$prefix$_[0]: $_[2]\n";
1432 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 $line = (split('\n', $line))[0] . "\n" if ($terse);
1434
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001435 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001436
1437 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001438}
1439sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001441}
Joe Perches000d1cc12011-07-25 17:13:25 -07001442
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001443sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001444 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001445 our $clean = 0;
1446 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001447 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001448 }
Joe Perches3705ce52013-07-03 15:05:31 -07001449 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001450}
1451sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001452 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001453 our $clean = 0;
1454 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001455 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001456 }
Joe Perches3705ce52013-07-03 15:05:31 -07001457 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001458}
1459sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001460 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001461 our $clean = 0;
1462 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001463 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001464 }
Joe Perches3705ce52013-07-03 15:05:31 -07001465 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001466}
1467
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001468sub check_absolute_file {
1469 my ($absolute, $herecurr) = @_;
1470 my $file = $absolute;
1471
1472 ##print "absolute<$absolute>\n";
1473
1474 # See if any suffix of this path is a path within the tree.
1475 while ($file =~ s@^[^/]*/@@) {
1476 if (-f "$root/$file") {
1477 ##print "file<$file>\n";
1478 last;
1479 }
1480 }
1481 if (! -f _) {
1482 return 0;
1483 }
1484
1485 # It is, so see if the prefix is acceptable.
1486 my $prefix = $absolute;
1487 substr($prefix, -length($file)) = '';
1488
1489 ##print "prefix<$prefix>\n";
1490 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001491 WARN("USE_RELATIVE_PATH",
1492 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001493 }
1494}
1495
Joe Perches3705ce52013-07-03 15:05:31 -07001496sub trim {
1497 my ($string) = @_;
1498
Joe Perchesb34c6482013-09-11 14:24:01 -07001499 $string =~ s/^\s+|\s+$//g;
1500
1501 return $string;
1502}
1503
1504sub ltrim {
1505 my ($string) = @_;
1506
1507 $string =~ s/^\s+//;
1508
1509 return $string;
1510}
1511
1512sub rtrim {
1513 my ($string) = @_;
1514
1515 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001516
1517 return $string;
1518}
1519
Joe Perches52ea8502013-11-12 15:10:09 -08001520sub string_find_replace {
1521 my ($string, $find, $replace) = @_;
1522
1523 $string =~ s/$find/$replace/g;
1524
1525 return $string;
1526}
1527
Joe Perches3705ce52013-07-03 15:05:31 -07001528sub tabify {
1529 my ($leading) = @_;
1530
1531 my $source_indent = 8;
1532 my $max_spaces_before_tab = $source_indent - 1;
1533 my $spaces_to_tab = " " x $source_indent;
1534
1535 #convert leading spaces to tabs
1536 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1537 #Remove spaces before a tab
1538 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1539
1540 return "$leading";
1541}
1542
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001543sub pos_last_openparen {
1544 my ($line) = @_;
1545
1546 my $pos = 0;
1547
1548 my $opens = $line =~ tr/\(/\(/;
1549 my $closes = $line =~ tr/\)/\)/;
1550
1551 my $last_openparen = 0;
1552
1553 if (($opens == 0) || ($closes >= $opens)) {
1554 return -1;
1555 }
1556
1557 my $len = length($line);
1558
1559 for ($pos = 0; $pos < $len; $pos++) {
1560 my $string = substr($line, $pos);
1561 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1562 $pos += length($1) - 1;
1563 } elsif (substr($line, $pos, 1) eq '(') {
1564 $last_openparen = $pos;
1565 } elsif (index($string, '(') == -1) {
1566 last;
1567 }
1568 }
1569
1570 return $last_openparen + 1;
1571}
1572
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001573sub process {
1574 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001575
1576 my $linenr=0;
1577 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001578 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001579 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001580 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001581
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001582 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001583 my $indent;
1584 my $previndent=0;
1585 my $stashindent=0;
1586
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001587 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001588 my $signoff = 0;
1589 my $is_patch = 0;
1590
Joe Perches15662b32011-10-31 17:13:12 -07001591 my $in_header_lines = 1;
1592 my $in_commit_log = 0; #Scanning lines before patch
1593
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001594 my $non_utf8_charset = 0;
1595
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001596 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001597 our $cnt_lines = 0;
1598 our $cnt_error = 0;
1599 our $cnt_warn = 0;
1600 our $cnt_chk = 0;
1601
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001602 # Trace the real file/line as we go.
1603 my $realfile = '';
1604 my $realline = 0;
1605 my $realcnt = 0;
1606 my $here = '';
1607 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001608 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001609 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001610 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001611
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001612 my $prev_values = 'E';
1613
1614 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001615 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001616 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001617 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001618 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001619
Joe Perches7e51f192013-09-11 14:23:57 -07001620 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001621
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001622 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001623 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001624 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001625 my @setup_docs = ();
1626 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001627
Joe Perchesd8b07712013-11-12 15:10:06 -08001628 my $camelcase_file_seeded = 0;
1629
Andy Whitcroft773647a2008-03-28 14:15:58 -07001630 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001631 my $line;
1632 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001633 $linenr++;
1634 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001635
Joe Perches3705ce52013-07-03 15:05:31 -07001636 push(@fixed, $rawline) if ($fix);
1637
Andy Whitcroft773647a2008-03-28 14:15:58 -07001638 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001639 $setup_docs = 0;
1640 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1641 $setup_docs = 1;
1642 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001643 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001644 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001645 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1646 $realline=$1-1;
1647 if (defined $2) {
1648 $realcnt=$3+1;
1649 } else {
1650 $realcnt=1+1;
1651 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001652 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001653
1654 # Guestimate if this is a continuing comment. Run
1655 # the context looking for a comment "edge". If this
1656 # edge is a close comment then we must be in a comment
1657 # at context start.
1658 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001659 my $cnt = $realcnt;
1660 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1661 next if (defined $rawlines[$ln - 1] &&
1662 $rawlines[$ln - 1] =~ /^-/);
1663 $cnt--;
1664 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001665 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001666 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1667 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1668 ($edge) = $1;
1669 last;
1670 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001671 }
1672 if (defined $edge && $edge eq '*/') {
1673 $in_comment = 1;
1674 }
1675
1676 # Guestimate if this is a continuing comment. If this
1677 # is the start of a diff block and this line starts
1678 # ' *' then it is very likely a comment.
1679 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001680 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001681 {
1682 $in_comment = 1;
1683 }
1684
1685 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1686 sanitise_line_reset($in_comment);
1687
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001688 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001689 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001690 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001691 $line = sanitise_line($rawline);
1692 }
1693 push(@lines, $line);
1694
1695 if ($realcnt > 1) {
1696 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1697 } else {
1698 $realcnt = 0;
1699 }
1700
1701 #print "==>$rawline\n";
1702 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001703
1704 if ($setup_docs && $line =~ /^\+/) {
1705 push(@setup_docs, $line);
1706 }
1707 }
1708
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001709 $prefix = '';
1710
Andy Whitcroft773647a2008-03-28 14:15:58 -07001711 $realcnt = 0;
1712 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001713 foreach my $line (@lines) {
1714 $linenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001715 my $sline = $line; #copy of $line
1716 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001717
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001718 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001719
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001720#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001721 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001722 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001723 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001724 $realline=$1-1;
1725 if (defined $2) {
1726 $realcnt=$3+1;
1727 } else {
1728 $realcnt=1+1;
1729 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001730 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001731 $prev_values = 'E';
1732
Andy Whitcroft773647a2008-03-28 14:15:58 -07001733 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001734 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001735 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001736 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001737 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001738
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001739# track the line number as we move through the hunk, note that
1740# new versions of GNU diff omit the leading space on completely
1741# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001742 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001743 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001744 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001745
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001746 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001747 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001748
1749 # Track the previous line.
1750 ($prevline, $stashline) = ($stashline, $line);
1751 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001752 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1753
Andy Whitcroft773647a2008-03-28 14:15:58 -07001754 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001755
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001756 } elsif ($realcnt == 1) {
1757 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001758 }
1759
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001760 my $hunk_line = ($realcnt != 0);
1761
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001762#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001763 $prefix = "$filename:$realline: " if ($emacs && $file);
1764 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1765
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001766 $here = "#$linenr: " if (!$file);
1767 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001768
1769 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001770 if ($line =~ /^diff --git.*?(\S+)$/) {
1771 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001772 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001773 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001774 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001775 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001776 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001777 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001778
1779 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001780 if (!$file && $tree && $p1_prefix ne '' &&
1781 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001782 WARN("PATCH_PREFIX",
1783 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001784 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001785
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001786 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001787 ERROR("MODIFIED_INCLUDE_ASM",
1788 "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 -07001789 }
1790 next;
1791 }
1792
Randy Dunlap389834b2007-06-08 13:47:03 -07001793 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001794
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001795 my $hereline = "$here\n$rawline\n";
1796 my $herecurr = "$here\n$rawline\n";
1797 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001798
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001799 $cnt_lines++ if ($realcnt != 0);
1800
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001801# Check for incorrect file permissions
1802 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1803 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001804 if ($realfile !~ m@scripts/@ &&
1805 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001806 ERROR("EXECUTE_PERMISSIONS",
1807 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001808 }
1809 }
1810
Joe Perches20112472011-07-25 17:13:23 -07001811# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001812 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001813 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001814 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001815 }
1816
1817# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001818 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001819 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001820 my $space_before = $1;
1821 my $sign_off = $2;
1822 my $space_after = $3;
1823 my $email = $4;
1824 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1825
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001826 if ($sign_off !~ /$signature_tags/) {
1827 WARN("BAD_SIGN_OFF",
1828 "Non-standard signature: $sign_off\n" . $herecurr);
1829 }
Joe Perches20112472011-07-25 17:13:23 -07001830 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001831 if (WARN("BAD_SIGN_OFF",
1832 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1833 $fix) {
1834 $fixed[$linenr - 1] =
1835 "$ucfirst_sign_off $email";
1836 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001837 }
Joe Perches20112472011-07-25 17:13:23 -07001838 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001839 if (WARN("BAD_SIGN_OFF",
1840 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1841 $fix) {
1842 $fixed[$linenr - 1] =
1843 "$ucfirst_sign_off $email";
1844 }
1845
Joe Perches20112472011-07-25 17:13:23 -07001846 }
1847 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001848 if (WARN("BAD_SIGN_OFF",
1849 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1850 $fix) {
1851 $fixed[$linenr - 1] =
1852 "$ucfirst_sign_off $email";
1853 }
Joe Perches20112472011-07-25 17:13:23 -07001854 }
1855
1856 my ($email_name, $email_address, $comment) = parse_email($email);
1857 my $suggested_email = format_email(($email_name, $email_address));
1858 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001859 ERROR("BAD_SIGN_OFF",
1860 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001861 } else {
1862 my $dequoted = $suggested_email;
1863 $dequoted =~ s/^"//;
1864 $dequoted =~ s/" </ </;
1865 # Don't force email to have quotes
1866 # Allow just an angle bracketed address
1867 if ("$dequoted$comment" ne $email &&
1868 "<$email_address>$comment" ne $email &&
1869 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001870 WARN("BAD_SIGN_OFF",
1871 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001872 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001873 }
Joe Perches7e51f192013-09-11 14:23:57 -07001874
1875# Check for duplicate signatures
1876 my $sig_nospace = $line;
1877 $sig_nospace =~ s/\s//g;
1878 $sig_nospace = lc($sig_nospace);
1879 if (defined $signatures{$sig_nospace}) {
1880 WARN("BAD_SIGN_OFF",
1881 "Duplicate signature\n" . $herecurr);
1882 } else {
1883 $signatures{$sig_nospace} = 1;
1884 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001885 }
1886
Andy Whitcroft00df3442007-06-08 13:47:06 -07001887# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001888 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001889 ERROR("CORRUPTED_PATCH",
1890 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001891 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001892 }
1893
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001894# Check for absolute kernel paths.
1895 if ($tree) {
1896 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1897 my $file = $1;
1898
1899 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1900 check_absolute_file($1, $herecurr)) {
1901 #
1902 } else {
1903 check_absolute_file($file, $herecurr);
1904 }
1905 }
1906 }
1907
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001908# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1909 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001910 $rawline !~ m/^$UTF8*$/) {
1911 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1912
1913 my $blank = copy_spacing($rawline);
1914 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1915 my $hereptr = "$hereline$ptr\n";
1916
Joe Perches34d99212011-07-25 17:13:26 -07001917 CHK("INVALID_UTF8",
1918 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001919 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001920
Joe Perches15662b32011-10-31 17:13:12 -07001921# Check if it's the start of a commit log
1922# (not a header line and we haven't seen the patch filename)
1923 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001924 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001925 $in_header_lines = 0;
1926 $in_commit_log = 1;
1927 }
1928
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001929# Check if there is UTF-8 in a commit log when a mail header has explicitly
1930# declined it, i.e defined some charset where it is missing.
1931 if ($in_header_lines &&
1932 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1933 $1 !~ /utf-8/i) {
1934 $non_utf8_charset = 1;
1935 }
1936
1937 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001938 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001939 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001940 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1941 }
1942
Andy Whitcroft306708542008-10-15 22:02:28 -07001943# ignore non-hunk lines and lines being removed
1944 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001945
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001946#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001947 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001948 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07001949 if (ERROR("DOS_LINE_ENDINGS",
1950 "DOS line endings\n" . $herevet) &&
1951 $fix) {
1952 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1953 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001954 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1955 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001956 if (ERROR("TRAILING_WHITESPACE",
1957 "trailing whitespace\n" . $herevet) &&
1958 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07001959 $fixed[$linenr - 1] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001960 }
1961
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001962 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001963 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001964
Josh Triplett4783f892013-11-12 15:10:12 -08001965# Check for FSF mailing addresses.
1966 if ($rawline =~ /You should have received a copy/ ||
1967 $rawline =~ /write to the Free Software/ ||
1968 $rawline =~ /59 Temple Place/ ||
1969 $rawline =~ /51 Franklin Street/) {
1970 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1971 my $msg_type = \&ERROR;
1972 $msg_type = \&CHK if ($file);
1973 &{$msg_type}("FSF_MAILING_ADDRESS",
1974 "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)
1975 }
1976
Andi Kleen33549572010-05-24 14:33:29 -07001977# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001978# Only applies when adding the entry originally, after that we do not have
1979# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001980 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001981 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001982 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001983 my $cnt = $realcnt;
1984 my $ln = $linenr + 1;
1985 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001986 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001987 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001988 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001989 $f = $lines[$ln - 1];
1990 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1991 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001992
1993 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001994
1995 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1996 $is_start = 1;
1997 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1998 $length = -1;
1999 }
2000
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002001 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002002 $f =~ s/#.*//;
2003 $f =~ s/^\s+//;
2004 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002005 if ($f =~ /^\s*config\s/) {
2006 $is_end = 1;
2007 last;
2008 }
Andi Kleen33549572010-05-24 14:33:29 -07002009 $length++;
2010 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002011 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08002012 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2013 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002014 }
2015
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002016# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2017 if ($realfile =~ /Kconfig/ &&
2018 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2019 WARN("CONFIG_EXPERIMENTAL",
2020 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2021 }
2022
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002023 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2024 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2025 my $flag = $1;
2026 my $replacement = {
2027 'EXTRA_AFLAGS' => 'asflags-y',
2028 'EXTRA_CFLAGS' => 'ccflags-y',
2029 'EXTRA_CPPFLAGS' => 'cppflags-y',
2030 'EXTRA_LDFLAGS' => 'ldflags-y',
2031 };
2032
2033 WARN("DEPRECATED_VARIABLE",
2034 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2035 }
2036
Andy Whitcroft5368df22008-10-15 22:02:27 -07002037# check we are in a valid source file if not then ignore this hunk
2038 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2039
Joe Perches6cd7f382012-12-17 16:01:54 -08002040#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002041 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002042 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002043 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002044 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002045 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002046 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002047 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002048 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002049 }
2050
Josh Triplettca56dc02012-03-23 15:02:21 -07002051# Check for user-visible strings broken across lines, which breaks the ability
2052# to grep for the string. Limited to strings used as parameters (those
2053# following an open parenthesis), which almost completely eliminates false
2054# positives, as well as warning only once per parameter rather than once per
2055# line of the string. Make an exception when the previous string ends in a
2056# newline (multiple lines in one string constant) or \n\t (common in inline
2057# assembly to indent the instruction on the following line).
2058 if ($line =~ /^\+\s*"/ &&
2059 $prevline =~ /"\s*$/ &&
2060 $prevline =~ /\(/ &&
2061 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
2062 WARN("SPLIT_STRING",
2063 "quoted string split across lines\n" . $hereprev);
2064 }
2065
Joe Perches5e79d962010-03-05 13:43:55 -08002066# check for spaces before a quoted newline
2067 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002068 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2069 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2070 $fix) {
2071 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2072 }
2073
Joe Perches5e79d962010-03-05 13:43:55 -08002074 }
2075
Andy Whitcroft8905a672007-11-28 16:21:06 -08002076# check for adding lines without a newline.
2077 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002078 WARN("MISSING_EOF_NEWLINE",
2079 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002080 }
2081
Mike Frysinger42e41c52009-09-21 17:04:40 -07002082# Blackfin: use hi/lo macros
2083 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2084 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2085 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002086 ERROR("LO_MACRO",
2087 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002088 }
2089 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2090 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002091 ERROR("HI_MACRO",
2092 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002093 }
2094 }
2095
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002096# check we are in a valid source file C or perl if not then ignore this hunk
2097 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002098
2099# at the beginning of a line any tabs must come first and anything
2100# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002101 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2102 $rawline =~ /^\+\s* \s*/) {
2103 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002104 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002105 if (ERROR("CODE_INDENT",
2106 "code indent should use tabs where possible\n" . $herevet) &&
2107 $fix) {
2108 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2109 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002110 }
2111
Alberto Panizzo08e44362010-03-05 13:43:54 -08002112# check for space before tabs.
2113 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2114 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002115 if (WARN("SPACE_BEFORE_TAB",
2116 "please, no space before tabs\n" . $herevet) &&
2117 $fix) {
2118 $fixed[$linenr - 1] =~
2119 s/(^\+.*) +\t/$1\t/;
2120 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002121 }
2122
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002123# check for && or || at the start of a line
2124 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2125 CHK("LOGICAL_CONTINUATIONS",
2126 "Logical continuations should be on the previous line\n" . $hereprev);
2127 }
2128
2129# check multi-line statement indentation matches previous line
2130 if ($^V && $^V ge 5.10.0 &&
2131 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2132 $prevline =~ /^\+(\t*)(.*)$/;
2133 my $oldindent = $1;
2134 my $rest = $2;
2135
2136 my $pos = pos_last_openparen($rest);
2137 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002138 $line =~ /^(\+| )([ \t]*)/;
2139 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002140
2141 my $goodtabindent = $oldindent .
2142 "\t" x ($pos / 8) .
2143 " " x ($pos % 8);
2144 my $goodspaceindent = $oldindent . " " x $pos;
2145
2146 if ($newindent ne $goodtabindent &&
2147 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002148
2149 if (CHK("PARENTHESIS_ALIGNMENT",
2150 "Alignment should match open parenthesis\n" . $hereprev) &&
2151 $fix && $line =~ /^\+/) {
2152 $fixed[$linenr - 1] =~
2153 s/^\+[ \t]*/\+$goodtabindent/;
2154 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002155 }
2156 }
2157 }
2158
Joe Perches23f780c2013-07-03 15:05:31 -07002159 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002160 if (CHK("SPACING",
2161 "No space is necessary after a cast\n" . $hereprev) &&
2162 $fix) {
2163 $fixed[$linenr - 1] =~
2164 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2165 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002166 }
2167
Joe Perches05880602012-10-04 17:13:35 -07002168 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002169 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2170 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002171 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2172 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2173 }
2174
2175 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002176 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2177 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002178 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002179 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2180 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2181 "networking block comments start with * on subsequent lines\n" . $hereprev);
2182 }
2183
2184 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002185 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2186 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2187 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2188 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002189 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2190 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2191 }
2192
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002193# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002194# Exceptions:
2195# 1) within comments
2196# 2) indented preprocessor commands
2197# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002198 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002199 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002200 if (WARN("LEADING_SPACE",
2201 "please, no spaces at the start of a line\n" . $herevet) &&
2202 $fix) {
2203 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2204 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002205 }
2206
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002207# check we are in a valid C source file if not then ignore this hunk
2208 next if ($realfile !~ /\.(h|c)$/);
2209
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002210# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2211 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2212 WARN("CONFIG_EXPERIMENTAL",
2213 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2214 }
2215
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002216# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002217 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002218 WARN("CVS_KEYWORD",
2219 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002220 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002221
Mike Frysinger42e41c52009-09-21 17:04:40 -07002222# Blackfin: don't use __builtin_bfin_[cs]sync
2223 if ($line =~ /__builtin_bfin_csync/) {
2224 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002225 ERROR("CSYNC",
2226 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002227 }
2228 if ($line =~ /__builtin_bfin_ssync/) {
2229 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002230 ERROR("SSYNC",
2231 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002232 }
2233
Joe Perches56e77d72013-02-21 16:44:14 -08002234# check for old HOTPLUG __dev<foo> section markings
2235 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2236 WARN("HOTPLUG_SECTION",
2237 "Using $1 is unnecessary\n" . $herecurr);
2238 }
2239
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002240# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002241 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2242 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002243#print "LINE<$line>\n";
2244 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002245 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002246 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002247 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002248 $stat =~ s/\n./\n /g;
2249 $cond =~ s/\n./\n /g;
2250
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002251#print "linenr<$linenr> <$stat>\n";
2252 # If this statement has no statement boundaries within
2253 # it there is no point in retrying a statement scan
2254 # until we hit end of it.
2255 my $frag = $stat; $frag =~ s/;+\s*$//;
2256 if ($frag !~ /(?:{|;)/) {
2257#print "skip<$line_nr_next>\n";
2258 $suppress_statement = $line_nr_next;
2259 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002260
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002261 # Find the real next line.
2262 $realline_next = $line_nr_next;
2263 if (defined $realline_next &&
2264 (!defined $lines[$realline_next - 1] ||
2265 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2266 $realline_next++;
2267 }
2268
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002269 my $s = $stat;
2270 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002271
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002272 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002273 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002274
2275 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002276 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002277
Andy Whitcroft463f2862009-09-21 17:04:34 -07002278 } elsif ($s =~ /^.\s*else\b/s) {
2279
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002280 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002281 } 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 -07002282 my $type = $1;
2283 $type =~ s/\s+/ /g;
2284 possible($type, "A:" . $s);
2285
Andy Whitcroft8905a672007-11-28 16:21:06 -08002286 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002287 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002288 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002289 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002290
2291 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002292 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002293 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002294 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002295
2296 # Check for any sort of function declaration.
2297 # int foo(something bar, other baz);
2298 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002299 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 -08002300 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002301
Andy Whitcroftcf655042008-03-04 14:28:20 -08002302 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002303 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002304 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002305
Andy Whitcroft8905a672007-11-28 16:21:06 -08002306 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002307 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002308
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002309 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002310 }
2311 }
2312 }
2313
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002314 }
2315
Andy Whitcroft653d4872007-06-23 17:16:34 -07002316#
2317# Checks which may be anchored in the context.
2318#
2319
2320# Check for switch () and associated case and default
2321# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002322 if ($line=~/\bswitch\s*\(.*\)/) {
2323 my $err = '';
2324 my $sep = '';
2325 my @ctx = ctx_block_outer($linenr, $realcnt);
2326 shift(@ctx);
2327 for my $ctx (@ctx) {
2328 my ($clen, $cindent) = line_stats($ctx);
2329 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2330 $indent != $cindent) {
2331 $err .= "$sep$ctx\n";
2332 $sep = '';
2333 } else {
2334 $sep = "[...]\n";
2335 }
2336 }
2337 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002338 ERROR("SWITCH_CASE_INDENT_LEVEL",
2339 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002340 }
2341 }
2342
2343# if/while/etc brace do not go on next line, unless defining a do while loop,
2344# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002345 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002346 my $pre_ctx = "$1$2";
2347
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002348 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002349
2350 if ($line =~ /^\+\t{6,}/) {
2351 WARN("DEEP_INDENTATION",
2352 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2353 }
2354
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002355 my $ctx_cnt = $realcnt - $#ctx - 1;
2356 my $ctx = join("\n", @ctx);
2357
Andy Whitcroft548596d2008-07-23 21:29:01 -07002358 my $ctx_ln = $linenr;
2359 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002360
Andy Whitcroft548596d2008-07-23 21:29:01 -07002361 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2362 defined $lines[$ctx_ln - 1] &&
2363 $lines[$ctx_ln - 1] =~ /^-/)) {
2364 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2365 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002366 $ctx_ln++;
2367 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002368
Andy Whitcroft53210162008-07-23 21:29:03 -07002369 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2370 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002371
2372 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002373 ERROR("OPEN_BRACE",
2374 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002375 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002376 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002377 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2378 $ctx =~ /\)\s*\;\s*$/ &&
2379 defined $lines[$ctx_ln - 1])
2380 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002381 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2382 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002383 WARN("TRAILING_SEMICOLON",
2384 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002385 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002386 }
2387 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002388 }
2389
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002390# Check relative indent for conditionals and blocks.
2391 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002392 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2393 ctx_statement_block($linenr, $realcnt, 0)
2394 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002395 my ($s, $c) = ($stat, $cond);
2396
2397 substr($s, 0, length($c), '');
2398
2399 # Make sure we remove the line prefixes as we have
2400 # none on the first line, and are going to readd them
2401 # where necessary.
2402 $s =~ s/\n./\n/gs;
2403
2404 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002405 my @newlines = ($c =~ /\n/gs);
2406 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002407
2408 # We want to check the first line inside the block
2409 # starting at the end of the conditional, so remove:
2410 # 1) any blank line termination
2411 # 2) any opening brace { on end of the line
2412 # 3) any do (...) {
2413 my $continuation = 0;
2414 my $check = 0;
2415 $s =~ s/^.*\bdo\b//;
2416 $s =~ s/^\s*{//;
2417 if ($s =~ s/^\s*\\//) {
2418 $continuation = 1;
2419 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002420 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002421 $check = 1;
2422 $cond_lines++;
2423 }
2424
2425 # Also ignore a loop construct at the end of a
2426 # preprocessor statement.
2427 if (($prevline =~ /^.\s*#\s*define\s/ ||
2428 $prevline =~ /\\\s*$/) && $continuation == 0) {
2429 $check = 0;
2430 }
2431
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002432 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002433 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002434 while ($cond_ptr != $cond_lines) {
2435 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002436
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002437 # If we see an #else/#elif then the code
2438 # is not linear.
2439 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2440 $check = 0;
2441 }
2442
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002443 # Ignore:
2444 # 1) blank lines, they should be at 0,
2445 # 2) preprocessor lines, and
2446 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002447 if ($continuation ||
2448 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002449 $s =~ /^\s*#\s*?/ ||
2450 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002451 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002452 if ($s =~ s/^.*?\n//) {
2453 $cond_lines++;
2454 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002455 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002456 }
2457
2458 my (undef, $sindent) = line_stats("+" . $s);
2459 my $stat_real = raw_line($linenr, $cond_lines);
2460
2461 # Check if either of these lines are modified, else
2462 # this is not this patch's fault.
2463 if (!defined($stat_real) ||
2464 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2465 $check = 0;
2466 }
2467 if (defined($stat_real) && $cond_lines > 1) {
2468 $stat_real = "[...]\n$stat_real";
2469 }
2470
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002471 #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 -07002472
2473 if ($check && (($sindent % 8) != 0 ||
2474 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002475 WARN("SUSPECT_CODE_INDENT",
2476 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002477 }
2478 }
2479
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002480 # Track the 'values' across context and added lines.
2481 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002482 my ($curr_values, $curr_vars) =
2483 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002484 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002485 if ($dbg_values) {
2486 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002487 print "$linenr > .$outline\n";
2488 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002489 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002490 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002491 $prev_values = substr($curr_values, -1);
2492
Andy Whitcroft00df3442007-06-08 13:47:06 -07002493#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002494 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002495
Andy Whitcroft653d4872007-06-23 17:16:34 -07002496# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002497 if ($dbg_type) {
2498 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002499 ERROR("TEST_TYPE",
2500 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002501 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002502 ERROR("TEST_NOT_TYPE",
2503 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002504 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002505 next;
2506 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002507# TEST: allow direct testing of the attribute matcher.
2508 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002509 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002510 ERROR("TEST_ATTR",
2511 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002512 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002513 ERROR("TEST_NOT_ATTR",
2514 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002515 }
2516 next;
2517 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002518
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002519# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002520 if ($line =~ /^.\s*{/ &&
2521 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002522 ERROR("OPEN_BRACE",
2523 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002524 }
2525
Andy Whitcroft653d4872007-06-23 17:16:34 -07002526#
2527# Checks which are anchored on the added line.
2528#
2529
2530# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002531 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002532 my $path = $1;
2533 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002534 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002535 "malformed #include filename\n" . $herecurr);
2536 }
2537 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2538 ERROR("UAPI_INCLUDE",
2539 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002540 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002541 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002542
2543# no C99 // comments
2544 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002545 if (ERROR("C99_COMMENTS",
2546 "do not use C99 // comments\n" . $herecurr) &&
2547 $fix) {
2548 my $line = $fixed[$linenr - 1];
2549 if ($line =~ /\/\/(.*)$/) {
2550 my $comment = trim($1);
2551 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2552 }
2553 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002554 }
2555 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002556 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002557 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002558
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002559# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2560# the whole statement.
2561#print "APW <$lines[$realline_next - 1]>\n";
2562 if (defined $realline_next &&
2563 exists $lines[$realline_next - 1] &&
2564 !defined $suppress_export{$realline_next} &&
2565 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2566 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002567 # Handle definitions which produce identifiers with
2568 # a prefix:
2569 # XXX(foo);
2570 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002571 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002572 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002573 $name =~ /^${Ident}_$2/) {
2574#print "FOO C name<$name>\n";
2575 $suppress_export{$realline_next} = 1;
2576
2577 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002578 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002579 ^.DEFINE_$Ident\(\Q$name\E\)|
2580 ^.DECLARE_$Ident\(\Q$name\E\)|
2581 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002582 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2583 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002584 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002585#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2586 $suppress_export{$realline_next} = 2;
2587 } else {
2588 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002589 }
2590 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002591 if (!defined $suppress_export{$linenr} &&
2592 $prevline =~ /^.\s*$/ &&
2593 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2594 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2595#print "FOO B <$lines[$linenr - 1]>\n";
2596 $suppress_export{$linenr} = 2;
2597 }
2598 if (defined $suppress_export{$linenr} &&
2599 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002600 WARN("EXPORT_SYMBOL",
2601 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002602 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002603
Joe Eloff5150bda2010-08-09 17:21:00 -07002604# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002605 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2606 if (ERROR("GLOBAL_INITIALISERS",
2607 "do not initialise globals to 0 or NULL\n" .
2608 $herecurr) &&
2609 $fix) {
2610 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2611 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002612 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002613# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002614 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2615 if (ERROR("INITIALISED_STATIC",
2616 "do not initialise statics to 0 or NULL\n" .
2617 $herecurr) &&
2618 $fix) {
2619 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2620 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002621 }
2622
Joe Perchescb710ec2010-10-26 14:23:20 -07002623# check for static const char * arrays.
2624 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002625 WARN("STATIC_CONST_CHAR_ARRAY",
2626 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002627 $herecurr);
2628 }
2629
2630# check for static char foo[] = "bar" declarations.
2631 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002632 WARN("STATIC_CONST_CHAR_ARRAY",
2633 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002634 $herecurr);
2635 }
2636
Joe Perches93ed0e22010-10-26 14:23:21 -07002637# check for declarations of struct pci_device_id
2638 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002639 WARN("DEFINE_PCI_DEVICE_TABLE",
2640 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002641 }
2642
Andy Whitcroft653d4872007-06-23 17:16:34 -07002643# check for new typedefs, only function parameters and sparse annotations
2644# make sense.
2645 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002646 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002647 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002648 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002649 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002650 WARN("NEW_TYPEDEFS",
2651 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002652 }
2653
2654# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002655 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002656 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2657 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002658 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002659
Andy Whitcroft65863862009-01-06 14:41:21 -08002660 # Should start with a space.
2661 $to =~ s/^(\S)/ $1/;
2662 # Should not end with a space.
2663 $to =~ s/\s+$//;
2664 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002665 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002666 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002667
Joe Perches3705ce52013-07-03 15:05:31 -07002668## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002669 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002670 if (ERROR("POINTER_LOCATION",
2671 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2672 $fix) {
2673 my $sub_from = $ident;
2674 my $sub_to = $ident;
2675 $sub_to =~ s/\Q$from\E/$to/;
2676 $fixed[$linenr - 1] =~
2677 s@\Q$sub_from\E@$sub_to@;
2678 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002679 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002680 }
2681 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2682 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002683 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002684
Andy Whitcroft65863862009-01-06 14:41:21 -08002685 # Should start with a space.
2686 $to =~ s/^(\S)/ $1/;
2687 # Should not end with a space.
2688 $to =~ s/\s+$//;
2689 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002690 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002691 }
2692 # Modifiers should have spaces.
2693 $to =~ s/(\b$Modifier$)/$1 /;
2694
Joe Perches3705ce52013-07-03 15:05:31 -07002695## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002696 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002697 if (ERROR("POINTER_LOCATION",
2698 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2699 $fix) {
2700
2701 my $sub_from = $match;
2702 my $sub_to = $match;
2703 $sub_to =~ s/\Q$from\E/$to/;
2704 $fixed[$linenr - 1] =~
2705 s@\Q$sub_from\E@$sub_to@;
2706 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002707 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002708 }
2709
2710# # no BUG() or BUG_ON()
2711# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2712# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2713# print "$herecurr";
2714# $clean = 0;
2715# }
2716
Andy Whitcroft8905a672007-11-28 16:21:06 -08002717 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002718 WARN("LINUX_VERSION_CODE",
2719 "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 -08002720 }
2721
Joe Perches17441222011-06-15 15:08:17 -07002722# check for uses of printk_ratelimit
2723 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002724 WARN("PRINTK_RATELIMITED",
2725"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002726 }
2727
Andy Whitcroft00df3442007-06-08 13:47:06 -07002728# printk should use KERN_* levels. Note that follow on printk's on the
2729# same line do not need a level, so we use the current block context
2730# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002731# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002732# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002733 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002734 my $ok = 0;
2735 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2736 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002737 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002738 # with "\n" ignore it, else it is to blame
2739 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2740 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2741 $ok = 1;
2742 }
2743 last;
2744 }
2745 }
2746 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002747 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2748 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002749 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002750 }
2751
Joe Perches243f3802012-05-31 16:26:09 -07002752 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2753 my $orig = $1;
2754 my $level = lc($orig);
2755 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002756 my $level2 = $level;
2757 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002758 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002759 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002760 }
2761
2762 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07002763 if (WARN("PREFER_PR_LEVEL",
2764 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2765 $fix) {
2766 $fixed[$linenr - 1] =~
2767 s/\bpr_warning\b/pr_warn/;
2768 }
Joe Perches243f3802012-05-31 16:26:09 -07002769 }
2770
Joe Perchesdc139312013-02-21 16:44:13 -08002771 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2772 my $orig = $1;
2773 my $level = lc($orig);
2774 $level = "warn" if ($level eq "warning");
2775 $level = "dbg" if ($level eq "debug");
2776 WARN("PREFER_DEV_LEVEL",
2777 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2778 }
2779
Andy Whitcroft653d4872007-06-23 17:16:34 -07002780# function brace can't be on same line, except for #defines of do while,
2781# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002782 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2783 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002784 ERROR("OPEN_BRACE",
2785 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002786 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002787
Andy Whitcroft8905a672007-11-28 16:21:06 -08002788# open braces for enum, union and struct go on the same line.
2789 if ($line =~ /^.\s*{/ &&
2790 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002791 ERROR("OPEN_BRACE",
2792 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002793 }
2794
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002795# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002796 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2797 if (WARN("SPACING",
2798 "missing space after $1 definition\n" . $herecurr) &&
2799 $fix) {
2800 $fixed[$linenr - 1] =~
2801 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2802 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002803 }
2804
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002805# check for spacing round square brackets; allowed:
2806# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002807# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2808# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002809 while ($line =~ /(.*?\s)\[/g) {
2810 my ($where, $prefix) = ($-[1], $1);
2811 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002812 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002813 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002814 if (ERROR("BRACKET_SPACE",
2815 "space prohibited before open square bracket '['\n" . $herecurr) &&
2816 $fix) {
2817 $fixed[$linenr - 1] =~
2818 s/^(\+.*?)\s+\[/$1\[/;
2819 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002820 }
2821 }
2822
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002823# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002824 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002825 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002826 my $ctx_before = substr($line, 0, $-[1]);
2827 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002828
2829 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002830 if ($name =~ /^(?:
2831 if|for|while|switch|return|case|
2832 volatile|__volatile__|
2833 __attribute__|format|__extension__|
2834 asm|__asm__)$/x)
2835 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002836 # cpp #define statements have non-optional spaces, ie
2837 # if there is a space between the name and the open
2838 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002839 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002840
2841 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002842 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002843
2844 # If this whole things ends with a type its most
2845 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002846 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002847
2848 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002849 if (WARN("SPACING",
2850 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2851 $fix) {
2852 $fixed[$linenr - 1] =~
2853 s/\b$name\s+\(/$name\(/;
2854 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002855 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002856 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002857
Andy Whitcroft653d4872007-06-23 17:16:34 -07002858# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002859 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002860 my $fixed_line = "";
2861 my $line_fixed = 0;
2862
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002863 my $ops = qr{
2864 <<=|>>=|<=|>=|==|!=|
2865 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2866 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002867 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08002868 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002869 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002870 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002871
2872## print("element count: <" . $#elements . ">\n");
2873## foreach my $el (@elements) {
2874## print("el: <$el>\n");
2875## }
2876
2877 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07002878 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002879
Joe Perches3705ce52013-07-03 15:05:31 -07002880 foreach my $el (@elements) {
2881 push(@fix_elements, substr($rawline, $off, length($el)));
2882 $off += length($el);
2883 }
2884
2885 $off = 0;
2886
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002887 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07002888 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002889
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002890 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002891
2892 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2893
2894## print("n: <$n> good: <$good>\n");
2895
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002896 $off += length($elements[$n]);
2897
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002898 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002899 my $ca = substr($opline, 0, $off);
2900 my $cc = '';
2901 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2902 $cc = substr($opline, $off + length($elements[$n + 1]));
2903 }
2904 my $cb = "$ca$;$cc";
2905
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002906 my $a = '';
2907 $a = 'V' if ($elements[$n] ne '');
2908 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002909 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002910 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2911 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002912 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002913
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002914 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002915
2916 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002917 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002918 $c = 'V' if ($elements[$n + 2] ne '');
2919 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002920 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002921 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2922 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002923 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002924 } else {
2925 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002926 }
2927
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002928 my $ctx = "${a}x${c}";
2929
2930 my $at = "(ctx:$ctx)";
2931
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002932 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002933 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002934
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002935 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002936 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002937
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002938 # Get the full operator variant.
2939 my $opv = $op . substr($curr_vars, $off, 1);
2940
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002941 # Ignore operators passed as parameters.
2942 if ($op_type ne 'V' &&
2943 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2944
Andy Whitcroftcf655042008-03-04 14:28:20 -08002945# # Ignore comments
2946# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002947
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002948 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002949 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002950 if ($ctx !~ /.x[WEBC]/ &&
2951 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002952 if (ERROR("SPACING",
2953 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002954 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002955 $line_fixed = 1;
2956 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002957 }
2958
2959 # // is a comment
2960 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002961
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002962 # No spaces for:
2963 # ->
2964 # : when part of a bitfield
2965 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002966 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002967 if (ERROR("SPACING",
2968 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002969 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002970 if (defined $fix_elements[$n + 2]) {
2971 $fix_elements[$n + 2] =~ s/^\s+//;
2972 }
Joe Perchesb34c6482013-09-11 14:24:01 -07002973 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002974 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002975 }
2976
2977 # , must have a space on the right.
2978 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002979 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002980 if (ERROR("SPACING",
2981 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002982 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002983 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07002984 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07002985 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002986 }
2987
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002988 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002989 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002990 #warn "'*' is part of type\n";
2991
2992 # unary operators should have a space before and
2993 # none after. May be left adjacent to another
2994 # unary operator, or a cast
2995 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002996 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002997 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002998 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002999 if (ERROR("SPACING",
3000 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003001 if ($n != $last_after + 2) {
3002 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3003 $line_fixed = 1;
3004 }
Joe Perches3705ce52013-07-03 15:05:31 -07003005 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003006 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003007 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003008 # A unary '*' may be const
3009
3010 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003011 if (ERROR("SPACING",
3012 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003013 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003014 if (defined $fix_elements[$n + 2]) {
3015 $fix_elements[$n + 2] =~ s/^\s+//;
3016 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003017 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003018 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003019 }
3020
3021 # unary ++ and unary -- are allowed no space on one side.
3022 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003023 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003024 if (ERROR("SPACING",
3025 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003026 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003027 $line_fixed = 1;
3028 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003029 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003030 if ($ctx =~ /Wx[BE]/ ||
3031 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003032 if (ERROR("SPACING",
3033 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003034 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003035 $line_fixed = 1;
3036 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003037 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003038 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003039 if (ERROR("SPACING",
3040 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003041 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003042 if (defined $fix_elements[$n + 2]) {
3043 $fix_elements[$n + 2] =~ s/^\s+//;
3044 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003045 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003046 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003047 }
3048
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003049 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003050 } elsif ($op eq '<<' or $op eq '>>' or
3051 $op eq '&' or $op eq '^' or $op eq '|' or
3052 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003053 $op eq '*' or $op eq '/' or
3054 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003055 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003056 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003057 if (ERROR("SPACING",
3058 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003059 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3060 if (defined $fix_elements[$n + 2]) {
3061 $fix_elements[$n + 2] =~ s/^\s+//;
3062 }
Joe Perches3705ce52013-07-03 15:05:31 -07003063 $line_fixed = 1;
3064 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003065 }
3066
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003067 # A colon needs no spaces before when it is
3068 # terminating a case value or a label.
3069 } elsif ($opv eq ':C' || $opv eq ':L') {
3070 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003071 if (ERROR("SPACING",
3072 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003073 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003074 $line_fixed = 1;
3075 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003076 }
3077
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003078 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003079 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003080 my $ok = 0;
3081
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003082 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003083 if (($op eq '<' &&
3084 $cc =~ /^\S+\@\S+>/) ||
3085 ($op eq '>' &&
3086 $ca =~ /<\S+\@\S+$/))
3087 {
3088 $ok = 1;
3089 }
3090
Joe Perches84731622013-11-12 15:10:05 -08003091 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003092 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003093 my $msg_type = \&ERROR;
3094 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3095
3096 if (&{$msg_type}("SPACING",
3097 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003098 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3099 if (defined $fix_elements[$n + 2]) {
3100 $fix_elements[$n + 2] =~ s/^\s+//;
3101 }
Joe Perches3705ce52013-07-03 15:05:31 -07003102 $line_fixed = 1;
3103 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003104 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003105 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003106 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003107
3108## print("n: <$n> GOOD: <$good>\n");
3109
3110 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003111 }
Joe Perches3705ce52013-07-03 15:05:31 -07003112
3113 if (($#elements % 2) == 0) {
3114 $fixed_line = $fixed_line . $fix_elements[$#elements];
3115 }
3116
3117 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3118 $fixed[$linenr - 1] = $fixed_line;
3119 }
3120
3121
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003122 }
3123
Joe Perches786b6322013-07-03 15:05:32 -07003124# check for whitespace before a non-naked semicolon
3125 if ($line =~ /^\+.*\S\s+;/) {
3126 if (WARN("SPACING",
3127 "space prohibited before semicolon\n" . $herecurr) &&
3128 $fix) {
3129 1 while $fixed[$linenr - 1] =~
3130 s/^(\+.*\S)\s+;/$1;/;
3131 }
3132 }
3133
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003134# check for multiple assignments
3135 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003136 CHK("MULTIPLE_ASSIGNMENTS",
3137 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003138 }
3139
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003140## # check for multiple declarations, allowing for a function declaration
3141## # continuation.
3142## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3143## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3144##
3145## # Remove any bracketed sections to ensure we do not
3146## # falsly report the parameters of functions.
3147## my $ln = $line;
3148## while ($ln =~ s/\([^\(\)]*\)//g) {
3149## }
3150## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003151## WARN("MULTIPLE_DECLARATION",
3152## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003153## }
3154## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003155
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003156#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003157 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3158 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003159 if (ERROR("SPACING",
3160 "space required before the open brace '{'\n" . $herecurr) &&
3161 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003162 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003163 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003164 }
3165
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003166## # check for blank lines before declarations
3167## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3168## $prevrawline =~ /^.\s*$/) {
3169## WARN("SPACING",
3170## "No blank lines before declarations\n" . $hereprev);
3171## }
3172##
3173
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003174# closing brace should have a space following it when it has anything
3175# on the line
3176 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003177 if (ERROR("SPACING",
3178 "space required after that close brace '}'\n" . $herecurr) &&
3179 $fix) {
3180 $fixed[$linenr - 1] =~
3181 s/}((?!(?:,|;|\)))\S)/} $1/;
3182 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003183 }
3184
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003185# check spacing on square brackets
3186 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003187 if (ERROR("SPACING",
3188 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3189 $fix) {
3190 $fixed[$linenr - 1] =~
3191 s/\[\s+/\[/;
3192 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003193 }
3194 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003195 if (ERROR("SPACING",
3196 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3197 $fix) {
3198 $fixed[$linenr - 1] =~
3199 s/\s+\]/\]/;
3200 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003201 }
3202
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003203# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003204 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3205 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003206 if (ERROR("SPACING",
3207 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3208 $fix) {
3209 $fixed[$linenr - 1] =~
3210 s/\(\s+/\(/;
3211 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003212 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003213 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003214 $line !~ /for\s*\(.*;\s+\)/ &&
3215 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003216 if (ERROR("SPACING",
3217 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3218 $fix) {
3219 $fixed[$linenr - 1] =~
3220 s/\s+\)/\)/;
3221 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003222 }
3223
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003224#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003225 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003226 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003227 if (WARN("INDENTED_LABEL",
3228 "labels should not be indented\n" . $herecurr) &&
3229 $fix) {
3230 $fixed[$linenr - 1] =~
3231 s/^(.)\s+/$1/;
3232 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003233 }
3234
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003235# Return is not a function.
Joe Perches507e5142013-11-12 15:10:13 -08003236 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003237 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003238 if ($^V && $^V ge 5.10.0 &&
3239 $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003240 ERROR("RETURN_PARENTHESES",
3241 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003242
3243 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003244 ERROR("SPACING",
3245 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003246 }
3247 }
Joe Perches507e5142013-11-12 15:10:13 -08003248
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003249# Return of what appears to be an errno should normally be -'ve
3250 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3251 my $name = $1;
3252 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003253 WARN("USE_NEGATIVE_ERRNO",
3254 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003255 }
3256 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003257
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003258# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003259 if ($line =~ /\b(if|while|for|switch)\(/) {
3260 if (ERROR("SPACING",
3261 "space required before the open parenthesis '('\n" . $herecurr) &&
3262 $fix) {
3263 $fixed[$linenr - 1] =~
3264 s/\b(if|while|for|switch)\(/$1 \(/;
3265 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003266 }
3267
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003268# Check for illegal assignment in if conditional -- and check for trailing
3269# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003270 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003271 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3272 ctx_statement_block($linenr, $realcnt, 0)
3273 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003274 my ($stat_next) = ctx_statement_block($line_nr_next,
3275 $remain_next, $off_next);
3276 $stat_next =~ s/\n./\n /g;
3277 ##print "stat<$stat> stat_next<$stat_next>\n";
3278
3279 if ($stat_next =~ /^\s*while\b/) {
3280 # If the statement carries leading newlines,
3281 # then count those as offsets.
3282 my ($whitespace) =
3283 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3284 my $offset =
3285 statement_rawlines($whitespace) - 1;
3286
3287 $suppress_whiletrailers{$line_nr_next +
3288 $offset} = 1;
3289 }
3290 }
3291 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003292 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003293 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003294 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003295
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003296 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003297 ERROR("ASSIGN_IN_IF",
3298 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003299 }
3300
3301 # Find out what is on the end of the line after the
3302 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003303 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003304 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003305 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003306 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3307 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003308 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003309 # Find out how long the conditional actually is.
3310 my @newlines = ($c =~ /\n/gs);
3311 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003312 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003313
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003314 $stat_real = raw_line($linenr, $cond_lines)
3315 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003316 if (defined($stat_real) && $cond_lines > 1) {
3317 $stat_real = "[...]\n$stat_real";
3318 }
3319
Joe Perches000d1cc12011-07-25 17:13:25 -07003320 ERROR("TRAILING_STATEMENTS",
3321 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003322 }
3323 }
3324
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003325# Check for bitwise tests written as boolean
3326 if ($line =~ /
3327 (?:
3328 (?:\[|\(|\&\&|\|\|)
3329 \s*0[xX][0-9]+\s*
3330 (?:\&\&|\|\|)
3331 |
3332 (?:\&\&|\|\|)
3333 \s*0[xX][0-9]+\s*
3334 (?:\&\&|\|\||\)|\])
3335 )/x)
3336 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003337 WARN("HEXADECIMAL_BOOLEAN_TEST",
3338 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003339 }
3340
Andy Whitcroft8905a672007-11-28 16:21:06 -08003341# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003342 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3343 my $s = $1;
3344 $s =~ s/$;//g; # Remove any comments
3345 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003346 ERROR("TRAILING_STATEMENTS",
3347 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003348 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003349 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003350# if should not continue a brace
3351 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003352 ERROR("TRAILING_STATEMENTS",
3353 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003354 $herecurr);
3355 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003356# case and default should not have general statements after them
3357 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3358 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003359 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003360 \s*return\s+
3361 )/xg)
3362 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003363 ERROR("TRAILING_STATEMENTS",
3364 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003365 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003366
3367 # Check for }<nl>else {, these must be at the same
3368 # indent level to be relevant to each other.
3369 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3370 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003371 ERROR("ELSE_AFTER_BRACE",
3372 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003373 }
3374
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003375 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3376 $previndent == $indent) {
3377 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3378
3379 # Find out what is on the end of the line after the
3380 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003381 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003382 $s =~ s/\n.*//g;
3383
3384 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003385 ERROR("WHILE_AFTER_BRACE",
3386 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003387 }
3388 }
3389
Joe Perches95e2c602013-07-03 15:05:20 -07003390#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003391 while ($line =~ m{($Constant|$Lval)}g) {
3392 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003393
3394#gcc binary extension
3395 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003396 if (WARN("GCC_BINARY_CONSTANT",
3397 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3398 $fix) {
3399 my $hexval = sprintf("0x%x", oct($var));
3400 $fixed[$linenr - 1] =~
3401 s/\b$var\b/$hexval/;
3402 }
Joe Perches95e2c602013-07-03 15:05:20 -07003403 }
3404
3405#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003406 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003407 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003408#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003409 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003410#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003411 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003412 while ($var =~ m{($Ident)}g) {
3413 my $word = $1;
3414 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08003415 if ($check) {
3416 seed_camelcase_includes();
3417 if (!$file && !$camelcase_file_seeded) {
3418 seed_camelcase_file($realfile);
3419 $camelcase_file_seeded = 1;
3420 }
3421 }
Joe Perches7e781f62013-09-11 14:23:55 -07003422 if (!defined $camelcase{$word}) {
3423 $camelcase{$word} = 1;
3424 CHK("CAMELCASE",
3425 "Avoid CamelCase: <$word>\n" . $herecurr);
3426 }
Joe Perches34456862013-07-03 15:05:34 -07003427 }
Joe Perches323c1262012-12-17 16:02:07 -08003428 }
3429 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003430
3431#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003432 if ($line =~ /\#\s*define.*\\\s+$/) {
3433 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3434 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3435 $fix) {
3436 $fixed[$linenr - 1] =~ s/\s+$//;
3437 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003438 }
3439
Andy Whitcroft653d4872007-06-23 17:16:34 -07003440#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003441 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003442 my $file = "$1.h";
3443 my $checkfile = "include/linux/$file";
3444 if (-f "$root/$checkfile" &&
3445 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003446 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003447 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003448 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003449 CHK("ARCH_INCLUDE_LINUX",
3450 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003451 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003452 WARN("INCLUDE_LINUX",
3453 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003454 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003455 }
3456 }
3457
Andy Whitcroft653d4872007-06-23 17:16:34 -07003458# multi-statement macros should be enclosed in a do while loop, grab the
3459# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003460# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003461 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3462 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003463 my $ln = $linenr;
3464 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003465 my ($off, $dstat, $dcond, $rest);
3466 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003467 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003468 ctx_statement_block($linenr, $realcnt, 0);
3469 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003470 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003471 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003472
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003473 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003474 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003475 $dstat =~ s/\\\n.//g;
3476 $dstat =~ s/^\s*//s;
3477 $dstat =~ s/\s*$//s;
3478
3479 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003480 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3481 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003482 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003483 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003484 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003485
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003486 # Flatten any obvious string concatentation.
3487 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3488 $dstat =~ s/$Ident\s*("X*")/$1/)
3489 {
3490 }
3491
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003492 my $exceptions = qr{
3493 $Declare|
3494 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003495 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003496 DECLARE_PER_CPU|
3497 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003498 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003499 union|
3500 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003501 \.$Ident\s*=\s*|
3502 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003503 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003504 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003505 if ($dstat ne '' &&
3506 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3507 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003508 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003509 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003510 $dstat !~ /$exceptions/ &&
3511 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003512 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003513 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003514 $dstat !~ /^for\s*$Constant$/ && # for (...)
3515 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3516 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07003517 $dstat !~ /^\({/ && # ({...
3518 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003519 {
3520 $ctx =~ s/\n*$//;
3521 my $herectx = $here . "\n";
3522 my $cnt = statement_rawlines($ctx);
3523
3524 for (my $n = 0; $n < $cnt; $n++) {
3525 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003526 }
3527
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003528 if ($dstat =~ /;/) {
3529 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3530 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3531 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003532 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003533 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003534 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003535 }
Joe Perches5023d342012-12-17 16:01:47 -08003536
Joe Perches481eb482012-12-17 16:01:56 -08003537# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003538
3539 } else {
3540 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003541 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3542 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003543 $line =~ /^\+.*\\$/) {
3544 WARN("LINE_CONTINUATIONS",
3545 "Avoid unnecessary line continuations\n" . $herecurr);
3546 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003547 }
3548
Joe Perchesb13edf72012-07-30 14:41:24 -07003549# do {} while (0) macro tests:
3550# single-statement macros do not need to be enclosed in do while (0) loop,
3551# macro should not end with a semicolon
3552 if ($^V && $^V ge 5.10.0 &&
3553 $realfile !~ m@/vmlinux.lds.h$@ &&
3554 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3555 my $ln = $linenr;
3556 my $cnt = $realcnt;
3557 my ($off, $dstat, $dcond, $rest);
3558 my $ctx = '';
3559 ($dstat, $dcond, $ln, $cnt, $off) =
3560 ctx_statement_block($linenr, $realcnt, 0);
3561 $ctx = $dstat;
3562
3563 $dstat =~ s/\\\n.//g;
3564
3565 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3566 my $stmts = $2;
3567 my $semis = $3;
3568
3569 $ctx =~ s/\n*$//;
3570 my $cnt = statement_rawlines($ctx);
3571 my $herectx = $here . "\n";
3572
3573 for (my $n = 0; $n < $cnt; $n++) {
3574 $herectx .= raw_line($linenr, $n) . "\n";
3575 }
3576
Joe Perchesac8e97f2012-08-21 16:15:53 -07003577 if (($stmts =~ tr/;/;/) == 1 &&
3578 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003579 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3580 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3581 }
3582 if (defined $semis && $semis ne "") {
3583 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3584 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3585 }
3586 }
3587 }
3588
Mike Frysinger080ba922009-01-06 14:41:25 -08003589# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3590# all assignments may have only one of the following with an assignment:
3591# .
3592# ALIGN(...)
3593# VMLINUX_SYMBOL(...)
3594 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003595 WARN("MISSING_VMLINUX_SYMBOL",
3596 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003597 }
3598
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003599# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003600 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3601 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003602 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003603 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003604 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3605 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003606 my @allowed = ();
3607 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003608 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003609 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003610 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003611 for my $chunk (@chunks) {
3612 my ($cond, $block) = @{$chunk};
3613
Andy Whitcroft773647a2008-03-28 14:15:58 -07003614 # If the condition carries leading newlines, then count those as offsets.
3615 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3616 my $offset = statement_rawlines($whitespace) - 1;
3617
Joe Perchesaad4f612012-03-23 15:02:19 -07003618 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003619 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3620
3621 # We have looked at and allowed this specific line.
3622 $suppress_ifbraces{$ln + $offset} = 1;
3623
3624 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003625 $ln += statement_rawlines($block) - 1;
3626
Andy Whitcroft773647a2008-03-28 14:15:58 -07003627 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003628
3629 $seen++ if ($block =~ /^\s*{/);
3630
Joe Perchesaad4f612012-03-23 15:02:19 -07003631 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003632 if (statement_lines($cond) > 1) {
3633 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003634 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003635 }
3636 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003637 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003638 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003639 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003640 if (statement_block_size($block) > 1) {
3641 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003642 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003643 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003644 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003645 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003646 if ($seen) {
3647 my $sum_allowed = 0;
3648 foreach (@allowed) {
3649 $sum_allowed += $_;
3650 }
3651 if ($sum_allowed == 0) {
3652 WARN("BRACES",
3653 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3654 } elsif ($sum_allowed != $allow &&
3655 $seen != $allow) {
3656 CHK("BRACES",
3657 "braces {} should be used on all arms of this statement\n" . $herectx);
3658 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003659 }
3660 }
3661 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003662 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003663 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003664 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003665
Andy Whitcroftcf655042008-03-04 14:28:20 -08003666 # Check the pre-context.
3667 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3668 #print "APW: ALLOWED: pre<$1>\n";
3669 $allowed = 1;
3670 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003671
3672 my ($level, $endln, @chunks) =
3673 ctx_statement_full($linenr, $realcnt, $-[0]);
3674
Andy Whitcroftcf655042008-03-04 14:28:20 -08003675 # Check the condition.
3676 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003677 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003678 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003679 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003680 }
3681 if (statement_lines($cond) > 1) {
3682 #print "APW: ALLOWED: cond<$cond>\n";
3683 $allowed = 1;
3684 }
3685 if ($block =~/\b(?:if|for|while)\b/) {
3686 #print "APW: ALLOWED: block<$block>\n";
3687 $allowed = 1;
3688 }
3689 if (statement_block_size($block) > 1) {
3690 #print "APW: ALLOWED: lines block<$block>\n";
3691 $allowed = 1;
3692 }
3693 # Check the post-context.
3694 if (defined $chunks[1]) {
3695 my ($cond, $block) = @{$chunks[1]};
3696 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003697 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003698 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003699 if ($block =~ /^\s*\{/) {
3700 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3701 $allowed = 1;
3702 }
3703 }
3704 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003705 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003706 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003707
Andy Whitcroftf0556632008-10-15 22:02:23 -07003708 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003709 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003710 }
3711
Joe Perches000d1cc12011-07-25 17:13:25 -07003712 WARN("BRACES",
3713 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003714 }
3715 }
3716
Joe Perches0979ae62012-12-17 16:01:59 -08003717# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003718 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003719 CHK("BRACES",
3720 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3721 }
Joe Perches77b9a532013-07-03 15:05:29 -07003722 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003723 CHK("BRACES",
3724 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3725 }
3726
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003727# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003728 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3729 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003730 WARN("VOLATILE",
3731 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003732 }
3733
Andy Whitcroft00df3442007-06-08 13:47:06 -07003734# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003735 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003736 CHK("REDUNDANT_CODE",
3737 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003738 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003739 }
3740
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003741# check for needless "if (<foo>) fn(<foo>)" uses
3742 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3743 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3744 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3745 WARN('NEEDLESS_IF',
3746 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003747 }
3748 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003749
Joe Perches8716de32013-09-11 14:24:05 -07003750# check for bad placement of section $InitAttribute (e.g.: __initdata)
3751 if ($line =~ /(\b$InitAttribute\b)/) {
3752 my $attr = $1;
3753 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3754 my $ptr = $1;
3755 my $var = $2;
3756 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3757 ERROR("MISPLACED_INIT",
3758 "$attr should be placed after $var\n" . $herecurr)) ||
3759 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3760 WARN("MISPLACED_INIT",
3761 "$attr should be placed after $var\n" . $herecurr))) &&
3762 $fix) {
3763 $fixed[$linenr - 1] =~ 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;
3764 }
3765 }
3766 }
3767
Joe Perchese970b882013-11-12 15:10:10 -08003768# check for $InitAttributeData (ie: __initdata) with const
3769 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3770 my $attr = $1;
3771 $attr =~ /($InitAttributePrefix)(.*)/;
3772 my $attr_prefix = $1;
3773 my $attr_type = $2;
3774 if (ERROR("INIT_ATTRIBUTE",
3775 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3776 $fix) {
3777 $fixed[$linenr - 1] =~
3778 s/$InitAttributeData/${attr_prefix}initconst/;
3779 }
3780 }
3781
3782# check for $InitAttributeConst (ie: __initconst) without const
3783 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3784 my $attr = $1;
3785 if (ERROR("INIT_ATTRIBUTE",
3786 "Use of $attr requires a separate use of const\n" . $herecurr) &&
3787 $fix) {
3788 my $lead = $fixed[$linenr - 1] =~
3789 /(^\+\s*(?:static\s+))/;
3790 $lead = rtrim($1);
3791 $lead = "$lead " if ($lead !~ /^\+$/);
3792 $lead = "${lead}const ";
3793 $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3794 }
3795 }
3796
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003797# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003798 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003799 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003800 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003801 CHK("USLEEP_RANGE",
3802 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003803 }
3804 }
3805
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003806# warn about unexpectedly long msleep's
3807 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3808 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003809 WARN("MSLEEP",
3810 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003811 }
3812 }
3813
Joe Perches36ec1932013-07-03 15:05:25 -07003814# check for comparisons of jiffies
3815 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3816 WARN("JIFFIES_COMPARISON",
3817 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3818 }
3819
Joe Perches9d7a34a2013-07-03 15:05:26 -07003820# check for comparisons of get_jiffies_64()
3821 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3822 WARN("JIFFIES_COMPARISON",
3823 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3824 }
3825
Andy Whitcroft00df3442007-06-08 13:47:06 -07003826# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003827# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003828# print "#ifdef in C files should be avoided\n";
3829# print "$herecurr";
3830# $clean = 0;
3831# }
3832
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003833# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003834 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003835 if (ERROR("SPACING",
3836 "exactly one space required after that #$1\n" . $herecurr) &&
3837 $fix) {
3838 $fixed[$linenr - 1] =~
3839 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3840 }
3841
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003842 }
3843
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003844# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003845 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3846 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003847 my $which = $1;
3848 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003849 CHK("UNCOMMENTED_DEFINITION",
3850 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003851 }
3852 }
3853# check for memory barriers without a comment.
3854 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3855 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08003856 WARN("MEMORY_BARRIER",
3857 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003858 }
3859 }
3860# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003861 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003862 CHK("ARCH_DEFINES",
3863 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003864 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003865
Tobias Klauserd4977c72010-05-24 14:33:30 -07003866# Check that the storage class is at the beginning of a declaration
3867 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003868 WARN("STORAGE_CLASS",
3869 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003870 }
3871
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003872# check the location of the inline attribute, that it is between
3873# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003874 if ($line =~ /\b$Type\s+$Inline\b/ ||
3875 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003876 ERROR("INLINE_LOCATION",
3877 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003878 }
3879
Andy Whitcroft8905a672007-11-28 16:21:06 -08003880# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08003881 if ($realfile !~ m@\binclude/uapi/@ &&
3882 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003883 if (WARN("INLINE",
3884 "plain inline is preferred over $1\n" . $herecurr) &&
3885 $fix) {
3886 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3887
3888 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003889 }
3890
Joe Perches3d130fd2011-01-12 17:00:00 -08003891# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08003892 if ($realfile !~ m@\binclude/uapi/@ &&
3893 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003894 WARN("PREFER_PACKED",
3895 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003896 }
3897
Joe Perches39b7e282011-07-25 17:13:24 -07003898# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08003899 if ($realfile !~ m@\binclude/uapi/@ &&
3900 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003901 WARN("PREFER_ALIGNED",
3902 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003903 }
3904
Joe Perches5f14d3b2012-01-10 15:09:52 -08003905# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08003906 if ($realfile !~ m@\binclude/uapi/@ &&
3907 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003908 if (WARN("PREFER_PRINTF",
3909 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3910 $fix) {
3911 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3912
3913 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08003914 }
3915
Joe Perches6061d942012-03-23 15:02:16 -07003916# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08003917 if ($realfile !~ m@\binclude/uapi/@ &&
3918 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003919 if (WARN("PREFER_SCANF",
3920 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3921 $fix) {
3922 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3923 }
Joe Perches6061d942012-03-23 15:02:16 -07003924 }
3925
Joe Perches8f53a9b2010-03-05 13:43:48 -08003926# check for sizeof(&)
3927 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003928 WARN("SIZEOF_ADDRESS",
3929 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003930 }
3931
Joe Perches66c80b62012-07-30 14:41:22 -07003932# check for sizeof without parenthesis
3933 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003934 if (WARN("SIZEOF_PARENTHESIS",
3935 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3936 $fix) {
3937 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3938 }
Joe Perches66c80b62012-07-30 14:41:22 -07003939 }
3940
Joe Perches428e2fd2011-05-24 17:13:39 -07003941# check for line continuations in quoted strings with odd counts of "
3942 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003943 WARN("LINE_CONTINUATIONS",
3944 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003945 }
3946
Joe Perches88982fe2012-12-17 16:02:00 -08003947# check for struct spinlock declarations
3948 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3949 WARN("USE_SPINLOCK_T",
3950 "struct spinlock should be spinlock_t\n" . $herecurr);
3951 }
3952
Joe Perchesa6962d72013-04-29 16:18:13 -07003953# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08003954 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07003955 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08003956 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003957 if (WARN("PREFER_SEQ_PUTS",
3958 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3959 $fix) {
3960 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3961 }
Joe Perchesa6962d72013-04-29 16:18:13 -07003962 }
3963 }
3964
Andy Whitcroft554e1652012-01-10 15:09:57 -08003965# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003966 if ($^V && $^V ge 5.10.0 &&
3967 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003968 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003969
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003970 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003971 my $ms_val = $7;
3972 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003973
Andy Whitcroft554e1652012-01-10 15:09:57 -08003974 if ($ms_size =~ /^(0x|)0$/i) {
3975 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003976 "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 -08003977 } elsif ($ms_size =~ /^(0x|)1$/i) {
3978 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003979 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3980 }
3981 }
3982
3983# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003984 if ($^V && $^V ge 5.10.0 &&
3985 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003986 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003987 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003988 my $call = $1;
3989 my $cast1 = deparenthesize($2);
3990 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003991 my $cast2 = deparenthesize($7);
3992 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003993 my $cast;
3994
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003995 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003996 $cast = "$cast1 or $cast2";
3997 } elsif ($cast1 ne "") {
3998 $cast = $cast1;
3999 } else {
4000 $cast = $cast2;
4001 }
4002 WARN("MINMAX",
4003 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004004 }
4005 }
4006
Joe Perches4a273192012-07-30 14:41:20 -07004007# check usleep_range arguments
4008 if ($^V && $^V ge 5.10.0 &&
4009 defined $stat &&
4010 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4011 my $min = $1;
4012 my $max = $7;
4013 if ($min eq $max) {
4014 WARN("USLEEP_RANGE",
4015 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4016 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4017 $min > $max) {
4018 WARN("USLEEP_RANGE",
4019 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4020 }
4021 }
4022
Joe Perches823b7942013-11-12 15:10:15 -08004023# check for naked sscanf
4024 if ($^V && $^V ge 5.10.0 &&
4025 defined $stat &&
4026 $stat =~ /\bsscanf\b/ &&
4027 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4028 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4029 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4030 my $lc = $stat =~ tr@\n@@;
4031 $lc = $lc + $linenr;
4032 my $stat_real = raw_line($linenr, 0);
4033 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4034 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4035 }
4036 WARN("NAKED_SSCANF",
4037 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4038 }
4039
Joe Perches70dc8a42013-09-11 14:23:58 -07004040# check for new externs in .h files.
4041 if ($realfile =~ /\.h$/ &&
4042 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004043 if (CHK("AVOID_EXTERNS",
4044 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004045 $fix) {
4046 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4047 }
4048 }
4049
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004050# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004051 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004052 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004053 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004054 my $function_name = $1;
4055 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004056
4057 my $s = $stat;
4058 if (defined $cond) {
4059 substr($s, 0, length($cond), '');
4060 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004061 if ($s =~ /^\s*;/ &&
4062 $function_name ne 'uninitialized_var')
4063 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004064 WARN("AVOID_EXTERNS",
4065 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004066 }
4067
4068 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004069 WARN("FUNCTION_ARGUMENTS",
4070 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004071 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004072
4073 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4074 $stat =~ /^.\s*extern\s+/)
4075 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004076 WARN("AVOID_EXTERNS",
4077 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004078 }
4079
4080# checks for new __setup's
4081 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4082 my $name = $1;
4083
4084 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004085 CHK("UNDOCUMENTED_SETUP",
4086 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004087 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004088 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004089
4090# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004091 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004092 WARN("UNNECESSARY_CASTS",
4093 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004094 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004095
Joe Perchesa640d252013-07-03 15:05:21 -07004096# alloc style
4097# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4098 if ($^V && $^V ge 5.10.0 &&
4099 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4100 CHK("ALLOC_SIZEOF_STRUCT",
4101 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4102 }
4103
Joe Perches972fdea2013-04-29 16:18:12 -07004104# check for krealloc arg reuse
4105 if ($^V && $^V ge 5.10.0 &&
4106 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4107 WARN("KREALLOC_ARG_REUSE",
4108 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4109 }
4110
Joe Perches5ce59ae2013-02-21 16:44:18 -08004111# check for alloc argument mismatch
4112 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4113 WARN("ALLOC_ARRAY_ARGS",
4114 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4115 }
4116
Joe Perchescaf2a542011-01-12 16:59:56 -08004117# check for multiple semicolons
4118 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004119 if (WARN("ONE_SEMICOLON",
4120 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4121 $fix) {
4122 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4123 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004124 }
4125
4126# check for switch/default statements without a break;
4127 if ($^V && $^V ge 5.10.0 &&
4128 defined $stat &&
4129 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4130 my $ctx = '';
4131 my $herectx = $here . "\n";
4132 my $cnt = statement_rawlines($stat);
4133 for (my $n = 0; $n < $cnt; $n++) {
4134 $herectx .= raw_line($linenr, $n) . "\n";
4135 }
4136 WARN("DEFAULT_NO_BREAK",
4137 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004138 }
4139
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004140# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004141 if ($line =~ /\b__FUNCTION__\b/) {
4142 if (WARN("USE_FUNC",
4143 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4144 $fix) {
4145 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4146 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004147 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004148
Joe Perches2c924882012-03-23 15:02:20 -07004149# check for use of yield()
4150 if ($line =~ /\byield\s*\(\s*\)/) {
4151 WARN("YIELD",
4152 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4153 }
4154
Joe Perches179f8f42013-07-03 15:05:30 -07004155# check for comparisons against true and false
4156 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4157 my $lead = $1;
4158 my $arg = $2;
4159 my $test = $3;
4160 my $otype = $4;
4161 my $trail = $5;
4162 my $op = "!";
4163
4164 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4165
4166 my $type = lc($otype);
4167 if ($type =~ /^(?:true|false)$/) {
4168 if (("$test" eq "==" && "$type" eq "true") ||
4169 ("$test" eq "!=" && "$type" eq "false")) {
4170 $op = "";
4171 }
4172
4173 CHK("BOOL_COMPARISON",
4174 "Using comparison to $otype is error prone\n" . $herecurr);
4175
4176## maybe suggesting a correct construct would better
4177## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4178
4179 }
4180 }
4181
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004182# check for semaphores initialized locked
4183 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004184 WARN("CONSIDER_COMPLETION",
4185 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004186 }
Joe Perches6712d852012-03-23 15:02:20 -07004187
Joe Perches67d0a072011-10-31 17:13:10 -07004188# recommend kstrto* over simple_strto* and strict_strto*
4189 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004190 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004191 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004192 }
Joe Perches6712d852012-03-23 15:02:20 -07004193
Michael Ellermanf3db6632008-07-23 21:28:57 -07004194# check for __initcall(), use device_initcall() explicitly please
4195 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004196 WARN("USE_DEVICE_INITCALL",
4197 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07004198 }
Joe Perches6712d852012-03-23 15:02:20 -07004199
Emese Revfy79404842010-03-05 13:43:53 -08004200# check for various ops structs, ensure they are const.
4201 my $struct_ops = qr{acpi_dock_ops|
4202 address_space_operations|
4203 backlight_ops|
4204 block_device_operations|
4205 dentry_operations|
4206 dev_pm_ops|
4207 dma_map_ops|
4208 extent_io_ops|
4209 file_lock_operations|
4210 file_operations|
4211 hv_ops|
4212 ide_dma_ops|
4213 intel_dvo_dev_ops|
4214 item_operations|
4215 iwl_ops|
4216 kgdb_arch|
4217 kgdb_io|
4218 kset_uevent_ops|
4219 lock_manager_operations|
4220 microcode_ops|
4221 mtrr_ops|
4222 neigh_ops|
4223 nlmsvc_binding|
4224 pci_raw_ops|
4225 pipe_buf_operations|
4226 platform_hibernation_ops|
4227 platform_suspend_ops|
4228 proto_ops|
4229 rpc_pipe_ops|
4230 seq_operations|
4231 snd_ac97_build_ops|
4232 soc_pcmcia_socket_ops|
4233 stacktrace_ops|
4234 sysfs_ops|
4235 tty_operations|
4236 usb_mon_operations|
4237 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004238 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004239 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004240 WARN("CONST_STRUCT",
4241 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004242 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004243 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004244
4245# use of NR_CPUS is usually wrong
4246# ignore definitions of NR_CPUS and usage to define arrays as likely right
4247 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004248 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4249 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004250 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4251 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4252 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004253 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004254 WARN("NR_CPUS",
4255 "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 -07004256 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004257
Joe Perches52ea8502013-11-12 15:10:09 -08004258# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4259 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4260 ERROR("DEFINE_ARCH_HAS",
4261 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4262 }
4263
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004264# check for %L{u,d,i} in strings
4265 my $string;
4266 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4267 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004268 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004269 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004270 WARN("PRINTF_L",
4271 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004272 last;
4273 }
4274 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004275
4276# whine mightly about in_atomic
4277 if ($line =~ /\bin_atomic\s*\(/) {
4278 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004279 ERROR("IN_ATOMIC",
4280 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004281 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004282 WARN("IN_ATOMIC",
4283 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004284 }
4285 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004286
4287# check for lockdep_set_novalidate_class
4288 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4289 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4290 if ($realfile !~ m@^kernel/lockdep@ &&
4291 $realfile !~ m@^include/linux/lockdep@ &&
4292 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004293 ERROR("LOCKDEP",
4294 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004295 }
4296 }
Dave Jones88f88312011-01-12 16:59:59 -08004297
4298 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4299 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004300 WARN("EXPORTED_WORLD_WRITABLE",
4301 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004302 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004303 }
4304
4305 # If we have no input at all, then there is nothing to report on
4306 # so just keep quiet.
4307 if ($#rawlines == -1) {
4308 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004309 }
4310
Andy Whitcroft8905a672007-11-28 16:21:06 -08004311 # In mailback mode only produce a report in the negative, for
4312 # things that appear to be patches.
4313 if ($mailback && ($clean == 1 || !$is_patch)) {
4314 exit(0);
4315 }
4316
4317 # This is not a patch, and we are are in 'no-patch' mode so
4318 # just keep quiet.
4319 if (!$chk_patch && !$is_patch) {
4320 exit(0);
4321 }
4322
4323 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004324 ERROR("NOT_UNIFIED_DIFF",
4325 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004326 }
4327 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004328 ERROR("MISSING_SIGN_OFF",
4329 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004330 }
4331
Andy Whitcroft8905a672007-11-28 16:21:06 -08004332 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004333 if ($summary && !($clean == 1 && $quiet == 1)) {
4334 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004335 print "total: $cnt_error errors, $cnt_warn warnings, " .
4336 (($check)? "$cnt_chk checks, " : "") .
4337 "$cnt_lines lines checked\n";
4338 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004339 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004340
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004341 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004342
4343 if ($^V lt 5.10.0) {
4344 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4345 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4346 }
4347
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004348 # If there were whitespace errors which cleanpatch can fix
4349 # then suggest that.
4350 if ($rpt_cleaners) {
4351 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4352 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004353 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004354 }
4355 }
4356
Joe Perches91bfe482013-09-11 14:23:59 -07004357 hash_show_words(\%use_type, "Used");
4358 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07004359
Joe Perches3705ce52013-07-03 15:05:31 -07004360 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4361 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4362 my $linecount = 0;
4363 my $f;
4364
4365 open($f, '>', $newfile)
4366 or die "$P: Can't open $newfile for write\n";
4367 foreach my $fixed_line (@fixed) {
4368 $linecount++;
4369 if ($file) {
4370 if ($linecount > 3) {
4371 $fixed_line =~ s/^\+//;
4372 print $f $fixed_line. "\n";
4373 }
4374 } else {
4375 print $f $fixed_line . "\n";
4376 }
4377 }
4378 close($f);
4379
4380 if (!$quiet) {
4381 print << "EOM";
4382Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4383
4384Do _NOT_ trust the results written to this file.
4385Do _NOT_ submit these changes without inspecting them for correctness.
4386
4387This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4388No warranties, expressed or implied...
4389
4390EOM
4391 }
4392 }
4393
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004394 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004395 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004396 }
4397 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004398 print << "EOM";
4399$vname has style problems, please review.
4400
4401If any of these errors are false positives, please report
4402them to the maintainer, see CHECKPATCH in MAINTAINERS.
4403EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004404 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004405
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004406 return $clean;
4407}