blob: 2ee9eb750560256187d4f236e328b5d7ad2656f8 [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;
Hannes Eder77f5b102009-09-21 17:04:37 -070040
41sub help {
42 my ($exitcode) = @_;
43
44 print << "EOM";
45Usage: $P [OPTION]... [FILE]...
46Version: $V
47
48Options:
49 -q, --quiet quiet
50 --no-tree run without a kernel tree
51 --no-signoff do not check for 'Signed-off-by' line
52 --patch treat FILE as patchfile (default)
53 --emacs emacs compile window format
54 --terse one line per report
55 -f, --file treat FILE as regular source file
56 --subjective, --strict enable more subjective tests
Joe Perches000d1cc12011-07-25 17:13:25 -070057 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080058 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070059 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070060 --root=PATH PATH to the kernel tree root
61 --no-summary suppress the per-file summary
62 --mailback only produce a report in case of warnings/errors
63 --summary-file include the filename in summary
64 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
65 'values', 'possible', 'type', and 'attr' (default
66 is all off)
67 --test-only=WORD report only warnings/errors containing WORD
68 literally
Joe Perches3705ce52013-07-03 15:05:31 -070069 --fix EXPERIMENTAL - may create horrible results
70 If correctable single-line errors exist, create
71 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
72 with potential errors corrected to the preferred
73 checkpatch style
Hannes Eder77f5b102009-09-21 17:04:37 -070074 -h, --help, --version display this help and exit
75
76When FILE is - read standard input.
77EOM
78
79 exit($exitcode);
80}
81
Joe Perches000d1cc12011-07-25 17:13:25 -070082my $conf = which_conf($configuration_file);
83if (-f $conf) {
84 my @conf_args;
85 open(my $conffile, '<', "$conf")
86 or warn "$P: Can't find a readable $configuration_file file $!\n";
87
88 while (<$conffile>) {
89 my $line = $_;
90
91 $line =~ s/\s*\n?$//g;
92 $line =~ s/^\s*//g;
93 $line =~ s/\s+/ /g;
94
95 next if ($line =~ m/^\s*#/);
96 next if ($line =~ m/^\s*$/);
97
98 my @words = split(" ", $line);
99 foreach my $word (@words) {
100 last if ($word =~ m/^#/);
101 push (@conf_args, $word);
102 }
103 }
104 close($conffile);
105 unshift(@ARGV, @conf_args) if @conf_args;
106}
107
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700108GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700109 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700110 'tree!' => \$tree,
111 'signoff!' => \$chk_signoff,
112 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800114 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700115 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700116 'subjective!' => \$check,
117 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700118 'ignore=s' => \@ignore,
119 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800120 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700121 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800122 'summary!' => \$summary,
123 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800124 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700125 'fix!' => \$fix,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800126 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700127 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700128 'h|help' => \$help,
129 'version' => \$help
130) or help(1);
131
132help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700133
134my $exit = 0;
135
136if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700137 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700138 exit(1);
139}
140
Joe Perches000d1cc12011-07-25 17:13:25 -0700141@ignore = split(/,/, join(',',@ignore));
142foreach my $word (@ignore) {
143 $word =~ s/\s*\n?$//g;
144 $word =~ s/^\s*//g;
145 $word =~ s/\s+/ /g;
146 $word =~ tr/[a-z]/[A-Z]/;
147
148 next if ($word =~ m/^\s*#/);
149 next if ($word =~ m/^\s*$/);
150
151 $ignore_type{$word}++;
152}
153
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800154my $dbg_values = 0;
155my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700156my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700157my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800158for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800159 ## no critic
160 eval "\${dbg_$key} = '$debug{$key}';";
161 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800162}
163
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700164my $rpt_cleaners = 0;
165
Andy Whitcroft8905a672007-11-28 16:21:06 -0800166if ($terse) {
167 $emacs = 1;
168 $quiet++;
169}
170
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700171if ($tree) {
172 if (defined $root) {
173 if (!top_of_kernel_tree($root)) {
174 die "$P: $root: --root does not point at a valid tree\n";
175 }
176 } else {
177 if (top_of_kernel_tree('.')) {
178 $root = '.';
179 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
180 top_of_kernel_tree($1)) {
181 $root = $1;
182 }
183 }
184
185 if (!defined $root) {
186 print "Must be run from the top-level dir. of a kernel tree\n";
187 exit(2);
188 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700189}
190
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700191my $emitted_corrupt = 0;
192
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700193our $Ident = qr{
194 [A-Za-z_][A-Za-z\d_]*
195 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
196 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700197our $Storage = qr{extern|static|asmlinkage};
198our $Sparse = qr{
199 __user|
200 __kernel|
201 __force|
202 __iomem|
203 __must_check|
204 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800205 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700206 __ref|
207 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800209
210# Notes to $Attribute:
211# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700212our $Attribute = qr{
213 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700214 __percpu|
215 __nocast|
216 __safe|
217 __bitwise__|
218 __packed__|
219 __packed2__|
220 __naked|
221 __maybe_unused|
222 __always_unused|
223 __noreturn|
224 __used|
225 __cold|
226 __noclone|
227 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228 __read_mostly|
229 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800230 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700231 ____cacheline_aligned|
232 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800233 ____cacheline_internodealigned_in_smp|
234 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700235 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700236our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700237our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700238our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
239our $Lval = qr{$Ident(?:$Member)*};
240
Joe Perches95e2c602013-07-03 15:05:20 -0700241our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
242our $Binary = qr{(?i)0b[01]+$Int_type?};
243our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
244our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800245our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
246our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
247our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800248our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700249our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800250our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800251our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700252our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700253our $Operators = qr{
254 <=|>=|==|!=|
255 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700256 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700257 }x;
258
Andy Whitcroft8905a672007-11-28 16:21:06 -0800259our $NonptrType;
260our $Type;
261our $Declare;
262
Joe Perches15662b32011-10-31 17:13:12 -0700263our $NON_ASCII_UTF8 = qr{
264 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700265 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
266 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
267 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
268 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
269 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
270 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
271}x;
272
Joe Perches15662b32011-10-31 17:13:12 -0700273our $UTF8 = qr{
274 [\x09\x0A\x0D\x20-\x7E] # ASCII
275 | $NON_ASCII_UTF8
276}x;
277
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700278our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700279 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700280 atomic_t
281)};
282
Joe Perches691e6692010-03-05 13:43:51 -0800283our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700284 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700285 (?:[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 -0700286 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700287 panic|
288 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800289)};
290
Joe Perches20112472011-07-25 17:13:23 -0700291our $signature_tags = qr{(?xi:
292 Signed-off-by:|
293 Acked-by:|
294 Tested-by:|
295 Reviewed-by:|
296 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700297 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700298 To:|
299 Cc:
300)};
301
Andy Whitcroft8905a672007-11-28 16:21:06 -0800302our @typeList = (
303 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700304 qr{(?:unsigned\s+)?char},
305 qr{(?:unsigned\s+)?short},
306 qr{(?:unsigned\s+)?int},
307 qr{(?:unsigned\s+)?long},
308 qr{(?:unsigned\s+)?long\s+int},
309 qr{(?:unsigned\s+)?long\s+long},
310 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800311 qr{unsigned},
312 qr{float},
313 qr{double},
314 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800315 qr{struct\s+$Ident},
316 qr{union\s+$Ident},
317 qr{enum\s+$Ident},
318 qr{${Ident}_t},
319 qr{${Ident}_handler},
320 qr{${Ident}_handler_fn},
321);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700322our @modifierList = (
323 qr{fastcall},
324);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800325
Wolfram Sang7840a942010-08-09 17:20:57 -0700326our $allowed_asm_includes = qr{(?x:
327 irq|
328 memory
329)};
330# memory.h: ARM has a custom one
331
Andy Whitcroft8905a672007-11-28 16:21:06 -0800332sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700333 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
334 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700335 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800336 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700337 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800338 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800339 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700340 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700341 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800342 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700343 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800344 }x;
345 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700346 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700347 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700348 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800349 }x;
350 $Declare = qr{(?:$Storage\s+)?$Type};
351}
352build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700353
Joe Perches7d2367a2011-07-25 17:13:22 -0700354our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700355
356# Using $balanced_parens, $LvalOrFunc, or $FuncArg
357# requires at least perl version v5.10.0
358# Any use must be runtime checked with $^V
359
360our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
361our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800362our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700363
364sub deparenthesize {
365 my ($string) = @_;
366 return "" if (!defined($string));
367 $string =~ s@^\s*\(\s*@@g;
368 $string =~ s@\s*\)\s*$@@g;
369 $string =~ s@\s+@ @g;
370 return $string;
371}
372
Joe Perches34456862013-07-03 15:05:34 -0700373sub seed_camelcase_file {
374 my ($file) = @_;
375
376 return if (!(-f $file));
377
378 local $/;
379
380 open(my $include_file, '<', "$file")
381 or warn "$P: Can't read '$file' $!\n";
382 my $text = <$include_file>;
383 close($include_file);
384
385 my @lines = split('\n', $text);
386
387 foreach my $line (@lines) {
388 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
389 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
390 $camelcase{$1} = 1;
391 }
392 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
393 $camelcase{$1} = 1;
394 }
395 }
396}
397
398my $camelcase_seeded = 0;
399sub seed_camelcase_includes {
400 return if ($camelcase_seeded);
401
402 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700403 my $camelcase_cache = "";
404 my @include_files = ();
405
406 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700407
Joe Perches34456862013-07-03 15:05:34 -0700408 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700409 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
410 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700411 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700412 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700413 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700414 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700415 @include_files = split('\n', $files);
416 foreach my $file (@include_files) {
417 my $date = POSIX::strftime("%Y%m%d%H%M",
418 localtime((stat $file)[9]));
419 $last_mod_date = $date if ($last_mod_date < $date);
420 }
421 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700422 }
Joe Perchesc707a812013-07-08 16:00:43 -0700423
424 if ($camelcase_cache ne "" && -f $camelcase_cache) {
425 open(my $camelcase_file, '<', "$camelcase_cache")
426 or warn "$P: Can't read '$camelcase_cache' $!\n";
427 while (<$camelcase_file>) {
428 chomp;
429 $camelcase{$_} = 1;
430 }
431 close($camelcase_file);
432
433 return;
434 }
435
436 if (-d ".git") {
437 $files = `git ls-files "include/*.h"`;
438 @include_files = split('\n', $files);
439 }
440
Joe Perches34456862013-07-03 15:05:34 -0700441 foreach my $file (@include_files) {
442 seed_camelcase_file($file);
443 }
Joe Perches351b2a12013-07-03 15:05:36 -0700444
Joe Perchesc707a812013-07-08 16:00:43 -0700445 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700446 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700447 open(my $camelcase_file, '>', "$camelcase_cache")
448 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700449 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
450 print $camelcase_file ("$_\n");
451 }
452 close($camelcase_file);
453 }
Joe Perches34456862013-07-03 15:05:34 -0700454}
455
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700456$chk_signoff = 0 if ($file);
457
Andy Whitcroft00df3442007-06-08 13:47:06 -0700458my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800459my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700460my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800461my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700462for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800463 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700464 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800465 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700466 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800467 } elsif ($filename eq '-') {
468 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700469 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800470 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700471 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700472 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800473 if ($filename eq '-') {
474 $vname = 'Your patch';
475 } else {
476 $vname = $filename;
477 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800478 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700479 chomp;
480 push(@rawlines, $_);
481 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800482 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800483 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700484 $exit = 1;
485 }
486 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800487 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700488 @fixed = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700489}
490
491exit($exit);
492
493sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700494 my ($root) = @_;
495
496 my @tree_check = (
497 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
498 "README", "Documentation", "arch", "include", "drivers",
499 "fs", "init", "ipc", "kernel", "lib", "scripts",
500 );
501
502 foreach my $check (@tree_check) {
503 if (! -e $root . '/' . $check) {
504 return 0;
505 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700506 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700507 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700508}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700509
Joe Perches20112472011-07-25 17:13:23 -0700510sub parse_email {
511 my ($formatted_email) = @_;
512
513 my $name = "";
514 my $address = "";
515 my $comment = "";
516
517 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
518 $name = $1;
519 $address = $2;
520 $comment = $3 if defined $3;
521 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
522 $address = $1;
523 $comment = $2 if defined $2;
524 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
525 $address = $1;
526 $comment = $2 if defined $2;
527 $formatted_email =~ s/$address.*$//;
528 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700529 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700530 $name =~ s/^\"|\"$//g;
531 # If there's a name left after stripping spaces and
532 # leading quotes, and the address doesn't have both
533 # leading and trailing angle brackets, the address
534 # is invalid. ie:
535 # "joe smith joe@smith.com" bad
536 # "joe smith <joe@smith.com" bad
537 if ($name ne "" && $address !~ /^<[^>]+>$/) {
538 $name = "";
539 $address = "";
540 $comment = "";
541 }
542 }
543
Joe Perches3705ce52013-07-03 15:05:31 -0700544 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700545 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700546 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700547 $address =~ s/^\<|\>$//g;
548
549 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
550 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
551 $name = "\"$name\"";
552 }
553
554 return ($name, $address, $comment);
555}
556
557sub format_email {
558 my ($name, $address) = @_;
559
560 my $formatted_email;
561
Joe Perches3705ce52013-07-03 15:05:31 -0700562 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700563 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700564 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700565
566 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
567 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
568 $name = "\"$name\"";
569 }
570
571 if ("$name" eq "") {
572 $formatted_email = "$address";
573 } else {
574 $formatted_email = "$name <$address>";
575 }
576
577 return $formatted_email;
578}
579
Joe Perches000d1cc12011-07-25 17:13:25 -0700580sub which_conf {
581 my ($conf) = @_;
582
583 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
584 if (-e "$path/$conf") {
585 return "$path/$conf";
586 }
587 }
588
589 return "";
590}
591
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700592sub expand_tabs {
593 my ($str) = @_;
594
595 my $res = '';
596 my $n = 0;
597 for my $c (split(//, $str)) {
598 if ($c eq "\t") {
599 $res .= ' ';
600 $n++;
601 for (; ($n % 8) != 0; $n++) {
602 $res .= ' ';
603 }
604 next;
605 }
606 $res .= $c;
607 $n++;
608 }
609
610 return $res;
611}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700612sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700613 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700614 return $res;
615}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700616
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700617sub line_stats {
618 my ($line) = @_;
619
620 # Drop the diff line leader and expand tabs
621 $line =~ s/^.//;
622 $line = expand_tabs($line);
623
624 # Pick the indent from the front of the line.
625 my ($white) = ($line =~ /^(\s*)/);
626
627 return (length($line), length($white));
628}
629
Andy Whitcroft773647a2008-03-28 14:15:58 -0700630my $sanitise_quote = '';
631
632sub sanitise_line_reset {
633 my ($in_comment) = @_;
634
635 if ($in_comment) {
636 $sanitise_quote = '*/';
637 } else {
638 $sanitise_quote = '';
639 }
640}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700641sub sanitise_line {
642 my ($line) = @_;
643
644 my $res = '';
645 my $l = '';
646
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800647 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700648 my $off = 0;
649 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700650
Andy Whitcroft773647a2008-03-28 14:15:58 -0700651 # Always copy over the diff marker.
652 $res = substr($line, 0, 1);
653
654 for ($off = 1; $off < length($line); $off++) {
655 $c = substr($line, $off, 1);
656
657 # Comments we are wacking completly including the begin
658 # and end, all to $;.
659 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
660 $sanitise_quote = '*/';
661
662 substr($res, $off, 2, "$;$;");
663 $off++;
664 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800665 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700666 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700667 $sanitise_quote = '';
668 substr($res, $off, 2, "$;$;");
669 $off++;
670 next;
671 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700672 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
673 $sanitise_quote = '//';
674
675 substr($res, $off, 2, $sanitise_quote);
676 $off++;
677 next;
678 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700679
680 # A \ in a string means ignore the next character.
681 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
682 $c eq "\\") {
683 substr($res, $off, 2, 'XX');
684 $off++;
685 next;
686 }
687 # Regular quotes.
688 if ($c eq "'" || $c eq '"') {
689 if ($sanitise_quote eq '') {
690 $sanitise_quote = $c;
691
692 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700693 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700694 } elsif ($sanitise_quote eq $c) {
695 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700696 }
697 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700698
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800699 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700700 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
701 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700702 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
703 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700704 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
705 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700706 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700707 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700708 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800709 }
710
Daniel Walker113f04a2009-09-21 17:04:35 -0700711 if ($sanitise_quote eq '//') {
712 $sanitise_quote = '';
713 }
714
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800715 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700716 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800717 my $clean = 'X' x length($1);
718 $res =~ s@\<.*\>@<$clean>@;
719
720 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700721 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800722 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700723 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800724 }
725
Andy Whitcroft00df3442007-06-08 13:47:06 -0700726 return $res;
727}
728
Joe Perchesa6962d72013-04-29 16:18:13 -0700729sub get_quoted_string {
730 my ($line, $rawline) = @_;
731
732 return "" if ($line !~ m/(\"[X]+\")/g);
733 return substr($rawline, $-[0], $+[0] - $-[0]);
734}
735
Andy Whitcroft8905a672007-11-28 16:21:06 -0800736sub ctx_statement_block {
737 my ($linenr, $remain, $off) = @_;
738 my $line = $linenr - 1;
739 my $blk = '';
740 my $soff = $off;
741 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700742 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800743
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800744 my $loff = 0;
745
Andy Whitcroft8905a672007-11-28 16:21:06 -0800746 my $type = '';
747 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800748 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800749 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800750 my $c;
751 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800752
753 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800754 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800755 @stack = (['', 0]) if ($#stack == -1);
756
Andy Whitcroft773647a2008-03-28 14:15:58 -0700757 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800758 # If we are about to drop off the end, pull in more
759 # context.
760 if ($off >= $len) {
761 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700762 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800763 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800764 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800765 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800766 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800767 $len = length($blk);
768 $line++;
769 last;
770 }
771 # Bail if there is no further context.
772 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800773 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800774 last;
775 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800776 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
777 $level++;
778 $type = '#';
779 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800780 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800781 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800782 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800783 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800784
Andy Whitcroft773647a2008-03-28 14:15:58 -0700785 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800786
787 # Handle nested #if/#else.
788 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
789 push(@stack, [ $type, $level ]);
790 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
791 ($type, $level) = @{$stack[$#stack - 1]};
792 } elsif ($remainder =~ /^#\s*endif\b/) {
793 ($type, $level) = @{pop(@stack)};
794 }
795
Andy Whitcroft8905a672007-11-28 16:21:06 -0800796 # Statement ends at the ';' or a close '}' at the
797 # outermost level.
798 if ($level == 0 && $c eq ';') {
799 last;
800 }
801
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800802 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700803 if ($level == 0 && $coff_set == 0 &&
804 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
805 $remainder =~ /^(else)(?:\s|{)/ &&
806 $remainder !~ /^else\s+if\b/) {
807 $coff = $off + length($1) - 1;
808 $coff_set = 1;
809 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
810 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800811 }
812
Andy Whitcroft8905a672007-11-28 16:21:06 -0800813 if (($type eq '' || $type eq '(') && $c eq '(') {
814 $level++;
815 $type = '(';
816 }
817 if ($type eq '(' && $c eq ')') {
818 $level--;
819 $type = ($level != 0)? '(' : '';
820
821 if ($level == 0 && $coff < $soff) {
822 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700823 $coff_set = 1;
824 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800825 }
826 }
827 if (($type eq '' || $type eq '{') && $c eq '{') {
828 $level++;
829 $type = '{';
830 }
831 if ($type eq '{' && $c eq '}') {
832 $level--;
833 $type = ($level != 0)? '{' : '';
834
835 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700836 if (substr($blk, $off + 1, 1) eq ';') {
837 $off++;
838 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800839 last;
840 }
841 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800842 # Preprocessor commands end at the newline unless escaped.
843 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
844 $level--;
845 $type = '';
846 $off++;
847 last;
848 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800849 $off++;
850 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700851 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800852 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700853 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800854 $line++;
855 $remain--;
856 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800857
858 my $statement = substr($blk, $soff, $off - $soff + 1);
859 my $condition = substr($blk, $soff, $coff - $soff + 1);
860
861 #warn "STATEMENT<$statement>\n";
862 #warn "CONDITION<$condition>\n";
863
Andy Whitcroft773647a2008-03-28 14:15:58 -0700864 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800865
866 return ($statement, $condition,
867 $line, $remain + 1, $off - $loff + 1, $level);
868}
869
Andy Whitcroftcf655042008-03-04 14:28:20 -0800870sub statement_lines {
871 my ($stmt) = @_;
872
873 # Strip the diff line prefixes and rip blank lines at start and end.
874 $stmt =~ s/(^|\n)./$1/g;
875 $stmt =~ s/^\s*//;
876 $stmt =~ s/\s*$//;
877
878 my @stmt_lines = ($stmt =~ /\n/g);
879
880 return $#stmt_lines + 2;
881}
882
883sub statement_rawlines {
884 my ($stmt) = @_;
885
886 my @stmt_lines = ($stmt =~ /\n/g);
887
888 return $#stmt_lines + 2;
889}
890
891sub statement_block_size {
892 my ($stmt) = @_;
893
894 $stmt =~ s/(^|\n)./$1/g;
895 $stmt =~ s/^\s*{//;
896 $stmt =~ s/}\s*$//;
897 $stmt =~ s/^\s*//;
898 $stmt =~ s/\s*$//;
899
900 my @stmt_lines = ($stmt =~ /\n/g);
901 my @stmt_statements = ($stmt =~ /;/g);
902
903 my $stmt_lines = $#stmt_lines + 2;
904 my $stmt_statements = $#stmt_statements + 1;
905
906 if ($stmt_lines > $stmt_statements) {
907 return $stmt_lines;
908 } else {
909 return $stmt_statements;
910 }
911}
912
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800913sub ctx_statement_full {
914 my ($linenr, $remain, $off) = @_;
915 my ($statement, $condition, $level);
916
917 my (@chunks);
918
Andy Whitcroftcf655042008-03-04 14:28:20 -0800919 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800920 ($statement, $condition, $linenr, $remain, $off, $level) =
921 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700922 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800923 push(@chunks, [ $condition, $statement ]);
924 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
925 return ($level, $linenr, @chunks);
926 }
927
928 # Pull in the following conditional/block pairs and see if they
929 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800930 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800931 ($statement, $condition, $linenr, $remain, $off, $level) =
932 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800933 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700934 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800935 #print "C: push\n";
936 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800937 }
938
939 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800940}
941
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700942sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700943 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700944 my $line;
945 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700946 my $blk = '';
947 my @o;
948 my @c;
949 my @res = ();
950
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700951 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800952 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700953 for ($line = $start; $remain > 0; $line++) {
954 next if ($rawlines[$line] =~ /^-/);
955 $remain--;
956
957 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800958
959 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700960 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800961 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700962 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800963 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700964 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800965 $level = pop(@stack);
966 }
967
Andy Whitcroft01464f32010-10-26 14:23:19 -0700968 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700969 ##print "C<$c>L<$level><$open$close>O<$off>\n";
970 if ($off > 0) {
971 $off--;
972 next;
973 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700974
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700975 if ($c eq $close && $level > 0) {
976 $level--;
977 last if ($level == 0);
978 } elsif ($c eq $open) {
979 $level++;
980 }
981 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700982
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700983 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700984 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700985 }
986
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700987 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700988 }
989
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700990 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700991}
992sub ctx_block_outer {
993 my ($linenr, $remain) = @_;
994
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700995 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
996 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700997}
998sub ctx_block {
999 my ($linenr, $remain) = @_;
1000
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001001 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1002 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001003}
1004sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001005 my ($linenr, $remain, $off) = @_;
1006
1007 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1008 return @r;
1009}
1010sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001011 my ($linenr, $remain) = @_;
1012
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001013 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001014}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001015sub ctx_statement_level {
1016 my ($linenr, $remain, $off) = @_;
1017
1018 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1019}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001020
1021sub ctx_locate_comment {
1022 my ($first_line, $end_line) = @_;
1023
1024 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001025 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001026 return $current_comment if (defined $current_comment);
1027
1028 # Look through the context and try and figure out if there is a
1029 # comment.
1030 my $in_comment = 0;
1031 $current_comment = '';
1032 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001033 my $line = $rawlines[$linenr - 1];
1034 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001035 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1036 $in_comment = 1;
1037 }
1038 if ($line =~ m@/\*@) {
1039 $in_comment = 1;
1040 }
1041 if (!$in_comment && $current_comment ne '') {
1042 $current_comment = '';
1043 }
1044 $current_comment .= $line . "\n" if ($in_comment);
1045 if ($line =~ m@\*/@) {
1046 $in_comment = 0;
1047 }
1048 }
1049
1050 chomp($current_comment);
1051 return($current_comment);
1052}
1053sub ctx_has_comment {
1054 my ($first_line, $end_line) = @_;
1055 my $cmt = ctx_locate_comment($first_line, $end_line);
1056
Andy Whitcroft00df3442007-06-08 13:47:06 -07001057 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001058 ##print "CMMT: $cmt\n";
1059
1060 return ($cmt ne '');
1061}
1062
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001063sub raw_line {
1064 my ($linenr, $cnt) = @_;
1065
1066 my $offset = $linenr - 1;
1067 $cnt++;
1068
1069 my $line;
1070 while ($cnt) {
1071 $line = $rawlines[$offset++];
1072 next if (defined($line) && $line =~ /^-/);
1073 $cnt--;
1074 }
1075
1076 return $line;
1077}
1078
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001079sub cat_vet {
1080 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001081 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001082
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001083 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001084 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1085 $res .= $1;
1086 if ($2 ne '') {
1087 $coded = sprintf("^%c", unpack('C', $2) + 64);
1088 $res .= $coded;
1089 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001090 }
1091 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001092
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001093 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001094}
1095
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001096my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001097my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001098my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001099my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001100
1101sub annotate_reset {
1102 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001103 $av_pending = '_';
1104 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001105 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001106}
1107
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001108sub annotate_values {
1109 my ($stream, $type) = @_;
1110
1111 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001112 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001113 my $cur = $stream;
1114
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001115 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001116
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001117 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001118 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001119 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001120 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001121 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001122 print "WS($1)\n" if ($dbg_values > 1);
1123 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001124 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001125 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001126 }
1127
Florian Micklerc023e4732011-01-12 16:59:58 -08001128 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001129 print "CAST($1)\n" if ($dbg_values > 1);
1130 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001131 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001132
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001133 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001134 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001135 $type = 'T';
1136
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001137 } elsif ($cur =~ /^($Modifier)\s*/) {
1138 print "MODIFIER($1)\n" if ($dbg_values > 1);
1139 $type = 'T';
1140
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001141 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001142 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001143 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001144 push(@av_paren_type, $type);
1145 if ($2 ne '') {
1146 $av_pending = 'N';
1147 }
1148 $type = 'E';
1149
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001150 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001151 print "UNDEF($1)\n" if ($dbg_values > 1);
1152 $av_preprocessor = 1;
1153 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001154
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001155 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001156 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001157 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001158
1159 push(@av_paren_type, $type);
1160 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001161 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001162
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001163 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001164 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1165 $av_preprocessor = 1;
1166
1167 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1168
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001169 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001170
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001171 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001172 print "PRE_END($1)\n" if ($dbg_values > 1);
1173
1174 $av_preprocessor = 1;
1175
1176 # Assume all arms of the conditional end as this
1177 # one does, and continue as if the #endif was not here.
1178 pop(@av_paren_type);
1179 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001180 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001181
1182 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001183 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001184
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001185 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1186 print "ATTR($1)\n" if ($dbg_values > 1);
1187 $av_pending = $type;
1188 $type = 'N';
1189
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001190 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001191 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001192 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001193 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001194 }
1195 $type = 'N';
1196
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001197 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001198 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001199 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001200 $type = 'N';
1201
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001202 } elsif ($cur =~/^(case)/o) {
1203 print "CASE($1)\n" if ($dbg_values > 1);
1204 $av_pend_colon = 'C';
1205 $type = 'N';
1206
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001207 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001208 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001209 $type = 'N';
1210
1211 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001212 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001213 push(@av_paren_type, $av_pending);
1214 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001215 $type = 'N';
1216
1217 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001218 my $new_type = pop(@av_paren_type);
1219 if ($new_type ne '_') {
1220 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001221 print "PAREN('$1') -> $type\n"
1222 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001223 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001224 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001225 }
1226
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001227 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001228 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001229 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001230 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001231
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001232 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1233 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001234 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001235 } elsif ($type eq 'E') {
1236 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001237 }
1238 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1239 $type = 'V';
1240
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001241 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001242 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001243 $type = 'V';
1244
1245 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001246 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001247 $type = 'N';
1248
Andy Whitcroftcf655042008-03-04 14:28:20 -08001249 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001250 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001251 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001252 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001253
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001254 } elsif ($cur =~/^(,)/) {
1255 print "COMMA($1)\n" if ($dbg_values > 1);
1256 $type = 'C';
1257
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001258 } elsif ($cur =~ /^(\?)/o) {
1259 print "QUESTION($1)\n" if ($dbg_values > 1);
1260 $type = 'N';
1261
1262 } elsif ($cur =~ /^(:)/o) {
1263 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1264
1265 substr($var, length($res), 1, $av_pend_colon);
1266 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1267 $type = 'E';
1268 } else {
1269 $type = 'N';
1270 }
1271 $av_pend_colon = 'O';
1272
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001273 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001274 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001275 $type = 'N';
1276
Andy Whitcroft0d413862008-10-15 22:02:16 -07001277 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001278 my $variant;
1279
1280 print "OPV($1)\n" if ($dbg_values > 1);
1281 if ($type eq 'V') {
1282 $variant = 'B';
1283 } else {
1284 $variant = 'U';
1285 }
1286
1287 substr($var, length($res), 1, $variant);
1288 $type = 'N';
1289
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001290 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001291 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001292 if ($1 ne '++' && $1 ne '--') {
1293 $type = 'N';
1294 }
1295
1296 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001298 }
1299 if (defined $1) {
1300 $cur = substr($cur, length($1));
1301 $res .= $type x length($1);
1302 }
1303 }
1304
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001305 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001306}
1307
Andy Whitcroft8905a672007-11-28 16:21:06 -08001308sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001309 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001310 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001311 ^(?:
1312 $Modifier|
1313 $Storage|
1314 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001315 DEFINE_\S+
1316 )$|
1317 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001318 goto|
1319 return|
1320 case|
1321 else|
1322 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001323 do|
1324 \#|
1325 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001326 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001327 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001328 )}x;
1329 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1330 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001331 # Check for modifiers.
1332 $possible =~ s/\s*$Storage\s*//g;
1333 $possible =~ s/\s*$Sparse\s*//g;
1334 if ($possible =~ /^\s*$/) {
1335
1336 } elsif ($possible =~ /\s/) {
1337 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001338 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001339 if ($modifier !~ $notPermitted) {
1340 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1341 push(@modifierList, $modifier);
1342 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001343 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001344
1345 } else {
1346 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1347 push(@typeList, $possible);
1348 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001349 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001350 } else {
1351 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 }
1353}
1354
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001355my $prefix = '';
1356
Joe Perches000d1cc12011-07-25 17:13:25 -07001357sub show_type {
1358 return !defined $ignore_type{$_[0]};
1359}
1360
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001361sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001362 if (!show_type($_[1]) ||
1363 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001364 return 0;
1365 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001366 my $line;
1367 if ($show_types) {
1368 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1369 } else {
1370 $line = "$prefix$_[0]: $_[2]\n";
1371 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001372 $line = (split('\n', $line))[0] . "\n" if ($terse);
1373
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001374 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001375
1376 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001377}
1378sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001379 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001380}
Joe Perches000d1cc12011-07-25 17:13:25 -07001381
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001382sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001383 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001384 our $clean = 0;
1385 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001386 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001387 }
Joe Perches3705ce52013-07-03 15:05:31 -07001388 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001389}
1390sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001391 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001392 our $clean = 0;
1393 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001394 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001395 }
Joe Perches3705ce52013-07-03 15:05:31 -07001396 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001397}
1398sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001399 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001400 our $clean = 0;
1401 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001402 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001403 }
Joe Perches3705ce52013-07-03 15:05:31 -07001404 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001405}
1406
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001407sub check_absolute_file {
1408 my ($absolute, $herecurr) = @_;
1409 my $file = $absolute;
1410
1411 ##print "absolute<$absolute>\n";
1412
1413 # See if any suffix of this path is a path within the tree.
1414 while ($file =~ s@^[^/]*/@@) {
1415 if (-f "$root/$file") {
1416 ##print "file<$file>\n";
1417 last;
1418 }
1419 }
1420 if (! -f _) {
1421 return 0;
1422 }
1423
1424 # It is, so see if the prefix is acceptable.
1425 my $prefix = $absolute;
1426 substr($prefix, -length($file)) = '';
1427
1428 ##print "prefix<$prefix>\n";
1429 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001430 WARN("USE_RELATIVE_PATH",
1431 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001432 }
1433}
1434
Joe Perches3705ce52013-07-03 15:05:31 -07001435sub trim {
1436 my ($string) = @_;
1437
1438 $string =~ s/(^\s+|\s+$)//g;
1439
1440 return $string;
1441}
1442
1443sub tabify {
1444 my ($leading) = @_;
1445
1446 my $source_indent = 8;
1447 my $max_spaces_before_tab = $source_indent - 1;
1448 my $spaces_to_tab = " " x $source_indent;
1449
1450 #convert leading spaces to tabs
1451 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1452 #Remove spaces before a tab
1453 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1454
1455 return "$leading";
1456}
1457
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001458sub pos_last_openparen {
1459 my ($line) = @_;
1460
1461 my $pos = 0;
1462
1463 my $opens = $line =~ tr/\(/\(/;
1464 my $closes = $line =~ tr/\)/\)/;
1465
1466 my $last_openparen = 0;
1467
1468 if (($opens == 0) || ($closes >= $opens)) {
1469 return -1;
1470 }
1471
1472 my $len = length($line);
1473
1474 for ($pos = 0; $pos < $len; $pos++) {
1475 my $string = substr($line, $pos);
1476 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1477 $pos += length($1) - 1;
1478 } elsif (substr($line, $pos, 1) eq '(') {
1479 $last_openparen = $pos;
1480 } elsif (index($string, '(') == -1) {
1481 last;
1482 }
1483 }
1484
1485 return $last_openparen + 1;
1486}
1487
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001488sub process {
1489 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001490
1491 my $linenr=0;
1492 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001493 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001494 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001495 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001496
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001497 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001498 my $indent;
1499 my $previndent=0;
1500 my $stashindent=0;
1501
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001502 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001503 my $signoff = 0;
1504 my $is_patch = 0;
1505
Joe Perches15662b32011-10-31 17:13:12 -07001506 my $in_header_lines = 1;
1507 my $in_commit_log = 0; #Scanning lines before patch
1508
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001509 my $non_utf8_charset = 0;
1510
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001511 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001512 our $cnt_lines = 0;
1513 our $cnt_error = 0;
1514 our $cnt_warn = 0;
1515 our $cnt_chk = 0;
1516
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001517 # Trace the real file/line as we go.
1518 my $realfile = '';
1519 my $realline = 0;
1520 my $realcnt = 0;
1521 my $here = '';
1522 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001523 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001524 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001525 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001526
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001527 my $prev_values = 'E';
1528
1529 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001530 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001531 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001532 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001533 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001534
Joe Perches323c1262012-12-17 16:02:07 -08001535
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001536 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001537 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001538 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001539 my @setup_docs = ();
1540 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001541
1542 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001543 my $line;
1544 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001545 $linenr++;
1546 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001547
Joe Perches3705ce52013-07-03 15:05:31 -07001548 push(@fixed, $rawline) if ($fix);
1549
Andy Whitcroft773647a2008-03-28 14:15:58 -07001550 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001551 $setup_docs = 0;
1552 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1553 $setup_docs = 1;
1554 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001555 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001556 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001557 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1558 $realline=$1-1;
1559 if (defined $2) {
1560 $realcnt=$3+1;
1561 } else {
1562 $realcnt=1+1;
1563 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001564 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001565
1566 # Guestimate if this is a continuing comment. Run
1567 # the context looking for a comment "edge". If this
1568 # edge is a close comment then we must be in a comment
1569 # at context start.
1570 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001571 my $cnt = $realcnt;
1572 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1573 next if (defined $rawlines[$ln - 1] &&
1574 $rawlines[$ln - 1] =~ /^-/);
1575 $cnt--;
1576 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001577 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001578 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1579 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1580 ($edge) = $1;
1581 last;
1582 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001583 }
1584 if (defined $edge && $edge eq '*/') {
1585 $in_comment = 1;
1586 }
1587
1588 # Guestimate if this is a continuing comment. If this
1589 # is the start of a diff block and this line starts
1590 # ' *' then it is very likely a comment.
1591 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001592 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001593 {
1594 $in_comment = 1;
1595 }
1596
1597 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1598 sanitise_line_reset($in_comment);
1599
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001600 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001601 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001602 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001603 $line = sanitise_line($rawline);
1604 }
1605 push(@lines, $line);
1606
1607 if ($realcnt > 1) {
1608 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1609 } else {
1610 $realcnt = 0;
1611 }
1612
1613 #print "==>$rawline\n";
1614 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001615
1616 if ($setup_docs && $line =~ /^\+/) {
1617 push(@setup_docs, $line);
1618 }
1619 }
1620
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001621 $prefix = '';
1622
Andy Whitcroft773647a2008-03-28 14:15:58 -07001623 $realcnt = 0;
1624 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001625 foreach my $line (@lines) {
1626 $linenr++;
1627
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001628 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001629
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001630#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001631 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001632 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001633 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001634 $realline=$1-1;
1635 if (defined $2) {
1636 $realcnt=$3+1;
1637 } else {
1638 $realcnt=1+1;
1639 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001640 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001641 $prev_values = 'E';
1642
Andy Whitcroft773647a2008-03-28 14:15:58 -07001643 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001644 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001645 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001646 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001647 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001648
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001649# track the line number as we move through the hunk, note that
1650# new versions of GNU diff omit the leading space on completely
1651# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001652 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001653 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001654 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001655
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001656 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001657 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001658
1659 # Track the previous line.
1660 ($prevline, $stashline) = ($stashline, $line);
1661 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001662 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1663
Andy Whitcroft773647a2008-03-28 14:15:58 -07001664 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001665
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001666 } elsif ($realcnt == 1) {
1667 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001668 }
1669
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001670 my $hunk_line = ($realcnt != 0);
1671
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001672#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001673 $prefix = "$filename:$realline: " if ($emacs && $file);
1674 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1675
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001676 $here = "#$linenr: " if (!$file);
1677 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001678
1679 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001680 if ($line =~ /^diff --git.*?(\S+)$/) {
1681 $realfile = $1;
1682 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001683 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001684 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001685 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001686 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001687 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001688
1689 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001690 if (!$file && $tree && $p1_prefix ne '' &&
1691 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001692 WARN("PATCH_PREFIX",
1693 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001694 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001695
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001696 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001697 ERROR("MODIFIED_INCLUDE_ASM",
1698 "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 -07001699 }
1700 next;
1701 }
1702
Randy Dunlap389834b2007-06-08 13:47:03 -07001703 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001704
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001705 my $hereline = "$here\n$rawline\n";
1706 my $herecurr = "$here\n$rawline\n";
1707 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001708
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001709 $cnt_lines++ if ($realcnt != 0);
1710
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001711# Check for incorrect file permissions
1712 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1713 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001714 if ($realfile !~ m@scripts/@ &&
1715 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001716 ERROR("EXECUTE_PERMISSIONS",
1717 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001718 }
1719 }
1720
Joe Perches20112472011-07-25 17:13:23 -07001721# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001722 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001723 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001724 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001725 }
1726
1727# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001728 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001729 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001730 my $space_before = $1;
1731 my $sign_off = $2;
1732 my $space_after = $3;
1733 my $email = $4;
1734 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1735
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001736 if ($sign_off !~ /$signature_tags/) {
1737 WARN("BAD_SIGN_OFF",
1738 "Non-standard signature: $sign_off\n" . $herecurr);
1739 }
Joe Perches20112472011-07-25 17:13:23 -07001740 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001741 if (WARN("BAD_SIGN_OFF",
1742 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1743 $fix) {
1744 $fixed[$linenr - 1] =
1745 "$ucfirst_sign_off $email";
1746 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001747 }
Joe Perches20112472011-07-25 17:13:23 -07001748 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001749 if (WARN("BAD_SIGN_OFF",
1750 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1751 $fix) {
1752 $fixed[$linenr - 1] =
1753 "$ucfirst_sign_off $email";
1754 }
1755
Joe Perches20112472011-07-25 17:13:23 -07001756 }
1757 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001758 if (WARN("BAD_SIGN_OFF",
1759 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1760 $fix) {
1761 $fixed[$linenr - 1] =
1762 "$ucfirst_sign_off $email";
1763 }
Joe Perches20112472011-07-25 17:13:23 -07001764 }
1765
1766 my ($email_name, $email_address, $comment) = parse_email($email);
1767 my $suggested_email = format_email(($email_name, $email_address));
1768 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001769 ERROR("BAD_SIGN_OFF",
1770 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001771 } else {
1772 my $dequoted = $suggested_email;
1773 $dequoted =~ s/^"//;
1774 $dequoted =~ s/" </ </;
1775 # Don't force email to have quotes
1776 # Allow just an angle bracketed address
1777 if ("$dequoted$comment" ne $email &&
1778 "<$email_address>$comment" ne $email &&
1779 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001780 WARN("BAD_SIGN_OFF",
1781 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001782 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001783 }
1784 }
1785
Andy Whitcroft00df3442007-06-08 13:47:06 -07001786# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001787 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001788 ERROR("CORRUPTED_PATCH",
1789 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001790 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001791 }
1792
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001793# Check for absolute kernel paths.
1794 if ($tree) {
1795 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1796 my $file = $1;
1797
1798 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1799 check_absolute_file($1, $herecurr)) {
1800 #
1801 } else {
1802 check_absolute_file($file, $herecurr);
1803 }
1804 }
1805 }
1806
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001807# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1808 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001809 $rawline !~ m/^$UTF8*$/) {
1810 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1811
1812 my $blank = copy_spacing($rawline);
1813 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1814 my $hereptr = "$hereline$ptr\n";
1815
Joe Perches34d99212011-07-25 17:13:26 -07001816 CHK("INVALID_UTF8",
1817 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001818 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001819
Joe Perches15662b32011-10-31 17:13:12 -07001820# Check if it's the start of a commit log
1821# (not a header line and we haven't seen the patch filename)
1822 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001823 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001824 $in_header_lines = 0;
1825 $in_commit_log = 1;
1826 }
1827
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001828# Check if there is UTF-8 in a commit log when a mail header has explicitly
1829# declined it, i.e defined some charset where it is missing.
1830 if ($in_header_lines &&
1831 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1832 $1 !~ /utf-8/i) {
1833 $non_utf8_charset = 1;
1834 }
1835
1836 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001837 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001838 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001839 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1840 }
1841
Andy Whitcroft306708542008-10-15 22:02:28 -07001842# ignore non-hunk lines and lines being removed
1843 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001844
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001845#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001846 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001847 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001848 ERROR("DOS_LINE_ENDINGS",
1849 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001850
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001851 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1852 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001853 if (ERROR("TRAILING_WHITESPACE",
1854 "trailing whitespace\n" . $herevet) &&
1855 $fix) {
1856 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1857 }
1858
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001859 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001860 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001861
Andi Kleen33549572010-05-24 14:33:29 -07001862# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001863# Only applies when adding the entry originally, after that we do not have
1864# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001865 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001866 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001867 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001868 my $cnt = $realcnt;
1869 my $ln = $linenr + 1;
1870 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001871 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001872 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001873 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001874 $f = $lines[$ln - 1];
1875 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1876 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001877
1878 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001879
1880 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1881 $is_start = 1;
1882 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1883 $length = -1;
1884 }
1885
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001886 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001887 $f =~ s/#.*//;
1888 $f =~ s/^\s+//;
1889 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001890 if ($f =~ /^\s*config\s/) {
1891 $is_end = 1;
1892 last;
1893 }
Andi Kleen33549572010-05-24 14:33:29 -07001894 $length++;
1895 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001896 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001897 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1898 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001899 }
1900
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001901# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1902 if ($realfile =~ /Kconfig/ &&
1903 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1904 WARN("CONFIG_EXPERIMENTAL",
1905 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1906 }
1907
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001908 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1909 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1910 my $flag = $1;
1911 my $replacement = {
1912 'EXTRA_AFLAGS' => 'asflags-y',
1913 'EXTRA_CFLAGS' => 'ccflags-y',
1914 'EXTRA_CPPFLAGS' => 'cppflags-y',
1915 'EXTRA_LDFLAGS' => 'ldflags-y',
1916 };
1917
1918 WARN("DEPRECATED_VARIABLE",
1919 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1920 }
1921
Andy Whitcroft5368df22008-10-15 22:02:27 -07001922# check we are in a valid source file if not then ignore this hunk
1923 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1924
Joe Perches6cd7f382012-12-17 16:01:54 -08001925#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001926 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001927 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001928 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001929 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001930 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001931 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001932 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001933 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001934 }
1935
Josh Triplettca56dc02012-03-23 15:02:21 -07001936# Check for user-visible strings broken across lines, which breaks the ability
1937# to grep for the string. Limited to strings used as parameters (those
1938# following an open parenthesis), which almost completely eliminates false
1939# positives, as well as warning only once per parameter rather than once per
1940# line of the string. Make an exception when the previous string ends in a
1941# newline (multiple lines in one string constant) or \n\t (common in inline
1942# assembly to indent the instruction on the following line).
1943 if ($line =~ /^\+\s*"/ &&
1944 $prevline =~ /"\s*$/ &&
1945 $prevline =~ /\(/ &&
1946 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1947 WARN("SPLIT_STRING",
1948 "quoted string split across lines\n" . $hereprev);
1949 }
1950
Joe Perches5e79d962010-03-05 13:43:55 -08001951# check for spaces before a quoted newline
1952 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07001953 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1954 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1955 $fix) {
1956 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1957 }
1958
Joe Perches5e79d962010-03-05 13:43:55 -08001959 }
1960
Andy Whitcroft8905a672007-11-28 16:21:06 -08001961# check for adding lines without a newline.
1962 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001963 WARN("MISSING_EOF_NEWLINE",
1964 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001965 }
1966
Mike Frysinger42e41c52009-09-21 17:04:40 -07001967# Blackfin: use hi/lo macros
1968 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1969 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1970 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001971 ERROR("LO_MACRO",
1972 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001973 }
1974 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1975 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001976 ERROR("HI_MACRO",
1977 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001978 }
1979 }
1980
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001981# check we are in a valid source file C or perl if not then ignore this hunk
1982 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001983
1984# at the beginning of a line any tabs must come first and anything
1985# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001986 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1987 $rawline =~ /^\+\s* \s*/) {
1988 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001989 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07001990 if (ERROR("CODE_INDENT",
1991 "code indent should use tabs where possible\n" . $herevet) &&
1992 $fix) {
1993 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
1994 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001995 }
1996
Alberto Panizzo08e44362010-03-05 13:43:54 -08001997# check for space before tabs.
1998 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1999 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002000 if (WARN("SPACE_BEFORE_TAB",
2001 "please, no space before tabs\n" . $herevet) &&
2002 $fix) {
2003 $fixed[$linenr - 1] =~
2004 s/(^\+.*) +\t/$1\t/;
2005 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002006 }
2007
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002008# check for && or || at the start of a line
2009 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2010 CHK("LOGICAL_CONTINUATIONS",
2011 "Logical continuations should be on the previous line\n" . $hereprev);
2012 }
2013
2014# check multi-line statement indentation matches previous line
2015 if ($^V && $^V ge 5.10.0 &&
2016 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2017 $prevline =~ /^\+(\t*)(.*)$/;
2018 my $oldindent = $1;
2019 my $rest = $2;
2020
2021 my $pos = pos_last_openparen($rest);
2022 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002023 $line =~ /^(\+| )([ \t]*)/;
2024 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002025
2026 my $goodtabindent = $oldindent .
2027 "\t" x ($pos / 8) .
2028 " " x ($pos % 8);
2029 my $goodspaceindent = $oldindent . " " x $pos;
2030
2031 if ($newindent ne $goodtabindent &&
2032 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002033
2034 if (CHK("PARENTHESIS_ALIGNMENT",
2035 "Alignment should match open parenthesis\n" . $hereprev) &&
2036 $fix && $line =~ /^\+/) {
2037 $fixed[$linenr - 1] =~
2038 s/^\+[ \t]*/\+$goodtabindent/;
2039 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002040 }
2041 }
2042 }
2043
Joe Perches23f780c2013-07-03 15:05:31 -07002044 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002045 if (CHK("SPACING",
2046 "No space is necessary after a cast\n" . $hereprev) &&
2047 $fix) {
2048 $fixed[$linenr - 1] =~
2049 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2050 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002051 }
2052
Joe Perches05880602012-10-04 17:13:35 -07002053 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002054 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2055 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002056 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2057 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2058 }
2059
2060 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002061 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2062 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2063 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2064 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2065 "networking block comments start with * on subsequent lines\n" . $hereprev);
2066 }
2067
2068 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002069 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2070 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2071 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2072 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002073 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2074 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2075 }
2076
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002077# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002078# Exceptions:
2079# 1) within comments
2080# 2) indented preprocessor commands
2081# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002082 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002083 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002084 if (WARN("LEADING_SPACE",
2085 "please, no spaces at the start of a line\n" . $herevet) &&
2086 $fix) {
2087 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2088 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002089 }
2090
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002091# check we are in a valid C source file if not then ignore this hunk
2092 next if ($realfile !~ /\.(h|c)$/);
2093
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002094# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2095 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2096 WARN("CONFIG_EXPERIMENTAL",
2097 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2098 }
2099
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002100# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002101 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002102 WARN("CVS_KEYWORD",
2103 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002104 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002105
Mike Frysinger42e41c52009-09-21 17:04:40 -07002106# Blackfin: don't use __builtin_bfin_[cs]sync
2107 if ($line =~ /__builtin_bfin_csync/) {
2108 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002109 ERROR("CSYNC",
2110 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002111 }
2112 if ($line =~ /__builtin_bfin_ssync/) {
2113 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002114 ERROR("SSYNC",
2115 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002116 }
2117
Joe Perches56e77d72013-02-21 16:44:14 -08002118# check for old HOTPLUG __dev<foo> section markings
2119 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2120 WARN("HOTPLUG_SECTION",
2121 "Using $1 is unnecessary\n" . $herecurr);
2122 }
2123
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002124# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002125 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2126 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002127#print "LINE<$line>\n";
2128 if ($linenr >= $suppress_statement &&
2129 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002130 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002131 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002132 $stat =~ s/\n./\n /g;
2133 $cond =~ s/\n./\n /g;
2134
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002135#print "linenr<$linenr> <$stat>\n";
2136 # If this statement has no statement boundaries within
2137 # it there is no point in retrying a statement scan
2138 # until we hit end of it.
2139 my $frag = $stat; $frag =~ s/;+\s*$//;
2140 if ($frag !~ /(?:{|;)/) {
2141#print "skip<$line_nr_next>\n";
2142 $suppress_statement = $line_nr_next;
2143 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002144
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002145 # Find the real next line.
2146 $realline_next = $line_nr_next;
2147 if (defined $realline_next &&
2148 (!defined $lines[$realline_next - 1] ||
2149 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2150 $realline_next++;
2151 }
2152
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002153 my $s = $stat;
2154 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002155
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002156 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002157 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002158
2159 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002160 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002161
Andy Whitcroft463f2862009-09-21 17:04:34 -07002162 } elsif ($s =~ /^.\s*else\b/s) {
2163
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002164 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002165 } 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 -07002166 my $type = $1;
2167 $type =~ s/\s+/ /g;
2168 possible($type, "A:" . $s);
2169
Andy Whitcroft8905a672007-11-28 16:21:06 -08002170 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002171 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002172 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002173 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002174
2175 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002176 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002177 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002178 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002179
2180 # Check for any sort of function declaration.
2181 # int foo(something bar, other baz);
2182 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002183 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 -08002184 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002185
Andy Whitcroftcf655042008-03-04 14:28:20 -08002186 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002187 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002188 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002189
Andy Whitcroft8905a672007-11-28 16:21:06 -08002190 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002191 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002192
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002193 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002194 }
2195 }
2196 }
2197
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002198 }
2199
Andy Whitcroft653d4872007-06-23 17:16:34 -07002200#
2201# Checks which may be anchored in the context.
2202#
2203
2204# Check for switch () and associated case and default
2205# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002206 if ($line=~/\bswitch\s*\(.*\)/) {
2207 my $err = '';
2208 my $sep = '';
2209 my @ctx = ctx_block_outer($linenr, $realcnt);
2210 shift(@ctx);
2211 for my $ctx (@ctx) {
2212 my ($clen, $cindent) = line_stats($ctx);
2213 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2214 $indent != $cindent) {
2215 $err .= "$sep$ctx\n";
2216 $sep = '';
2217 } else {
2218 $sep = "[...]\n";
2219 }
2220 }
2221 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002222 ERROR("SWITCH_CASE_INDENT_LEVEL",
2223 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002224 }
2225 }
2226
2227# if/while/etc brace do not go on next line, unless defining a do while loop,
2228# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002229 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002230 my $pre_ctx = "$1$2";
2231
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002232 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002233
2234 if ($line =~ /^\+\t{6,}/) {
2235 WARN("DEEP_INDENTATION",
2236 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2237 }
2238
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002239 my $ctx_cnt = $realcnt - $#ctx - 1;
2240 my $ctx = join("\n", @ctx);
2241
Andy Whitcroft548596d2008-07-23 21:29:01 -07002242 my $ctx_ln = $linenr;
2243 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002244
Andy Whitcroft548596d2008-07-23 21:29:01 -07002245 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2246 defined $lines[$ctx_ln - 1] &&
2247 $lines[$ctx_ln - 1] =~ /^-/)) {
2248 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2249 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002250 $ctx_ln++;
2251 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002252
Andy Whitcroft53210162008-07-23 21:29:03 -07002253 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2254 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002255
2256 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002257 ERROR("OPEN_BRACE",
2258 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002259 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002260 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002261 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2262 $ctx =~ /\)\s*\;\s*$/ &&
2263 defined $lines[$ctx_ln - 1])
2264 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002265 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2266 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002267 WARN("TRAILING_SEMICOLON",
2268 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002269 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002270 }
2271 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002272 }
2273
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002274# Check relative indent for conditionals and blocks.
2275 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002276 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2277 ctx_statement_block($linenr, $realcnt, 0)
2278 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002279 my ($s, $c) = ($stat, $cond);
2280
2281 substr($s, 0, length($c), '');
2282
2283 # Make sure we remove the line prefixes as we have
2284 # none on the first line, and are going to readd them
2285 # where necessary.
2286 $s =~ s/\n./\n/gs;
2287
2288 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002289 my @newlines = ($c =~ /\n/gs);
2290 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002291
2292 # We want to check the first line inside the block
2293 # starting at the end of the conditional, so remove:
2294 # 1) any blank line termination
2295 # 2) any opening brace { on end of the line
2296 # 3) any do (...) {
2297 my $continuation = 0;
2298 my $check = 0;
2299 $s =~ s/^.*\bdo\b//;
2300 $s =~ s/^\s*{//;
2301 if ($s =~ s/^\s*\\//) {
2302 $continuation = 1;
2303 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002304 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002305 $check = 1;
2306 $cond_lines++;
2307 }
2308
2309 # Also ignore a loop construct at the end of a
2310 # preprocessor statement.
2311 if (($prevline =~ /^.\s*#\s*define\s/ ||
2312 $prevline =~ /\\\s*$/) && $continuation == 0) {
2313 $check = 0;
2314 }
2315
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002316 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002317 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002318 while ($cond_ptr != $cond_lines) {
2319 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002320
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002321 # If we see an #else/#elif then the code
2322 # is not linear.
2323 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2324 $check = 0;
2325 }
2326
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002327 # Ignore:
2328 # 1) blank lines, they should be at 0,
2329 # 2) preprocessor lines, and
2330 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002331 if ($continuation ||
2332 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002333 $s =~ /^\s*#\s*?/ ||
2334 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002335 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002336 if ($s =~ s/^.*?\n//) {
2337 $cond_lines++;
2338 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002339 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002340 }
2341
2342 my (undef, $sindent) = line_stats("+" . $s);
2343 my $stat_real = raw_line($linenr, $cond_lines);
2344
2345 # Check if either of these lines are modified, else
2346 # this is not this patch's fault.
2347 if (!defined($stat_real) ||
2348 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2349 $check = 0;
2350 }
2351 if (defined($stat_real) && $cond_lines > 1) {
2352 $stat_real = "[...]\n$stat_real";
2353 }
2354
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002355 #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 -07002356
2357 if ($check && (($sindent % 8) != 0 ||
2358 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002359 WARN("SUSPECT_CODE_INDENT",
2360 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002361 }
2362 }
2363
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002364 # Track the 'values' across context and added lines.
2365 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002366 my ($curr_values, $curr_vars) =
2367 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002368 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002369 if ($dbg_values) {
2370 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002371 print "$linenr > .$outline\n";
2372 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002373 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002374 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002375 $prev_values = substr($curr_values, -1);
2376
Andy Whitcroft00df3442007-06-08 13:47:06 -07002377#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002378 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002379
Andy Whitcroft653d4872007-06-23 17:16:34 -07002380# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002381 if ($dbg_type) {
2382 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002383 ERROR("TEST_TYPE",
2384 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002385 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002386 ERROR("TEST_NOT_TYPE",
2387 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002388 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002389 next;
2390 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002391# TEST: allow direct testing of the attribute matcher.
2392 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002393 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002394 ERROR("TEST_ATTR",
2395 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002396 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002397 ERROR("TEST_NOT_ATTR",
2398 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002399 }
2400 next;
2401 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002402
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002403# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002404 if ($line =~ /^.\s*{/ &&
2405 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002406 ERROR("OPEN_BRACE",
2407 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002408 }
2409
Andy Whitcroft653d4872007-06-23 17:16:34 -07002410#
2411# Checks which are anchored on the added line.
2412#
2413
2414# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002415 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002416 my $path = $1;
2417 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002418 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002419 "malformed #include filename\n" . $herecurr);
2420 }
2421 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2422 ERROR("UAPI_INCLUDE",
2423 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002424 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002425 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002426
2427# no C99 // comments
2428 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002429 if (ERROR("C99_COMMENTS",
2430 "do not use C99 // comments\n" . $herecurr) &&
2431 $fix) {
2432 my $line = $fixed[$linenr - 1];
2433 if ($line =~ /\/\/(.*)$/) {
2434 my $comment = trim($1);
2435 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2436 }
2437 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002438 }
2439 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002440 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002441 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002442
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002443# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2444# the whole statement.
2445#print "APW <$lines[$realline_next - 1]>\n";
2446 if (defined $realline_next &&
2447 exists $lines[$realline_next - 1] &&
2448 !defined $suppress_export{$realline_next} &&
2449 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2450 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002451 # Handle definitions which produce identifiers with
2452 # a prefix:
2453 # XXX(foo);
2454 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002455 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002456 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002457 $name =~ /^${Ident}_$2/) {
2458#print "FOO C name<$name>\n";
2459 $suppress_export{$realline_next} = 1;
2460
2461 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002462 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002463 ^.DEFINE_$Ident\(\Q$name\E\)|
2464 ^.DECLARE_$Ident\(\Q$name\E\)|
2465 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002466 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2467 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002468 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002469#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2470 $suppress_export{$realline_next} = 2;
2471 } else {
2472 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002473 }
2474 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002475 if (!defined $suppress_export{$linenr} &&
2476 $prevline =~ /^.\s*$/ &&
2477 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2478 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2479#print "FOO B <$lines[$linenr - 1]>\n";
2480 $suppress_export{$linenr} = 2;
2481 }
2482 if (defined $suppress_export{$linenr} &&
2483 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002484 WARN("EXPORT_SYMBOL",
2485 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002486 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002487
Joe Eloff5150bda2010-08-09 17:21:00 -07002488# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002489 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002490 ERROR("GLOBAL_INITIALISERS",
2491 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002492 $herecurr);
2493 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002494# check for static initialisers.
Andy Whitcroft2d1bafd72009-01-06 14:41:28 -08002495 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002496 ERROR("INITIALISED_STATIC",
2497 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002498 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002499 }
2500
Joe Perchescb710ec2010-10-26 14:23:20 -07002501# check for static const char * arrays.
2502 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002503 WARN("STATIC_CONST_CHAR_ARRAY",
2504 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002505 $herecurr);
2506 }
2507
2508# check for static char foo[] = "bar" declarations.
2509 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002510 WARN("STATIC_CONST_CHAR_ARRAY",
2511 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002512 $herecurr);
2513 }
2514
Joe Perches93ed0e22010-10-26 14:23:21 -07002515# check for declarations of struct pci_device_id
2516 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002517 WARN("DEFINE_PCI_DEVICE_TABLE",
2518 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002519 }
2520
Andy Whitcroft653d4872007-06-23 17:16:34 -07002521# check for new typedefs, only function parameters and sparse annotations
2522# make sense.
2523 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002524 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002525 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002526 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002527 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002528 WARN("NEW_TYPEDEFS",
2529 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002530 }
2531
2532# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002533 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002534 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2535 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002536 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002537
Andy Whitcroft65863862009-01-06 14:41:21 -08002538 # Should start with a space.
2539 $to =~ s/^(\S)/ $1/;
2540 # Should not end with a space.
2541 $to =~ s/\s+$//;
2542 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002543 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002544 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002545
Joe Perches3705ce52013-07-03 15:05:31 -07002546## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002547 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002548 if (ERROR("POINTER_LOCATION",
2549 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2550 $fix) {
2551 my $sub_from = $ident;
2552 my $sub_to = $ident;
2553 $sub_to =~ s/\Q$from\E/$to/;
2554 $fixed[$linenr - 1] =~
2555 s@\Q$sub_from\E@$sub_to@;
2556 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002557 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002558 }
2559 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2560 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002561 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002562
Andy Whitcroft65863862009-01-06 14:41:21 -08002563 # Should start with a space.
2564 $to =~ s/^(\S)/ $1/;
2565 # Should not end with a space.
2566 $to =~ s/\s+$//;
2567 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002568 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002569 }
2570 # Modifiers should have spaces.
2571 $to =~ s/(\b$Modifier$)/$1 /;
2572
Joe Perches3705ce52013-07-03 15:05:31 -07002573## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002574 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002575 if (ERROR("POINTER_LOCATION",
2576 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2577 $fix) {
2578
2579 my $sub_from = $match;
2580 my $sub_to = $match;
2581 $sub_to =~ s/\Q$from\E/$to/;
2582 $fixed[$linenr - 1] =~
2583 s@\Q$sub_from\E@$sub_to@;
2584 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002585 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002586 }
2587
2588# # no BUG() or BUG_ON()
2589# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2590# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2591# print "$herecurr";
2592# $clean = 0;
2593# }
2594
Andy Whitcroft8905a672007-11-28 16:21:06 -08002595 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002596 WARN("LINUX_VERSION_CODE",
2597 "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 -08002598 }
2599
Joe Perches17441222011-06-15 15:08:17 -07002600# check for uses of printk_ratelimit
2601 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002602 WARN("PRINTK_RATELIMITED",
2603"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002604 }
2605
Andy Whitcroft00df3442007-06-08 13:47:06 -07002606# printk should use KERN_* levels. Note that follow on printk's on the
2607# same line do not need a level, so we use the current block context
2608# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002609# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002610# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002611 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002612 my $ok = 0;
2613 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2614 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002615 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002616 # with "\n" ignore it, else it is to blame
2617 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2618 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2619 $ok = 1;
2620 }
2621 last;
2622 }
2623 }
2624 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002625 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2626 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002627 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002628 }
2629
Joe Perches243f3802012-05-31 16:26:09 -07002630 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2631 my $orig = $1;
2632 my $level = lc($orig);
2633 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002634 my $level2 = $level;
2635 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002636 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002637 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002638 }
2639
2640 if ($line =~ /\bpr_warning\s*\(/) {
2641 WARN("PREFER_PR_LEVEL",
2642 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2643 }
2644
Joe Perchesdc139312013-02-21 16:44:13 -08002645 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2646 my $orig = $1;
2647 my $level = lc($orig);
2648 $level = "warn" if ($level eq "warning");
2649 $level = "dbg" if ($level eq "debug");
2650 WARN("PREFER_DEV_LEVEL",
2651 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2652 }
2653
Andy Whitcroft653d4872007-06-23 17:16:34 -07002654# function brace can't be on same line, except for #defines of do while,
2655# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002656 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2657 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002658 ERROR("OPEN_BRACE",
2659 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002660 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002661
Andy Whitcroft8905a672007-11-28 16:21:06 -08002662# open braces for enum, union and struct go on the same line.
2663 if ($line =~ /^.\s*{/ &&
2664 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002665 ERROR("OPEN_BRACE",
2666 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002667 }
2668
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002669# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002670 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2671 if (WARN("SPACING",
2672 "missing space after $1 definition\n" . $herecurr) &&
2673 $fix) {
2674 $fixed[$linenr - 1] =~
2675 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2676 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002677 }
2678
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002679# check for spacing round square brackets; allowed:
2680# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002681# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2682# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002683 while ($line =~ /(.*?\s)\[/g) {
2684 my ($where, $prefix) = ($-[1], $1);
2685 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002686 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002687 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002688 if (ERROR("BRACKET_SPACE",
2689 "space prohibited before open square bracket '['\n" . $herecurr) &&
2690 $fix) {
2691 $fixed[$linenr - 1] =~
2692 s/^(\+.*?)\s+\[/$1\[/;
2693 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002694 }
2695 }
2696
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002697# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002698 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002699 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002700 my $ctx_before = substr($line, 0, $-[1]);
2701 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002702
2703 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002704 if ($name =~ /^(?:
2705 if|for|while|switch|return|case|
2706 volatile|__volatile__|
2707 __attribute__|format|__extension__|
2708 asm|__asm__)$/x)
2709 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002710 # cpp #define statements have non-optional spaces, ie
2711 # if there is a space between the name and the open
2712 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002713 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002714
2715 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002716 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002717
2718 # If this whole things ends with a type its most
2719 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002720 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002721
2722 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002723 if (WARN("SPACING",
2724 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2725 $fix) {
2726 $fixed[$linenr - 1] =~
2727 s/\b$name\s+\(/$name\(/;
2728 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002729 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002730 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002731
Andy Whitcroft653d4872007-06-23 17:16:34 -07002732# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002733 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002734 my $fixed_line = "";
2735 my $line_fixed = 0;
2736
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002737 my $ops = qr{
2738 <<=|>>=|<=|>=|==|!=|
2739 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2740 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002741 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2742 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002743 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002744 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002745
2746## print("element count: <" . $#elements . ">\n");
2747## foreach my $el (@elements) {
2748## print("el: <$el>\n");
2749## }
2750
2751 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07002752 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002753
Joe Perches3705ce52013-07-03 15:05:31 -07002754 foreach my $el (@elements) {
2755 push(@fix_elements, substr($rawline, $off, length($el)));
2756 $off += length($el);
2757 }
2758
2759 $off = 0;
2760
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002761 my $blank = copy_spacing($opline);
2762
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002763 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002764
2765 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2766
2767## print("n: <$n> good: <$good>\n");
2768
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002769 $off += length($elements[$n]);
2770
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002771 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002772 my $ca = substr($opline, 0, $off);
2773 my $cc = '';
2774 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2775 $cc = substr($opline, $off + length($elements[$n + 1]));
2776 }
2777 my $cb = "$ca$;$cc";
2778
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002779 my $a = '';
2780 $a = 'V' if ($elements[$n] ne '');
2781 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002782 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002783 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2784 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002785 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002786
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002787 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002788
2789 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002790 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002791 $c = 'V' if ($elements[$n + 2] ne '');
2792 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002793 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002794 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2795 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002796 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002797 } else {
2798 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002799 }
2800
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002801 my $ctx = "${a}x${c}";
2802
2803 my $at = "(ctx:$ctx)";
2804
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002805 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002806 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002807
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002808 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002809 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002810
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002811 # Get the full operator variant.
2812 my $opv = $op . substr($curr_vars, $off, 1);
2813
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002814 # Ignore operators passed as parameters.
2815 if ($op_type ne 'V' &&
2816 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2817
Andy Whitcroftcf655042008-03-04 14:28:20 -08002818# # Ignore comments
2819# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002820
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002821 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002822 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002823 if ($ctx !~ /.x[WEBC]/ &&
2824 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002825 if (ERROR("SPACING",
2826 "space required after that '$op' $at\n" . $hereptr)) {
2827 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2828 $line_fixed = 1;
2829 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002830 }
2831
2832 # // is a comment
2833 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002834
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002835 # No spaces for:
2836 # ->
2837 # : when part of a bitfield
2838 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002839 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002840 if (ERROR("SPACING",
2841 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2842 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2843 $line_fixed = 1;
2844 if (defined $fix_elements[$n + 2]) {
2845 $fix_elements[$n + 2] =~ s/^\s+//;
2846 }
2847 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002848 }
2849
2850 # , must have a space on the right.
2851 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002852 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002853 if (ERROR("SPACING",
2854 "space required after that '$op' $at\n" . $hereptr)) {
2855 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2856 $line_fixed = 1;
2857 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002858 }
2859
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002860 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002861 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002862 #warn "'*' is part of type\n";
2863
2864 # unary operators should have a space before and
2865 # none after. May be left adjacent to another
2866 # unary operator, or a cast
2867 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002868 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002869 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002870 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002871 if (ERROR("SPACING",
2872 "space required before that '$op' $at\n" . $hereptr)) {
2873 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2874 $line_fixed = 1;
2875 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002876 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002877 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002878 # A unary '*' may be const
2879
2880 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002881 if (ERROR("SPACING",
2882 "space prohibited after that '$op' $at\n" . $hereptr)) {
2883 $fixed_line =~ s/\s+$//;
2884 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2885 $line_fixed = 1;
2886 if (defined $fix_elements[$n + 2]) {
2887 $fix_elements[$n + 2] =~ s/^\s+//;
2888 }
2889 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002890 }
2891
2892 # unary ++ and unary -- are allowed no space on one side.
2893 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002894 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002895 if (ERROR("SPACING",
2896 "space required one side of that '$op' $at\n" . $hereptr)) {
2897 $fixed_line =~ s/\s+$//;
2898 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2899 $line_fixed = 1;
2900 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002901 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002902 if ($ctx =~ /Wx[BE]/ ||
2903 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002904 if (ERROR("SPACING",
2905 "space prohibited before that '$op' $at\n" . $hereptr)) {
2906 $fixed_line =~ s/\s+$//;
2907 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2908 $line_fixed = 1;
2909 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002910 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002911 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002912 if (ERROR("SPACING",
2913 "space prohibited after that '$op' $at\n" . $hereptr)) {
2914 $fixed_line =~ s/\s+$//;
2915 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2916 $line_fixed = 1;
2917 if (defined $fix_elements[$n + 2]) {
2918 $fix_elements[$n + 2] =~ s/^\s+//;
2919 }
2920 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002921 }
2922
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002923 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002924 } elsif ($op eq '<<' or $op eq '>>' or
2925 $op eq '&' or $op eq '^' or $op eq '|' or
2926 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002927 $op eq '*' or $op eq '/' or
2928 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002929 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002930 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002931 if (ERROR("SPACING",
2932 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2933 $fixed_line =~ s/\s+$//;
2934 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2935 $line_fixed = 1;
2936 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002937 }
2938
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002939 # A colon needs no spaces before when it is
2940 # terminating a case value or a label.
2941 } elsif ($opv eq ':C' || $opv eq ':L') {
2942 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07002943 if (ERROR("SPACING",
2944 "space prohibited before that '$op' $at\n" . $hereptr)) {
2945 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2946 $line_fixed = 1;
2947 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002948 }
2949
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002950 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002951 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002952 my $ok = 0;
2953
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002954 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002955 if (($op eq '<' &&
2956 $cc =~ /^\S+\@\S+>/) ||
2957 ($op eq '>' &&
2958 $ca =~ /<\S+\@\S+$/))
2959 {
2960 $ok = 1;
2961 }
2962
2963 # Ignore ?:
2964 if (($opv eq ':O' && $ca =~ /\?$/) ||
2965 ($op eq '?' && $cc =~ /^:/)) {
2966 $ok = 1;
2967 }
2968
2969 if ($ok == 0) {
Joe Perches3705ce52013-07-03 15:05:31 -07002970 if (ERROR("SPACING",
2971 "spaces required around that '$op' $at\n" . $hereptr)) {
2972 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2973 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
2974 $line_fixed = 1;
2975 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002976 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002977 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002978 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002979
2980## print("n: <$n> GOOD: <$good>\n");
2981
2982 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002983 }
Joe Perches3705ce52013-07-03 15:05:31 -07002984
2985 if (($#elements % 2) == 0) {
2986 $fixed_line = $fixed_line . $fix_elements[$#elements];
2987 }
2988
2989 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2990 $fixed[$linenr - 1] = $fixed_line;
2991 }
2992
2993
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002994 }
2995
Joe Perches786b6322013-07-03 15:05:32 -07002996# check for whitespace before a non-naked semicolon
2997 if ($line =~ /^\+.*\S\s+;/) {
2998 if (WARN("SPACING",
2999 "space prohibited before semicolon\n" . $herecurr) &&
3000 $fix) {
3001 1 while $fixed[$linenr - 1] =~
3002 s/^(\+.*\S)\s+;/$1;/;
3003 }
3004 }
3005
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003006# check for multiple assignments
3007 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003008 CHK("MULTIPLE_ASSIGNMENTS",
3009 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003010 }
3011
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003012## # check for multiple declarations, allowing for a function declaration
3013## # continuation.
3014## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3015## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3016##
3017## # Remove any bracketed sections to ensure we do not
3018## # falsly report the parameters of functions.
3019## my $ln = $line;
3020## while ($ln =~ s/\([^\(\)]*\)//g) {
3021## }
3022## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003023## WARN("MULTIPLE_DECLARATION",
3024## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003025## }
3026## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003027
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003028#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003029 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3030 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003031 if (ERROR("SPACING",
3032 "space required before the open brace '{'\n" . $herecurr) &&
3033 $fix) {
3034 $fixed[$linenr - 1] =~
3035 s/^(\+.*(?:do|\))){/$1 {/;
3036 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003037 }
3038
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003039## # check for blank lines before declarations
3040## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3041## $prevrawline =~ /^.\s*$/) {
3042## WARN("SPACING",
3043## "No blank lines before declarations\n" . $hereprev);
3044## }
3045##
3046
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003047# closing brace should have a space following it when it has anything
3048# on the line
3049 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003050 ERROR("SPACING",
3051 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003052 }
3053
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003054# check spacing on square brackets
3055 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003056 if (ERROR("SPACING",
3057 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3058 $fix) {
3059 $fixed[$linenr - 1] =~
3060 s/\[\s+/\[/;
3061 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003062 }
3063 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003064 if (ERROR("SPACING",
3065 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3066 $fix) {
3067 $fixed[$linenr - 1] =~
3068 s/\s+\]/\]/;
3069 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003070 }
3071
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003072# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003073 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3074 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003075 if (ERROR("SPACING",
3076 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3077 $fix) {
3078 $fixed[$linenr - 1] =~
3079 s/\(\s+/\(/;
3080 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003081 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003082 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003083 $line !~ /for\s*\(.*;\s+\)/ &&
3084 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003085 if (ERROR("SPACING",
3086 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3087 $fix) {
3088 $fixed[$linenr - 1] =~
3089 s/\s+\)/\)/;
3090 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003091 }
3092
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003093#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003094 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003095 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003096 if (WARN("INDENTED_LABEL",
3097 "labels should not be indented\n" . $herecurr) &&
3098 $fix) {
3099 $fixed[$linenr - 1] =~
3100 s/^(.)\s+/$1/;
3101 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003102 }
3103
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003104# Return is not a function.
3105 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3106 my $spacing = $1;
3107 my $value = $2;
3108
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003109 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003110 $value =~ s/\(/ \(/g;
3111 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003112 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003113 $value !~ /(?:$Ident|-?$Constant)\s*
3114 $Compare\s*
3115 (?:$Ident|-?$Constant)/x &&
3116 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003117 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003118#print "value<$value>\n";
3119 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003120 ERROR("RETURN_PARENTHESES",
3121 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003122
3123 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003124 ERROR("SPACING",
3125 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003126 }
3127 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003128# Return of what appears to be an errno should normally be -'ve
3129 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3130 my $name = $1;
3131 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003132 WARN("USE_NEGATIVE_ERRNO",
3133 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003134 }
3135 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003136
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003137# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003138 if ($line =~ /\b(if|while|for|switch)\(/) {
3139 if (ERROR("SPACING",
3140 "space required before the open parenthesis '('\n" . $herecurr) &&
3141 $fix) {
3142 $fixed[$linenr - 1] =~
3143 s/\b(if|while|for|switch)\(/$1 \(/;
3144 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003145 }
3146
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003147# Check for illegal assignment in if conditional -- and check for trailing
3148# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003149 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003150 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3151 ctx_statement_block($linenr, $realcnt, 0)
3152 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003153 my ($stat_next) = ctx_statement_block($line_nr_next,
3154 $remain_next, $off_next);
3155 $stat_next =~ s/\n./\n /g;
3156 ##print "stat<$stat> stat_next<$stat_next>\n";
3157
3158 if ($stat_next =~ /^\s*while\b/) {
3159 # If the statement carries leading newlines,
3160 # then count those as offsets.
3161 my ($whitespace) =
3162 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3163 my $offset =
3164 statement_rawlines($whitespace) - 1;
3165
3166 $suppress_whiletrailers{$line_nr_next +
3167 $offset} = 1;
3168 }
3169 }
3170 if (!defined $suppress_whiletrailers{$linenr} &&
3171 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003172 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003173
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003174 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003175 ERROR("ASSIGN_IN_IF",
3176 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003177 }
3178
3179 # Find out what is on the end of the line after the
3180 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003181 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003182 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003183 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003184 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3185 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003186 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003187 # Find out how long the conditional actually is.
3188 my @newlines = ($c =~ /\n/gs);
3189 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003190 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003191
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003192 $stat_real = raw_line($linenr, $cond_lines)
3193 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003194 if (defined($stat_real) && $cond_lines > 1) {
3195 $stat_real = "[...]\n$stat_real";
3196 }
3197
Joe Perches000d1cc12011-07-25 17:13:25 -07003198 ERROR("TRAILING_STATEMENTS",
3199 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003200 }
3201 }
3202
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003203# Check for bitwise tests written as boolean
3204 if ($line =~ /
3205 (?:
3206 (?:\[|\(|\&\&|\|\|)
3207 \s*0[xX][0-9]+\s*
3208 (?:\&\&|\|\|)
3209 |
3210 (?:\&\&|\|\|)
3211 \s*0[xX][0-9]+\s*
3212 (?:\&\&|\|\||\)|\])
3213 )/x)
3214 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003215 WARN("HEXADECIMAL_BOOLEAN_TEST",
3216 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003217 }
3218
Andy Whitcroft8905a672007-11-28 16:21:06 -08003219# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003220 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3221 my $s = $1;
3222 $s =~ s/$;//g; # Remove any comments
3223 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003224 ERROR("TRAILING_STATEMENTS",
3225 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003226 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003227 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003228# if should not continue a brace
3229 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003230 ERROR("TRAILING_STATEMENTS",
3231 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003232 $herecurr);
3233 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003234# case and default should not have general statements after them
3235 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3236 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003237 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003238 \s*return\s+
3239 )/xg)
3240 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003241 ERROR("TRAILING_STATEMENTS",
3242 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003243 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003244
3245 # Check for }<nl>else {, these must be at the same
3246 # indent level to be relevant to each other.
3247 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3248 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003249 ERROR("ELSE_AFTER_BRACE",
3250 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003251 }
3252
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003253 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3254 $previndent == $indent) {
3255 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3256
3257 # Find out what is on the end of the line after the
3258 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003259 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003260 $s =~ s/\n.*//g;
3261
3262 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003263 ERROR("WHILE_AFTER_BRACE",
3264 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003265 }
3266 }
3267
Joe Perches95e2c602013-07-03 15:05:20 -07003268#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003269 while ($line =~ m{($Constant|$Lval)}g) {
3270 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003271
3272#gcc binary extension
3273 if ($var =~ /^$Binary$/) {
3274 WARN("GCC_BINARY_CONSTANT",
3275 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
3276 }
3277
3278#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003279 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003280 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003281#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003282 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003283#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003284 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3285 seed_camelcase_includes() if ($check);
3286 if (!defined $camelcase{$var}) {
3287 $camelcase{$var} = 1;
3288 CHK("CAMELCASE",
3289 "Avoid CamelCase: <$var>\n" . $herecurr);
3290 }
Joe Perches323c1262012-12-17 16:02:07 -08003291 }
3292 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003293
3294#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003295 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003296 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3297 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003298 }
3299
Andy Whitcroft653d4872007-06-23 17:16:34 -07003300#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003301 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003302 my $file = "$1.h";
3303 my $checkfile = "include/linux/$file";
3304 if (-f "$root/$checkfile" &&
3305 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003306 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003307 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003308 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003309 CHK("ARCH_INCLUDE_LINUX",
3310 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003311 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003312 WARN("INCLUDE_LINUX",
3313 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003314 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003315 }
3316 }
3317
Andy Whitcroft653d4872007-06-23 17:16:34 -07003318# multi-statement macros should be enclosed in a do while loop, grab the
3319# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003320# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003321 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3322 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003323 my $ln = $linenr;
3324 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003325 my ($off, $dstat, $dcond, $rest);
3326 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003327 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003328 ctx_statement_block($linenr, $realcnt, 0);
3329 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003330 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003331 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003332
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003333 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003334 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003335 $dstat =~ s/\\\n.//g;
3336 $dstat =~ s/^\s*//s;
3337 $dstat =~ s/\s*$//s;
3338
3339 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003340 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3341 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003342 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003343 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003344 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003345
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003346 # Flatten any obvious string concatentation.
3347 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3348 $dstat =~ s/$Ident\s*("X*")/$1/)
3349 {
3350 }
3351
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003352 my $exceptions = qr{
3353 $Declare|
3354 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003355 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003356 DECLARE_PER_CPU|
3357 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003358 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003359 union|
3360 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003361 \.$Ident\s*=\s*|
3362 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003363 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003364 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003365 if ($dstat ne '' &&
3366 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3367 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003368 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003369 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003370 $dstat !~ /$exceptions/ &&
3371 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003372 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003373 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003374 $dstat !~ /^for\s*$Constant$/ && # for (...)
3375 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3376 $dstat !~ /^do\s*{/ && # do {...
3377 $dstat !~ /^\({/) # ({...
3378 {
3379 $ctx =~ s/\n*$//;
3380 my $herectx = $here . "\n";
3381 my $cnt = statement_rawlines($ctx);
3382
3383 for (my $n = 0; $n < $cnt; $n++) {
3384 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003385 }
3386
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003387 if ($dstat =~ /;/) {
3388 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3389 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3390 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003391 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003392 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003393 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003394 }
Joe Perches5023d342012-12-17 16:01:47 -08003395
Joe Perches481eb482012-12-17 16:01:56 -08003396# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003397
3398 } else {
3399 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003400 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3401 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003402 $line =~ /^\+.*\\$/) {
3403 WARN("LINE_CONTINUATIONS",
3404 "Avoid unnecessary line continuations\n" . $herecurr);
3405 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003406 }
3407
Joe Perchesb13edf72012-07-30 14:41:24 -07003408# do {} while (0) macro tests:
3409# single-statement macros do not need to be enclosed in do while (0) loop,
3410# macro should not end with a semicolon
3411 if ($^V && $^V ge 5.10.0 &&
3412 $realfile !~ m@/vmlinux.lds.h$@ &&
3413 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3414 my $ln = $linenr;
3415 my $cnt = $realcnt;
3416 my ($off, $dstat, $dcond, $rest);
3417 my $ctx = '';
3418 ($dstat, $dcond, $ln, $cnt, $off) =
3419 ctx_statement_block($linenr, $realcnt, 0);
3420 $ctx = $dstat;
3421
3422 $dstat =~ s/\\\n.//g;
3423
3424 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3425 my $stmts = $2;
3426 my $semis = $3;
3427
3428 $ctx =~ s/\n*$//;
3429 my $cnt = statement_rawlines($ctx);
3430 my $herectx = $here . "\n";
3431
3432 for (my $n = 0; $n < $cnt; $n++) {
3433 $herectx .= raw_line($linenr, $n) . "\n";
3434 }
3435
Joe Perchesac8e97f2012-08-21 16:15:53 -07003436 if (($stmts =~ tr/;/;/) == 1 &&
3437 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003438 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3439 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3440 }
3441 if (defined $semis && $semis ne "") {
3442 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3443 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3444 }
3445 }
3446 }
3447
Mike Frysinger080ba922009-01-06 14:41:25 -08003448# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3449# all assignments may have only one of the following with an assignment:
3450# .
3451# ALIGN(...)
3452# VMLINUX_SYMBOL(...)
3453 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003454 WARN("MISSING_VMLINUX_SYMBOL",
3455 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003456 }
3457
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003458# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003459 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3460 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003461 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003462 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003463 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3464 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003465 my @allowed = ();
3466 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003467 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003468 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003469 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003470 for my $chunk (@chunks) {
3471 my ($cond, $block) = @{$chunk};
3472
Andy Whitcroft773647a2008-03-28 14:15:58 -07003473 # If the condition carries leading newlines, then count those as offsets.
3474 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3475 my $offset = statement_rawlines($whitespace) - 1;
3476
Joe Perchesaad4f612012-03-23 15:02:19 -07003477 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003478 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3479
3480 # We have looked at and allowed this specific line.
3481 $suppress_ifbraces{$ln + $offset} = 1;
3482
3483 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003484 $ln += statement_rawlines($block) - 1;
3485
Andy Whitcroft773647a2008-03-28 14:15:58 -07003486 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003487
3488 $seen++ if ($block =~ /^\s*{/);
3489
Joe Perchesaad4f612012-03-23 15:02:19 -07003490 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003491 if (statement_lines($cond) > 1) {
3492 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003493 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003494 }
3495 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003496 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003497 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003498 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003499 if (statement_block_size($block) > 1) {
3500 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003501 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003502 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003503 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003504 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003505 if ($seen) {
3506 my $sum_allowed = 0;
3507 foreach (@allowed) {
3508 $sum_allowed += $_;
3509 }
3510 if ($sum_allowed == 0) {
3511 WARN("BRACES",
3512 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3513 } elsif ($sum_allowed != $allow &&
3514 $seen != $allow) {
3515 CHK("BRACES",
3516 "braces {} should be used on all arms of this statement\n" . $herectx);
3517 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003518 }
3519 }
3520 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003521 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003522 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003523 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003524
Andy Whitcroftcf655042008-03-04 14:28:20 -08003525 # Check the pre-context.
3526 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3527 #print "APW: ALLOWED: pre<$1>\n";
3528 $allowed = 1;
3529 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003530
3531 my ($level, $endln, @chunks) =
3532 ctx_statement_full($linenr, $realcnt, $-[0]);
3533
Andy Whitcroftcf655042008-03-04 14:28:20 -08003534 # Check the condition.
3535 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003536 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003537 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003538 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003539 }
3540 if (statement_lines($cond) > 1) {
3541 #print "APW: ALLOWED: cond<$cond>\n";
3542 $allowed = 1;
3543 }
3544 if ($block =~/\b(?:if|for|while)\b/) {
3545 #print "APW: ALLOWED: block<$block>\n";
3546 $allowed = 1;
3547 }
3548 if (statement_block_size($block) > 1) {
3549 #print "APW: ALLOWED: lines block<$block>\n";
3550 $allowed = 1;
3551 }
3552 # Check the post-context.
3553 if (defined $chunks[1]) {
3554 my ($cond, $block) = @{$chunks[1]};
3555 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003556 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003557 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003558 if ($block =~ /^\s*\{/) {
3559 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3560 $allowed = 1;
3561 }
3562 }
3563 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003564 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003565 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003566
Andy Whitcroftf0556632008-10-15 22:02:23 -07003567 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003568 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003569 }
3570
Joe Perches000d1cc12011-07-25 17:13:25 -07003571 WARN("BRACES",
3572 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003573 }
3574 }
3575
Joe Perches0979ae62012-12-17 16:01:59 -08003576# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003577 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003578 CHK("BRACES",
3579 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3580 }
Joe Perches77b9a532013-07-03 15:05:29 -07003581 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003582 CHK("BRACES",
3583 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3584 }
3585
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003586# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003587 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3588 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003589 WARN("VOLATILE",
3590 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003591 }
3592
Andy Whitcroft00df3442007-06-08 13:47:06 -07003593# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003594 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003595 CHK("REDUNDANT_CODE",
3596 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003597 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003598 }
3599
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003600# check for needless "if (<foo>) fn(<foo>)" uses
3601 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3602 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3603 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3604 WARN('NEEDLESS_IF',
3605 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003606 }
3607 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003608
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003609# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003610 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003611 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003612 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003613 CHK("USLEEP_RANGE",
3614 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003615 }
3616 }
3617
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003618# warn about unexpectedly long msleep's
3619 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3620 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003621 WARN("MSLEEP",
3622 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003623 }
3624 }
3625
Joe Perches36ec1932013-07-03 15:05:25 -07003626# check for comparisons of jiffies
3627 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3628 WARN("JIFFIES_COMPARISON",
3629 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3630 }
3631
Joe Perches9d7a34a2013-07-03 15:05:26 -07003632# check for comparisons of get_jiffies_64()
3633 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3634 WARN("JIFFIES_COMPARISON",
3635 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3636 }
3637
Andy Whitcroft00df3442007-06-08 13:47:06 -07003638# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003639# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003640# print "#ifdef in C files should be avoided\n";
3641# print "$herecurr";
3642# $clean = 0;
3643# }
3644
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003645# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003646 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003647 if (ERROR("SPACING",
3648 "exactly one space required after that #$1\n" . $herecurr) &&
3649 $fix) {
3650 $fixed[$linenr - 1] =~
3651 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3652 }
3653
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003654 }
3655
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003656# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003657 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3658 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003659 my $which = $1;
3660 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003661 CHK("UNCOMMENTED_DEFINITION",
3662 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003663 }
3664 }
3665# check for memory barriers without a comment.
3666 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3667 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003668 CHK("MEMORY_BARRIER",
3669 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003670 }
3671 }
3672# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003673 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003674 CHK("ARCH_DEFINES",
3675 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003676 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003677
Tobias Klauserd4977c72010-05-24 14:33:30 -07003678# Check that the storage class is at the beginning of a declaration
3679 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003680 WARN("STORAGE_CLASS",
3681 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003682 }
3683
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003684# check the location of the inline attribute, that it is between
3685# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003686 if ($line =~ /\b$Type\s+$Inline\b/ ||
3687 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003688 ERROR("INLINE_LOCATION",
3689 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003690 }
3691
Andy Whitcroft8905a672007-11-28 16:21:06 -08003692# Check for __inline__ and __inline, prefer inline
3693 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003694 WARN("INLINE",
3695 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003696 }
3697
Joe Perches3d130fd2011-01-12 17:00:00 -08003698# Check for __attribute__ packed, prefer __packed
3699 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003700 WARN("PREFER_PACKED",
3701 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003702 }
3703
Joe Perches39b7e282011-07-25 17:13:24 -07003704# Check for __attribute__ aligned, prefer __aligned
3705 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003706 WARN("PREFER_ALIGNED",
3707 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003708 }
3709
Joe Perches5f14d3b2012-01-10 15:09:52 -08003710# Check for __attribute__ format(printf, prefer __printf
3711 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3712 WARN("PREFER_PRINTF",
3713 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3714 }
3715
Joe Perches6061d942012-03-23 15:02:16 -07003716# Check for __attribute__ format(scanf, prefer __scanf
3717 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3718 WARN("PREFER_SCANF",
3719 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3720 }
3721
Joe Perches8f53a9b2010-03-05 13:43:48 -08003722# check for sizeof(&)
3723 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003724 WARN("SIZEOF_ADDRESS",
3725 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003726 }
3727
Joe Perches66c80b62012-07-30 14:41:22 -07003728# check for sizeof without parenthesis
3729 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3730 WARN("SIZEOF_PARENTHESIS",
3731 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3732 }
3733
Joe Perches428e2fd2011-05-24 17:13:39 -07003734# check for line continuations in quoted strings with odd counts of "
3735 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003736 WARN("LINE_CONTINUATIONS",
3737 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003738 }
3739
Joe Perches88982fe2012-12-17 16:02:00 -08003740# check for struct spinlock declarations
3741 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3742 WARN("USE_SPINLOCK_T",
3743 "struct spinlock should be spinlock_t\n" . $herecurr);
3744 }
3745
Joe Perchesa6962d72013-04-29 16:18:13 -07003746# check for seq_printf uses that could be seq_puts
3747 if ($line =~ /\bseq_printf\s*\(/) {
3748 my $fmt = get_quoted_string($line, $rawline);
3749 if ($fmt !~ /[^\\]\%/) {
3750 WARN("PREFER_SEQ_PUTS",
3751 "Prefer seq_puts to seq_printf\n" . $herecurr);
3752 }
3753 }
3754
Andy Whitcroft554e1652012-01-10 15:09:57 -08003755# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003756 if ($^V && $^V ge 5.10.0 &&
3757 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003758 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003759
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003760 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003761 my $ms_val = $7;
3762 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003763
Andy Whitcroft554e1652012-01-10 15:09:57 -08003764 if ($ms_size =~ /^(0x|)0$/i) {
3765 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003766 "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 -08003767 } elsif ($ms_size =~ /^(0x|)1$/i) {
3768 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003769 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3770 }
3771 }
3772
3773# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003774 if ($^V && $^V ge 5.10.0 &&
3775 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003776 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003777 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003778 my $call = $1;
3779 my $cast1 = deparenthesize($2);
3780 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003781 my $cast2 = deparenthesize($7);
3782 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003783 my $cast;
3784
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003785 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003786 $cast = "$cast1 or $cast2";
3787 } elsif ($cast1 ne "") {
3788 $cast = $cast1;
3789 } else {
3790 $cast = $cast2;
3791 }
3792 WARN("MINMAX",
3793 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003794 }
3795 }
3796
Joe Perches4a273192012-07-30 14:41:20 -07003797# check usleep_range arguments
3798 if ($^V && $^V ge 5.10.0 &&
3799 defined $stat &&
3800 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3801 my $min = $1;
3802 my $max = $7;
3803 if ($min eq $max) {
3804 WARN("USLEEP_RANGE",
3805 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3806 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3807 $min > $max) {
3808 WARN("USLEEP_RANGE",
3809 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3810 }
3811 }
3812
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003813# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003814 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003815 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003816 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003817 my $function_name = $1;
3818 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003819
3820 my $s = $stat;
3821 if (defined $cond) {
3822 substr($s, 0, length($cond), '');
3823 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003824 if ($s =~ /^\s*;/ &&
3825 $function_name ne 'uninitialized_var')
3826 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003827 WARN("AVOID_EXTERNS",
3828 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003829 }
3830
3831 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003832 WARN("FUNCTION_ARGUMENTS",
3833 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003834 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003835
3836 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3837 $stat =~ /^.\s*extern\s+/)
3838 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003839 WARN("AVOID_EXTERNS",
3840 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003841 }
3842
3843# checks for new __setup's
3844 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3845 my $name = $1;
3846
3847 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003848 CHK("UNDOCUMENTED_SETUP",
3849 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003850 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003851 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003852
3853# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003854 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003855 WARN("UNNECESSARY_CASTS",
3856 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003857 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003858
Joe Perchesa640d252013-07-03 15:05:21 -07003859# alloc style
3860# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3861 if ($^V && $^V ge 5.10.0 &&
3862 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3863 CHK("ALLOC_SIZEOF_STRUCT",
3864 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3865 }
3866
Joe Perches972fdea2013-04-29 16:18:12 -07003867# check for krealloc arg reuse
3868 if ($^V && $^V ge 5.10.0 &&
3869 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3870 WARN("KREALLOC_ARG_REUSE",
3871 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3872 }
3873
Joe Perches5ce59ae2013-02-21 16:44:18 -08003874# check for alloc argument mismatch
3875 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3876 WARN("ALLOC_ARRAY_ARGS",
3877 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3878 }
3879
Joe Perchescaf2a542011-01-12 16:59:56 -08003880# check for multiple semicolons
3881 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08003882 WARN("ONE_SEMICOLON",
3883 "Statements terminations use 1 semicolon\n" . $herecurr);
3884 }
3885
3886# check for switch/default statements without a break;
3887 if ($^V && $^V ge 5.10.0 &&
3888 defined $stat &&
3889 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3890 my $ctx = '';
3891 my $herectx = $here . "\n";
3892 my $cnt = statement_rawlines($stat);
3893 for (my $n = 0; $n < $cnt; $n++) {
3894 $herectx .= raw_line($linenr, $n) . "\n";
3895 }
3896 WARN("DEFAULT_NO_BREAK",
3897 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08003898 }
3899
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003900# check for gcc specific __FUNCTION__
3901 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 WARN("USE_FUNC",
3903 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003904 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003905
Joe Perches2c924882012-03-23 15:02:20 -07003906# check for use of yield()
3907 if ($line =~ /\byield\s*\(\s*\)/) {
3908 WARN("YIELD",
3909 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3910 }
3911
Joe Perches179f8f42013-07-03 15:05:30 -07003912# check for comparisons against true and false
3913 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3914 my $lead = $1;
3915 my $arg = $2;
3916 my $test = $3;
3917 my $otype = $4;
3918 my $trail = $5;
3919 my $op = "!";
3920
3921 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3922
3923 my $type = lc($otype);
3924 if ($type =~ /^(?:true|false)$/) {
3925 if (("$test" eq "==" && "$type" eq "true") ||
3926 ("$test" eq "!=" && "$type" eq "false")) {
3927 $op = "";
3928 }
3929
3930 CHK("BOOL_COMPARISON",
3931 "Using comparison to $otype is error prone\n" . $herecurr);
3932
3933## maybe suggesting a correct construct would better
3934## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3935
3936 }
3937 }
3938
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003939# check for semaphores initialized locked
3940 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003941 WARN("CONSIDER_COMPLETION",
3942 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003943 }
Joe Perches6712d852012-03-23 15:02:20 -07003944
Joe Perches67d0a072011-10-31 17:13:10 -07003945# recommend kstrto* over simple_strto* and strict_strto*
3946 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003947 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003948 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003949 }
Joe Perches6712d852012-03-23 15:02:20 -07003950
Michael Ellermanf3db6632008-07-23 21:28:57 -07003951# check for __initcall(), use device_initcall() explicitly please
3952 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003953 WARN("USE_DEVICE_INITCALL",
3954 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003955 }
Joe Perches6712d852012-03-23 15:02:20 -07003956
Emese Revfy79404842010-03-05 13:43:53 -08003957# check for various ops structs, ensure they are const.
3958 my $struct_ops = qr{acpi_dock_ops|
3959 address_space_operations|
3960 backlight_ops|
3961 block_device_operations|
3962 dentry_operations|
3963 dev_pm_ops|
3964 dma_map_ops|
3965 extent_io_ops|
3966 file_lock_operations|
3967 file_operations|
3968 hv_ops|
3969 ide_dma_ops|
3970 intel_dvo_dev_ops|
3971 item_operations|
3972 iwl_ops|
3973 kgdb_arch|
3974 kgdb_io|
3975 kset_uevent_ops|
3976 lock_manager_operations|
3977 microcode_ops|
3978 mtrr_ops|
3979 neigh_ops|
3980 nlmsvc_binding|
3981 pci_raw_ops|
3982 pipe_buf_operations|
3983 platform_hibernation_ops|
3984 platform_suspend_ops|
3985 proto_ops|
3986 rpc_pipe_ops|
3987 seq_operations|
3988 snd_ac97_build_ops|
3989 soc_pcmcia_socket_ops|
3990 stacktrace_ops|
3991 sysfs_ops|
3992 tty_operations|
3993 usb_mon_operations|
3994 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003995 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003996 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003997 WARN("CONST_STRUCT",
3998 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003999 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004000 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004001
4002# use of NR_CPUS is usually wrong
4003# ignore definitions of NR_CPUS and usage to define arrays as likely right
4004 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004005 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4006 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004007 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4008 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4009 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004010 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004011 WARN("NR_CPUS",
4012 "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 -07004013 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004014
4015# check for %L{u,d,i} in strings
4016 my $string;
4017 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4018 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004019 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004020 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004021 WARN("PRINTF_L",
4022 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004023 last;
4024 }
4025 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004026
4027# whine mightly about in_atomic
4028 if ($line =~ /\bin_atomic\s*\(/) {
4029 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004030 ERROR("IN_ATOMIC",
4031 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004032 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004033 WARN("IN_ATOMIC",
4034 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004035 }
4036 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004037
4038# check for lockdep_set_novalidate_class
4039 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4040 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4041 if ($realfile !~ m@^kernel/lockdep@ &&
4042 $realfile !~ m@^include/linux/lockdep@ &&
4043 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004044 ERROR("LOCKDEP",
4045 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004046 }
4047 }
Dave Jones88f88312011-01-12 16:59:59 -08004048
4049 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4050 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004051 WARN("EXPORTED_WORLD_WRITABLE",
4052 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004053 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004054 }
4055
4056 # If we have no input at all, then there is nothing to report on
4057 # so just keep quiet.
4058 if ($#rawlines == -1) {
4059 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004060 }
4061
Andy Whitcroft8905a672007-11-28 16:21:06 -08004062 # In mailback mode only produce a report in the negative, for
4063 # things that appear to be patches.
4064 if ($mailback && ($clean == 1 || !$is_patch)) {
4065 exit(0);
4066 }
4067
4068 # This is not a patch, and we are are in 'no-patch' mode so
4069 # just keep quiet.
4070 if (!$chk_patch && !$is_patch) {
4071 exit(0);
4072 }
4073
4074 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004075 ERROR("NOT_UNIFIED_DIFF",
4076 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004077 }
4078 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004079 ERROR("MISSING_SIGN_OFF",
4080 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004081 }
4082
Andy Whitcroft8905a672007-11-28 16:21:06 -08004083 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004084 if ($summary && !($clean == 1 && $quiet == 1)) {
4085 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004086 print "total: $cnt_error errors, $cnt_warn warnings, " .
4087 (($check)? "$cnt_chk checks, " : "") .
4088 "$cnt_lines lines checked\n";
4089 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004090 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004091
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004092 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004093
4094 if ($^V lt 5.10.0) {
4095 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4096 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4097 }
4098
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004099 # If there were whitespace errors which cleanpatch can fix
4100 # then suggest that.
4101 if ($rpt_cleaners) {
4102 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4103 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004104 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004105 }
4106 }
4107
Artem Bityutskiy11232682012-03-23 15:02:17 -07004108 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004109 print "NOTE: Ignored message types:";
4110 foreach my $ignore (sort keys %ignore_type) {
4111 print " $ignore";
4112 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07004113 print "\n\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07004114 }
4115
Joe Perches3705ce52013-07-03 15:05:31 -07004116 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4117 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4118 my $linecount = 0;
4119 my $f;
4120
4121 open($f, '>', $newfile)
4122 or die "$P: Can't open $newfile for write\n";
4123 foreach my $fixed_line (@fixed) {
4124 $linecount++;
4125 if ($file) {
4126 if ($linecount > 3) {
4127 $fixed_line =~ s/^\+//;
4128 print $f $fixed_line. "\n";
4129 }
4130 } else {
4131 print $f $fixed_line . "\n";
4132 }
4133 }
4134 close($f);
4135
4136 if (!$quiet) {
4137 print << "EOM";
4138Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4139
4140Do _NOT_ trust the results written to this file.
4141Do _NOT_ submit these changes without inspecting them for correctness.
4142
4143This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4144No warranties, expressed or implied...
4145
4146EOM
4147 }
4148 }
4149
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004150 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004151 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004152 }
4153 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004154 print << "EOM";
4155$vname has style problems, please review.
4156
4157If any of these errors are false positives, please report
4158them to the maintainer, see CHECKPATCH in MAINTAINERS.
4159EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004160 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004161
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004162 return $clean;
4163}