blob: 9185883f5885b9c24d939781288534a67483fc85 [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 Perches000d1cc12011-07-25 17:13:25 -070034my %ignore_type = ();
Joe Perches34456862013-07-03 15:05:34 -070035my %camelcase = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070036my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070037my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070038my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080039my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070040my $ignore_perl_version = 0;
41my $minimum_perl_version = 5.10.0;
Hannes Eder77f5b102009-09-21 17:04:37 -070042
43sub help {
44 my ($exitcode) = @_;
45
46 print << "EOM";
47Usage: $P [OPTION]... [FILE]...
48Version: $V
49
50Options:
51 -q, --quiet quiet
52 --no-tree run without a kernel tree
53 --no-signoff do not check for 'Signed-off-by' line
54 --patch treat FILE as patchfile (default)
55 --emacs emacs compile window format
56 --terse one line per report
57 -f, --file treat FILE as regular source file
58 --subjective, --strict enable more subjective tests
Joe Perches000d1cc12011-07-25 17:13:25 -070059 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080060 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070061 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070062 --root=PATH PATH to the kernel tree root
63 --no-summary suppress the per-file summary
64 --mailback only produce a report in case of warnings/errors
65 --summary-file include the filename in summary
66 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
67 'values', 'possible', 'type', and 'attr' (default
68 is all off)
69 --test-only=WORD report only warnings/errors containing WORD
70 literally
Joe Perches3705ce52013-07-03 15:05:31 -070071 --fix EXPERIMENTAL - may create horrible results
72 If correctable single-line errors exist, create
73 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
74 with potential errors corrected to the preferred
75 checkpatch style
Dave Hansend62a2012013-09-11 14:23:56 -070076 --ignore-perl-version override checking of perl version. expect
77 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070078 -h, --help, --version display this help and exit
79
80When FILE is - read standard input.
81EOM
82
83 exit($exitcode);
84}
85
Joe Perches000d1cc12011-07-25 17:13:25 -070086my $conf = which_conf($configuration_file);
87if (-f $conf) {
88 my @conf_args;
89 open(my $conffile, '<', "$conf")
90 or warn "$P: Can't find a readable $configuration_file file $!\n";
91
92 while (<$conffile>) {
93 my $line = $_;
94
95 $line =~ s/\s*\n?$//g;
96 $line =~ s/^\s*//g;
97 $line =~ s/\s+/ /g;
98
99 next if ($line =~ m/^\s*#/);
100 next if ($line =~ m/^\s*$/);
101
102 my @words = split(" ", $line);
103 foreach my $word (@words) {
104 last if ($word =~ m/^#/);
105 push (@conf_args, $word);
106 }
107 }
108 close($conffile);
109 unshift(@ARGV, @conf_args) if @conf_args;
110}
111
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700112GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700114 'tree!' => \$tree,
115 'signoff!' => \$chk_signoff,
116 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700117 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800118 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700119 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700120 'subjective!' => \$check,
121 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700122 'ignore=s' => \@ignore,
123 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800124 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700125 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800126 'summary!' => \$summary,
127 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800128 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700129 'fix!' => \$fix,
Dave Hansend62a2012013-09-11 14:23:56 -0700130 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800131 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700132 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700133 'h|help' => \$help,
134 'version' => \$help
135) or help(1);
136
137help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700138
139my $exit = 0;
140
Dave Hansend62a2012013-09-11 14:23:56 -0700141if ($^V && $^V lt $minimum_perl_version) {
142 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
143 if (!$ignore_perl_version) {
144 exit(1);
145 }
146}
147
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700148if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700149 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700150 exit(1);
151}
152
Joe Perches000d1cc12011-07-25 17:13:25 -0700153@ignore = split(/,/, join(',',@ignore));
154foreach my $word (@ignore) {
155 $word =~ s/\s*\n?$//g;
156 $word =~ s/^\s*//g;
157 $word =~ s/\s+/ /g;
158 $word =~ tr/[a-z]/[A-Z]/;
159
160 next if ($word =~ m/^\s*#/);
161 next if ($word =~ m/^\s*$/);
162
163 $ignore_type{$word}++;
164}
165
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800166my $dbg_values = 0;
167my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700168my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700169my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800170for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800171 ## no critic
172 eval "\${dbg_$key} = '$debug{$key}';";
173 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800174}
175
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700176my $rpt_cleaners = 0;
177
Andy Whitcroft8905a672007-11-28 16:21:06 -0800178if ($terse) {
179 $emacs = 1;
180 $quiet++;
181}
182
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700183if ($tree) {
184 if (defined $root) {
185 if (!top_of_kernel_tree($root)) {
186 die "$P: $root: --root does not point at a valid tree\n";
187 }
188 } else {
189 if (top_of_kernel_tree('.')) {
190 $root = '.';
191 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
192 top_of_kernel_tree($1)) {
193 $root = $1;
194 }
195 }
196
197 if (!defined $root) {
198 print "Must be run from the top-level dir. of a kernel tree\n";
199 exit(2);
200 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700201}
202
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203my $emitted_corrupt = 0;
204
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700205our $Ident = qr{
206 [A-Za-z_][A-Za-z\d_]*
207 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
208 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700209our $Storage = qr{extern|static|asmlinkage};
210our $Sparse = qr{
211 __user|
212 __kernel|
213 __force|
214 __iomem|
215 __must_check|
216 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800217 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700218 __ref|
219 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700220 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800221
222# Notes to $Attribute:
223# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700224our $Attribute = qr{
225 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700226 __percpu|
227 __nocast|
228 __safe|
229 __bitwise__|
230 __packed__|
231 __packed2__|
232 __naked|
233 __maybe_unused|
234 __always_unused|
235 __noreturn|
236 __used|
237 __cold|
238 __noclone|
239 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700240 __read_mostly|
241 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800242 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700243 ____cacheline_aligned|
244 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800245 ____cacheline_internodealigned_in_smp|
246 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700247 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700248our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700249our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700250our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
251our $Lval = qr{$Ident(?:$Member)*};
252
Joe Perches95e2c602013-07-03 15:05:20 -0700253our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
254our $Binary = qr{(?i)0b[01]+$Int_type?};
255our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
256our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800257our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
258our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
259our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800260our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700261our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800262our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800263our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700264our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265our $Operators = qr{
266 <=|>=|==|!=|
267 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700268 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700269 }x;
270
Andy Whitcroft8905a672007-11-28 16:21:06 -0800271our $NonptrType;
272our $Type;
273our $Declare;
274
Joe Perches15662b32011-10-31 17:13:12 -0700275our $NON_ASCII_UTF8 = qr{
276 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700277 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
278 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
279 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
280 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
281 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
282 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
283}x;
284
Joe Perches15662b32011-10-31 17:13:12 -0700285our $UTF8 = qr{
286 [\x09\x0A\x0D\x20-\x7E] # ASCII
287 | $NON_ASCII_UTF8
288}x;
289
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700290our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700291 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700292 atomic_t
293)};
294
Joe Perches691e6692010-03-05 13:43:51 -0800295our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700296 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700297 (?:[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 -0700298 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700299 panic|
300 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800301)};
302
Joe Perches20112472011-07-25 17:13:23 -0700303our $signature_tags = qr{(?xi:
304 Signed-off-by:|
305 Acked-by:|
306 Tested-by:|
307 Reviewed-by:|
308 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700309 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700310 To:|
311 Cc:
312)};
313
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314our @typeList = (
315 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700316 qr{(?:unsigned\s+)?char},
317 qr{(?:unsigned\s+)?short},
318 qr{(?:unsigned\s+)?int},
319 qr{(?:unsigned\s+)?long},
320 qr{(?:unsigned\s+)?long\s+int},
321 qr{(?:unsigned\s+)?long\s+long},
322 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800323 qr{unsigned},
324 qr{float},
325 qr{double},
326 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800327 qr{struct\s+$Ident},
328 qr{union\s+$Ident},
329 qr{enum\s+$Ident},
330 qr{${Ident}_t},
331 qr{${Ident}_handler},
332 qr{${Ident}_handler_fn},
333);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700334our @modifierList = (
335 qr{fastcall},
336);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800337
Wolfram Sang7840a942010-08-09 17:20:57 -0700338our $allowed_asm_includes = qr{(?x:
339 irq|
340 memory
341)};
342# memory.h: ARM has a custom one
343
Andy Whitcroft8905a672007-11-28 16:21:06 -0800344sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700345 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
346 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700347 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800348 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700349 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800350 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800351 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700352 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700353 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800354 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700355 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800356 }x;
357 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700358 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700359 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700360 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800361 }x;
362 $Declare = qr{(?:$Storage\s+)?$Type};
363}
364build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365
Joe Perches7d2367a2011-07-25 17:13:22 -0700366our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700367
368# Using $balanced_parens, $LvalOrFunc, or $FuncArg
369# requires at least perl version v5.10.0
370# Any use must be runtime checked with $^V
371
372our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
373our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800374our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700375
376sub deparenthesize {
377 my ($string) = @_;
378 return "" if (!defined($string));
379 $string =~ s@^\s*\(\s*@@g;
380 $string =~ s@\s*\)\s*$@@g;
381 $string =~ s@\s+@ @g;
382 return $string;
383}
384
Joe Perches34456862013-07-03 15:05:34 -0700385sub seed_camelcase_file {
386 my ($file) = @_;
387
388 return if (!(-f $file));
389
390 local $/;
391
392 open(my $include_file, '<', "$file")
393 or warn "$P: Can't read '$file' $!\n";
394 my $text = <$include_file>;
395 close($include_file);
396
397 my @lines = split('\n', $text);
398
399 foreach my $line (@lines) {
400 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
401 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
402 $camelcase{$1} = 1;
403 }
404 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
405 $camelcase{$1} = 1;
406 }
407 }
408}
409
410my $camelcase_seeded = 0;
411sub seed_camelcase_includes {
412 return if ($camelcase_seeded);
413
414 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700415 my $camelcase_cache = "";
416 my @include_files = ();
417
418 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700419
Joe Perches34456862013-07-03 15:05:34 -0700420 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700421 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
422 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700423 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700424 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700425 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700426 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700427 @include_files = split('\n', $files);
428 foreach my $file (@include_files) {
429 my $date = POSIX::strftime("%Y%m%d%H%M",
430 localtime((stat $file)[9]));
431 $last_mod_date = $date if ($last_mod_date < $date);
432 }
433 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700434 }
Joe Perchesc707a812013-07-08 16:00:43 -0700435
436 if ($camelcase_cache ne "" && -f $camelcase_cache) {
437 open(my $camelcase_file, '<', "$camelcase_cache")
438 or warn "$P: Can't read '$camelcase_cache' $!\n";
439 while (<$camelcase_file>) {
440 chomp;
441 $camelcase{$_} = 1;
442 }
443 close($camelcase_file);
444
445 return;
446 }
447
448 if (-d ".git") {
449 $files = `git ls-files "include/*.h"`;
450 @include_files = split('\n', $files);
451 }
452
Joe Perches34456862013-07-03 15:05:34 -0700453 foreach my $file (@include_files) {
454 seed_camelcase_file($file);
455 }
Joe Perches351b2a12013-07-03 15:05:36 -0700456
Joe Perchesc707a812013-07-08 16:00:43 -0700457 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700458 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700459 open(my $camelcase_file, '>', "$camelcase_cache")
460 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700461 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
462 print $camelcase_file ("$_\n");
463 }
464 close($camelcase_file);
465 }
Joe Perches34456862013-07-03 15:05:34 -0700466}
467
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700468$chk_signoff = 0 if ($file);
469
Andy Whitcroft00df3442007-06-08 13:47:06 -0700470my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800471my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700472my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800473my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700474for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800475 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700476 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800477 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700478 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800479 } elsif ($filename eq '-') {
480 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700481 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800482 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700483 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700484 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800485 if ($filename eq '-') {
486 $vname = 'Your patch';
487 } else {
488 $vname = $filename;
489 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800490 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700491 chomp;
492 push(@rawlines, $_);
493 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800494 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800495 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700496 $exit = 1;
497 }
498 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800499 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700500 @fixed = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700501}
502
503exit($exit);
504
505sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700506 my ($root) = @_;
507
508 my @tree_check = (
509 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
510 "README", "Documentation", "arch", "include", "drivers",
511 "fs", "init", "ipc", "kernel", "lib", "scripts",
512 );
513
514 foreach my $check (@tree_check) {
515 if (! -e $root . '/' . $check) {
516 return 0;
517 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700518 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700519 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700520}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700521
Joe Perches20112472011-07-25 17:13:23 -0700522sub parse_email {
523 my ($formatted_email) = @_;
524
525 my $name = "";
526 my $address = "";
527 my $comment = "";
528
529 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
530 $name = $1;
531 $address = $2;
532 $comment = $3 if defined $3;
533 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
534 $address = $1;
535 $comment = $2 if defined $2;
536 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
537 $address = $1;
538 $comment = $2 if defined $2;
539 $formatted_email =~ s/$address.*$//;
540 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700541 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700542 $name =~ s/^\"|\"$//g;
543 # If there's a name left after stripping spaces and
544 # leading quotes, and the address doesn't have both
545 # leading and trailing angle brackets, the address
546 # is invalid. ie:
547 # "joe smith joe@smith.com" bad
548 # "joe smith <joe@smith.com" bad
549 if ($name ne "" && $address !~ /^<[^>]+>$/) {
550 $name = "";
551 $address = "";
552 $comment = "";
553 }
554 }
555
Joe Perches3705ce52013-07-03 15:05:31 -0700556 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700557 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700558 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700559 $address =~ s/^\<|\>$//g;
560
561 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
562 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
563 $name = "\"$name\"";
564 }
565
566 return ($name, $address, $comment);
567}
568
569sub format_email {
570 my ($name, $address) = @_;
571
572 my $formatted_email;
573
Joe Perches3705ce52013-07-03 15:05:31 -0700574 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700575 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700576 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700577
578 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
579 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
580 $name = "\"$name\"";
581 }
582
583 if ("$name" eq "") {
584 $formatted_email = "$address";
585 } else {
586 $formatted_email = "$name <$address>";
587 }
588
589 return $formatted_email;
590}
591
Joe Perches000d1cc12011-07-25 17:13:25 -0700592sub which_conf {
593 my ($conf) = @_;
594
595 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
596 if (-e "$path/$conf") {
597 return "$path/$conf";
598 }
599 }
600
601 return "";
602}
603
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700604sub expand_tabs {
605 my ($str) = @_;
606
607 my $res = '';
608 my $n = 0;
609 for my $c (split(//, $str)) {
610 if ($c eq "\t") {
611 $res .= ' ';
612 $n++;
613 for (; ($n % 8) != 0; $n++) {
614 $res .= ' ';
615 }
616 next;
617 }
618 $res .= $c;
619 $n++;
620 }
621
622 return $res;
623}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700624sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700625 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700626 return $res;
627}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700628
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700629sub line_stats {
630 my ($line) = @_;
631
632 # Drop the diff line leader and expand tabs
633 $line =~ s/^.//;
634 $line = expand_tabs($line);
635
636 # Pick the indent from the front of the line.
637 my ($white) = ($line =~ /^(\s*)/);
638
639 return (length($line), length($white));
640}
641
Andy Whitcroft773647a2008-03-28 14:15:58 -0700642my $sanitise_quote = '';
643
644sub sanitise_line_reset {
645 my ($in_comment) = @_;
646
647 if ($in_comment) {
648 $sanitise_quote = '*/';
649 } else {
650 $sanitise_quote = '';
651 }
652}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700653sub sanitise_line {
654 my ($line) = @_;
655
656 my $res = '';
657 my $l = '';
658
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800659 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700660 my $off = 0;
661 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700662
Andy Whitcroft773647a2008-03-28 14:15:58 -0700663 # Always copy over the diff marker.
664 $res = substr($line, 0, 1);
665
666 for ($off = 1; $off < length($line); $off++) {
667 $c = substr($line, $off, 1);
668
669 # Comments we are wacking completly including the begin
670 # and end, all to $;.
671 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
672 $sanitise_quote = '*/';
673
674 substr($res, $off, 2, "$;$;");
675 $off++;
676 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800677 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700678 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700679 $sanitise_quote = '';
680 substr($res, $off, 2, "$;$;");
681 $off++;
682 next;
683 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700684 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
685 $sanitise_quote = '//';
686
687 substr($res, $off, 2, $sanitise_quote);
688 $off++;
689 next;
690 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700691
692 # A \ in a string means ignore the next character.
693 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
694 $c eq "\\") {
695 substr($res, $off, 2, 'XX');
696 $off++;
697 next;
698 }
699 # Regular quotes.
700 if ($c eq "'" || $c eq '"') {
701 if ($sanitise_quote eq '') {
702 $sanitise_quote = $c;
703
704 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700705 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700706 } elsif ($sanitise_quote eq $c) {
707 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700708 }
709 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700710
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800711 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700712 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
713 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700714 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
715 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700716 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
717 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700718 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700719 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700720 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800721 }
722
Daniel Walker113f04a2009-09-21 17:04:35 -0700723 if ($sanitise_quote eq '//') {
724 $sanitise_quote = '';
725 }
726
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800727 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700728 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800729 my $clean = 'X' x length($1);
730 $res =~ s@\<.*\>@<$clean>@;
731
732 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700733 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800734 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700735 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800736 }
737
Andy Whitcroft00df3442007-06-08 13:47:06 -0700738 return $res;
739}
740
Joe Perchesa6962d72013-04-29 16:18:13 -0700741sub get_quoted_string {
742 my ($line, $rawline) = @_;
743
744 return "" if ($line !~ m/(\"[X]+\")/g);
745 return substr($rawline, $-[0], $+[0] - $-[0]);
746}
747
Andy Whitcroft8905a672007-11-28 16:21:06 -0800748sub ctx_statement_block {
749 my ($linenr, $remain, $off) = @_;
750 my $line = $linenr - 1;
751 my $blk = '';
752 my $soff = $off;
753 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700754 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800755
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800756 my $loff = 0;
757
Andy Whitcroft8905a672007-11-28 16:21:06 -0800758 my $type = '';
759 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800760 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800761 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800762 my $c;
763 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800764
765 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800766 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800767 @stack = (['', 0]) if ($#stack == -1);
768
Andy Whitcroft773647a2008-03-28 14:15:58 -0700769 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800770 # If we are about to drop off the end, pull in more
771 # context.
772 if ($off >= $len) {
773 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700774 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800775 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800776 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800777 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800778 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800779 $len = length($blk);
780 $line++;
781 last;
782 }
783 # Bail if there is no further context.
784 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800785 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800786 last;
787 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800788 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
789 $level++;
790 $type = '#';
791 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800792 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800793 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800794 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800795 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800796
Andy Whitcroft773647a2008-03-28 14:15:58 -0700797 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800798
799 # Handle nested #if/#else.
800 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
801 push(@stack, [ $type, $level ]);
802 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
803 ($type, $level) = @{$stack[$#stack - 1]};
804 } elsif ($remainder =~ /^#\s*endif\b/) {
805 ($type, $level) = @{pop(@stack)};
806 }
807
Andy Whitcroft8905a672007-11-28 16:21:06 -0800808 # Statement ends at the ';' or a close '}' at the
809 # outermost level.
810 if ($level == 0 && $c eq ';') {
811 last;
812 }
813
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800814 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700815 if ($level == 0 && $coff_set == 0 &&
816 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
817 $remainder =~ /^(else)(?:\s|{)/ &&
818 $remainder !~ /^else\s+if\b/) {
819 $coff = $off + length($1) - 1;
820 $coff_set = 1;
821 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
822 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800823 }
824
Andy Whitcroft8905a672007-11-28 16:21:06 -0800825 if (($type eq '' || $type eq '(') && $c eq '(') {
826 $level++;
827 $type = '(';
828 }
829 if ($type eq '(' && $c eq ')') {
830 $level--;
831 $type = ($level != 0)? '(' : '';
832
833 if ($level == 0 && $coff < $soff) {
834 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700835 $coff_set = 1;
836 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800837 }
838 }
839 if (($type eq '' || $type eq '{') && $c eq '{') {
840 $level++;
841 $type = '{';
842 }
843 if ($type eq '{' && $c eq '}') {
844 $level--;
845 $type = ($level != 0)? '{' : '';
846
847 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700848 if (substr($blk, $off + 1, 1) eq ';') {
849 $off++;
850 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800851 last;
852 }
853 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800854 # Preprocessor commands end at the newline unless escaped.
855 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
856 $level--;
857 $type = '';
858 $off++;
859 last;
860 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800861 $off++;
862 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700863 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800864 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700865 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800866 $line++;
867 $remain--;
868 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800869
870 my $statement = substr($blk, $soff, $off - $soff + 1);
871 my $condition = substr($blk, $soff, $coff - $soff + 1);
872
873 #warn "STATEMENT<$statement>\n";
874 #warn "CONDITION<$condition>\n";
875
Andy Whitcroft773647a2008-03-28 14:15:58 -0700876 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800877
878 return ($statement, $condition,
879 $line, $remain + 1, $off - $loff + 1, $level);
880}
881
Andy Whitcroftcf655042008-03-04 14:28:20 -0800882sub statement_lines {
883 my ($stmt) = @_;
884
885 # Strip the diff line prefixes and rip blank lines at start and end.
886 $stmt =~ s/(^|\n)./$1/g;
887 $stmt =~ s/^\s*//;
888 $stmt =~ s/\s*$//;
889
890 my @stmt_lines = ($stmt =~ /\n/g);
891
892 return $#stmt_lines + 2;
893}
894
895sub statement_rawlines {
896 my ($stmt) = @_;
897
898 my @stmt_lines = ($stmt =~ /\n/g);
899
900 return $#stmt_lines + 2;
901}
902
903sub statement_block_size {
904 my ($stmt) = @_;
905
906 $stmt =~ s/(^|\n)./$1/g;
907 $stmt =~ s/^\s*{//;
908 $stmt =~ s/}\s*$//;
909 $stmt =~ s/^\s*//;
910 $stmt =~ s/\s*$//;
911
912 my @stmt_lines = ($stmt =~ /\n/g);
913 my @stmt_statements = ($stmt =~ /;/g);
914
915 my $stmt_lines = $#stmt_lines + 2;
916 my $stmt_statements = $#stmt_statements + 1;
917
918 if ($stmt_lines > $stmt_statements) {
919 return $stmt_lines;
920 } else {
921 return $stmt_statements;
922 }
923}
924
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800925sub ctx_statement_full {
926 my ($linenr, $remain, $off) = @_;
927 my ($statement, $condition, $level);
928
929 my (@chunks);
930
Andy Whitcroftcf655042008-03-04 14:28:20 -0800931 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800932 ($statement, $condition, $linenr, $remain, $off, $level) =
933 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700934 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800935 push(@chunks, [ $condition, $statement ]);
936 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
937 return ($level, $linenr, @chunks);
938 }
939
940 # Pull in the following conditional/block pairs and see if they
941 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800942 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800943 ($statement, $condition, $linenr, $remain, $off, $level) =
944 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800945 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700946 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800947 #print "C: push\n";
948 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800949 }
950
951 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800952}
953
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700954sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700955 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700956 my $line;
957 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700958 my $blk = '';
959 my @o;
960 my @c;
961 my @res = ();
962
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700963 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800964 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700965 for ($line = $start; $remain > 0; $line++) {
966 next if ($rawlines[$line] =~ /^-/);
967 $remain--;
968
969 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800970
971 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700972 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800973 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700974 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800975 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700976 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800977 $level = pop(@stack);
978 }
979
Andy Whitcroft01464f32010-10-26 14:23:19 -0700980 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700981 ##print "C<$c>L<$level><$open$close>O<$off>\n";
982 if ($off > 0) {
983 $off--;
984 next;
985 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700986
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700987 if ($c eq $close && $level > 0) {
988 $level--;
989 last if ($level == 0);
990 } elsif ($c eq $open) {
991 $level++;
992 }
993 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700994
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700995 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700996 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700997 }
998
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700999 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001000 }
1001
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001002 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001003}
1004sub ctx_block_outer {
1005 my ($linenr, $remain) = @_;
1006
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001007 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1008 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001009}
1010sub ctx_block {
1011 my ($linenr, $remain) = @_;
1012
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001013 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1014 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001015}
1016sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001017 my ($linenr, $remain, $off) = @_;
1018
1019 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1020 return @r;
1021}
1022sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001023 my ($linenr, $remain) = @_;
1024
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001025 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001026}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001027sub ctx_statement_level {
1028 my ($linenr, $remain, $off) = @_;
1029
1030 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1031}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001032
1033sub ctx_locate_comment {
1034 my ($first_line, $end_line) = @_;
1035
1036 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001037 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001038 return $current_comment if (defined $current_comment);
1039
1040 # Look through the context and try and figure out if there is a
1041 # comment.
1042 my $in_comment = 0;
1043 $current_comment = '';
1044 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001045 my $line = $rawlines[$linenr - 1];
1046 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001047 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1048 $in_comment = 1;
1049 }
1050 if ($line =~ m@/\*@) {
1051 $in_comment = 1;
1052 }
1053 if (!$in_comment && $current_comment ne '') {
1054 $current_comment = '';
1055 }
1056 $current_comment .= $line . "\n" if ($in_comment);
1057 if ($line =~ m@\*/@) {
1058 $in_comment = 0;
1059 }
1060 }
1061
1062 chomp($current_comment);
1063 return($current_comment);
1064}
1065sub ctx_has_comment {
1066 my ($first_line, $end_line) = @_;
1067 my $cmt = ctx_locate_comment($first_line, $end_line);
1068
Andy Whitcroft00df3442007-06-08 13:47:06 -07001069 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001070 ##print "CMMT: $cmt\n";
1071
1072 return ($cmt ne '');
1073}
1074
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001075sub raw_line {
1076 my ($linenr, $cnt) = @_;
1077
1078 my $offset = $linenr - 1;
1079 $cnt++;
1080
1081 my $line;
1082 while ($cnt) {
1083 $line = $rawlines[$offset++];
1084 next if (defined($line) && $line =~ /^-/);
1085 $cnt--;
1086 }
1087
1088 return $line;
1089}
1090
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001091sub cat_vet {
1092 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001093 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001094
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001095 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001096 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1097 $res .= $1;
1098 if ($2 ne '') {
1099 $coded = sprintf("^%c", unpack('C', $2) + 64);
1100 $res .= $coded;
1101 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001102 }
1103 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001104
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001105 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001106}
1107
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001108my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001109my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001110my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001111my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001112
1113sub annotate_reset {
1114 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001115 $av_pending = '_';
1116 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001117 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001118}
1119
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001120sub annotate_values {
1121 my ($stream, $type) = @_;
1122
1123 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001124 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125 my $cur = $stream;
1126
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001127 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001128
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001129 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001130 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001131 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001132 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001133 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001134 print "WS($1)\n" if ($dbg_values > 1);
1135 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001136 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001137 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001138 }
1139
Florian Micklerc023e4732011-01-12 16:59:58 -08001140 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001141 print "CAST($1)\n" if ($dbg_values > 1);
1142 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001143 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001144
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001145 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001146 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001147 $type = 'T';
1148
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001149 } elsif ($cur =~ /^($Modifier)\s*/) {
1150 print "MODIFIER($1)\n" if ($dbg_values > 1);
1151 $type = 'T';
1152
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001153 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001154 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001155 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001156 push(@av_paren_type, $type);
1157 if ($2 ne '') {
1158 $av_pending = 'N';
1159 }
1160 $type = 'E';
1161
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001162 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001163 print "UNDEF($1)\n" if ($dbg_values > 1);
1164 $av_preprocessor = 1;
1165 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001166
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001167 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001168 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001169 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001170
1171 push(@av_paren_type, $type);
1172 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001173 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001174
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001175 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001176 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1177 $av_preprocessor = 1;
1178
1179 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1180
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001181 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001182
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001183 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001184 print "PRE_END($1)\n" if ($dbg_values > 1);
1185
1186 $av_preprocessor = 1;
1187
1188 # Assume all arms of the conditional end as this
1189 # one does, and continue as if the #endif was not here.
1190 pop(@av_paren_type);
1191 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001192 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001193
1194 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001195 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001196
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001197 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1198 print "ATTR($1)\n" if ($dbg_values > 1);
1199 $av_pending = $type;
1200 $type = 'N';
1201
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001202 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001203 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001204 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001205 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001206 }
1207 $type = 'N';
1208
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001209 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001210 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001211 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001212 $type = 'N';
1213
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001214 } elsif ($cur =~/^(case)/o) {
1215 print "CASE($1)\n" if ($dbg_values > 1);
1216 $av_pend_colon = 'C';
1217 $type = 'N';
1218
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001219 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001220 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001221 $type = 'N';
1222
1223 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001224 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001225 push(@av_paren_type, $av_pending);
1226 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001227 $type = 'N';
1228
1229 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001230 my $new_type = pop(@av_paren_type);
1231 if ($new_type ne '_') {
1232 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001233 print "PAREN('$1') -> $type\n"
1234 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001235 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001236 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001237 }
1238
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001239 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001240 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001241 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001242 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001243
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001244 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1245 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001246 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001247 } elsif ($type eq 'E') {
1248 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001249 }
1250 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1251 $type = 'V';
1252
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001253 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001254 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001255 $type = 'V';
1256
1257 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001258 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001259 $type = 'N';
1260
Andy Whitcroftcf655042008-03-04 14:28:20 -08001261 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001262 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001263 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001264 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001265
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001266 } elsif ($cur =~/^(,)/) {
1267 print "COMMA($1)\n" if ($dbg_values > 1);
1268 $type = 'C';
1269
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001270 } elsif ($cur =~ /^(\?)/o) {
1271 print "QUESTION($1)\n" if ($dbg_values > 1);
1272 $type = 'N';
1273
1274 } elsif ($cur =~ /^(:)/o) {
1275 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1276
1277 substr($var, length($res), 1, $av_pend_colon);
1278 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1279 $type = 'E';
1280 } else {
1281 $type = 'N';
1282 }
1283 $av_pend_colon = 'O';
1284
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001285 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001286 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001287 $type = 'N';
1288
Andy Whitcroft0d413862008-10-15 22:02:16 -07001289 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001290 my $variant;
1291
1292 print "OPV($1)\n" if ($dbg_values > 1);
1293 if ($type eq 'V') {
1294 $variant = 'B';
1295 } else {
1296 $variant = 'U';
1297 }
1298
1299 substr($var, length($res), 1, $variant);
1300 $type = 'N';
1301
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001302 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001303 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001304 if ($1 ne '++' && $1 ne '--') {
1305 $type = 'N';
1306 }
1307
1308 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001309 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001310 }
1311 if (defined $1) {
1312 $cur = substr($cur, length($1));
1313 $res .= $type x length($1);
1314 }
1315 }
1316
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001317 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001318}
1319
Andy Whitcroft8905a672007-11-28 16:21:06 -08001320sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001321 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001322 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001323 ^(?:
1324 $Modifier|
1325 $Storage|
1326 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001327 DEFINE_\S+
1328 )$|
1329 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001330 goto|
1331 return|
1332 case|
1333 else|
1334 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001335 do|
1336 \#|
1337 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001338 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001339 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001340 )}x;
1341 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1342 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001343 # Check for modifiers.
1344 $possible =~ s/\s*$Storage\s*//g;
1345 $possible =~ s/\s*$Sparse\s*//g;
1346 if ($possible =~ /^\s*$/) {
1347
1348 } elsif ($possible =~ /\s/) {
1349 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001350 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001351 if ($modifier !~ $notPermitted) {
1352 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1353 push(@modifierList, $modifier);
1354 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001355 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001356
1357 } else {
1358 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1359 push(@typeList, $possible);
1360 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001361 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001362 } else {
1363 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001364 }
1365}
1366
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001367my $prefix = '';
1368
Joe Perches000d1cc12011-07-25 17:13:25 -07001369sub show_type {
1370 return !defined $ignore_type{$_[0]};
1371}
1372
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001373sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001374 if (!show_type($_[1]) ||
1375 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001376 return 0;
1377 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001378 my $line;
1379 if ($show_types) {
1380 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1381 } else {
1382 $line = "$prefix$_[0]: $_[2]\n";
1383 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001384 $line = (split('\n', $line))[0] . "\n" if ($terse);
1385
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001386 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001387
1388 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001389}
1390sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001391 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001392}
Joe Perches000d1cc12011-07-25 17:13:25 -07001393
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001394sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001395 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001396 our $clean = 0;
1397 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001398 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001399 }
Joe Perches3705ce52013-07-03 15:05:31 -07001400 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001401}
1402sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001403 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001404 our $clean = 0;
1405 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001406 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001407 }
Joe Perches3705ce52013-07-03 15:05:31 -07001408 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001409}
1410sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001411 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001412 our $clean = 0;
1413 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001414 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001415 }
Joe Perches3705ce52013-07-03 15:05:31 -07001416 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001417}
1418
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001419sub check_absolute_file {
1420 my ($absolute, $herecurr) = @_;
1421 my $file = $absolute;
1422
1423 ##print "absolute<$absolute>\n";
1424
1425 # See if any suffix of this path is a path within the tree.
1426 while ($file =~ s@^[^/]*/@@) {
1427 if (-f "$root/$file") {
1428 ##print "file<$file>\n";
1429 last;
1430 }
1431 }
1432 if (! -f _) {
1433 return 0;
1434 }
1435
1436 # It is, so see if the prefix is acceptable.
1437 my $prefix = $absolute;
1438 substr($prefix, -length($file)) = '';
1439
1440 ##print "prefix<$prefix>\n";
1441 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001442 WARN("USE_RELATIVE_PATH",
1443 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001444 }
1445}
1446
Joe Perches3705ce52013-07-03 15:05:31 -07001447sub trim {
1448 my ($string) = @_;
1449
1450 $string =~ s/(^\s+|\s+$)//g;
1451
1452 return $string;
1453}
1454
1455sub tabify {
1456 my ($leading) = @_;
1457
1458 my $source_indent = 8;
1459 my $max_spaces_before_tab = $source_indent - 1;
1460 my $spaces_to_tab = " " x $source_indent;
1461
1462 #convert leading spaces to tabs
1463 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1464 #Remove spaces before a tab
1465 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1466
1467 return "$leading";
1468}
1469
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001470sub pos_last_openparen {
1471 my ($line) = @_;
1472
1473 my $pos = 0;
1474
1475 my $opens = $line =~ tr/\(/\(/;
1476 my $closes = $line =~ tr/\)/\)/;
1477
1478 my $last_openparen = 0;
1479
1480 if (($opens == 0) || ($closes >= $opens)) {
1481 return -1;
1482 }
1483
1484 my $len = length($line);
1485
1486 for ($pos = 0; $pos < $len; $pos++) {
1487 my $string = substr($line, $pos);
1488 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1489 $pos += length($1) - 1;
1490 } elsif (substr($line, $pos, 1) eq '(') {
1491 $last_openparen = $pos;
1492 } elsif (index($string, '(') == -1) {
1493 last;
1494 }
1495 }
1496
1497 return $last_openparen + 1;
1498}
1499
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001500sub process {
1501 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001502
1503 my $linenr=0;
1504 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001505 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001506 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001507 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001508
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001509 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001510 my $indent;
1511 my $previndent=0;
1512 my $stashindent=0;
1513
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001514 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001515 my $signoff = 0;
1516 my $is_patch = 0;
1517
Joe Perches15662b32011-10-31 17:13:12 -07001518 my $in_header_lines = 1;
1519 my $in_commit_log = 0; #Scanning lines before patch
1520
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001521 my $non_utf8_charset = 0;
1522
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001523 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001524 our $cnt_lines = 0;
1525 our $cnt_error = 0;
1526 our $cnt_warn = 0;
1527 our $cnt_chk = 0;
1528
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001529 # Trace the real file/line as we go.
1530 my $realfile = '';
1531 my $realline = 0;
1532 my $realcnt = 0;
1533 my $here = '';
1534 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001535 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001536 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001537 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001538
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001539 my $prev_values = 'E';
1540
1541 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001542 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001543 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001544 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001545 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001546
Joe Perches7e51f192013-09-11 14:23:57 -07001547 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001548
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001549 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001550 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001551 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001552 my @setup_docs = ();
1553 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001554
1555 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001556 my $line;
1557 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001558 $linenr++;
1559 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001560
Joe Perches3705ce52013-07-03 15:05:31 -07001561 push(@fixed, $rawline) if ($fix);
1562
Andy Whitcroft773647a2008-03-28 14:15:58 -07001563 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001564 $setup_docs = 0;
1565 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1566 $setup_docs = 1;
1567 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001568 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001569 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001570 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1571 $realline=$1-1;
1572 if (defined $2) {
1573 $realcnt=$3+1;
1574 } else {
1575 $realcnt=1+1;
1576 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001577 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001578
1579 # Guestimate if this is a continuing comment. Run
1580 # the context looking for a comment "edge". If this
1581 # edge is a close comment then we must be in a comment
1582 # at context start.
1583 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001584 my $cnt = $realcnt;
1585 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1586 next if (defined $rawlines[$ln - 1] &&
1587 $rawlines[$ln - 1] =~ /^-/);
1588 $cnt--;
1589 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001590 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001591 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1592 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1593 ($edge) = $1;
1594 last;
1595 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001596 }
1597 if (defined $edge && $edge eq '*/') {
1598 $in_comment = 1;
1599 }
1600
1601 # Guestimate if this is a continuing comment. If this
1602 # is the start of a diff block and this line starts
1603 # ' *' then it is very likely a comment.
1604 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001605 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001606 {
1607 $in_comment = 1;
1608 }
1609
1610 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1611 sanitise_line_reset($in_comment);
1612
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001613 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001614 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001615 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001616 $line = sanitise_line($rawline);
1617 }
1618 push(@lines, $line);
1619
1620 if ($realcnt > 1) {
1621 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1622 } else {
1623 $realcnt = 0;
1624 }
1625
1626 #print "==>$rawline\n";
1627 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001628
1629 if ($setup_docs && $line =~ /^\+/) {
1630 push(@setup_docs, $line);
1631 }
1632 }
1633
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001634 $prefix = '';
1635
Andy Whitcroft773647a2008-03-28 14:15:58 -07001636 $realcnt = 0;
1637 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001638 foreach my $line (@lines) {
1639 $linenr++;
1640
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001641 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001642
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001643#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001644 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001645 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001646 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001647 $realline=$1-1;
1648 if (defined $2) {
1649 $realcnt=$3+1;
1650 } else {
1651 $realcnt=1+1;
1652 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001653 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001654 $prev_values = 'E';
1655
Andy Whitcroft773647a2008-03-28 14:15:58 -07001656 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001657 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001658 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001659 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001660 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001661
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001662# track the line number as we move through the hunk, note that
1663# new versions of GNU diff omit the leading space on completely
1664# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001665 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001666 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001667 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001668
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001669 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001670 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001671
1672 # Track the previous line.
1673 ($prevline, $stashline) = ($stashline, $line);
1674 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001675 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1676
Andy Whitcroft773647a2008-03-28 14:15:58 -07001677 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001678
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001679 } elsif ($realcnt == 1) {
1680 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001681 }
1682
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001683 my $hunk_line = ($realcnt != 0);
1684
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001685#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001686 $prefix = "$filename:$realline: " if ($emacs && $file);
1687 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1688
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001689 $here = "#$linenr: " if (!$file);
1690 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001691
1692 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001693 if ($line =~ /^diff --git.*?(\S+)$/) {
1694 $realfile = $1;
1695 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001696 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001697 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001698 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001699 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001700 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001701
1702 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001703 if (!$file && $tree && $p1_prefix ne '' &&
1704 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001705 WARN("PATCH_PREFIX",
1706 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001707 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001708
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001709 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001710 ERROR("MODIFIED_INCLUDE_ASM",
1711 "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 -07001712 }
1713 next;
1714 }
1715
Randy Dunlap389834b2007-06-08 13:47:03 -07001716 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001717
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001718 my $hereline = "$here\n$rawline\n";
1719 my $herecurr = "$here\n$rawline\n";
1720 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001721
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001722 $cnt_lines++ if ($realcnt != 0);
1723
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001724# Check for incorrect file permissions
1725 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1726 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001727 if ($realfile !~ m@scripts/@ &&
1728 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001729 ERROR("EXECUTE_PERMISSIONS",
1730 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001731 }
1732 }
1733
Joe Perches20112472011-07-25 17:13:23 -07001734# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001735 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001736 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001737 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001738 }
1739
1740# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001741 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001742 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001743 my $space_before = $1;
1744 my $sign_off = $2;
1745 my $space_after = $3;
1746 my $email = $4;
1747 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1748
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001749 if ($sign_off !~ /$signature_tags/) {
1750 WARN("BAD_SIGN_OFF",
1751 "Non-standard signature: $sign_off\n" . $herecurr);
1752 }
Joe Perches20112472011-07-25 17:13:23 -07001753 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001754 if (WARN("BAD_SIGN_OFF",
1755 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1756 $fix) {
1757 $fixed[$linenr - 1] =
1758 "$ucfirst_sign_off $email";
1759 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001760 }
Joe Perches20112472011-07-25 17:13:23 -07001761 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001762 if (WARN("BAD_SIGN_OFF",
1763 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1764 $fix) {
1765 $fixed[$linenr - 1] =
1766 "$ucfirst_sign_off $email";
1767 }
1768
Joe Perches20112472011-07-25 17:13:23 -07001769 }
1770 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001771 if (WARN("BAD_SIGN_OFF",
1772 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1773 $fix) {
1774 $fixed[$linenr - 1] =
1775 "$ucfirst_sign_off $email";
1776 }
Joe Perches20112472011-07-25 17:13:23 -07001777 }
1778
1779 my ($email_name, $email_address, $comment) = parse_email($email);
1780 my $suggested_email = format_email(($email_name, $email_address));
1781 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001782 ERROR("BAD_SIGN_OFF",
1783 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001784 } else {
1785 my $dequoted = $suggested_email;
1786 $dequoted =~ s/^"//;
1787 $dequoted =~ s/" </ </;
1788 # Don't force email to have quotes
1789 # Allow just an angle bracketed address
1790 if ("$dequoted$comment" ne $email &&
1791 "<$email_address>$comment" ne $email &&
1792 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001793 WARN("BAD_SIGN_OFF",
1794 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001795 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001796 }
Joe Perches7e51f192013-09-11 14:23:57 -07001797
1798# Check for duplicate signatures
1799 my $sig_nospace = $line;
1800 $sig_nospace =~ s/\s//g;
1801 $sig_nospace = lc($sig_nospace);
1802 if (defined $signatures{$sig_nospace}) {
1803 WARN("BAD_SIGN_OFF",
1804 "Duplicate signature\n" . $herecurr);
1805 } else {
1806 $signatures{$sig_nospace} = 1;
1807 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001808 }
1809
Andy Whitcroft00df3442007-06-08 13:47:06 -07001810# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001811 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001812 ERROR("CORRUPTED_PATCH",
1813 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001814 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001815 }
1816
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001817# Check for absolute kernel paths.
1818 if ($tree) {
1819 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1820 my $file = $1;
1821
1822 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1823 check_absolute_file($1, $herecurr)) {
1824 #
1825 } else {
1826 check_absolute_file($file, $herecurr);
1827 }
1828 }
1829 }
1830
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001831# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1832 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001833 $rawline !~ m/^$UTF8*$/) {
1834 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1835
1836 my $blank = copy_spacing($rawline);
1837 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1838 my $hereptr = "$hereline$ptr\n";
1839
Joe Perches34d99212011-07-25 17:13:26 -07001840 CHK("INVALID_UTF8",
1841 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001842 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001843
Joe Perches15662b32011-10-31 17:13:12 -07001844# Check if it's the start of a commit log
1845# (not a header line and we haven't seen the patch filename)
1846 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001847 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001848 $in_header_lines = 0;
1849 $in_commit_log = 1;
1850 }
1851
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001852# Check if there is UTF-8 in a commit log when a mail header has explicitly
1853# declined it, i.e defined some charset where it is missing.
1854 if ($in_header_lines &&
1855 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1856 $1 !~ /utf-8/i) {
1857 $non_utf8_charset = 1;
1858 }
1859
1860 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001861 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001862 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001863 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1864 }
1865
Andy Whitcroft306708542008-10-15 22:02:28 -07001866# ignore non-hunk lines and lines being removed
1867 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001868
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001869#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001870 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001871 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07001872 if (ERROR("DOS_LINE_ENDINGS",
1873 "DOS line endings\n" . $herevet) &&
1874 $fix) {
1875 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1876 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001877 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1878 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001879 if (ERROR("TRAILING_WHITESPACE",
1880 "trailing whitespace\n" . $herevet) &&
1881 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07001882 $fixed[$linenr - 1] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001883 }
1884
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001885 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001886 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001887
Andi Kleen33549572010-05-24 14:33:29 -07001888# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001889# Only applies when adding the entry originally, after that we do not have
1890# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001891 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001892 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001893 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001894 my $cnt = $realcnt;
1895 my $ln = $linenr + 1;
1896 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001897 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001898 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001899 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001900 $f = $lines[$ln - 1];
1901 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1902 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001903
1904 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001905
1906 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1907 $is_start = 1;
1908 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1909 $length = -1;
1910 }
1911
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001912 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001913 $f =~ s/#.*//;
1914 $f =~ s/^\s+//;
1915 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001916 if ($f =~ /^\s*config\s/) {
1917 $is_end = 1;
1918 last;
1919 }
Andi Kleen33549572010-05-24 14:33:29 -07001920 $length++;
1921 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001922 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001923 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1924 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001925 }
1926
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001927# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1928 if ($realfile =~ /Kconfig/ &&
1929 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1930 WARN("CONFIG_EXPERIMENTAL",
1931 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1932 }
1933
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001934 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1935 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1936 my $flag = $1;
1937 my $replacement = {
1938 'EXTRA_AFLAGS' => 'asflags-y',
1939 'EXTRA_CFLAGS' => 'ccflags-y',
1940 'EXTRA_CPPFLAGS' => 'cppflags-y',
1941 'EXTRA_LDFLAGS' => 'ldflags-y',
1942 };
1943
1944 WARN("DEPRECATED_VARIABLE",
1945 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1946 }
1947
Andy Whitcroft5368df22008-10-15 22:02:27 -07001948# check we are in a valid source file if not then ignore this hunk
1949 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1950
Joe Perches6cd7f382012-12-17 16:01:54 -08001951#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001952 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001953 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001954 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001955 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001956 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001957 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001958 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001959 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001960 }
1961
Josh Triplettca56dc02012-03-23 15:02:21 -07001962# Check for user-visible strings broken across lines, which breaks the ability
1963# to grep for the string. Limited to strings used as parameters (those
1964# following an open parenthesis), which almost completely eliminates false
1965# positives, as well as warning only once per parameter rather than once per
1966# line of the string. Make an exception when the previous string ends in a
1967# newline (multiple lines in one string constant) or \n\t (common in inline
1968# assembly to indent the instruction on the following line).
1969 if ($line =~ /^\+\s*"/ &&
1970 $prevline =~ /"\s*$/ &&
1971 $prevline =~ /\(/ &&
1972 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1973 WARN("SPLIT_STRING",
1974 "quoted string split across lines\n" . $hereprev);
1975 }
1976
Joe Perches5e79d962010-03-05 13:43:55 -08001977# check for spaces before a quoted newline
1978 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07001979 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1980 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1981 $fix) {
1982 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1983 }
1984
Joe Perches5e79d962010-03-05 13:43:55 -08001985 }
1986
Andy Whitcroft8905a672007-11-28 16:21:06 -08001987# check for adding lines without a newline.
1988 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001989 WARN("MISSING_EOF_NEWLINE",
1990 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001991 }
1992
Mike Frysinger42e41c52009-09-21 17:04:40 -07001993# Blackfin: use hi/lo macros
1994 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1995 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1996 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001997 ERROR("LO_MACRO",
1998 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001999 }
2000 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2001 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002002 ERROR("HI_MACRO",
2003 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002004 }
2005 }
2006
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002007# check we are in a valid source file C or perl if not then ignore this hunk
2008 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002009
2010# at the beginning of a line any tabs must come first and anything
2011# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002012 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2013 $rawline =~ /^\+\s* \s*/) {
2014 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002015 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002016 if (ERROR("CODE_INDENT",
2017 "code indent should use tabs where possible\n" . $herevet) &&
2018 $fix) {
2019 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2020 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002021 }
2022
Alberto Panizzo08e44362010-03-05 13:43:54 -08002023# check for space before tabs.
2024 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2025 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002026 if (WARN("SPACE_BEFORE_TAB",
2027 "please, no space before tabs\n" . $herevet) &&
2028 $fix) {
2029 $fixed[$linenr - 1] =~
2030 s/(^\+.*) +\t/$1\t/;
2031 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002032 }
2033
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002034# check for && or || at the start of a line
2035 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2036 CHK("LOGICAL_CONTINUATIONS",
2037 "Logical continuations should be on the previous line\n" . $hereprev);
2038 }
2039
2040# check multi-line statement indentation matches previous line
2041 if ($^V && $^V ge 5.10.0 &&
2042 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2043 $prevline =~ /^\+(\t*)(.*)$/;
2044 my $oldindent = $1;
2045 my $rest = $2;
2046
2047 my $pos = pos_last_openparen($rest);
2048 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002049 $line =~ /^(\+| )([ \t]*)/;
2050 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002051
2052 my $goodtabindent = $oldindent .
2053 "\t" x ($pos / 8) .
2054 " " x ($pos % 8);
2055 my $goodspaceindent = $oldindent . " " x $pos;
2056
2057 if ($newindent ne $goodtabindent &&
2058 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002059
2060 if (CHK("PARENTHESIS_ALIGNMENT",
2061 "Alignment should match open parenthesis\n" . $hereprev) &&
2062 $fix && $line =~ /^\+/) {
2063 $fixed[$linenr - 1] =~
2064 s/^\+[ \t]*/\+$goodtabindent/;
2065 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002066 }
2067 }
2068 }
2069
Joe Perches23f780c2013-07-03 15:05:31 -07002070 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002071 if (CHK("SPACING",
2072 "No space is necessary after a cast\n" . $hereprev) &&
2073 $fix) {
2074 $fixed[$linenr - 1] =~
2075 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2076 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002077 }
2078
Joe Perches05880602012-10-04 17:13:35 -07002079 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002080 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2081 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002082 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2083 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2084 }
2085
2086 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002087 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2088 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002089 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002090 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2091 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2092 "networking block comments start with * on subsequent lines\n" . $hereprev);
2093 }
2094
2095 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002096 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2097 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2098 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2099 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002100 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2101 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2102 }
2103
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002104# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002105# Exceptions:
2106# 1) within comments
2107# 2) indented preprocessor commands
2108# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002109 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002110 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002111 if (WARN("LEADING_SPACE",
2112 "please, no spaces at the start of a line\n" . $herevet) &&
2113 $fix) {
2114 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2115 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002116 }
2117
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002118# check we are in a valid C source file if not then ignore this hunk
2119 next if ($realfile !~ /\.(h|c)$/);
2120
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002121# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2122 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2123 WARN("CONFIG_EXPERIMENTAL",
2124 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2125 }
2126
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002127# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002128 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002129 WARN("CVS_KEYWORD",
2130 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002131 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002132
Mike Frysinger42e41c52009-09-21 17:04:40 -07002133# Blackfin: don't use __builtin_bfin_[cs]sync
2134 if ($line =~ /__builtin_bfin_csync/) {
2135 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002136 ERROR("CSYNC",
2137 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002138 }
2139 if ($line =~ /__builtin_bfin_ssync/) {
2140 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002141 ERROR("SSYNC",
2142 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002143 }
2144
Joe Perches56e77d72013-02-21 16:44:14 -08002145# check for old HOTPLUG __dev<foo> section markings
2146 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2147 WARN("HOTPLUG_SECTION",
2148 "Using $1 is unnecessary\n" . $herecurr);
2149 }
2150
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002151# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002152 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2153 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002154#print "LINE<$line>\n";
2155 if ($linenr >= $suppress_statement &&
2156 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002157 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002158 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002159 $stat =~ s/\n./\n /g;
2160 $cond =~ s/\n./\n /g;
2161
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002162#print "linenr<$linenr> <$stat>\n";
2163 # If this statement has no statement boundaries within
2164 # it there is no point in retrying a statement scan
2165 # until we hit end of it.
2166 my $frag = $stat; $frag =~ s/;+\s*$//;
2167 if ($frag !~ /(?:{|;)/) {
2168#print "skip<$line_nr_next>\n";
2169 $suppress_statement = $line_nr_next;
2170 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002171
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002172 # Find the real next line.
2173 $realline_next = $line_nr_next;
2174 if (defined $realline_next &&
2175 (!defined $lines[$realline_next - 1] ||
2176 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2177 $realline_next++;
2178 }
2179
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002180 my $s = $stat;
2181 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002182
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002183 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002184 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002185
2186 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002187 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002188
Andy Whitcroft463f2862009-09-21 17:04:34 -07002189 } elsif ($s =~ /^.\s*else\b/s) {
2190
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002191 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002192 } 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 -07002193 my $type = $1;
2194 $type =~ s/\s+/ /g;
2195 possible($type, "A:" . $s);
2196
Andy Whitcroft8905a672007-11-28 16:21:06 -08002197 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002198 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002199 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002200 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002201
2202 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002203 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002204 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002205 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002206
2207 # Check for any sort of function declaration.
2208 # int foo(something bar, other baz);
2209 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002210 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 -08002211 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002212
Andy Whitcroftcf655042008-03-04 14:28:20 -08002213 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002214 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002215 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002216
Andy Whitcroft8905a672007-11-28 16:21:06 -08002217 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002218 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002219
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002220 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002221 }
2222 }
2223 }
2224
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002225 }
2226
Andy Whitcroft653d4872007-06-23 17:16:34 -07002227#
2228# Checks which may be anchored in the context.
2229#
2230
2231# Check for switch () and associated case and default
2232# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002233 if ($line=~/\bswitch\s*\(.*\)/) {
2234 my $err = '';
2235 my $sep = '';
2236 my @ctx = ctx_block_outer($linenr, $realcnt);
2237 shift(@ctx);
2238 for my $ctx (@ctx) {
2239 my ($clen, $cindent) = line_stats($ctx);
2240 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2241 $indent != $cindent) {
2242 $err .= "$sep$ctx\n";
2243 $sep = '';
2244 } else {
2245 $sep = "[...]\n";
2246 }
2247 }
2248 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002249 ERROR("SWITCH_CASE_INDENT_LEVEL",
2250 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002251 }
2252 }
2253
2254# if/while/etc brace do not go on next line, unless defining a do while loop,
2255# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002256 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002257 my $pre_ctx = "$1$2";
2258
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002259 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002260
2261 if ($line =~ /^\+\t{6,}/) {
2262 WARN("DEEP_INDENTATION",
2263 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2264 }
2265
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002266 my $ctx_cnt = $realcnt - $#ctx - 1;
2267 my $ctx = join("\n", @ctx);
2268
Andy Whitcroft548596d2008-07-23 21:29:01 -07002269 my $ctx_ln = $linenr;
2270 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002271
Andy Whitcroft548596d2008-07-23 21:29:01 -07002272 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2273 defined $lines[$ctx_ln - 1] &&
2274 $lines[$ctx_ln - 1] =~ /^-/)) {
2275 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2276 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002277 $ctx_ln++;
2278 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002279
Andy Whitcroft53210162008-07-23 21:29:03 -07002280 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2281 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002282
2283 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002284 ERROR("OPEN_BRACE",
2285 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002286 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002287 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002288 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2289 $ctx =~ /\)\s*\;\s*$/ &&
2290 defined $lines[$ctx_ln - 1])
2291 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002292 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2293 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002294 WARN("TRAILING_SEMICOLON",
2295 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002296 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002297 }
2298 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002299 }
2300
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002301# Check relative indent for conditionals and blocks.
2302 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002303 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2304 ctx_statement_block($linenr, $realcnt, 0)
2305 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002306 my ($s, $c) = ($stat, $cond);
2307
2308 substr($s, 0, length($c), '');
2309
2310 # Make sure we remove the line prefixes as we have
2311 # none on the first line, and are going to readd them
2312 # where necessary.
2313 $s =~ s/\n./\n/gs;
2314
2315 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002316 my @newlines = ($c =~ /\n/gs);
2317 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002318
2319 # We want to check the first line inside the block
2320 # starting at the end of the conditional, so remove:
2321 # 1) any blank line termination
2322 # 2) any opening brace { on end of the line
2323 # 3) any do (...) {
2324 my $continuation = 0;
2325 my $check = 0;
2326 $s =~ s/^.*\bdo\b//;
2327 $s =~ s/^\s*{//;
2328 if ($s =~ s/^\s*\\//) {
2329 $continuation = 1;
2330 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002331 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002332 $check = 1;
2333 $cond_lines++;
2334 }
2335
2336 # Also ignore a loop construct at the end of a
2337 # preprocessor statement.
2338 if (($prevline =~ /^.\s*#\s*define\s/ ||
2339 $prevline =~ /\\\s*$/) && $continuation == 0) {
2340 $check = 0;
2341 }
2342
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002343 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002344 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002345 while ($cond_ptr != $cond_lines) {
2346 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002347
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002348 # If we see an #else/#elif then the code
2349 # is not linear.
2350 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2351 $check = 0;
2352 }
2353
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002354 # Ignore:
2355 # 1) blank lines, they should be at 0,
2356 # 2) preprocessor lines, and
2357 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002358 if ($continuation ||
2359 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002360 $s =~ /^\s*#\s*?/ ||
2361 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002362 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002363 if ($s =~ s/^.*?\n//) {
2364 $cond_lines++;
2365 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002366 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002367 }
2368
2369 my (undef, $sindent) = line_stats("+" . $s);
2370 my $stat_real = raw_line($linenr, $cond_lines);
2371
2372 # Check if either of these lines are modified, else
2373 # this is not this patch's fault.
2374 if (!defined($stat_real) ||
2375 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2376 $check = 0;
2377 }
2378 if (defined($stat_real) && $cond_lines > 1) {
2379 $stat_real = "[...]\n$stat_real";
2380 }
2381
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002382 #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 -07002383
2384 if ($check && (($sindent % 8) != 0 ||
2385 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002386 WARN("SUSPECT_CODE_INDENT",
2387 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002388 }
2389 }
2390
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002391 # Track the 'values' across context and added lines.
2392 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002393 my ($curr_values, $curr_vars) =
2394 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002395 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002396 if ($dbg_values) {
2397 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002398 print "$linenr > .$outline\n";
2399 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002400 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002401 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002402 $prev_values = substr($curr_values, -1);
2403
Andy Whitcroft00df3442007-06-08 13:47:06 -07002404#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002405 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002406
Andy Whitcroft653d4872007-06-23 17:16:34 -07002407# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002408 if ($dbg_type) {
2409 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002410 ERROR("TEST_TYPE",
2411 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002412 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002413 ERROR("TEST_NOT_TYPE",
2414 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002415 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002416 next;
2417 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002418# TEST: allow direct testing of the attribute matcher.
2419 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002420 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002421 ERROR("TEST_ATTR",
2422 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002423 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002424 ERROR("TEST_NOT_ATTR",
2425 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002426 }
2427 next;
2428 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002429
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002430# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002431 if ($line =~ /^.\s*{/ &&
2432 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002433 ERROR("OPEN_BRACE",
2434 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002435 }
2436
Andy Whitcroft653d4872007-06-23 17:16:34 -07002437#
2438# Checks which are anchored on the added line.
2439#
2440
2441# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002442 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002443 my $path = $1;
2444 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002445 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002446 "malformed #include filename\n" . $herecurr);
2447 }
2448 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2449 ERROR("UAPI_INCLUDE",
2450 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002451 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002452 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002453
2454# no C99 // comments
2455 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002456 if (ERROR("C99_COMMENTS",
2457 "do not use C99 // comments\n" . $herecurr) &&
2458 $fix) {
2459 my $line = $fixed[$linenr - 1];
2460 if ($line =~ /\/\/(.*)$/) {
2461 my $comment = trim($1);
2462 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2463 }
2464 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002465 }
2466 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002467 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002468 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002469
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002470# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2471# the whole statement.
2472#print "APW <$lines[$realline_next - 1]>\n";
2473 if (defined $realline_next &&
2474 exists $lines[$realline_next - 1] &&
2475 !defined $suppress_export{$realline_next} &&
2476 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2477 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002478 # Handle definitions which produce identifiers with
2479 # a prefix:
2480 # XXX(foo);
2481 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002482 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002483 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002484 $name =~ /^${Ident}_$2/) {
2485#print "FOO C name<$name>\n";
2486 $suppress_export{$realline_next} = 1;
2487
2488 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002489 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002490 ^.DEFINE_$Ident\(\Q$name\E\)|
2491 ^.DECLARE_$Ident\(\Q$name\E\)|
2492 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002493 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2494 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002495 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002496#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2497 $suppress_export{$realline_next} = 2;
2498 } else {
2499 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002500 }
2501 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002502 if (!defined $suppress_export{$linenr} &&
2503 $prevline =~ /^.\s*$/ &&
2504 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2505 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2506#print "FOO B <$lines[$linenr - 1]>\n";
2507 $suppress_export{$linenr} = 2;
2508 }
2509 if (defined $suppress_export{$linenr} &&
2510 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002511 WARN("EXPORT_SYMBOL",
2512 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002513 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002514
Joe Eloff5150bda2010-08-09 17:21:00 -07002515# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002516 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2517 if (ERROR("GLOBAL_INITIALISERS",
2518 "do not initialise globals to 0 or NULL\n" .
2519 $herecurr) &&
2520 $fix) {
2521 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2522 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002523 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002524# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002525 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2526 if (ERROR("INITIALISED_STATIC",
2527 "do not initialise statics to 0 or NULL\n" .
2528 $herecurr) &&
2529 $fix) {
2530 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2531 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002532 }
2533
Joe Perchescb710ec2010-10-26 14:23:20 -07002534# check for static const char * arrays.
2535 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002536 WARN("STATIC_CONST_CHAR_ARRAY",
2537 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002538 $herecurr);
2539 }
2540
2541# check for static char foo[] = "bar" declarations.
2542 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002543 WARN("STATIC_CONST_CHAR_ARRAY",
2544 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002545 $herecurr);
2546 }
2547
Joe Perches93ed0e22010-10-26 14:23:21 -07002548# check for declarations of struct pci_device_id
2549 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002550 WARN("DEFINE_PCI_DEVICE_TABLE",
2551 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002552 }
2553
Andy Whitcroft653d4872007-06-23 17:16:34 -07002554# check for new typedefs, only function parameters and sparse annotations
2555# make sense.
2556 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002557 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002558 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002559 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002560 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002561 WARN("NEW_TYPEDEFS",
2562 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002563 }
2564
2565# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002566 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002567 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2568 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002569 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002570
Andy Whitcroft65863862009-01-06 14:41:21 -08002571 # Should start with a space.
2572 $to =~ s/^(\S)/ $1/;
2573 # Should not end with a space.
2574 $to =~ s/\s+$//;
2575 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002576 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002577 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002578
Joe Perches3705ce52013-07-03 15:05:31 -07002579## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002580 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002581 if (ERROR("POINTER_LOCATION",
2582 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2583 $fix) {
2584 my $sub_from = $ident;
2585 my $sub_to = $ident;
2586 $sub_to =~ s/\Q$from\E/$to/;
2587 $fixed[$linenr - 1] =~
2588 s@\Q$sub_from\E@$sub_to@;
2589 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002590 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002591 }
2592 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2593 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002594 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002595
Andy Whitcroft65863862009-01-06 14:41:21 -08002596 # Should start with a space.
2597 $to =~ s/^(\S)/ $1/;
2598 # Should not end with a space.
2599 $to =~ s/\s+$//;
2600 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002601 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002602 }
2603 # Modifiers should have spaces.
2604 $to =~ s/(\b$Modifier$)/$1 /;
2605
Joe Perches3705ce52013-07-03 15:05:31 -07002606## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002607 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002608 if (ERROR("POINTER_LOCATION",
2609 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2610 $fix) {
2611
2612 my $sub_from = $match;
2613 my $sub_to = $match;
2614 $sub_to =~ s/\Q$from\E/$to/;
2615 $fixed[$linenr - 1] =~
2616 s@\Q$sub_from\E@$sub_to@;
2617 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002618 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002619 }
2620
2621# # no BUG() or BUG_ON()
2622# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2623# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2624# print "$herecurr";
2625# $clean = 0;
2626# }
2627
Andy Whitcroft8905a672007-11-28 16:21:06 -08002628 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002629 WARN("LINUX_VERSION_CODE",
2630 "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 -08002631 }
2632
Joe Perches17441222011-06-15 15:08:17 -07002633# check for uses of printk_ratelimit
2634 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002635 WARN("PRINTK_RATELIMITED",
2636"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002637 }
2638
Andy Whitcroft00df3442007-06-08 13:47:06 -07002639# printk should use KERN_* levels. Note that follow on printk's on the
2640# same line do not need a level, so we use the current block context
2641# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002642# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002643# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002644 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002645 my $ok = 0;
2646 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2647 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002648 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002649 # with "\n" ignore it, else it is to blame
2650 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2651 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2652 $ok = 1;
2653 }
2654 last;
2655 }
2656 }
2657 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002658 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2659 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002660 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002661 }
2662
Joe Perches243f3802012-05-31 16:26:09 -07002663 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2664 my $orig = $1;
2665 my $level = lc($orig);
2666 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002667 my $level2 = $level;
2668 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002669 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002670 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002671 }
2672
2673 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07002674 if (WARN("PREFER_PR_LEVEL",
2675 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2676 $fix) {
2677 $fixed[$linenr - 1] =~
2678 s/\bpr_warning\b/pr_warn/;
2679 }
Joe Perches243f3802012-05-31 16:26:09 -07002680 }
2681
Joe Perchesdc139312013-02-21 16:44:13 -08002682 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2683 my $orig = $1;
2684 my $level = lc($orig);
2685 $level = "warn" if ($level eq "warning");
2686 $level = "dbg" if ($level eq "debug");
2687 WARN("PREFER_DEV_LEVEL",
2688 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2689 }
2690
Andy Whitcroft653d4872007-06-23 17:16:34 -07002691# function brace can't be on same line, except for #defines of do while,
2692# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002693 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2694 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002695 ERROR("OPEN_BRACE",
2696 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002697 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002698
Andy Whitcroft8905a672007-11-28 16:21:06 -08002699# open braces for enum, union and struct go on the same line.
2700 if ($line =~ /^.\s*{/ &&
2701 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002702 ERROR("OPEN_BRACE",
2703 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002704 }
2705
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002706# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002707 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2708 if (WARN("SPACING",
2709 "missing space after $1 definition\n" . $herecurr) &&
2710 $fix) {
2711 $fixed[$linenr - 1] =~
2712 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2713 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002714 }
2715
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002716# check for spacing round square brackets; allowed:
2717# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002718# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2719# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002720 while ($line =~ /(.*?\s)\[/g) {
2721 my ($where, $prefix) = ($-[1], $1);
2722 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002723 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002724 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002725 if (ERROR("BRACKET_SPACE",
2726 "space prohibited before open square bracket '['\n" . $herecurr) &&
2727 $fix) {
2728 $fixed[$linenr - 1] =~
2729 s/^(\+.*?)\s+\[/$1\[/;
2730 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002731 }
2732 }
2733
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002734# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002735 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002736 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002737 my $ctx_before = substr($line, 0, $-[1]);
2738 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002739
2740 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002741 if ($name =~ /^(?:
2742 if|for|while|switch|return|case|
2743 volatile|__volatile__|
2744 __attribute__|format|__extension__|
2745 asm|__asm__)$/x)
2746 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002747 # cpp #define statements have non-optional spaces, ie
2748 # if there is a space between the name and the open
2749 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002750 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002751
2752 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002753 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002754
2755 # If this whole things ends with a type its most
2756 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002757 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002758
2759 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002760 if (WARN("SPACING",
2761 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2762 $fix) {
2763 $fixed[$linenr - 1] =~
2764 s/\b$name\s+\(/$name\(/;
2765 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002766 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002767 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002768
Andy Whitcroft653d4872007-06-23 17:16:34 -07002769# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002770 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002771 my $fixed_line = "";
2772 my $line_fixed = 0;
2773
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002774 my $ops = qr{
2775 <<=|>>=|<=|>=|==|!=|
2776 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2777 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002778 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2779 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002780 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002781 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002782
2783## print("element count: <" . $#elements . ">\n");
2784## foreach my $el (@elements) {
2785## print("el: <$el>\n");
2786## }
2787
2788 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07002789 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002790
Joe Perches3705ce52013-07-03 15:05:31 -07002791 foreach my $el (@elements) {
2792 push(@fix_elements, substr($rawline, $off, length($el)));
2793 $off += length($el);
2794 }
2795
2796 $off = 0;
2797
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002798 my $blank = copy_spacing($opline);
2799
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002800 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002801
2802 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2803
2804## print("n: <$n> good: <$good>\n");
2805
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002806 $off += length($elements[$n]);
2807
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002808 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002809 my $ca = substr($opline, 0, $off);
2810 my $cc = '';
2811 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2812 $cc = substr($opline, $off + length($elements[$n + 1]));
2813 }
2814 my $cb = "$ca$;$cc";
2815
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002816 my $a = '';
2817 $a = 'V' if ($elements[$n] ne '');
2818 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002819 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002820 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2821 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002822 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002823
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002824 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002825
2826 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002827 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002828 $c = 'V' if ($elements[$n + 2] ne '');
2829 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002830 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002831 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2832 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002833 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002834 } else {
2835 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002836 }
2837
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002838 my $ctx = "${a}x${c}";
2839
2840 my $at = "(ctx:$ctx)";
2841
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002842 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002843 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002844
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002845 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002846 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002847
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002848 # Get the full operator variant.
2849 my $opv = $op . substr($curr_vars, $off, 1);
2850
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002851 # Ignore operators passed as parameters.
2852 if ($op_type ne 'V' &&
2853 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2854
Andy Whitcroftcf655042008-03-04 14:28:20 -08002855# # Ignore comments
2856# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002857
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002858 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002859 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002860 if ($ctx !~ /.x[WEBC]/ &&
2861 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002862 if (ERROR("SPACING",
2863 "space required after that '$op' $at\n" . $hereptr)) {
2864 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2865 $line_fixed = 1;
2866 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002867 }
2868
2869 # // is a comment
2870 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002871
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002872 # No spaces for:
2873 # ->
2874 # : when part of a bitfield
2875 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002876 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002877 if (ERROR("SPACING",
2878 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2879 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2880 $line_fixed = 1;
2881 if (defined $fix_elements[$n + 2]) {
2882 $fix_elements[$n + 2] =~ s/^\s+//;
2883 }
2884 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002885 }
2886
2887 # , must have a space on the right.
2888 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002889 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002890 if (ERROR("SPACING",
2891 "space required after that '$op' $at\n" . $hereptr)) {
2892 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2893 $line_fixed = 1;
2894 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002895 }
2896
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002897 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002898 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002899 #warn "'*' is part of type\n";
2900
2901 # unary operators should have a space before and
2902 # none after. May be left adjacent to another
2903 # unary operator, or a cast
2904 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002905 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002906 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002907 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002908 if (ERROR("SPACING",
2909 "space required before that '$op' $at\n" . $hereptr)) {
2910 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2911 $line_fixed = 1;
2912 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002913 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002914 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002915 # A unary '*' may be const
2916
2917 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002918 if (ERROR("SPACING",
2919 "space prohibited after that '$op' $at\n" . $hereptr)) {
2920 $fixed_line =~ s/\s+$//;
2921 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2922 $line_fixed = 1;
2923 if (defined $fix_elements[$n + 2]) {
2924 $fix_elements[$n + 2] =~ s/^\s+//;
2925 }
2926 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002927 }
2928
2929 # unary ++ and unary -- are allowed no space on one side.
2930 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002931 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002932 if (ERROR("SPACING",
2933 "space required one side of that '$op' $at\n" . $hereptr)) {
2934 $fixed_line =~ s/\s+$//;
2935 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2936 $line_fixed = 1;
2937 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002938 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002939 if ($ctx =~ /Wx[BE]/ ||
2940 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002941 if (ERROR("SPACING",
2942 "space prohibited before that '$op' $at\n" . $hereptr)) {
2943 $fixed_line =~ s/\s+$//;
2944 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2945 $line_fixed = 1;
2946 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002947 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002948 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002949 if (ERROR("SPACING",
2950 "space prohibited after that '$op' $at\n" . $hereptr)) {
2951 $fixed_line =~ s/\s+$//;
2952 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2953 $line_fixed = 1;
2954 if (defined $fix_elements[$n + 2]) {
2955 $fix_elements[$n + 2] =~ s/^\s+//;
2956 }
2957 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002958 }
2959
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002960 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002961 } elsif ($op eq '<<' or $op eq '>>' or
2962 $op eq '&' or $op eq '^' or $op eq '|' or
2963 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002964 $op eq '*' or $op eq '/' or
2965 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002966 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002967 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002968 if (ERROR("SPACING",
2969 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2970 $fixed_line =~ s/\s+$//;
2971 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2972 $line_fixed = 1;
2973 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002974 }
2975
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002976 # A colon needs no spaces before when it is
2977 # terminating a case value or a label.
2978 } elsif ($opv eq ':C' || $opv eq ':L') {
2979 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07002980 if (ERROR("SPACING",
2981 "space prohibited before that '$op' $at\n" . $hereptr)) {
2982 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2983 $line_fixed = 1;
2984 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002985 }
2986
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002987 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002988 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002989 my $ok = 0;
2990
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002991 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002992 if (($op eq '<' &&
2993 $cc =~ /^\S+\@\S+>/) ||
2994 ($op eq '>' &&
2995 $ca =~ /<\S+\@\S+$/))
2996 {
2997 $ok = 1;
2998 }
2999
3000 # Ignore ?:
3001 if (($opv eq ':O' && $ca =~ /\?$/) ||
3002 ($op eq '?' && $cc =~ /^:/)) {
3003 $ok = 1;
3004 }
3005
3006 if ($ok == 0) {
Joe Perches3705ce52013-07-03 15:05:31 -07003007 if (ERROR("SPACING",
3008 "spaces required around that '$op' $at\n" . $hereptr)) {
3009 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3010 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
3011 $line_fixed = 1;
3012 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003013 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003014 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003015 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003016
3017## print("n: <$n> GOOD: <$good>\n");
3018
3019 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003020 }
Joe Perches3705ce52013-07-03 15:05:31 -07003021
3022 if (($#elements % 2) == 0) {
3023 $fixed_line = $fixed_line . $fix_elements[$#elements];
3024 }
3025
3026 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3027 $fixed[$linenr - 1] = $fixed_line;
3028 }
3029
3030
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003031 }
3032
Joe Perches786b6322013-07-03 15:05:32 -07003033# check for whitespace before a non-naked semicolon
3034 if ($line =~ /^\+.*\S\s+;/) {
3035 if (WARN("SPACING",
3036 "space prohibited before semicolon\n" . $herecurr) &&
3037 $fix) {
3038 1 while $fixed[$linenr - 1] =~
3039 s/^(\+.*\S)\s+;/$1;/;
3040 }
3041 }
3042
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003043# check for multiple assignments
3044 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003045 CHK("MULTIPLE_ASSIGNMENTS",
3046 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003047 }
3048
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003049## # check for multiple declarations, allowing for a function declaration
3050## # continuation.
3051## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3052## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3053##
3054## # Remove any bracketed sections to ensure we do not
3055## # falsly report the parameters of functions.
3056## my $ln = $line;
3057## while ($ln =~ s/\([^\(\)]*\)//g) {
3058## }
3059## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003060## WARN("MULTIPLE_DECLARATION",
3061## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003062## }
3063## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003064
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003065#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003066 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3067 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003068 if (ERROR("SPACING",
3069 "space required before the open brace '{'\n" . $herecurr) &&
3070 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003071 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003072 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003073 }
3074
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003075## # check for blank lines before declarations
3076## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3077## $prevrawline =~ /^.\s*$/) {
3078## WARN("SPACING",
3079## "No blank lines before declarations\n" . $hereprev);
3080## }
3081##
3082
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003083# closing brace should have a space following it when it has anything
3084# on the line
3085 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003086 if (ERROR("SPACING",
3087 "space required after that close brace '}'\n" . $herecurr) &&
3088 $fix) {
3089 $fixed[$linenr - 1] =~
3090 s/}((?!(?:,|;|\)))\S)/} $1/;
3091 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003092 }
3093
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003094# check spacing on square brackets
3095 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003096 if (ERROR("SPACING",
3097 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3098 $fix) {
3099 $fixed[$linenr - 1] =~
3100 s/\[\s+/\[/;
3101 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003102 }
3103 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003104 if (ERROR("SPACING",
3105 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3106 $fix) {
3107 $fixed[$linenr - 1] =~
3108 s/\s+\]/\]/;
3109 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003110 }
3111
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003112# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003113 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3114 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003115 if (ERROR("SPACING",
3116 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3117 $fix) {
3118 $fixed[$linenr - 1] =~
3119 s/\(\s+/\(/;
3120 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003121 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003122 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003123 $line !~ /for\s*\(.*;\s+\)/ &&
3124 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003125 if (ERROR("SPACING",
3126 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3127 $fix) {
3128 $fixed[$linenr - 1] =~
3129 s/\s+\)/\)/;
3130 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003131 }
3132
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003133#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003134 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003135 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003136 if (WARN("INDENTED_LABEL",
3137 "labels should not be indented\n" . $herecurr) &&
3138 $fix) {
3139 $fixed[$linenr - 1] =~
3140 s/^(.)\s+/$1/;
3141 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003142 }
3143
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003144# Return is not a function.
3145 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3146 my $spacing = $1;
3147 my $value = $2;
3148
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003149 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003150 $value =~ s/\(/ \(/g;
3151 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003152 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003153 $value !~ /(?:$Ident|-?$Constant)\s*
3154 $Compare\s*
3155 (?:$Ident|-?$Constant)/x &&
3156 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003157 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003158#print "value<$value>\n";
3159 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003160 ERROR("RETURN_PARENTHESES",
3161 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003162
3163 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003164 ERROR("SPACING",
3165 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003166 }
3167 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003168# Return of what appears to be an errno should normally be -'ve
3169 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3170 my $name = $1;
3171 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003172 WARN("USE_NEGATIVE_ERRNO",
3173 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003174 }
3175 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003176
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003177# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003178 if ($line =~ /\b(if|while|for|switch)\(/) {
3179 if (ERROR("SPACING",
3180 "space required before the open parenthesis '('\n" . $herecurr) &&
3181 $fix) {
3182 $fixed[$linenr - 1] =~
3183 s/\b(if|while|for|switch)\(/$1 \(/;
3184 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003185 }
3186
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003187# Check for illegal assignment in if conditional -- and check for trailing
3188# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003189 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003190 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3191 ctx_statement_block($linenr, $realcnt, 0)
3192 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003193 my ($stat_next) = ctx_statement_block($line_nr_next,
3194 $remain_next, $off_next);
3195 $stat_next =~ s/\n./\n /g;
3196 ##print "stat<$stat> stat_next<$stat_next>\n";
3197
3198 if ($stat_next =~ /^\s*while\b/) {
3199 # If the statement carries leading newlines,
3200 # then count those as offsets.
3201 my ($whitespace) =
3202 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3203 my $offset =
3204 statement_rawlines($whitespace) - 1;
3205
3206 $suppress_whiletrailers{$line_nr_next +
3207 $offset} = 1;
3208 }
3209 }
3210 if (!defined $suppress_whiletrailers{$linenr} &&
3211 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003212 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003213
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003214 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003215 ERROR("ASSIGN_IN_IF",
3216 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003217 }
3218
3219 # Find out what is on the end of the line after the
3220 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003221 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003222 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003223 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003224 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3225 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003226 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003227 # Find out how long the conditional actually is.
3228 my @newlines = ($c =~ /\n/gs);
3229 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003230 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003231
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003232 $stat_real = raw_line($linenr, $cond_lines)
3233 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003234 if (defined($stat_real) && $cond_lines > 1) {
3235 $stat_real = "[...]\n$stat_real";
3236 }
3237
Joe Perches000d1cc12011-07-25 17:13:25 -07003238 ERROR("TRAILING_STATEMENTS",
3239 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003240 }
3241 }
3242
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003243# Check for bitwise tests written as boolean
3244 if ($line =~ /
3245 (?:
3246 (?:\[|\(|\&\&|\|\|)
3247 \s*0[xX][0-9]+\s*
3248 (?:\&\&|\|\|)
3249 |
3250 (?:\&\&|\|\|)
3251 \s*0[xX][0-9]+\s*
3252 (?:\&\&|\|\||\)|\])
3253 )/x)
3254 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003255 WARN("HEXADECIMAL_BOOLEAN_TEST",
3256 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003257 }
3258
Andy Whitcroft8905a672007-11-28 16:21:06 -08003259# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003260 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3261 my $s = $1;
3262 $s =~ s/$;//g; # Remove any comments
3263 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003264 ERROR("TRAILING_STATEMENTS",
3265 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003266 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003267 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003268# if should not continue a brace
3269 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003270 ERROR("TRAILING_STATEMENTS",
3271 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003272 $herecurr);
3273 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003274# case and default should not have general statements after them
3275 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3276 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003277 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003278 \s*return\s+
3279 )/xg)
3280 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003281 ERROR("TRAILING_STATEMENTS",
3282 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003283 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003284
3285 # Check for }<nl>else {, these must be at the same
3286 # indent level to be relevant to each other.
3287 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3288 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003289 ERROR("ELSE_AFTER_BRACE",
3290 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003291 }
3292
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003293 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3294 $previndent == $indent) {
3295 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3296
3297 # Find out what is on the end of the line after the
3298 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003299 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003300 $s =~ s/\n.*//g;
3301
3302 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003303 ERROR("WHILE_AFTER_BRACE",
3304 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003305 }
3306 }
3307
Joe Perches95e2c602013-07-03 15:05:20 -07003308#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003309 while ($line =~ m{($Constant|$Lval)}g) {
3310 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003311
3312#gcc binary extension
3313 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003314 if (WARN("GCC_BINARY_CONSTANT",
3315 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3316 $fix) {
3317 my $hexval = sprintf("0x%x", oct($var));
3318 $fixed[$linenr - 1] =~
3319 s/\b$var\b/$hexval/;
3320 }
Joe Perches95e2c602013-07-03 15:05:20 -07003321 }
3322
3323#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003324 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003325 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003326#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003327 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003328#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003329 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003330 while ($var =~ m{($Ident)}g) {
3331 my $word = $1;
3332 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3333 seed_camelcase_includes() if ($check);
3334 if (!defined $camelcase{$word}) {
3335 $camelcase{$word} = 1;
3336 CHK("CAMELCASE",
3337 "Avoid CamelCase: <$word>\n" . $herecurr);
3338 }
Joe Perches34456862013-07-03 15:05:34 -07003339 }
Joe Perches323c1262012-12-17 16:02:07 -08003340 }
3341 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003342
3343#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003344 if ($line =~ /\#\s*define.*\\\s+$/) {
3345 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3346 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3347 $fix) {
3348 $fixed[$linenr - 1] =~ s/\s+$//;
3349 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003350 }
3351
Andy Whitcroft653d4872007-06-23 17:16:34 -07003352#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003353 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003354 my $file = "$1.h";
3355 my $checkfile = "include/linux/$file";
3356 if (-f "$root/$checkfile" &&
3357 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003358 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003359 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003360 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003361 CHK("ARCH_INCLUDE_LINUX",
3362 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003363 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003364 WARN("INCLUDE_LINUX",
3365 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003366 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003367 }
3368 }
3369
Andy Whitcroft653d4872007-06-23 17:16:34 -07003370# multi-statement macros should be enclosed in a do while loop, grab the
3371# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003372# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003373 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3374 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003375 my $ln = $linenr;
3376 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003377 my ($off, $dstat, $dcond, $rest);
3378 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003379 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003380 ctx_statement_block($linenr, $realcnt, 0);
3381 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003382 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003383 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003384
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003385 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003386 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003387 $dstat =~ s/\\\n.//g;
3388 $dstat =~ s/^\s*//s;
3389 $dstat =~ s/\s*$//s;
3390
3391 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003392 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3393 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003394 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003395 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003396 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003397
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003398 # Flatten any obvious string concatentation.
3399 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3400 $dstat =~ s/$Ident\s*("X*")/$1/)
3401 {
3402 }
3403
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003404 my $exceptions = qr{
3405 $Declare|
3406 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003407 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003408 DECLARE_PER_CPU|
3409 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003410 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003411 union|
3412 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003413 \.$Ident\s*=\s*|
3414 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003415 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003416 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003417 if ($dstat ne '' &&
3418 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3419 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003420 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003421 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003422 $dstat !~ /$exceptions/ &&
3423 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003424 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003425 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003426 $dstat !~ /^for\s*$Constant$/ && # for (...)
3427 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3428 $dstat !~ /^do\s*{/ && # do {...
3429 $dstat !~ /^\({/) # ({...
3430 {
3431 $ctx =~ s/\n*$//;
3432 my $herectx = $here . "\n";
3433 my $cnt = statement_rawlines($ctx);
3434
3435 for (my $n = 0; $n < $cnt; $n++) {
3436 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003437 }
3438
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003439 if ($dstat =~ /;/) {
3440 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3441 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3442 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003443 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003444 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003445 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003446 }
Joe Perches5023d342012-12-17 16:01:47 -08003447
Joe Perches481eb482012-12-17 16:01:56 -08003448# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003449
3450 } else {
3451 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003452 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3453 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003454 $line =~ /^\+.*\\$/) {
3455 WARN("LINE_CONTINUATIONS",
3456 "Avoid unnecessary line continuations\n" . $herecurr);
3457 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003458 }
3459
Joe Perchesb13edf72012-07-30 14:41:24 -07003460# do {} while (0) macro tests:
3461# single-statement macros do not need to be enclosed in do while (0) loop,
3462# macro should not end with a semicolon
3463 if ($^V && $^V ge 5.10.0 &&
3464 $realfile !~ m@/vmlinux.lds.h$@ &&
3465 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3466 my $ln = $linenr;
3467 my $cnt = $realcnt;
3468 my ($off, $dstat, $dcond, $rest);
3469 my $ctx = '';
3470 ($dstat, $dcond, $ln, $cnt, $off) =
3471 ctx_statement_block($linenr, $realcnt, 0);
3472 $ctx = $dstat;
3473
3474 $dstat =~ s/\\\n.//g;
3475
3476 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3477 my $stmts = $2;
3478 my $semis = $3;
3479
3480 $ctx =~ s/\n*$//;
3481 my $cnt = statement_rawlines($ctx);
3482 my $herectx = $here . "\n";
3483
3484 for (my $n = 0; $n < $cnt; $n++) {
3485 $herectx .= raw_line($linenr, $n) . "\n";
3486 }
3487
Joe Perchesac8e97f2012-08-21 16:15:53 -07003488 if (($stmts =~ tr/;/;/) == 1 &&
3489 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003490 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3491 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3492 }
3493 if (defined $semis && $semis ne "") {
3494 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3495 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3496 }
3497 }
3498 }
3499
Mike Frysinger080ba922009-01-06 14:41:25 -08003500# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3501# all assignments may have only one of the following with an assignment:
3502# .
3503# ALIGN(...)
3504# VMLINUX_SYMBOL(...)
3505 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003506 WARN("MISSING_VMLINUX_SYMBOL",
3507 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003508 }
3509
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003510# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003511 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3512 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003513 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003514 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003515 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3516 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003517 my @allowed = ();
3518 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003519 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003520 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003521 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003522 for my $chunk (@chunks) {
3523 my ($cond, $block) = @{$chunk};
3524
Andy Whitcroft773647a2008-03-28 14:15:58 -07003525 # If the condition carries leading newlines, then count those as offsets.
3526 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3527 my $offset = statement_rawlines($whitespace) - 1;
3528
Joe Perchesaad4f612012-03-23 15:02:19 -07003529 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003530 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3531
3532 # We have looked at and allowed this specific line.
3533 $suppress_ifbraces{$ln + $offset} = 1;
3534
3535 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003536 $ln += statement_rawlines($block) - 1;
3537
Andy Whitcroft773647a2008-03-28 14:15:58 -07003538 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003539
3540 $seen++ if ($block =~ /^\s*{/);
3541
Joe Perchesaad4f612012-03-23 15:02:19 -07003542 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003543 if (statement_lines($cond) > 1) {
3544 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003545 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003546 }
3547 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003548 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003549 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003550 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003551 if (statement_block_size($block) > 1) {
3552 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003553 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003554 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003555 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003556 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003557 if ($seen) {
3558 my $sum_allowed = 0;
3559 foreach (@allowed) {
3560 $sum_allowed += $_;
3561 }
3562 if ($sum_allowed == 0) {
3563 WARN("BRACES",
3564 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3565 } elsif ($sum_allowed != $allow &&
3566 $seen != $allow) {
3567 CHK("BRACES",
3568 "braces {} should be used on all arms of this statement\n" . $herectx);
3569 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003570 }
3571 }
3572 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003573 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003574 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003575 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003576
Andy Whitcroftcf655042008-03-04 14:28:20 -08003577 # Check the pre-context.
3578 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3579 #print "APW: ALLOWED: pre<$1>\n";
3580 $allowed = 1;
3581 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003582
3583 my ($level, $endln, @chunks) =
3584 ctx_statement_full($linenr, $realcnt, $-[0]);
3585
Andy Whitcroftcf655042008-03-04 14:28:20 -08003586 # Check the condition.
3587 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003588 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003589 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003590 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003591 }
3592 if (statement_lines($cond) > 1) {
3593 #print "APW: ALLOWED: cond<$cond>\n";
3594 $allowed = 1;
3595 }
3596 if ($block =~/\b(?:if|for|while)\b/) {
3597 #print "APW: ALLOWED: block<$block>\n";
3598 $allowed = 1;
3599 }
3600 if (statement_block_size($block) > 1) {
3601 #print "APW: ALLOWED: lines block<$block>\n";
3602 $allowed = 1;
3603 }
3604 # Check the post-context.
3605 if (defined $chunks[1]) {
3606 my ($cond, $block) = @{$chunks[1]};
3607 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003608 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003609 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003610 if ($block =~ /^\s*\{/) {
3611 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3612 $allowed = 1;
3613 }
3614 }
3615 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003616 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003617 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003618
Andy Whitcroftf0556632008-10-15 22:02:23 -07003619 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003620 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003621 }
3622
Joe Perches000d1cc12011-07-25 17:13:25 -07003623 WARN("BRACES",
3624 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003625 }
3626 }
3627
Joe Perches0979ae62012-12-17 16:01:59 -08003628# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003629 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003630 CHK("BRACES",
3631 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3632 }
Joe Perches77b9a532013-07-03 15:05:29 -07003633 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003634 CHK("BRACES",
3635 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3636 }
3637
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003638# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003639 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3640 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003641 WARN("VOLATILE",
3642 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003643 }
3644
Andy Whitcroft00df3442007-06-08 13:47:06 -07003645# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003646 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003647 CHK("REDUNDANT_CODE",
3648 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003649 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003650 }
3651
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003652# check for needless "if (<foo>) fn(<foo>)" uses
3653 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3654 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3655 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3656 WARN('NEEDLESS_IF',
3657 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003658 }
3659 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003660
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003661# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003662 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003663 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003664 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003665 CHK("USLEEP_RANGE",
3666 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003667 }
3668 }
3669
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003670# warn about unexpectedly long msleep's
3671 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3672 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003673 WARN("MSLEEP",
3674 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003675 }
3676 }
3677
Joe Perches36ec1932013-07-03 15:05:25 -07003678# check for comparisons of jiffies
3679 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3680 WARN("JIFFIES_COMPARISON",
3681 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3682 }
3683
Joe Perches9d7a34a2013-07-03 15:05:26 -07003684# check for comparisons of get_jiffies_64()
3685 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3686 WARN("JIFFIES_COMPARISON",
3687 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3688 }
3689
Andy Whitcroft00df3442007-06-08 13:47:06 -07003690# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003691# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003692# print "#ifdef in C files should be avoided\n";
3693# print "$herecurr";
3694# $clean = 0;
3695# }
3696
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003697# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003698 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003699 if (ERROR("SPACING",
3700 "exactly one space required after that #$1\n" . $herecurr) &&
3701 $fix) {
3702 $fixed[$linenr - 1] =~
3703 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3704 }
3705
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003706 }
3707
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003708# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003709 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3710 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003711 my $which = $1;
3712 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003713 CHK("UNCOMMENTED_DEFINITION",
3714 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003715 }
3716 }
3717# check for memory barriers without a comment.
3718 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3719 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003720 CHK("MEMORY_BARRIER",
3721 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003722 }
3723 }
3724# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003725 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003726 CHK("ARCH_DEFINES",
3727 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003728 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003729
Tobias Klauserd4977c72010-05-24 14:33:30 -07003730# Check that the storage class is at the beginning of a declaration
3731 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003732 WARN("STORAGE_CLASS",
3733 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003734 }
3735
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003736# check the location of the inline attribute, that it is between
3737# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003738 if ($line =~ /\b$Type\s+$Inline\b/ ||
3739 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003740 ERROR("INLINE_LOCATION",
3741 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003742 }
3743
Andy Whitcroft8905a672007-11-28 16:21:06 -08003744# Check for __inline__ and __inline, prefer inline
3745 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003746 if (WARN("INLINE",
3747 "plain inline is preferred over $1\n" . $herecurr) &&
3748 $fix) {
3749 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3750
3751 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003752 }
3753
Joe Perches3d130fd2011-01-12 17:00:00 -08003754# Check for __attribute__ packed, prefer __packed
3755 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003756 WARN("PREFER_PACKED",
3757 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003758 }
3759
Joe Perches39b7e282011-07-25 17:13:24 -07003760# Check for __attribute__ aligned, prefer __aligned
3761 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003762 WARN("PREFER_ALIGNED",
3763 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003764 }
3765
Joe Perches5f14d3b2012-01-10 15:09:52 -08003766# Check for __attribute__ format(printf, prefer __printf
3767 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003768 if (WARN("PREFER_PRINTF",
3769 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3770 $fix) {
3771 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3772
3773 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08003774 }
3775
Joe Perches6061d942012-03-23 15:02:16 -07003776# Check for __attribute__ format(scanf, prefer __scanf
3777 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003778 if (WARN("PREFER_SCANF",
3779 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3780 $fix) {
3781 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3782 }
Joe Perches6061d942012-03-23 15:02:16 -07003783 }
3784
Joe Perches8f53a9b2010-03-05 13:43:48 -08003785# check for sizeof(&)
3786 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003787 WARN("SIZEOF_ADDRESS",
3788 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003789 }
3790
Joe Perches66c80b62012-07-30 14:41:22 -07003791# check for sizeof without parenthesis
3792 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003793 if (WARN("SIZEOF_PARENTHESIS",
3794 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3795 $fix) {
3796 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3797 }
Joe Perches66c80b62012-07-30 14:41:22 -07003798 }
3799
Joe Perches428e2fd2011-05-24 17:13:39 -07003800# check for line continuations in quoted strings with odd counts of "
3801 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003802 WARN("LINE_CONTINUATIONS",
3803 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003804 }
3805
Joe Perches88982fe2012-12-17 16:02:00 -08003806# check for struct spinlock declarations
3807 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3808 WARN("USE_SPINLOCK_T",
3809 "struct spinlock should be spinlock_t\n" . $herecurr);
3810 }
3811
Joe Perchesa6962d72013-04-29 16:18:13 -07003812# check for seq_printf uses that could be seq_puts
3813 if ($line =~ /\bseq_printf\s*\(/) {
3814 my $fmt = get_quoted_string($line, $rawline);
3815 if ($fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003816 if (WARN("PREFER_SEQ_PUTS",
3817 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3818 $fix) {
3819 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3820 }
Joe Perchesa6962d72013-04-29 16:18:13 -07003821 }
3822 }
3823
Andy Whitcroft554e1652012-01-10 15:09:57 -08003824# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003825 if ($^V && $^V ge 5.10.0 &&
3826 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003827 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003828
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003829 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003830 my $ms_val = $7;
3831 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003832
Andy Whitcroft554e1652012-01-10 15:09:57 -08003833 if ($ms_size =~ /^(0x|)0$/i) {
3834 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003835 "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 -08003836 } elsif ($ms_size =~ /^(0x|)1$/i) {
3837 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003838 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3839 }
3840 }
3841
3842# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003843 if ($^V && $^V ge 5.10.0 &&
3844 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003845 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003846 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003847 my $call = $1;
3848 my $cast1 = deparenthesize($2);
3849 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003850 my $cast2 = deparenthesize($7);
3851 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003852 my $cast;
3853
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003854 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003855 $cast = "$cast1 or $cast2";
3856 } elsif ($cast1 ne "") {
3857 $cast = $cast1;
3858 } else {
3859 $cast = $cast2;
3860 }
3861 WARN("MINMAX",
3862 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003863 }
3864 }
3865
Joe Perches4a273192012-07-30 14:41:20 -07003866# check usleep_range arguments
3867 if ($^V && $^V ge 5.10.0 &&
3868 defined $stat &&
3869 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3870 my $min = $1;
3871 my $max = $7;
3872 if ($min eq $max) {
3873 WARN("USLEEP_RANGE",
3874 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3875 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3876 $min > $max) {
3877 WARN("USLEEP_RANGE",
3878 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3879 }
3880 }
3881
Joe Perches70dc8a42013-09-11 14:23:58 -07003882# check for new externs in .h files.
3883 if ($realfile =~ /\.h$/ &&
3884 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3885 if (WARN("AVOID_EXTERNS",
3886 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
3887 $fix) {
3888 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3889 }
3890 }
3891
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003892# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003893 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003894 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003895 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003896 my $function_name = $1;
3897 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003898
3899 my $s = $stat;
3900 if (defined $cond) {
3901 substr($s, 0, length($cond), '');
3902 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003903 if ($s =~ /^\s*;/ &&
3904 $function_name ne 'uninitialized_var')
3905 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003906 WARN("AVOID_EXTERNS",
3907 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003908 }
3909
3910 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003911 WARN("FUNCTION_ARGUMENTS",
3912 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003913 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003914
3915 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3916 $stat =~ /^.\s*extern\s+/)
3917 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003918 WARN("AVOID_EXTERNS",
3919 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003920 }
3921
3922# checks for new __setup's
3923 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3924 my $name = $1;
3925
3926 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003927 CHK("UNDOCUMENTED_SETUP",
3928 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003929 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003930 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003931
3932# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003933 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003934 WARN("UNNECESSARY_CASTS",
3935 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003936 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003937
Joe Perchesa640d252013-07-03 15:05:21 -07003938# alloc style
3939# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3940 if ($^V && $^V ge 5.10.0 &&
3941 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3942 CHK("ALLOC_SIZEOF_STRUCT",
3943 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3944 }
3945
Joe Perches972fdea2013-04-29 16:18:12 -07003946# check for krealloc arg reuse
3947 if ($^V && $^V ge 5.10.0 &&
3948 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3949 WARN("KREALLOC_ARG_REUSE",
3950 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3951 }
3952
Joe Perches5ce59ae2013-02-21 16:44:18 -08003953# check for alloc argument mismatch
3954 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3955 WARN("ALLOC_ARRAY_ARGS",
3956 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3957 }
3958
Joe Perchescaf2a542011-01-12 16:59:56 -08003959# check for multiple semicolons
3960 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003961 if (WARN("ONE_SEMICOLON",
3962 "Statements terminations use 1 semicolon\n" . $herecurr) &&
3963 $fix) {
3964 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
3965 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08003966 }
3967
3968# check for switch/default statements without a break;
3969 if ($^V && $^V ge 5.10.0 &&
3970 defined $stat &&
3971 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3972 my $ctx = '';
3973 my $herectx = $here . "\n";
3974 my $cnt = statement_rawlines($stat);
3975 for (my $n = 0; $n < $cnt; $n++) {
3976 $herectx .= raw_line($linenr, $n) . "\n";
3977 }
3978 WARN("DEFAULT_NO_BREAK",
3979 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08003980 }
3981
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003982# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07003983 if ($line =~ /\b__FUNCTION__\b/) {
3984 if (WARN("USE_FUNC",
3985 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
3986 $fix) {
3987 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
3988 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003989 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003990
Joe Perches2c924882012-03-23 15:02:20 -07003991# check for use of yield()
3992 if ($line =~ /\byield\s*\(\s*\)/) {
3993 WARN("YIELD",
3994 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3995 }
3996
Joe Perches179f8f42013-07-03 15:05:30 -07003997# check for comparisons against true and false
3998 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3999 my $lead = $1;
4000 my $arg = $2;
4001 my $test = $3;
4002 my $otype = $4;
4003 my $trail = $5;
4004 my $op = "!";
4005
4006 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4007
4008 my $type = lc($otype);
4009 if ($type =~ /^(?:true|false)$/) {
4010 if (("$test" eq "==" && "$type" eq "true") ||
4011 ("$test" eq "!=" && "$type" eq "false")) {
4012 $op = "";
4013 }
4014
4015 CHK("BOOL_COMPARISON",
4016 "Using comparison to $otype is error prone\n" . $herecurr);
4017
4018## maybe suggesting a correct construct would better
4019## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4020
4021 }
4022 }
4023
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004024# check for semaphores initialized locked
4025 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004026 WARN("CONSIDER_COMPLETION",
4027 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004028 }
Joe Perches6712d852012-03-23 15:02:20 -07004029
Joe Perches67d0a072011-10-31 17:13:10 -07004030# recommend kstrto* over simple_strto* and strict_strto*
4031 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004032 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004033 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004034 }
Joe Perches6712d852012-03-23 15:02:20 -07004035
Michael Ellermanf3db6632008-07-23 21:28:57 -07004036# check for __initcall(), use device_initcall() explicitly please
4037 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004038 WARN("USE_DEVICE_INITCALL",
4039 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07004040 }
Joe Perches6712d852012-03-23 15:02:20 -07004041
Emese Revfy79404842010-03-05 13:43:53 -08004042# check for various ops structs, ensure they are const.
4043 my $struct_ops = qr{acpi_dock_ops|
4044 address_space_operations|
4045 backlight_ops|
4046 block_device_operations|
4047 dentry_operations|
4048 dev_pm_ops|
4049 dma_map_ops|
4050 extent_io_ops|
4051 file_lock_operations|
4052 file_operations|
4053 hv_ops|
4054 ide_dma_ops|
4055 intel_dvo_dev_ops|
4056 item_operations|
4057 iwl_ops|
4058 kgdb_arch|
4059 kgdb_io|
4060 kset_uevent_ops|
4061 lock_manager_operations|
4062 microcode_ops|
4063 mtrr_ops|
4064 neigh_ops|
4065 nlmsvc_binding|
4066 pci_raw_ops|
4067 pipe_buf_operations|
4068 platform_hibernation_ops|
4069 platform_suspend_ops|
4070 proto_ops|
4071 rpc_pipe_ops|
4072 seq_operations|
4073 snd_ac97_build_ops|
4074 soc_pcmcia_socket_ops|
4075 stacktrace_ops|
4076 sysfs_ops|
4077 tty_operations|
4078 usb_mon_operations|
4079 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004080 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004081 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004082 WARN("CONST_STRUCT",
4083 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004084 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004085 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004086
4087# use of NR_CPUS is usually wrong
4088# ignore definitions of NR_CPUS and usage to define arrays as likely right
4089 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004090 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4091 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004092 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4093 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4094 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004095 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004096 WARN("NR_CPUS",
4097 "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 -07004098 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004099
4100# check for %L{u,d,i} in strings
4101 my $string;
4102 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4103 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004104 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004105 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004106 WARN("PRINTF_L",
4107 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004108 last;
4109 }
4110 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004111
4112# whine mightly about in_atomic
4113 if ($line =~ /\bin_atomic\s*\(/) {
4114 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004115 ERROR("IN_ATOMIC",
4116 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004117 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004118 WARN("IN_ATOMIC",
4119 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004120 }
4121 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004122
4123# check for lockdep_set_novalidate_class
4124 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4125 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4126 if ($realfile !~ m@^kernel/lockdep@ &&
4127 $realfile !~ m@^include/linux/lockdep@ &&
4128 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004129 ERROR("LOCKDEP",
4130 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004131 }
4132 }
Dave Jones88f88312011-01-12 16:59:59 -08004133
4134 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4135 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004136 WARN("EXPORTED_WORLD_WRITABLE",
4137 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004138 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004139 }
4140
4141 # If we have no input at all, then there is nothing to report on
4142 # so just keep quiet.
4143 if ($#rawlines == -1) {
4144 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004145 }
4146
Andy Whitcroft8905a672007-11-28 16:21:06 -08004147 # In mailback mode only produce a report in the negative, for
4148 # things that appear to be patches.
4149 if ($mailback && ($clean == 1 || !$is_patch)) {
4150 exit(0);
4151 }
4152
4153 # This is not a patch, and we are are in 'no-patch' mode so
4154 # just keep quiet.
4155 if (!$chk_patch && !$is_patch) {
4156 exit(0);
4157 }
4158
4159 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004160 ERROR("NOT_UNIFIED_DIFF",
4161 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004162 }
4163 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004164 ERROR("MISSING_SIGN_OFF",
4165 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004166 }
4167
Andy Whitcroft8905a672007-11-28 16:21:06 -08004168 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004169 if ($summary && !($clean == 1 && $quiet == 1)) {
4170 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004171 print "total: $cnt_error errors, $cnt_warn warnings, " .
4172 (($check)? "$cnt_chk checks, " : "") .
4173 "$cnt_lines lines checked\n";
4174 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004175 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004176
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004177 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004178
4179 if ($^V lt 5.10.0) {
4180 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4181 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4182 }
4183
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004184 # If there were whitespace errors which cleanpatch can fix
4185 # then suggest that.
4186 if ($rpt_cleaners) {
4187 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4188 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004189 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004190 }
4191 }
4192
Artem Bityutskiy11232682012-03-23 15:02:17 -07004193 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004194 print "NOTE: Ignored message types:";
4195 foreach my $ignore (sort keys %ignore_type) {
4196 print " $ignore";
4197 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07004198 print "\n\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07004199 }
4200
Joe Perches3705ce52013-07-03 15:05:31 -07004201 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4202 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4203 my $linecount = 0;
4204 my $f;
4205
4206 open($f, '>', $newfile)
4207 or die "$P: Can't open $newfile for write\n";
4208 foreach my $fixed_line (@fixed) {
4209 $linecount++;
4210 if ($file) {
4211 if ($linecount > 3) {
4212 $fixed_line =~ s/^\+//;
4213 print $f $fixed_line. "\n";
4214 }
4215 } else {
4216 print $f $fixed_line . "\n";
4217 }
4218 }
4219 close($f);
4220
4221 if (!$quiet) {
4222 print << "EOM";
4223Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4224
4225Do _NOT_ trust the results written to this file.
4226Do _NOT_ submit these changes without inspecting them for correctness.
4227
4228This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4229No warranties, expressed or implied...
4230
4231EOM
4232 }
4233 }
4234
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004235 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004236 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004237 }
4238 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004239 print << "EOM";
4240$vname has style problems, please review.
4241
4242If any of these errors are false positives, please report
4243them to the maintainer, see CHECKPATCH in MAINTAINERS.
4244EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004245 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004246
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004247 return $clean;
4248}