blob: c03e4278b07c2af8dc51df8d95b69b4ac425a76f [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070010
11my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070012$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
Joe Perches000d1cc12011-07-25 17:13:25 -070014my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070015
16use Getopt::Long qw(:config no_auto_abbrev);
17
18my $quiet = 0;
19my $tree = 1;
20my $chk_signoff = 1;
21my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070022my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070023my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080024my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $file = 0;
26my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080027my $summary = 1;
28my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080029my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070030my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070031my $fix = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080033my %debug;
Joe Perches34456862013-07-03 15:05:34 -070034my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070035my %use_type = ();
36my @use = ();
37my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070038my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070039my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070040my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080041my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070042my $ignore_perl_version = 0;
43my $minimum_perl_version = 5.10.0;
Hannes Eder77f5b102009-09-21 17:04:37 -070044
45sub help {
46 my ($exitcode) = @_;
47
48 print << "EOM";
49Usage: $P [OPTION]... [FILE]...
50Version: $V
51
52Options:
53 -q, --quiet quiet
54 --no-tree run without a kernel tree
55 --no-signoff do not check for 'Signed-off-by' line
56 --patch treat FILE as patchfile (default)
57 --emacs emacs compile window format
58 --terse one line per report
59 -f, --file treat FILE as regular source file
60 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070061 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070062 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080063 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070064 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070065 --root=PATH PATH to the kernel tree root
66 --no-summary suppress the per-file summary
67 --mailback only produce a report in case of warnings/errors
68 --summary-file include the filename in summary
69 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
70 'values', 'possible', 'type', and 'attr' (default
71 is all off)
72 --test-only=WORD report only warnings/errors containing WORD
73 literally
Joe Perches3705ce52013-07-03 15:05:31 -070074 --fix EXPERIMENTAL - may create horrible results
75 If correctable single-line errors exist, create
76 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
77 with potential errors corrected to the preferred
78 checkpatch style
Dave Hansend62a2012013-09-11 14:23:56 -070079 --ignore-perl-version override checking of perl version. expect
80 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070081 -h, --help, --version display this help and exit
82
83When FILE is - read standard input.
84EOM
85
86 exit($exitcode);
87}
88
Joe Perches000d1cc12011-07-25 17:13:25 -070089my $conf = which_conf($configuration_file);
90if (-f $conf) {
91 my @conf_args;
92 open(my $conffile, '<', "$conf")
93 or warn "$P: Can't find a readable $configuration_file file $!\n";
94
95 while (<$conffile>) {
96 my $line = $_;
97
98 $line =~ s/\s*\n?$//g;
99 $line =~ s/^\s*//g;
100 $line =~ s/\s+/ /g;
101
102 next if ($line =~ m/^\s*#/);
103 next if ($line =~ m/^\s*$/);
104
105 my @words = split(" ", $line);
106 foreach my $word (@words) {
107 last if ($word =~ m/^#/);
108 push (@conf_args, $word);
109 }
110 }
111 close($conffile);
112 unshift(@ARGV, @conf_args) if @conf_args;
113}
114
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700115GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700116 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700117 'tree!' => \$tree,
118 'signoff!' => \$chk_signoff,
119 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700120 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800121 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700122 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700123 'subjective!' => \$check,
124 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700125 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700126 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700127 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800128 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700129 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800130 'summary!' => \$summary,
131 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800132 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700133 'fix!' => \$fix,
Dave Hansend62a2012013-09-11 14:23:56 -0700134 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800135 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700136 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700137 'h|help' => \$help,
138 'version' => \$help
139) or help(1);
140
141help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700142
143my $exit = 0;
144
Dave Hansend62a2012013-09-11 14:23:56 -0700145if ($^V && $^V lt $minimum_perl_version) {
146 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
147 if (!$ignore_perl_version) {
148 exit(1);
149 }
150}
151
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700152if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700153 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700154 exit(1);
155}
156
Joe Perches91bfe482013-09-11 14:23:59 -0700157sub hash_save_array_words {
158 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700159
Joe Perches91bfe482013-09-11 14:23:59 -0700160 my @array = split(/,/, join(',', @$arrayRef));
161 foreach my $word (@array) {
162 $word =~ s/\s*\n?$//g;
163 $word =~ s/^\s*//g;
164 $word =~ s/\s+/ /g;
165 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700166
Joe Perches91bfe482013-09-11 14:23:59 -0700167 next if ($word =~ m/^\s*#/);
168 next if ($word =~ m/^\s*$/);
169
170 $hashRef->{$word}++;
171 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700172}
173
Joe Perches91bfe482013-09-11 14:23:59 -0700174sub hash_show_words {
175 my ($hashRef, $prefix) = @_;
176
Joe Perches58cb3cf2013-09-11 14:24:04 -0700177 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700178 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700179 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700180 print " $word";
181 }
182 print "\n\n";
183 }
184}
185
186hash_save_array_words(\%ignore_type, \@ignore);
187hash_save_array_words(\%use_type, \@use);
188
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800189my $dbg_values = 0;
190my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700191my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700192my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800193for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800194 ## no critic
195 eval "\${dbg_$key} = '$debug{$key}';";
196 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800197}
198
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700199my $rpt_cleaners = 0;
200
Andy Whitcroft8905a672007-11-28 16:21:06 -0800201if ($terse) {
202 $emacs = 1;
203 $quiet++;
204}
205
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700206if ($tree) {
207 if (defined $root) {
208 if (!top_of_kernel_tree($root)) {
209 die "$P: $root: --root does not point at a valid tree\n";
210 }
211 } else {
212 if (top_of_kernel_tree('.')) {
213 $root = '.';
214 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
215 top_of_kernel_tree($1)) {
216 $root = $1;
217 }
218 }
219
220 if (!defined $root) {
221 print "Must be run from the top-level dir. of a kernel tree\n";
222 exit(2);
223 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700224}
225
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700226my $emitted_corrupt = 0;
227
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700228our $Ident = qr{
229 [A-Za-z_][A-Za-z\d_]*
230 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
231 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700232our $Storage = qr{extern|static|asmlinkage};
233our $Sparse = qr{
234 __user|
235 __kernel|
236 __force|
237 __iomem|
238 __must_check|
239 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800240 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700241 __ref|
242 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700243 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800244
Joe Perches8716de32013-09-11 14:24:05 -0700245our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
246
Wolfram Sang52131292010-03-05 13:43:51 -0800247# Notes to $Attribute:
248# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700249our $Attribute = qr{
250 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700251 __percpu|
252 __nocast|
253 __safe|
254 __bitwise__|
255 __packed__|
256 __packed2__|
257 __naked|
258 __maybe_unused|
259 __always_unused|
260 __noreturn|
261 __used|
262 __cold|
263 __noclone|
264 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265 __read_mostly|
266 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700267 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700268 ____cacheline_aligned|
269 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800270 ____cacheline_internodealigned_in_smp|
271 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700272 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700273our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700274our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700275our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
276our $Lval = qr{$Ident(?:$Member)*};
277
Joe Perches95e2c602013-07-03 15:05:20 -0700278our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
279our $Binary = qr{(?i)0b[01]+$Int_type?};
280our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
281our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800282our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
283our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
284our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800285our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700286our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800287our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800288our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700289our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290our $Operators = qr{
291 <=|>=|==|!=|
292 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700293 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294 }x;
295
Andy Whitcroft8905a672007-11-28 16:21:06 -0800296our $NonptrType;
Joe Perches8716de32013-09-11 14:24:05 -0700297our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800298our $Type;
299our $Declare;
300
Joe Perches15662b32011-10-31 17:13:12 -0700301our $NON_ASCII_UTF8 = qr{
302 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700303 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
304 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
305 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
306 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
307 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
308 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
309}x;
310
Joe Perches15662b32011-10-31 17:13:12 -0700311our $UTF8 = qr{
312 [\x09\x0A\x0D\x20-\x7E] # ASCII
313 | $NON_ASCII_UTF8
314}x;
315
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700316our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700317 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700318 atomic_t
319)};
320
Joe Perches691e6692010-03-05 13:43:51 -0800321our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700322 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700323 (?:[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 -0700324 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700325 panic|
326 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800327)};
328
Joe Perches20112472011-07-25 17:13:23 -0700329our $signature_tags = qr{(?xi:
330 Signed-off-by:|
331 Acked-by:|
332 Tested-by:|
333 Reviewed-by:|
334 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700335 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700336 To:|
337 Cc:
338)};
339
Andy Whitcroft8905a672007-11-28 16:21:06 -0800340our @typeList = (
341 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700342 qr{(?:unsigned\s+)?char},
343 qr{(?:unsigned\s+)?short},
344 qr{(?:unsigned\s+)?int},
345 qr{(?:unsigned\s+)?long},
346 qr{(?:unsigned\s+)?long\s+int},
347 qr{(?:unsigned\s+)?long\s+long},
348 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800349 qr{unsigned},
350 qr{float},
351 qr{double},
352 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800353 qr{struct\s+$Ident},
354 qr{union\s+$Ident},
355 qr{enum\s+$Ident},
356 qr{${Ident}_t},
357 qr{${Ident}_handler},
358 qr{${Ident}_handler_fn},
359);
Joe Perches8716de32013-09-11 14:24:05 -0700360our @typeListWithAttr = (
361 @typeList,
362 qr{struct\s+$InitAttribute\s+$Ident},
363 qr{union\s+$InitAttribute\s+$Ident},
364);
365
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700366our @modifierList = (
367 qr{fastcall},
368);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800369
Wolfram Sang7840a942010-08-09 17:20:57 -0700370our $allowed_asm_includes = qr{(?x:
371 irq|
372 memory
373)};
374# memory.h: ARM has a custom one
375
Andy Whitcroft8905a672007-11-28 16:21:06 -0800376sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700377 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
378 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700379 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700380 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800381 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700382 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800383 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800384 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700385 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700386 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800387 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700388 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800389 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700390 $NonptrTypeWithAttr = qr{
391 (?:$Modifier\s+|const\s+)*
392 (?:
393 (?:typeof|__typeof__)\s*\([^\)]*\)|
394 (?:$typeTypedefs\b)|
395 (?:${allWithAttr}\b)
396 )
397 (?:\s+$Modifier|\s+const)*
398 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700400 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700401 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700402 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403 }x;
404 $Declare = qr{(?:$Storage\s+)?$Type};
405}
406build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700407
Joe Perches7d2367a2011-07-25 17:13:22 -0700408our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700409
410# Using $balanced_parens, $LvalOrFunc, or $FuncArg
411# requires at least perl version v5.10.0
412# Any use must be runtime checked with $^V
413
414our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
415our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800416our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700417
418sub deparenthesize {
419 my ($string) = @_;
420 return "" if (!defined($string));
421 $string =~ s@^\s*\(\s*@@g;
422 $string =~ s@\s*\)\s*$@@g;
423 $string =~ s@\s+@ @g;
424 return $string;
425}
426
Joe Perches34456862013-07-03 15:05:34 -0700427sub seed_camelcase_file {
428 my ($file) = @_;
429
430 return if (!(-f $file));
431
432 local $/;
433
434 open(my $include_file, '<', "$file")
435 or warn "$P: Can't read '$file' $!\n";
436 my $text = <$include_file>;
437 close($include_file);
438
439 my @lines = split('\n', $text);
440
441 foreach my $line (@lines) {
442 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
443 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
444 $camelcase{$1} = 1;
445 }
Joe Perchesd8b07712013-11-12 15:10:06 -0800446 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
Joe Perches34456862013-07-03 15:05:34 -0700447 $camelcase{$1} = 1;
448 }
449 }
450}
451
452my $camelcase_seeded = 0;
453sub seed_camelcase_includes {
454 return if ($camelcase_seeded);
455
456 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700457 my $camelcase_cache = "";
458 my @include_files = ();
459
460 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700461
Joe Perches34456862013-07-03 15:05:34 -0700462 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700463 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
464 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700465 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700466 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700467 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700468 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700469 @include_files = split('\n', $files);
470 foreach my $file (@include_files) {
471 my $date = POSIX::strftime("%Y%m%d%H%M",
472 localtime((stat $file)[9]));
473 $last_mod_date = $date if ($last_mod_date < $date);
474 }
475 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700476 }
Joe Perchesc707a812013-07-08 16:00:43 -0700477
478 if ($camelcase_cache ne "" && -f $camelcase_cache) {
479 open(my $camelcase_file, '<', "$camelcase_cache")
480 or warn "$P: Can't read '$camelcase_cache' $!\n";
481 while (<$camelcase_file>) {
482 chomp;
483 $camelcase{$_} = 1;
484 }
485 close($camelcase_file);
486
487 return;
488 }
489
490 if (-d ".git") {
491 $files = `git ls-files "include/*.h"`;
492 @include_files = split('\n', $files);
493 }
494
Joe Perches34456862013-07-03 15:05:34 -0700495 foreach my $file (@include_files) {
496 seed_camelcase_file($file);
497 }
Joe Perches351b2a12013-07-03 15:05:36 -0700498
Joe Perchesc707a812013-07-08 16:00:43 -0700499 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700500 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700501 open(my $camelcase_file, '>', "$camelcase_cache")
502 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700503 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
504 print $camelcase_file ("$_\n");
505 }
506 close($camelcase_file);
507 }
Joe Perches34456862013-07-03 15:05:34 -0700508}
509
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700510$chk_signoff = 0 if ($file);
511
Andy Whitcroft00df3442007-06-08 13:47:06 -0700512my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800513my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700514my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800515my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700516for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800517 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700518 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800519 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700520 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800521 } elsif ($filename eq '-') {
522 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700523 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800524 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700525 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700526 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800527 if ($filename eq '-') {
528 $vname = 'Your patch';
529 } else {
530 $vname = $filename;
531 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800532 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700533 chomp;
534 push(@rawlines, $_);
535 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800536 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800537 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700538 $exit = 1;
539 }
540 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800541 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700542 @fixed = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700543}
544
545exit($exit);
546
547sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700548 my ($root) = @_;
549
550 my @tree_check = (
551 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
552 "README", "Documentation", "arch", "include", "drivers",
553 "fs", "init", "ipc", "kernel", "lib", "scripts",
554 );
555
556 foreach my $check (@tree_check) {
557 if (! -e $root . '/' . $check) {
558 return 0;
559 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700560 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700561 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700562}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700563
Joe Perches20112472011-07-25 17:13:23 -0700564sub parse_email {
565 my ($formatted_email) = @_;
566
567 my $name = "";
568 my $address = "";
569 my $comment = "";
570
571 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
572 $name = $1;
573 $address = $2;
574 $comment = $3 if defined $3;
575 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
576 $address = $1;
577 $comment = $2 if defined $2;
578 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
579 $address = $1;
580 $comment = $2 if defined $2;
581 $formatted_email =~ s/$address.*$//;
582 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700583 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700584 $name =~ s/^\"|\"$//g;
585 # If there's a name left after stripping spaces and
586 # leading quotes, and the address doesn't have both
587 # leading and trailing angle brackets, the address
588 # is invalid. ie:
589 # "joe smith joe@smith.com" bad
590 # "joe smith <joe@smith.com" bad
591 if ($name ne "" && $address !~ /^<[^>]+>$/) {
592 $name = "";
593 $address = "";
594 $comment = "";
595 }
596 }
597
Joe Perches3705ce52013-07-03 15:05:31 -0700598 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700599 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700600 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700601 $address =~ s/^\<|\>$//g;
602
603 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
604 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
605 $name = "\"$name\"";
606 }
607
608 return ($name, $address, $comment);
609}
610
611sub format_email {
612 my ($name, $address) = @_;
613
614 my $formatted_email;
615
Joe Perches3705ce52013-07-03 15:05:31 -0700616 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700617 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700618 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700619
620 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
621 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
622 $name = "\"$name\"";
623 }
624
625 if ("$name" eq "") {
626 $formatted_email = "$address";
627 } else {
628 $formatted_email = "$name <$address>";
629 }
630
631 return $formatted_email;
632}
633
Joe Perches000d1cc12011-07-25 17:13:25 -0700634sub which_conf {
635 my ($conf) = @_;
636
637 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
638 if (-e "$path/$conf") {
639 return "$path/$conf";
640 }
641 }
642
643 return "";
644}
645
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700646sub expand_tabs {
647 my ($str) = @_;
648
649 my $res = '';
650 my $n = 0;
651 for my $c (split(//, $str)) {
652 if ($c eq "\t") {
653 $res .= ' ';
654 $n++;
655 for (; ($n % 8) != 0; $n++) {
656 $res .= ' ';
657 }
658 next;
659 }
660 $res .= $c;
661 $n++;
662 }
663
664 return $res;
665}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700666sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700667 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700668 return $res;
669}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700670
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700671sub line_stats {
672 my ($line) = @_;
673
674 # Drop the diff line leader and expand tabs
675 $line =~ s/^.//;
676 $line = expand_tabs($line);
677
678 # Pick the indent from the front of the line.
679 my ($white) = ($line =~ /^(\s*)/);
680
681 return (length($line), length($white));
682}
683
Andy Whitcroft773647a2008-03-28 14:15:58 -0700684my $sanitise_quote = '';
685
686sub sanitise_line_reset {
687 my ($in_comment) = @_;
688
689 if ($in_comment) {
690 $sanitise_quote = '*/';
691 } else {
692 $sanitise_quote = '';
693 }
694}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700695sub sanitise_line {
696 my ($line) = @_;
697
698 my $res = '';
699 my $l = '';
700
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800701 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700702 my $off = 0;
703 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700704
Andy Whitcroft773647a2008-03-28 14:15:58 -0700705 # Always copy over the diff marker.
706 $res = substr($line, 0, 1);
707
708 for ($off = 1; $off < length($line); $off++) {
709 $c = substr($line, $off, 1);
710
711 # Comments we are wacking completly including the begin
712 # and end, all to $;.
713 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
714 $sanitise_quote = '*/';
715
716 substr($res, $off, 2, "$;$;");
717 $off++;
718 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800719 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700720 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700721 $sanitise_quote = '';
722 substr($res, $off, 2, "$;$;");
723 $off++;
724 next;
725 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700726 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
727 $sanitise_quote = '//';
728
729 substr($res, $off, 2, $sanitise_quote);
730 $off++;
731 next;
732 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700733
734 # A \ in a string means ignore the next character.
735 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
736 $c eq "\\") {
737 substr($res, $off, 2, 'XX');
738 $off++;
739 next;
740 }
741 # Regular quotes.
742 if ($c eq "'" || $c eq '"') {
743 if ($sanitise_quote eq '') {
744 $sanitise_quote = $c;
745
746 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700747 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700748 } elsif ($sanitise_quote eq $c) {
749 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700750 }
751 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700752
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800753 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700754 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
755 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700756 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
757 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700758 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
759 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700760 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700761 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700762 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800763 }
764
Daniel Walker113f04a2009-09-21 17:04:35 -0700765 if ($sanitise_quote eq '//') {
766 $sanitise_quote = '';
767 }
768
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800769 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700770 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800771 my $clean = 'X' x length($1);
772 $res =~ s@\<.*\>@<$clean>@;
773
774 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700775 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800776 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700777 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800778 }
779
Andy Whitcroft00df3442007-06-08 13:47:06 -0700780 return $res;
781}
782
Joe Perchesa6962d72013-04-29 16:18:13 -0700783sub get_quoted_string {
784 my ($line, $rawline) = @_;
785
786 return "" if ($line !~ m/(\"[X]+\")/g);
787 return substr($rawline, $-[0], $+[0] - $-[0]);
788}
789
Andy Whitcroft8905a672007-11-28 16:21:06 -0800790sub ctx_statement_block {
791 my ($linenr, $remain, $off) = @_;
792 my $line = $linenr - 1;
793 my $blk = '';
794 my $soff = $off;
795 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700796 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800797
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800798 my $loff = 0;
799
Andy Whitcroft8905a672007-11-28 16:21:06 -0800800 my $type = '';
801 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800802 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800803 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800804 my $c;
805 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800806
807 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800808 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800809 @stack = (['', 0]) if ($#stack == -1);
810
Andy Whitcroft773647a2008-03-28 14:15:58 -0700811 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800812 # If we are about to drop off the end, pull in more
813 # context.
814 if ($off >= $len) {
815 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700816 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800817 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800818 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800819 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800820 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800821 $len = length($blk);
822 $line++;
823 last;
824 }
825 # Bail if there is no further context.
826 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800827 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800828 last;
829 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800830 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
831 $level++;
832 $type = '#';
833 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800834 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800835 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800836 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800837 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800838
Andy Whitcroft773647a2008-03-28 14:15:58 -0700839 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800840
841 # Handle nested #if/#else.
842 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
843 push(@stack, [ $type, $level ]);
844 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
845 ($type, $level) = @{$stack[$#stack - 1]};
846 } elsif ($remainder =~ /^#\s*endif\b/) {
847 ($type, $level) = @{pop(@stack)};
848 }
849
Andy Whitcroft8905a672007-11-28 16:21:06 -0800850 # Statement ends at the ';' or a close '}' at the
851 # outermost level.
852 if ($level == 0 && $c eq ';') {
853 last;
854 }
855
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800856 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700857 if ($level == 0 && $coff_set == 0 &&
858 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
859 $remainder =~ /^(else)(?:\s|{)/ &&
860 $remainder !~ /^else\s+if\b/) {
861 $coff = $off + length($1) - 1;
862 $coff_set = 1;
863 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
864 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800865 }
866
Andy Whitcroft8905a672007-11-28 16:21:06 -0800867 if (($type eq '' || $type eq '(') && $c eq '(') {
868 $level++;
869 $type = '(';
870 }
871 if ($type eq '(' && $c eq ')') {
872 $level--;
873 $type = ($level != 0)? '(' : '';
874
875 if ($level == 0 && $coff < $soff) {
876 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700877 $coff_set = 1;
878 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800879 }
880 }
881 if (($type eq '' || $type eq '{') && $c eq '{') {
882 $level++;
883 $type = '{';
884 }
885 if ($type eq '{' && $c eq '}') {
886 $level--;
887 $type = ($level != 0)? '{' : '';
888
889 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700890 if (substr($blk, $off + 1, 1) eq ';') {
891 $off++;
892 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800893 last;
894 }
895 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800896 # Preprocessor commands end at the newline unless escaped.
897 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
898 $level--;
899 $type = '';
900 $off++;
901 last;
902 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800903 $off++;
904 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700905 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800906 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700907 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800908 $line++;
909 $remain--;
910 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800911
912 my $statement = substr($blk, $soff, $off - $soff + 1);
913 my $condition = substr($blk, $soff, $coff - $soff + 1);
914
915 #warn "STATEMENT<$statement>\n";
916 #warn "CONDITION<$condition>\n";
917
Andy Whitcroft773647a2008-03-28 14:15:58 -0700918 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800919
920 return ($statement, $condition,
921 $line, $remain + 1, $off - $loff + 1, $level);
922}
923
Andy Whitcroftcf655042008-03-04 14:28:20 -0800924sub statement_lines {
925 my ($stmt) = @_;
926
927 # Strip the diff line prefixes and rip blank lines at start and end.
928 $stmt =~ s/(^|\n)./$1/g;
929 $stmt =~ s/^\s*//;
930 $stmt =~ s/\s*$//;
931
932 my @stmt_lines = ($stmt =~ /\n/g);
933
934 return $#stmt_lines + 2;
935}
936
937sub statement_rawlines {
938 my ($stmt) = @_;
939
940 my @stmt_lines = ($stmt =~ /\n/g);
941
942 return $#stmt_lines + 2;
943}
944
945sub statement_block_size {
946 my ($stmt) = @_;
947
948 $stmt =~ s/(^|\n)./$1/g;
949 $stmt =~ s/^\s*{//;
950 $stmt =~ s/}\s*$//;
951 $stmt =~ s/^\s*//;
952 $stmt =~ s/\s*$//;
953
954 my @stmt_lines = ($stmt =~ /\n/g);
955 my @stmt_statements = ($stmt =~ /;/g);
956
957 my $stmt_lines = $#stmt_lines + 2;
958 my $stmt_statements = $#stmt_statements + 1;
959
960 if ($stmt_lines > $stmt_statements) {
961 return $stmt_lines;
962 } else {
963 return $stmt_statements;
964 }
965}
966
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800967sub ctx_statement_full {
968 my ($linenr, $remain, $off) = @_;
969 my ($statement, $condition, $level);
970
971 my (@chunks);
972
Andy Whitcroftcf655042008-03-04 14:28:20 -0800973 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800974 ($statement, $condition, $linenr, $remain, $off, $level) =
975 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700976 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800977 push(@chunks, [ $condition, $statement ]);
978 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
979 return ($level, $linenr, @chunks);
980 }
981
982 # Pull in the following conditional/block pairs and see if they
983 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800984 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800985 ($statement, $condition, $linenr, $remain, $off, $level) =
986 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800987 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700988 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800989 #print "C: push\n";
990 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800991 }
992
993 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800994}
995
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700996sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700997 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700998 my $line;
999 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001000 my $blk = '';
1001 my @o;
1002 my @c;
1003 my @res = ();
1004
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001005 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001006 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001007 for ($line = $start; $remain > 0; $line++) {
1008 next if ($rawlines[$line] =~ /^-/);
1009 $remain--;
1010
1011 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001012
1013 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001014 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001015 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001016 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001017 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001018 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001019 $level = pop(@stack);
1020 }
1021
Andy Whitcroft01464f32010-10-26 14:23:19 -07001022 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001023 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1024 if ($off > 0) {
1025 $off--;
1026 next;
1027 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001028
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001029 if ($c eq $close && $level > 0) {
1030 $level--;
1031 last if ($level == 0);
1032 } elsif ($c eq $open) {
1033 $level++;
1034 }
1035 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001036
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001037 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001038 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001039 }
1040
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001041 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001042 }
1043
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001044 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001045}
1046sub ctx_block_outer {
1047 my ($linenr, $remain) = @_;
1048
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001049 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1050 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001051}
1052sub ctx_block {
1053 my ($linenr, $remain) = @_;
1054
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001055 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1056 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001057}
1058sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001059 my ($linenr, $remain, $off) = @_;
1060
1061 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1062 return @r;
1063}
1064sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001065 my ($linenr, $remain) = @_;
1066
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001067 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001068}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001069sub ctx_statement_level {
1070 my ($linenr, $remain, $off) = @_;
1071
1072 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1073}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001074
1075sub ctx_locate_comment {
1076 my ($first_line, $end_line) = @_;
1077
1078 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001079 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001080 return $current_comment if (defined $current_comment);
1081
1082 # Look through the context and try and figure out if there is a
1083 # comment.
1084 my $in_comment = 0;
1085 $current_comment = '';
1086 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001087 my $line = $rawlines[$linenr - 1];
1088 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001089 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1090 $in_comment = 1;
1091 }
1092 if ($line =~ m@/\*@) {
1093 $in_comment = 1;
1094 }
1095 if (!$in_comment && $current_comment ne '') {
1096 $current_comment = '';
1097 }
1098 $current_comment .= $line . "\n" if ($in_comment);
1099 if ($line =~ m@\*/@) {
1100 $in_comment = 0;
1101 }
1102 }
1103
1104 chomp($current_comment);
1105 return($current_comment);
1106}
1107sub ctx_has_comment {
1108 my ($first_line, $end_line) = @_;
1109 my $cmt = ctx_locate_comment($first_line, $end_line);
1110
Andy Whitcroft00df3442007-06-08 13:47:06 -07001111 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001112 ##print "CMMT: $cmt\n";
1113
1114 return ($cmt ne '');
1115}
1116
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001117sub raw_line {
1118 my ($linenr, $cnt) = @_;
1119
1120 my $offset = $linenr - 1;
1121 $cnt++;
1122
1123 my $line;
1124 while ($cnt) {
1125 $line = $rawlines[$offset++];
1126 next if (defined($line) && $line =~ /^-/);
1127 $cnt--;
1128 }
1129
1130 return $line;
1131}
1132
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001133sub cat_vet {
1134 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001135 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001136
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001137 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001138 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1139 $res .= $1;
1140 if ($2 ne '') {
1141 $coded = sprintf("^%c", unpack('C', $2) + 64);
1142 $res .= $coded;
1143 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001144 }
1145 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001146
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001147 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001148}
1149
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001150my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001151my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001152my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001153my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001154
1155sub annotate_reset {
1156 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001157 $av_pending = '_';
1158 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001159 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001160}
1161
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001162sub annotate_values {
1163 my ($stream, $type) = @_;
1164
1165 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001166 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001167 my $cur = $stream;
1168
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001169 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001170
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001171 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001172 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001173 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001174 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001175 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001176 print "WS($1)\n" if ($dbg_values > 1);
1177 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001178 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001179 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001180 }
1181
Florian Micklerc023e4732011-01-12 16:59:58 -08001182 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001183 print "CAST($1)\n" if ($dbg_values > 1);
1184 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001185 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001186
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001187 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001188 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001189 $type = 'T';
1190
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001191 } elsif ($cur =~ /^($Modifier)\s*/) {
1192 print "MODIFIER($1)\n" if ($dbg_values > 1);
1193 $type = 'T';
1194
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001195 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001196 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001197 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001198 push(@av_paren_type, $type);
1199 if ($2 ne '') {
1200 $av_pending = 'N';
1201 }
1202 $type = 'E';
1203
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001204 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001205 print "UNDEF($1)\n" if ($dbg_values > 1);
1206 $av_preprocessor = 1;
1207 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001208
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001209 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001210 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001211 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001212
1213 push(@av_paren_type, $type);
1214 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001215 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001216
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001217 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001218 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1219 $av_preprocessor = 1;
1220
1221 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1222
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001223 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001224
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001225 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001226 print "PRE_END($1)\n" if ($dbg_values > 1);
1227
1228 $av_preprocessor = 1;
1229
1230 # Assume all arms of the conditional end as this
1231 # one does, and continue as if the #endif was not here.
1232 pop(@av_paren_type);
1233 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001234 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001235
1236 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001237 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001238
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001239 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1240 print "ATTR($1)\n" if ($dbg_values > 1);
1241 $av_pending = $type;
1242 $type = 'N';
1243
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001244 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001245 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001246 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001247 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001248 }
1249 $type = 'N';
1250
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001251 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001252 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001253 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001254 $type = 'N';
1255
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001256 } elsif ($cur =~/^(case)/o) {
1257 print "CASE($1)\n" if ($dbg_values > 1);
1258 $av_pend_colon = 'C';
1259 $type = 'N';
1260
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001261 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001262 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001263 $type = 'N';
1264
1265 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001266 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001267 push(@av_paren_type, $av_pending);
1268 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001269 $type = 'N';
1270
1271 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001272 my $new_type = pop(@av_paren_type);
1273 if ($new_type ne '_') {
1274 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001275 print "PAREN('$1') -> $type\n"
1276 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001277 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001278 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001279 }
1280
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001281 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001282 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001283 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001284 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001285
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001286 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1287 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001288 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001289 } elsif ($type eq 'E') {
1290 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001291 }
1292 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1293 $type = 'V';
1294
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001295 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001296 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001297 $type = 'V';
1298
1299 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001300 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001301 $type = 'N';
1302
Andy Whitcroftcf655042008-03-04 14:28:20 -08001303 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001304 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001305 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001306 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001307
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001308 } elsif ($cur =~/^(,)/) {
1309 print "COMMA($1)\n" if ($dbg_values > 1);
1310 $type = 'C';
1311
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001312 } elsif ($cur =~ /^(\?)/o) {
1313 print "QUESTION($1)\n" if ($dbg_values > 1);
1314 $type = 'N';
1315
1316 } elsif ($cur =~ /^(:)/o) {
1317 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1318
1319 substr($var, length($res), 1, $av_pend_colon);
1320 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1321 $type = 'E';
1322 } else {
1323 $type = 'N';
1324 }
1325 $av_pend_colon = 'O';
1326
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001327 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001328 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001329 $type = 'N';
1330
Andy Whitcroft0d413862008-10-15 22:02:16 -07001331 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001332 my $variant;
1333
1334 print "OPV($1)\n" if ($dbg_values > 1);
1335 if ($type eq 'V') {
1336 $variant = 'B';
1337 } else {
1338 $variant = 'U';
1339 }
1340
1341 substr($var, length($res), 1, $variant);
1342 $type = 'N';
1343
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001344 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001345 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001346 if ($1 ne '++' && $1 ne '--') {
1347 $type = 'N';
1348 }
1349
1350 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001351 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001352 }
1353 if (defined $1) {
1354 $cur = substr($cur, length($1));
1355 $res .= $type x length($1);
1356 }
1357 }
1358
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001359 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001360}
1361
Andy Whitcroft8905a672007-11-28 16:21:06 -08001362sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001363 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001364 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001365 ^(?:
1366 $Modifier|
1367 $Storage|
1368 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001369 DEFINE_\S+
1370 )$|
1371 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001372 goto|
1373 return|
1374 case|
1375 else|
1376 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001377 do|
1378 \#|
1379 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001380 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001381 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001382 )}x;
1383 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1384 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001385 # Check for modifiers.
1386 $possible =~ s/\s*$Storage\s*//g;
1387 $possible =~ s/\s*$Sparse\s*//g;
1388 if ($possible =~ /^\s*$/) {
1389
1390 } elsif ($possible =~ /\s/) {
1391 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001392 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001393 if ($modifier !~ $notPermitted) {
1394 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1395 push(@modifierList, $modifier);
1396 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001397 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001398
1399 } else {
1400 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1401 push(@typeList, $possible);
1402 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001403 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001404 } else {
1405 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001406 }
1407}
1408
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001409my $prefix = '';
1410
Joe Perches000d1cc12011-07-25 17:13:25 -07001411sub show_type {
Joe Perches91bfe482013-09-11 14:23:59 -07001412 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1413
1414 return !defined $ignore_type{$_[0]};
Joe Perches000d1cc12011-07-25 17:13:25 -07001415}
1416
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001417sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001418 if (!show_type($_[1]) ||
1419 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001420 return 0;
1421 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001422 my $line;
1423 if ($show_types) {
1424 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1425 } else {
1426 $line = "$prefix$_[0]: $_[2]\n";
1427 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001428 $line = (split('\n', $line))[0] . "\n" if ($terse);
1429
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001430 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001431
1432 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001433}
1434sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001435 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001436}
Joe Perches000d1cc12011-07-25 17:13:25 -07001437
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001438sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001439 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001440 our $clean = 0;
1441 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001442 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001443 }
Joe Perches3705ce52013-07-03 15:05:31 -07001444 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001445}
1446sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001447 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001448 our $clean = 0;
1449 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001450 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001451 }
Joe Perches3705ce52013-07-03 15:05:31 -07001452 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001453}
1454sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001455 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001456 our $clean = 0;
1457 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001458 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001459 }
Joe Perches3705ce52013-07-03 15:05:31 -07001460 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001461}
1462
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001463sub check_absolute_file {
1464 my ($absolute, $herecurr) = @_;
1465 my $file = $absolute;
1466
1467 ##print "absolute<$absolute>\n";
1468
1469 # See if any suffix of this path is a path within the tree.
1470 while ($file =~ s@^[^/]*/@@) {
1471 if (-f "$root/$file") {
1472 ##print "file<$file>\n";
1473 last;
1474 }
1475 }
1476 if (! -f _) {
1477 return 0;
1478 }
1479
1480 # It is, so see if the prefix is acceptable.
1481 my $prefix = $absolute;
1482 substr($prefix, -length($file)) = '';
1483
1484 ##print "prefix<$prefix>\n";
1485 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001486 WARN("USE_RELATIVE_PATH",
1487 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001488 }
1489}
1490
Joe Perches3705ce52013-07-03 15:05:31 -07001491sub trim {
1492 my ($string) = @_;
1493
Joe Perchesb34c6482013-09-11 14:24:01 -07001494 $string =~ s/^\s+|\s+$//g;
1495
1496 return $string;
1497}
1498
1499sub ltrim {
1500 my ($string) = @_;
1501
1502 $string =~ s/^\s+//;
1503
1504 return $string;
1505}
1506
1507sub rtrim {
1508 my ($string) = @_;
1509
1510 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001511
1512 return $string;
1513}
1514
1515sub tabify {
1516 my ($leading) = @_;
1517
1518 my $source_indent = 8;
1519 my $max_spaces_before_tab = $source_indent - 1;
1520 my $spaces_to_tab = " " x $source_indent;
1521
1522 #convert leading spaces to tabs
1523 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1524 #Remove spaces before a tab
1525 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1526
1527 return "$leading";
1528}
1529
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001530sub pos_last_openparen {
1531 my ($line) = @_;
1532
1533 my $pos = 0;
1534
1535 my $opens = $line =~ tr/\(/\(/;
1536 my $closes = $line =~ tr/\)/\)/;
1537
1538 my $last_openparen = 0;
1539
1540 if (($opens == 0) || ($closes >= $opens)) {
1541 return -1;
1542 }
1543
1544 my $len = length($line);
1545
1546 for ($pos = 0; $pos < $len; $pos++) {
1547 my $string = substr($line, $pos);
1548 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1549 $pos += length($1) - 1;
1550 } elsif (substr($line, $pos, 1) eq '(') {
1551 $last_openparen = $pos;
1552 } elsif (index($string, '(') == -1) {
1553 last;
1554 }
1555 }
1556
1557 return $last_openparen + 1;
1558}
1559
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001560sub process {
1561 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001562
1563 my $linenr=0;
1564 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001565 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001566 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001567 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001568
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001569 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001570 my $indent;
1571 my $previndent=0;
1572 my $stashindent=0;
1573
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001574 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001575 my $signoff = 0;
1576 my $is_patch = 0;
1577
Joe Perches15662b32011-10-31 17:13:12 -07001578 my $in_header_lines = 1;
1579 my $in_commit_log = 0; #Scanning lines before patch
1580
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001581 my $non_utf8_charset = 0;
1582
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001583 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001584 our $cnt_lines = 0;
1585 our $cnt_error = 0;
1586 our $cnt_warn = 0;
1587 our $cnt_chk = 0;
1588
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001589 # Trace the real file/line as we go.
1590 my $realfile = '';
1591 my $realline = 0;
1592 my $realcnt = 0;
1593 my $here = '';
1594 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001595 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001596 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001597 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001598
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001599 my $prev_values = 'E';
1600
1601 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001602 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001603 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001604 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001605 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001606
Joe Perches7e51f192013-09-11 14:23:57 -07001607 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001608
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001609 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001610 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001611 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001612 my @setup_docs = ();
1613 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001614
Joe Perchesd8b07712013-11-12 15:10:06 -08001615 my $camelcase_file_seeded = 0;
1616
Andy Whitcroft773647a2008-03-28 14:15:58 -07001617 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001618 my $line;
1619 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001620 $linenr++;
1621 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001622
Joe Perches3705ce52013-07-03 15:05:31 -07001623 push(@fixed, $rawline) if ($fix);
1624
Andy Whitcroft773647a2008-03-28 14:15:58 -07001625 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001626 $setup_docs = 0;
1627 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1628 $setup_docs = 1;
1629 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001630 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001631 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001632 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1633 $realline=$1-1;
1634 if (defined $2) {
1635 $realcnt=$3+1;
1636 } else {
1637 $realcnt=1+1;
1638 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001639 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001640
1641 # Guestimate if this is a continuing comment. Run
1642 # the context looking for a comment "edge". If this
1643 # edge is a close comment then we must be in a comment
1644 # at context start.
1645 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001646 my $cnt = $realcnt;
1647 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1648 next if (defined $rawlines[$ln - 1] &&
1649 $rawlines[$ln - 1] =~ /^-/);
1650 $cnt--;
1651 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001652 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001653 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1654 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1655 ($edge) = $1;
1656 last;
1657 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001658 }
1659 if (defined $edge && $edge eq '*/') {
1660 $in_comment = 1;
1661 }
1662
1663 # Guestimate if this is a continuing comment. If this
1664 # is the start of a diff block and this line starts
1665 # ' *' then it is very likely a comment.
1666 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001667 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001668 {
1669 $in_comment = 1;
1670 }
1671
1672 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1673 sanitise_line_reset($in_comment);
1674
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001675 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001676 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001677 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001678 $line = sanitise_line($rawline);
1679 }
1680 push(@lines, $line);
1681
1682 if ($realcnt > 1) {
1683 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1684 } else {
1685 $realcnt = 0;
1686 }
1687
1688 #print "==>$rawline\n";
1689 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001690
1691 if ($setup_docs && $line =~ /^\+/) {
1692 push(@setup_docs, $line);
1693 }
1694 }
1695
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001696 $prefix = '';
1697
Andy Whitcroft773647a2008-03-28 14:15:58 -07001698 $realcnt = 0;
1699 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001700 foreach my $line (@lines) {
1701 $linenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001702 my $sline = $line; #copy of $line
1703 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001704
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001705 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001706
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001707#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001708 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001709 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001710 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001711 $realline=$1-1;
1712 if (defined $2) {
1713 $realcnt=$3+1;
1714 } else {
1715 $realcnt=1+1;
1716 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001717 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001718 $prev_values = 'E';
1719
Andy Whitcroft773647a2008-03-28 14:15:58 -07001720 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001721 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001722 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001723 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001724 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001725
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001726# track the line number as we move through the hunk, note that
1727# new versions of GNU diff omit the leading space on completely
1728# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001729 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001730 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001731 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001732
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001733 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001734 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001735
1736 # Track the previous line.
1737 ($prevline, $stashline) = ($stashline, $line);
1738 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001739 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1740
Andy Whitcroft773647a2008-03-28 14:15:58 -07001741 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001743 } elsif ($realcnt == 1) {
1744 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001745 }
1746
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001747 my $hunk_line = ($realcnt != 0);
1748
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001749#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001750 $prefix = "$filename:$realline: " if ($emacs && $file);
1751 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1752
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001753 $here = "#$linenr: " if (!$file);
1754 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001755
1756 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001757 if ($line =~ /^diff --git.*?(\S+)$/) {
1758 $realfile = $1;
1759 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001760 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001761 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001762 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001763 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001764 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001765
1766 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001767 if (!$file && $tree && $p1_prefix ne '' &&
1768 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001769 WARN("PATCH_PREFIX",
1770 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001771 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001772
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001773 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001774 ERROR("MODIFIED_INCLUDE_ASM",
1775 "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 -07001776 }
1777 next;
1778 }
1779
Randy Dunlap389834b2007-06-08 13:47:03 -07001780 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001781
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001782 my $hereline = "$here\n$rawline\n";
1783 my $herecurr = "$here\n$rawline\n";
1784 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001785
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001786 $cnt_lines++ if ($realcnt != 0);
1787
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001788# Check for incorrect file permissions
1789 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1790 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001791 if ($realfile !~ m@scripts/@ &&
1792 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001793 ERROR("EXECUTE_PERMISSIONS",
1794 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001795 }
1796 }
1797
Joe Perches20112472011-07-25 17:13:23 -07001798# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001799 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001800 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001801 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001802 }
1803
1804# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001805 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001806 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001807 my $space_before = $1;
1808 my $sign_off = $2;
1809 my $space_after = $3;
1810 my $email = $4;
1811 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1812
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001813 if ($sign_off !~ /$signature_tags/) {
1814 WARN("BAD_SIGN_OFF",
1815 "Non-standard signature: $sign_off\n" . $herecurr);
1816 }
Joe Perches20112472011-07-25 17:13:23 -07001817 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001818 if (WARN("BAD_SIGN_OFF",
1819 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1820 $fix) {
1821 $fixed[$linenr - 1] =
1822 "$ucfirst_sign_off $email";
1823 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001824 }
Joe Perches20112472011-07-25 17:13:23 -07001825 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001826 if (WARN("BAD_SIGN_OFF",
1827 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1828 $fix) {
1829 $fixed[$linenr - 1] =
1830 "$ucfirst_sign_off $email";
1831 }
1832
Joe Perches20112472011-07-25 17:13:23 -07001833 }
1834 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001835 if (WARN("BAD_SIGN_OFF",
1836 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1837 $fix) {
1838 $fixed[$linenr - 1] =
1839 "$ucfirst_sign_off $email";
1840 }
Joe Perches20112472011-07-25 17:13:23 -07001841 }
1842
1843 my ($email_name, $email_address, $comment) = parse_email($email);
1844 my $suggested_email = format_email(($email_name, $email_address));
1845 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001846 ERROR("BAD_SIGN_OFF",
1847 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001848 } else {
1849 my $dequoted = $suggested_email;
1850 $dequoted =~ s/^"//;
1851 $dequoted =~ s/" </ </;
1852 # Don't force email to have quotes
1853 # Allow just an angle bracketed address
1854 if ("$dequoted$comment" ne $email &&
1855 "<$email_address>$comment" ne $email &&
1856 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001857 WARN("BAD_SIGN_OFF",
1858 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001859 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001860 }
Joe Perches7e51f192013-09-11 14:23:57 -07001861
1862# Check for duplicate signatures
1863 my $sig_nospace = $line;
1864 $sig_nospace =~ s/\s//g;
1865 $sig_nospace = lc($sig_nospace);
1866 if (defined $signatures{$sig_nospace}) {
1867 WARN("BAD_SIGN_OFF",
1868 "Duplicate signature\n" . $herecurr);
1869 } else {
1870 $signatures{$sig_nospace} = 1;
1871 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001872 }
1873
Andy Whitcroft00df3442007-06-08 13:47:06 -07001874# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001875 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001876 ERROR("CORRUPTED_PATCH",
1877 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001878 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001879 }
1880
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001881# Check for absolute kernel paths.
1882 if ($tree) {
1883 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1884 my $file = $1;
1885
1886 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1887 check_absolute_file($1, $herecurr)) {
1888 #
1889 } else {
1890 check_absolute_file($file, $herecurr);
1891 }
1892 }
1893 }
1894
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001895# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1896 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001897 $rawline !~ m/^$UTF8*$/) {
1898 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1899
1900 my $blank = copy_spacing($rawline);
1901 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1902 my $hereptr = "$hereline$ptr\n";
1903
Joe Perches34d99212011-07-25 17:13:26 -07001904 CHK("INVALID_UTF8",
1905 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001906 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001907
Joe Perches15662b32011-10-31 17:13:12 -07001908# Check if it's the start of a commit log
1909# (not a header line and we haven't seen the patch filename)
1910 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001911 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001912 $in_header_lines = 0;
1913 $in_commit_log = 1;
1914 }
1915
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001916# Check if there is UTF-8 in a commit log when a mail header has explicitly
1917# declined it, i.e defined some charset where it is missing.
1918 if ($in_header_lines &&
1919 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1920 $1 !~ /utf-8/i) {
1921 $non_utf8_charset = 1;
1922 }
1923
1924 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001925 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001926 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001927 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1928 }
1929
Andy Whitcroft306708542008-10-15 22:02:28 -07001930# ignore non-hunk lines and lines being removed
1931 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001932
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001933#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001934 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001935 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07001936 if (ERROR("DOS_LINE_ENDINGS",
1937 "DOS line endings\n" . $herevet) &&
1938 $fix) {
1939 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1940 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001941 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1942 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001943 if (ERROR("TRAILING_WHITESPACE",
1944 "trailing whitespace\n" . $herevet) &&
1945 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07001946 $fixed[$linenr - 1] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001947 }
1948
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001949 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001950 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001951
Andi Kleen33549572010-05-24 14:33:29 -07001952# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001953# Only applies when adding the entry originally, after that we do not have
1954# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001955 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001956 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001957 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001958 my $cnt = $realcnt;
1959 my $ln = $linenr + 1;
1960 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001961 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001962 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001963 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001964 $f = $lines[$ln - 1];
1965 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1966 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001967
1968 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001969
1970 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1971 $is_start = 1;
1972 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1973 $length = -1;
1974 }
1975
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001976 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001977 $f =~ s/#.*//;
1978 $f =~ s/^\s+//;
1979 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001980 if ($f =~ /^\s*config\s/) {
1981 $is_end = 1;
1982 last;
1983 }
Andi Kleen33549572010-05-24 14:33:29 -07001984 $length++;
1985 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001986 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001987 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1988 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001989 }
1990
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001991# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1992 if ($realfile =~ /Kconfig/ &&
1993 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1994 WARN("CONFIG_EXPERIMENTAL",
1995 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1996 }
1997
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001998 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1999 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2000 my $flag = $1;
2001 my $replacement = {
2002 'EXTRA_AFLAGS' => 'asflags-y',
2003 'EXTRA_CFLAGS' => 'ccflags-y',
2004 'EXTRA_CPPFLAGS' => 'cppflags-y',
2005 'EXTRA_LDFLAGS' => 'ldflags-y',
2006 };
2007
2008 WARN("DEPRECATED_VARIABLE",
2009 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2010 }
2011
Andy Whitcroft5368df22008-10-15 22:02:27 -07002012# check we are in a valid source file if not then ignore this hunk
2013 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2014
Joe Perches6cd7f382012-12-17 16:01:54 -08002015#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002016 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002017 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002018 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002019 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002020 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002021 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002022 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002023 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002024 }
2025
Josh Triplettca56dc02012-03-23 15:02:21 -07002026# Check for user-visible strings broken across lines, which breaks the ability
2027# to grep for the string. Limited to strings used as parameters (those
2028# following an open parenthesis), which almost completely eliminates false
2029# positives, as well as warning only once per parameter rather than once per
2030# line of the string. Make an exception when the previous string ends in a
2031# newline (multiple lines in one string constant) or \n\t (common in inline
2032# assembly to indent the instruction on the following line).
2033 if ($line =~ /^\+\s*"/ &&
2034 $prevline =~ /"\s*$/ &&
2035 $prevline =~ /\(/ &&
2036 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
2037 WARN("SPLIT_STRING",
2038 "quoted string split across lines\n" . $hereprev);
2039 }
2040
Joe Perches5e79d962010-03-05 13:43:55 -08002041# check for spaces before a quoted newline
2042 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002043 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2044 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2045 $fix) {
2046 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2047 }
2048
Joe Perches5e79d962010-03-05 13:43:55 -08002049 }
2050
Andy Whitcroft8905a672007-11-28 16:21:06 -08002051# check for adding lines without a newline.
2052 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002053 WARN("MISSING_EOF_NEWLINE",
2054 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002055 }
2056
Mike Frysinger42e41c52009-09-21 17:04:40 -07002057# Blackfin: use hi/lo macros
2058 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2059 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2060 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002061 ERROR("LO_MACRO",
2062 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002063 }
2064 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2065 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002066 ERROR("HI_MACRO",
2067 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002068 }
2069 }
2070
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002071# check we are in a valid source file C or perl if not then ignore this hunk
2072 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002073
2074# at the beginning of a line any tabs must come first and anything
2075# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002076 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2077 $rawline =~ /^\+\s* \s*/) {
2078 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002079 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002080 if (ERROR("CODE_INDENT",
2081 "code indent should use tabs where possible\n" . $herevet) &&
2082 $fix) {
2083 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2084 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002085 }
2086
Alberto Panizzo08e44362010-03-05 13:43:54 -08002087# check for space before tabs.
2088 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2089 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002090 if (WARN("SPACE_BEFORE_TAB",
2091 "please, no space before tabs\n" . $herevet) &&
2092 $fix) {
2093 $fixed[$linenr - 1] =~
2094 s/(^\+.*) +\t/$1\t/;
2095 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002096 }
2097
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002098# check for && or || at the start of a line
2099 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2100 CHK("LOGICAL_CONTINUATIONS",
2101 "Logical continuations should be on the previous line\n" . $hereprev);
2102 }
2103
2104# check multi-line statement indentation matches previous line
2105 if ($^V && $^V ge 5.10.0 &&
2106 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2107 $prevline =~ /^\+(\t*)(.*)$/;
2108 my $oldindent = $1;
2109 my $rest = $2;
2110
2111 my $pos = pos_last_openparen($rest);
2112 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002113 $line =~ /^(\+| )([ \t]*)/;
2114 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002115
2116 my $goodtabindent = $oldindent .
2117 "\t" x ($pos / 8) .
2118 " " x ($pos % 8);
2119 my $goodspaceindent = $oldindent . " " x $pos;
2120
2121 if ($newindent ne $goodtabindent &&
2122 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002123
2124 if (CHK("PARENTHESIS_ALIGNMENT",
2125 "Alignment should match open parenthesis\n" . $hereprev) &&
2126 $fix && $line =~ /^\+/) {
2127 $fixed[$linenr - 1] =~
2128 s/^\+[ \t]*/\+$goodtabindent/;
2129 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002130 }
2131 }
2132 }
2133
Joe Perches23f780c2013-07-03 15:05:31 -07002134 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002135 if (CHK("SPACING",
2136 "No space is necessary after a cast\n" . $hereprev) &&
2137 $fix) {
2138 $fixed[$linenr - 1] =~
2139 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2140 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002141 }
2142
Joe Perches05880602012-10-04 17:13:35 -07002143 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002144 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2145 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002146 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2147 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2148 }
2149
2150 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002151 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2152 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002153 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002154 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2155 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2156 "networking block comments start with * on subsequent lines\n" . $hereprev);
2157 }
2158
2159 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002160 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2161 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2162 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2163 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002164 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2165 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2166 }
2167
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002168# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002169# Exceptions:
2170# 1) within comments
2171# 2) indented preprocessor commands
2172# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002173 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002174 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002175 if (WARN("LEADING_SPACE",
2176 "please, no spaces at the start of a line\n" . $herevet) &&
2177 $fix) {
2178 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2179 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002180 }
2181
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002182# check we are in a valid C source file if not then ignore this hunk
2183 next if ($realfile !~ /\.(h|c)$/);
2184
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002185# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2186 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2187 WARN("CONFIG_EXPERIMENTAL",
2188 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2189 }
2190
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002191# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002192 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002193 WARN("CVS_KEYWORD",
2194 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002195 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002196
Mike Frysinger42e41c52009-09-21 17:04:40 -07002197# Blackfin: don't use __builtin_bfin_[cs]sync
2198 if ($line =~ /__builtin_bfin_csync/) {
2199 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002200 ERROR("CSYNC",
2201 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002202 }
2203 if ($line =~ /__builtin_bfin_ssync/) {
2204 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002205 ERROR("SSYNC",
2206 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002207 }
2208
Joe Perches56e77d72013-02-21 16:44:14 -08002209# check for old HOTPLUG __dev<foo> section markings
2210 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2211 WARN("HOTPLUG_SECTION",
2212 "Using $1 is unnecessary\n" . $herecurr);
2213 }
2214
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002215# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002216 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2217 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002218#print "LINE<$line>\n";
2219 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002220 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002221 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002222 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002223 $stat =~ s/\n./\n /g;
2224 $cond =~ s/\n./\n /g;
2225
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002226#print "linenr<$linenr> <$stat>\n";
2227 # If this statement has no statement boundaries within
2228 # it there is no point in retrying a statement scan
2229 # until we hit end of it.
2230 my $frag = $stat; $frag =~ s/;+\s*$//;
2231 if ($frag !~ /(?:{|;)/) {
2232#print "skip<$line_nr_next>\n";
2233 $suppress_statement = $line_nr_next;
2234 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002235
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002236 # Find the real next line.
2237 $realline_next = $line_nr_next;
2238 if (defined $realline_next &&
2239 (!defined $lines[$realline_next - 1] ||
2240 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2241 $realline_next++;
2242 }
2243
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002244 my $s = $stat;
2245 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002246
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002247 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002248 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002249
2250 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002251 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002252
Andy Whitcroft463f2862009-09-21 17:04:34 -07002253 } elsif ($s =~ /^.\s*else\b/s) {
2254
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002255 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002256 } 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 -07002257 my $type = $1;
2258 $type =~ s/\s+/ /g;
2259 possible($type, "A:" . $s);
2260
Andy Whitcroft8905a672007-11-28 16:21:06 -08002261 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002262 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002263 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002264 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002265
2266 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002267 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002268 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002269 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002270
2271 # Check for any sort of function declaration.
2272 # int foo(something bar, other baz);
2273 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002274 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 -08002275 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002276
Andy Whitcroftcf655042008-03-04 14:28:20 -08002277 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002278 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002279 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002280
Andy Whitcroft8905a672007-11-28 16:21:06 -08002281 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002282 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002283
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002284 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002285 }
2286 }
2287 }
2288
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002289 }
2290
Andy Whitcroft653d4872007-06-23 17:16:34 -07002291#
2292# Checks which may be anchored in the context.
2293#
2294
2295# Check for switch () and associated case and default
2296# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002297 if ($line=~/\bswitch\s*\(.*\)/) {
2298 my $err = '';
2299 my $sep = '';
2300 my @ctx = ctx_block_outer($linenr, $realcnt);
2301 shift(@ctx);
2302 for my $ctx (@ctx) {
2303 my ($clen, $cindent) = line_stats($ctx);
2304 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2305 $indent != $cindent) {
2306 $err .= "$sep$ctx\n";
2307 $sep = '';
2308 } else {
2309 $sep = "[...]\n";
2310 }
2311 }
2312 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002313 ERROR("SWITCH_CASE_INDENT_LEVEL",
2314 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002315 }
2316 }
2317
2318# if/while/etc brace do not go on next line, unless defining a do while loop,
2319# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002320 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002321 my $pre_ctx = "$1$2";
2322
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002323 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002324
2325 if ($line =~ /^\+\t{6,}/) {
2326 WARN("DEEP_INDENTATION",
2327 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2328 }
2329
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002330 my $ctx_cnt = $realcnt - $#ctx - 1;
2331 my $ctx = join("\n", @ctx);
2332
Andy Whitcroft548596d2008-07-23 21:29:01 -07002333 my $ctx_ln = $linenr;
2334 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002335
Andy Whitcroft548596d2008-07-23 21:29:01 -07002336 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2337 defined $lines[$ctx_ln - 1] &&
2338 $lines[$ctx_ln - 1] =~ /^-/)) {
2339 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2340 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002341 $ctx_ln++;
2342 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002343
Andy Whitcroft53210162008-07-23 21:29:03 -07002344 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2345 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002346
2347 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002348 ERROR("OPEN_BRACE",
2349 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002350 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002351 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002352 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2353 $ctx =~ /\)\s*\;\s*$/ &&
2354 defined $lines[$ctx_ln - 1])
2355 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002356 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2357 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002358 WARN("TRAILING_SEMICOLON",
2359 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002360 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002361 }
2362 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002363 }
2364
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002365# Check relative indent for conditionals and blocks.
2366 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002367 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2368 ctx_statement_block($linenr, $realcnt, 0)
2369 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002370 my ($s, $c) = ($stat, $cond);
2371
2372 substr($s, 0, length($c), '');
2373
2374 # Make sure we remove the line prefixes as we have
2375 # none on the first line, and are going to readd them
2376 # where necessary.
2377 $s =~ s/\n./\n/gs;
2378
2379 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002380 my @newlines = ($c =~ /\n/gs);
2381 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002382
2383 # We want to check the first line inside the block
2384 # starting at the end of the conditional, so remove:
2385 # 1) any blank line termination
2386 # 2) any opening brace { on end of the line
2387 # 3) any do (...) {
2388 my $continuation = 0;
2389 my $check = 0;
2390 $s =~ s/^.*\bdo\b//;
2391 $s =~ s/^\s*{//;
2392 if ($s =~ s/^\s*\\//) {
2393 $continuation = 1;
2394 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002395 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002396 $check = 1;
2397 $cond_lines++;
2398 }
2399
2400 # Also ignore a loop construct at the end of a
2401 # preprocessor statement.
2402 if (($prevline =~ /^.\s*#\s*define\s/ ||
2403 $prevline =~ /\\\s*$/) && $continuation == 0) {
2404 $check = 0;
2405 }
2406
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002407 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002408 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002409 while ($cond_ptr != $cond_lines) {
2410 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002411
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002412 # If we see an #else/#elif then the code
2413 # is not linear.
2414 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2415 $check = 0;
2416 }
2417
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002418 # Ignore:
2419 # 1) blank lines, they should be at 0,
2420 # 2) preprocessor lines, and
2421 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002422 if ($continuation ||
2423 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002424 $s =~ /^\s*#\s*?/ ||
2425 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002426 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002427 if ($s =~ s/^.*?\n//) {
2428 $cond_lines++;
2429 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002430 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002431 }
2432
2433 my (undef, $sindent) = line_stats("+" . $s);
2434 my $stat_real = raw_line($linenr, $cond_lines);
2435
2436 # Check if either of these lines are modified, else
2437 # this is not this patch's fault.
2438 if (!defined($stat_real) ||
2439 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2440 $check = 0;
2441 }
2442 if (defined($stat_real) && $cond_lines > 1) {
2443 $stat_real = "[...]\n$stat_real";
2444 }
2445
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002446 #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 -07002447
2448 if ($check && (($sindent % 8) != 0 ||
2449 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002450 WARN("SUSPECT_CODE_INDENT",
2451 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002452 }
2453 }
2454
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002455 # Track the 'values' across context and added lines.
2456 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002457 my ($curr_values, $curr_vars) =
2458 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002459 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002460 if ($dbg_values) {
2461 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002462 print "$linenr > .$outline\n";
2463 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002464 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002465 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002466 $prev_values = substr($curr_values, -1);
2467
Andy Whitcroft00df3442007-06-08 13:47:06 -07002468#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002469 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002470
Andy Whitcroft653d4872007-06-23 17:16:34 -07002471# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002472 if ($dbg_type) {
2473 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002474 ERROR("TEST_TYPE",
2475 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002476 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002477 ERROR("TEST_NOT_TYPE",
2478 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002479 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002480 next;
2481 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002482# TEST: allow direct testing of the attribute matcher.
2483 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002484 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002485 ERROR("TEST_ATTR",
2486 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002487 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002488 ERROR("TEST_NOT_ATTR",
2489 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002490 }
2491 next;
2492 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002493
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002494# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002495 if ($line =~ /^.\s*{/ &&
2496 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002497 ERROR("OPEN_BRACE",
2498 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002499 }
2500
Andy Whitcroft653d4872007-06-23 17:16:34 -07002501#
2502# Checks which are anchored on the added line.
2503#
2504
2505# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002506 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002507 my $path = $1;
2508 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002509 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002510 "malformed #include filename\n" . $herecurr);
2511 }
2512 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2513 ERROR("UAPI_INCLUDE",
2514 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002515 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002516 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002517
2518# no C99 // comments
2519 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002520 if (ERROR("C99_COMMENTS",
2521 "do not use C99 // comments\n" . $herecurr) &&
2522 $fix) {
2523 my $line = $fixed[$linenr - 1];
2524 if ($line =~ /\/\/(.*)$/) {
2525 my $comment = trim($1);
2526 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2527 }
2528 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002529 }
2530 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002531 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002532 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002533
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002534# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2535# the whole statement.
2536#print "APW <$lines[$realline_next - 1]>\n";
2537 if (defined $realline_next &&
2538 exists $lines[$realline_next - 1] &&
2539 !defined $suppress_export{$realline_next} &&
2540 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2541 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002542 # Handle definitions which produce identifiers with
2543 # a prefix:
2544 # XXX(foo);
2545 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002546 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002547 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002548 $name =~ /^${Ident}_$2/) {
2549#print "FOO C name<$name>\n";
2550 $suppress_export{$realline_next} = 1;
2551
2552 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002553 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002554 ^.DEFINE_$Ident\(\Q$name\E\)|
2555 ^.DECLARE_$Ident\(\Q$name\E\)|
2556 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002557 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2558 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002559 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002560#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2561 $suppress_export{$realline_next} = 2;
2562 } else {
2563 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002564 }
2565 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002566 if (!defined $suppress_export{$linenr} &&
2567 $prevline =~ /^.\s*$/ &&
2568 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2569 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2570#print "FOO B <$lines[$linenr - 1]>\n";
2571 $suppress_export{$linenr} = 2;
2572 }
2573 if (defined $suppress_export{$linenr} &&
2574 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002575 WARN("EXPORT_SYMBOL",
2576 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002577 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002578
Joe Eloff5150bda2010-08-09 17:21:00 -07002579# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002580 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2581 if (ERROR("GLOBAL_INITIALISERS",
2582 "do not initialise globals to 0 or NULL\n" .
2583 $herecurr) &&
2584 $fix) {
2585 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2586 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002587 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002588# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002589 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2590 if (ERROR("INITIALISED_STATIC",
2591 "do not initialise statics to 0 or NULL\n" .
2592 $herecurr) &&
2593 $fix) {
2594 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2595 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002596 }
2597
Joe Perchescb710ec2010-10-26 14:23:20 -07002598# check for static const char * arrays.
2599 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002600 WARN("STATIC_CONST_CHAR_ARRAY",
2601 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002602 $herecurr);
2603 }
2604
2605# check for static char foo[] = "bar" declarations.
2606 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002607 WARN("STATIC_CONST_CHAR_ARRAY",
2608 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002609 $herecurr);
2610 }
2611
Joe Perches93ed0e22010-10-26 14:23:21 -07002612# check for declarations of struct pci_device_id
2613 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002614 WARN("DEFINE_PCI_DEVICE_TABLE",
2615 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002616 }
2617
Andy Whitcroft653d4872007-06-23 17:16:34 -07002618# check for new typedefs, only function parameters and sparse annotations
2619# make sense.
2620 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002621 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002622 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002623 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002624 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002625 WARN("NEW_TYPEDEFS",
2626 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002627 }
2628
2629# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002630 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002631 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2632 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002633 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002634
Andy Whitcroft65863862009-01-06 14:41:21 -08002635 # Should start with a space.
2636 $to =~ s/^(\S)/ $1/;
2637 # Should not end with a space.
2638 $to =~ s/\s+$//;
2639 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002640 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002641 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002642
Joe Perches3705ce52013-07-03 15:05:31 -07002643## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002644 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002645 if (ERROR("POINTER_LOCATION",
2646 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2647 $fix) {
2648 my $sub_from = $ident;
2649 my $sub_to = $ident;
2650 $sub_to =~ s/\Q$from\E/$to/;
2651 $fixed[$linenr - 1] =~
2652 s@\Q$sub_from\E@$sub_to@;
2653 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002654 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002655 }
2656 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2657 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002658 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002659
Andy Whitcroft65863862009-01-06 14:41:21 -08002660 # Should start with a space.
2661 $to =~ s/^(\S)/ $1/;
2662 # Should not end with a space.
2663 $to =~ s/\s+$//;
2664 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002665 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002666 }
2667 # Modifiers should have spaces.
2668 $to =~ s/(\b$Modifier$)/$1 /;
2669
Joe Perches3705ce52013-07-03 15:05:31 -07002670## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002671 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002672 if (ERROR("POINTER_LOCATION",
2673 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2674 $fix) {
2675
2676 my $sub_from = $match;
2677 my $sub_to = $match;
2678 $sub_to =~ s/\Q$from\E/$to/;
2679 $fixed[$linenr - 1] =~
2680 s@\Q$sub_from\E@$sub_to@;
2681 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002682 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002683 }
2684
2685# # no BUG() or BUG_ON()
2686# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2687# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2688# print "$herecurr";
2689# $clean = 0;
2690# }
2691
Andy Whitcroft8905a672007-11-28 16:21:06 -08002692 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002693 WARN("LINUX_VERSION_CODE",
2694 "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 -08002695 }
2696
Joe Perches17441222011-06-15 15:08:17 -07002697# check for uses of printk_ratelimit
2698 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002699 WARN("PRINTK_RATELIMITED",
2700"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002701 }
2702
Andy Whitcroft00df3442007-06-08 13:47:06 -07002703# printk should use KERN_* levels. Note that follow on printk's on the
2704# same line do not need a level, so we use the current block context
2705# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002706# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002707# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002708 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002709 my $ok = 0;
2710 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2711 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002712 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002713 # with "\n" ignore it, else it is to blame
2714 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2715 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2716 $ok = 1;
2717 }
2718 last;
2719 }
2720 }
2721 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002722 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2723 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002724 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002725 }
2726
Joe Perches243f3802012-05-31 16:26:09 -07002727 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2728 my $orig = $1;
2729 my $level = lc($orig);
2730 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002731 my $level2 = $level;
2732 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002733 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002734 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002735 }
2736
2737 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07002738 if (WARN("PREFER_PR_LEVEL",
2739 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2740 $fix) {
2741 $fixed[$linenr - 1] =~
2742 s/\bpr_warning\b/pr_warn/;
2743 }
Joe Perches243f3802012-05-31 16:26:09 -07002744 }
2745
Joe Perchesdc139312013-02-21 16:44:13 -08002746 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2747 my $orig = $1;
2748 my $level = lc($orig);
2749 $level = "warn" if ($level eq "warning");
2750 $level = "dbg" if ($level eq "debug");
2751 WARN("PREFER_DEV_LEVEL",
2752 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2753 }
2754
Andy Whitcroft653d4872007-06-23 17:16:34 -07002755# function brace can't be on same line, except for #defines of do while,
2756# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002757 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2758 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002759 ERROR("OPEN_BRACE",
2760 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002761 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002762
Andy Whitcroft8905a672007-11-28 16:21:06 -08002763# open braces for enum, union and struct go on the same line.
2764 if ($line =~ /^.\s*{/ &&
2765 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002766 ERROR("OPEN_BRACE",
2767 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002768 }
2769
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002770# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002771 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2772 if (WARN("SPACING",
2773 "missing space after $1 definition\n" . $herecurr) &&
2774 $fix) {
2775 $fixed[$linenr - 1] =~
2776 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2777 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002778 }
2779
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002780# check for spacing round square brackets; allowed:
2781# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002782# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2783# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002784 while ($line =~ /(.*?\s)\[/g) {
2785 my ($where, $prefix) = ($-[1], $1);
2786 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002787 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002788 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002789 if (ERROR("BRACKET_SPACE",
2790 "space prohibited before open square bracket '['\n" . $herecurr) &&
2791 $fix) {
2792 $fixed[$linenr - 1] =~
2793 s/^(\+.*?)\s+\[/$1\[/;
2794 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002795 }
2796 }
2797
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002798# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002799 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002800 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002801 my $ctx_before = substr($line, 0, $-[1]);
2802 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002803
2804 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002805 if ($name =~ /^(?:
2806 if|for|while|switch|return|case|
2807 volatile|__volatile__|
2808 __attribute__|format|__extension__|
2809 asm|__asm__)$/x)
2810 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002811 # cpp #define statements have non-optional spaces, ie
2812 # if there is a space between the name and the open
2813 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002814 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002815
2816 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002817 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002818
2819 # If this whole things ends with a type its most
2820 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002821 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002822
2823 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002824 if (WARN("SPACING",
2825 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2826 $fix) {
2827 $fixed[$linenr - 1] =~
2828 s/\b$name\s+\(/$name\(/;
2829 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002830 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002831 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002832
Andy Whitcroft653d4872007-06-23 17:16:34 -07002833# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002834 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002835 my $fixed_line = "";
2836 my $line_fixed = 0;
2837
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002838 my $ops = qr{
2839 <<=|>>=|<=|>=|==|!=|
2840 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2841 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002842 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08002843 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002844 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002845 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002846
2847## print("element count: <" . $#elements . ">\n");
2848## foreach my $el (@elements) {
2849## print("el: <$el>\n");
2850## }
2851
2852 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07002853 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002854
Joe Perches3705ce52013-07-03 15:05:31 -07002855 foreach my $el (@elements) {
2856 push(@fix_elements, substr($rawline, $off, length($el)));
2857 $off += length($el);
2858 }
2859
2860 $off = 0;
2861
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002862 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07002863 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002864
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002865 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002866
2867 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2868
2869## print("n: <$n> good: <$good>\n");
2870
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002871 $off += length($elements[$n]);
2872
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002873 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002874 my $ca = substr($opline, 0, $off);
2875 my $cc = '';
2876 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2877 $cc = substr($opline, $off + length($elements[$n + 1]));
2878 }
2879 my $cb = "$ca$;$cc";
2880
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002881 my $a = '';
2882 $a = 'V' if ($elements[$n] ne '');
2883 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002884 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002885 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2886 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002887 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002888
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002889 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002890
2891 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002892 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002893 $c = 'V' if ($elements[$n + 2] ne '');
2894 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002895 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002896 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2897 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002898 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002899 } else {
2900 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002901 }
2902
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002903 my $ctx = "${a}x${c}";
2904
2905 my $at = "(ctx:$ctx)";
2906
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002907 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002908 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002909
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002910 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002911 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002912
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002913 # Get the full operator variant.
2914 my $opv = $op . substr($curr_vars, $off, 1);
2915
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002916 # Ignore operators passed as parameters.
2917 if ($op_type ne 'V' &&
2918 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2919
Andy Whitcroftcf655042008-03-04 14:28:20 -08002920# # Ignore comments
2921# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002922
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002923 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002924 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002925 if ($ctx !~ /.x[WEBC]/ &&
2926 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002927 if (ERROR("SPACING",
2928 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002929 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002930 $line_fixed = 1;
2931 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002932 }
2933
2934 # // is a comment
2935 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002936
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002937 # No spaces for:
2938 # ->
2939 # : when part of a bitfield
2940 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002941 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002942 if (ERROR("SPACING",
2943 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002944 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002945 if (defined $fix_elements[$n + 2]) {
2946 $fix_elements[$n + 2] =~ s/^\s+//;
2947 }
Joe Perchesb34c6482013-09-11 14:24:01 -07002948 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002949 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002950 }
2951
2952 # , must have a space on the right.
2953 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002954 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002955 if (ERROR("SPACING",
2956 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002957 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002958 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07002959 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07002960 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002961 }
2962
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002963 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002964 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002965 #warn "'*' is part of type\n";
2966
2967 # unary operators should have a space before and
2968 # none after. May be left adjacent to another
2969 # unary operator, or a cast
2970 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002971 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002972 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002973 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002974 if (ERROR("SPACING",
2975 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002976 if ($n != $last_after + 2) {
2977 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
2978 $line_fixed = 1;
2979 }
Joe Perches3705ce52013-07-03 15:05:31 -07002980 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002981 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002982 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002983 # A unary '*' may be const
2984
2985 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002986 if (ERROR("SPACING",
2987 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002988 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002989 if (defined $fix_elements[$n + 2]) {
2990 $fix_elements[$n + 2] =~ s/^\s+//;
2991 }
Joe Perchesb34c6482013-09-11 14:24:01 -07002992 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002993 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002994 }
2995
2996 # unary ++ and unary -- are allowed no space on one side.
2997 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002998 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002999 if (ERROR("SPACING",
3000 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003001 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003002 $line_fixed = 1;
3003 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003004 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003005 if ($ctx =~ /Wx[BE]/ ||
3006 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003007 if (ERROR("SPACING",
3008 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003009 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003010 $line_fixed = 1;
3011 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003012 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003013 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003014 if (ERROR("SPACING",
3015 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003016 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003017 if (defined $fix_elements[$n + 2]) {
3018 $fix_elements[$n + 2] =~ s/^\s+//;
3019 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003020 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003021 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003022 }
3023
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003024 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003025 } elsif ($op eq '<<' or $op eq '>>' or
3026 $op eq '&' or $op eq '^' or $op eq '|' or
3027 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003028 $op eq '*' or $op eq '/' or
3029 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003030 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003031 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003032 if (ERROR("SPACING",
3033 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003034 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3035 if (defined $fix_elements[$n + 2]) {
3036 $fix_elements[$n + 2] =~ s/^\s+//;
3037 }
Joe Perches3705ce52013-07-03 15:05:31 -07003038 $line_fixed = 1;
3039 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003040 }
3041
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003042 # A colon needs no spaces before when it is
3043 # terminating a case value or a label.
3044 } elsif ($opv eq ':C' || $opv eq ':L') {
3045 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003046 if (ERROR("SPACING",
3047 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003048 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003049 $line_fixed = 1;
3050 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003051 }
3052
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003053 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003054 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003055 my $ok = 0;
3056
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003057 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003058 if (($op eq '<' &&
3059 $cc =~ /^\S+\@\S+>/) ||
3060 ($op eq '>' &&
3061 $ca =~ /<\S+\@\S+$/))
3062 {
3063 $ok = 1;
3064 }
3065
Joe Perches84731622013-11-12 15:10:05 -08003066 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003067 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003068 my $msg_type = \&ERROR;
3069 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3070
3071 if (&{$msg_type}("SPACING",
3072 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003073 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3074 if (defined $fix_elements[$n + 2]) {
3075 $fix_elements[$n + 2] =~ s/^\s+//;
3076 }
Joe Perches3705ce52013-07-03 15:05:31 -07003077 $line_fixed = 1;
3078 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003079 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003080 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003081 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003082
3083## print("n: <$n> GOOD: <$good>\n");
3084
3085 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003086 }
Joe Perches3705ce52013-07-03 15:05:31 -07003087
3088 if (($#elements % 2) == 0) {
3089 $fixed_line = $fixed_line . $fix_elements[$#elements];
3090 }
3091
3092 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3093 $fixed[$linenr - 1] = $fixed_line;
3094 }
3095
3096
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003097 }
3098
Joe Perches786b6322013-07-03 15:05:32 -07003099# check for whitespace before a non-naked semicolon
3100 if ($line =~ /^\+.*\S\s+;/) {
3101 if (WARN("SPACING",
3102 "space prohibited before semicolon\n" . $herecurr) &&
3103 $fix) {
3104 1 while $fixed[$linenr - 1] =~
3105 s/^(\+.*\S)\s+;/$1;/;
3106 }
3107 }
3108
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003109# check for multiple assignments
3110 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003111 CHK("MULTIPLE_ASSIGNMENTS",
3112 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003113 }
3114
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003115## # check for multiple declarations, allowing for a function declaration
3116## # continuation.
3117## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3118## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3119##
3120## # Remove any bracketed sections to ensure we do not
3121## # falsly report the parameters of functions.
3122## my $ln = $line;
3123## while ($ln =~ s/\([^\(\)]*\)//g) {
3124## }
3125## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003126## WARN("MULTIPLE_DECLARATION",
3127## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003128## }
3129## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003130
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003131#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003132 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3133 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003134 if (ERROR("SPACING",
3135 "space required before the open brace '{'\n" . $herecurr) &&
3136 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003137 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003138 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003139 }
3140
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003141## # check for blank lines before declarations
3142## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3143## $prevrawline =~ /^.\s*$/) {
3144## WARN("SPACING",
3145## "No blank lines before declarations\n" . $hereprev);
3146## }
3147##
3148
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003149# closing brace should have a space following it when it has anything
3150# on the line
3151 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003152 if (ERROR("SPACING",
3153 "space required after that close brace '}'\n" . $herecurr) &&
3154 $fix) {
3155 $fixed[$linenr - 1] =~
3156 s/}((?!(?:,|;|\)))\S)/} $1/;
3157 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003158 }
3159
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003160# check spacing on square brackets
3161 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003162 if (ERROR("SPACING",
3163 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3164 $fix) {
3165 $fixed[$linenr - 1] =~
3166 s/\[\s+/\[/;
3167 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003168 }
3169 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003170 if (ERROR("SPACING",
3171 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3172 $fix) {
3173 $fixed[$linenr - 1] =~
3174 s/\s+\]/\]/;
3175 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003176 }
3177
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003178# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003179 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3180 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003181 if (ERROR("SPACING",
3182 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3183 $fix) {
3184 $fixed[$linenr - 1] =~
3185 s/\(\s+/\(/;
3186 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003187 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003188 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003189 $line !~ /for\s*\(.*;\s+\)/ &&
3190 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003191 if (ERROR("SPACING",
3192 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3193 $fix) {
3194 $fixed[$linenr - 1] =~
3195 s/\s+\)/\)/;
3196 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003197 }
3198
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003199#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003200 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003201 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003202 if (WARN("INDENTED_LABEL",
3203 "labels should not be indented\n" . $herecurr) &&
3204 $fix) {
3205 $fixed[$linenr - 1] =~
3206 s/^(.)\s+/$1/;
3207 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003208 }
3209
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003210# Return is not a function.
3211 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3212 my $spacing = $1;
3213 my $value = $2;
3214
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003215 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003216 $value =~ s/\(/ \(/g;
3217 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003218 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003219 $value !~ /(?:$Ident|-?$Constant)\s*
3220 $Compare\s*
3221 (?:$Ident|-?$Constant)/x &&
3222 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003223 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003224#print "value<$value>\n";
3225 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003226 ERROR("RETURN_PARENTHESES",
3227 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003228
3229 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003230 ERROR("SPACING",
3231 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003232 }
3233 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003234# Return of what appears to be an errno should normally be -'ve
3235 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3236 my $name = $1;
3237 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003238 WARN("USE_NEGATIVE_ERRNO",
3239 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003240 }
3241 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003242
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003243# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003244 if ($line =~ /\b(if|while|for|switch)\(/) {
3245 if (ERROR("SPACING",
3246 "space required before the open parenthesis '('\n" . $herecurr) &&
3247 $fix) {
3248 $fixed[$linenr - 1] =~
3249 s/\b(if|while|for|switch)\(/$1 \(/;
3250 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003251 }
3252
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003253# Check for illegal assignment in if conditional -- and check for trailing
3254# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003255 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003256 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3257 ctx_statement_block($linenr, $realcnt, 0)
3258 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003259 my ($stat_next) = ctx_statement_block($line_nr_next,
3260 $remain_next, $off_next);
3261 $stat_next =~ s/\n./\n /g;
3262 ##print "stat<$stat> stat_next<$stat_next>\n";
3263
3264 if ($stat_next =~ /^\s*while\b/) {
3265 # If the statement carries leading newlines,
3266 # then count those as offsets.
3267 my ($whitespace) =
3268 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3269 my $offset =
3270 statement_rawlines($whitespace) - 1;
3271
3272 $suppress_whiletrailers{$line_nr_next +
3273 $offset} = 1;
3274 }
3275 }
3276 if (!defined $suppress_whiletrailers{$linenr} &&
3277 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003278 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003279
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003280 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003281 ERROR("ASSIGN_IN_IF",
3282 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003283 }
3284
3285 # Find out what is on the end of the line after the
3286 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003287 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003288 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003289 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003290 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3291 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003292 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003293 # Find out how long the conditional actually is.
3294 my @newlines = ($c =~ /\n/gs);
3295 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003296 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003297
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003298 $stat_real = raw_line($linenr, $cond_lines)
3299 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003300 if (defined($stat_real) && $cond_lines > 1) {
3301 $stat_real = "[...]\n$stat_real";
3302 }
3303
Joe Perches000d1cc12011-07-25 17:13:25 -07003304 ERROR("TRAILING_STATEMENTS",
3305 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003306 }
3307 }
3308
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003309# Check for bitwise tests written as boolean
3310 if ($line =~ /
3311 (?:
3312 (?:\[|\(|\&\&|\|\|)
3313 \s*0[xX][0-9]+\s*
3314 (?:\&\&|\|\|)
3315 |
3316 (?:\&\&|\|\|)
3317 \s*0[xX][0-9]+\s*
3318 (?:\&\&|\|\||\)|\])
3319 )/x)
3320 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003321 WARN("HEXADECIMAL_BOOLEAN_TEST",
3322 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003323 }
3324
Andy Whitcroft8905a672007-11-28 16:21:06 -08003325# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003326 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3327 my $s = $1;
3328 $s =~ s/$;//g; # Remove any comments
3329 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003330 ERROR("TRAILING_STATEMENTS",
3331 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003332 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003333 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003334# if should not continue a brace
3335 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003336 ERROR("TRAILING_STATEMENTS",
3337 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003338 $herecurr);
3339 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003340# case and default should not have general statements after them
3341 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3342 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003343 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003344 \s*return\s+
3345 )/xg)
3346 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003347 ERROR("TRAILING_STATEMENTS",
3348 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003349 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003350
3351 # Check for }<nl>else {, these must be at the same
3352 # indent level to be relevant to each other.
3353 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3354 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003355 ERROR("ELSE_AFTER_BRACE",
3356 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003357 }
3358
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003359 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3360 $previndent == $indent) {
3361 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3362
3363 # Find out what is on the end of the line after the
3364 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003365 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003366 $s =~ s/\n.*//g;
3367
3368 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003369 ERROR("WHILE_AFTER_BRACE",
3370 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003371 }
3372 }
3373
Joe Perches95e2c602013-07-03 15:05:20 -07003374#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003375 while ($line =~ m{($Constant|$Lval)}g) {
3376 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003377
3378#gcc binary extension
3379 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003380 if (WARN("GCC_BINARY_CONSTANT",
3381 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3382 $fix) {
3383 my $hexval = sprintf("0x%x", oct($var));
3384 $fixed[$linenr - 1] =~
3385 s/\b$var\b/$hexval/;
3386 }
Joe Perches95e2c602013-07-03 15:05:20 -07003387 }
3388
3389#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003390 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003391 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003392#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003393 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003394#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003395 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003396 while ($var =~ m{($Ident)}g) {
3397 my $word = $1;
3398 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08003399 if ($check) {
3400 seed_camelcase_includes();
3401 if (!$file && !$camelcase_file_seeded) {
3402 seed_camelcase_file($realfile);
3403 $camelcase_file_seeded = 1;
3404 }
3405 }
Joe Perches7e781f62013-09-11 14:23:55 -07003406 if (!defined $camelcase{$word}) {
3407 $camelcase{$word} = 1;
3408 CHK("CAMELCASE",
3409 "Avoid CamelCase: <$word>\n" . $herecurr);
3410 }
Joe Perches34456862013-07-03 15:05:34 -07003411 }
Joe Perches323c1262012-12-17 16:02:07 -08003412 }
3413 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003414
3415#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003416 if ($line =~ /\#\s*define.*\\\s+$/) {
3417 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3418 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3419 $fix) {
3420 $fixed[$linenr - 1] =~ s/\s+$//;
3421 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003422 }
3423
Andy Whitcroft653d4872007-06-23 17:16:34 -07003424#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003425 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003426 my $file = "$1.h";
3427 my $checkfile = "include/linux/$file";
3428 if (-f "$root/$checkfile" &&
3429 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003430 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003431 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003432 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003433 CHK("ARCH_INCLUDE_LINUX",
3434 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003435 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003436 WARN("INCLUDE_LINUX",
3437 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003438 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003439 }
3440 }
3441
Andy Whitcroft653d4872007-06-23 17:16:34 -07003442# multi-statement macros should be enclosed in a do while loop, grab the
3443# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003444# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003445 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3446 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003447 my $ln = $linenr;
3448 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003449 my ($off, $dstat, $dcond, $rest);
3450 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003451 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003452 ctx_statement_block($linenr, $realcnt, 0);
3453 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003454 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003455 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003456
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003457 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003458 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003459 $dstat =~ s/\\\n.//g;
3460 $dstat =~ s/^\s*//s;
3461 $dstat =~ s/\s*$//s;
3462
3463 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003464 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3465 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003466 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003467 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003468 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003469
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003470 # Flatten any obvious string concatentation.
3471 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3472 $dstat =~ s/$Ident\s*("X*")/$1/)
3473 {
3474 }
3475
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003476 my $exceptions = qr{
3477 $Declare|
3478 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003479 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003480 DECLARE_PER_CPU|
3481 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003482 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003483 union|
3484 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003485 \.$Ident\s*=\s*|
3486 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003487 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003488 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003489 if ($dstat ne '' &&
3490 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3491 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003492 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003493 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003494 $dstat !~ /$exceptions/ &&
3495 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003496 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003497 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003498 $dstat !~ /^for\s*$Constant$/ && # for (...)
3499 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3500 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07003501 $dstat !~ /^\({/ && # ({...
3502 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003503 {
3504 $ctx =~ s/\n*$//;
3505 my $herectx = $here . "\n";
3506 my $cnt = statement_rawlines($ctx);
3507
3508 for (my $n = 0; $n < $cnt; $n++) {
3509 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003510 }
3511
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003512 if ($dstat =~ /;/) {
3513 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3514 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3515 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003516 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003517 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003518 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003519 }
Joe Perches5023d342012-12-17 16:01:47 -08003520
Joe Perches481eb482012-12-17 16:01:56 -08003521# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003522
3523 } else {
3524 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003525 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3526 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003527 $line =~ /^\+.*\\$/) {
3528 WARN("LINE_CONTINUATIONS",
3529 "Avoid unnecessary line continuations\n" . $herecurr);
3530 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003531 }
3532
Joe Perchesb13edf72012-07-30 14:41:24 -07003533# do {} while (0) macro tests:
3534# single-statement macros do not need to be enclosed in do while (0) loop,
3535# macro should not end with a semicolon
3536 if ($^V && $^V ge 5.10.0 &&
3537 $realfile !~ m@/vmlinux.lds.h$@ &&
3538 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3539 my $ln = $linenr;
3540 my $cnt = $realcnt;
3541 my ($off, $dstat, $dcond, $rest);
3542 my $ctx = '';
3543 ($dstat, $dcond, $ln, $cnt, $off) =
3544 ctx_statement_block($linenr, $realcnt, 0);
3545 $ctx = $dstat;
3546
3547 $dstat =~ s/\\\n.//g;
3548
3549 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3550 my $stmts = $2;
3551 my $semis = $3;
3552
3553 $ctx =~ s/\n*$//;
3554 my $cnt = statement_rawlines($ctx);
3555 my $herectx = $here . "\n";
3556
3557 for (my $n = 0; $n < $cnt; $n++) {
3558 $herectx .= raw_line($linenr, $n) . "\n";
3559 }
3560
Joe Perchesac8e97f2012-08-21 16:15:53 -07003561 if (($stmts =~ tr/;/;/) == 1 &&
3562 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003563 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3564 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3565 }
3566 if (defined $semis && $semis ne "") {
3567 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3568 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3569 }
3570 }
3571 }
3572
Mike Frysinger080ba922009-01-06 14:41:25 -08003573# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3574# all assignments may have only one of the following with an assignment:
3575# .
3576# ALIGN(...)
3577# VMLINUX_SYMBOL(...)
3578 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003579 WARN("MISSING_VMLINUX_SYMBOL",
3580 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003581 }
3582
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003583# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003584 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3585 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003586 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003587 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003588 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3589 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003590 my @allowed = ();
3591 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003592 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003593 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003594 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003595 for my $chunk (@chunks) {
3596 my ($cond, $block) = @{$chunk};
3597
Andy Whitcroft773647a2008-03-28 14:15:58 -07003598 # If the condition carries leading newlines, then count those as offsets.
3599 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3600 my $offset = statement_rawlines($whitespace) - 1;
3601
Joe Perchesaad4f612012-03-23 15:02:19 -07003602 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003603 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3604
3605 # We have looked at and allowed this specific line.
3606 $suppress_ifbraces{$ln + $offset} = 1;
3607
3608 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003609 $ln += statement_rawlines($block) - 1;
3610
Andy Whitcroft773647a2008-03-28 14:15:58 -07003611 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003612
3613 $seen++ if ($block =~ /^\s*{/);
3614
Joe Perchesaad4f612012-03-23 15:02:19 -07003615 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003616 if (statement_lines($cond) > 1) {
3617 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003618 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003619 }
3620 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003621 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003622 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003623 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003624 if (statement_block_size($block) > 1) {
3625 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003626 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003627 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003628 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003629 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003630 if ($seen) {
3631 my $sum_allowed = 0;
3632 foreach (@allowed) {
3633 $sum_allowed += $_;
3634 }
3635 if ($sum_allowed == 0) {
3636 WARN("BRACES",
3637 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3638 } elsif ($sum_allowed != $allow &&
3639 $seen != $allow) {
3640 CHK("BRACES",
3641 "braces {} should be used on all arms of this statement\n" . $herectx);
3642 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003643 }
3644 }
3645 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003646 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003647 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003648 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003649
Andy Whitcroftcf655042008-03-04 14:28:20 -08003650 # Check the pre-context.
3651 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3652 #print "APW: ALLOWED: pre<$1>\n";
3653 $allowed = 1;
3654 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003655
3656 my ($level, $endln, @chunks) =
3657 ctx_statement_full($linenr, $realcnt, $-[0]);
3658
Andy Whitcroftcf655042008-03-04 14:28:20 -08003659 # Check the condition.
3660 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003661 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003662 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003663 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003664 }
3665 if (statement_lines($cond) > 1) {
3666 #print "APW: ALLOWED: cond<$cond>\n";
3667 $allowed = 1;
3668 }
3669 if ($block =~/\b(?:if|for|while)\b/) {
3670 #print "APW: ALLOWED: block<$block>\n";
3671 $allowed = 1;
3672 }
3673 if (statement_block_size($block) > 1) {
3674 #print "APW: ALLOWED: lines block<$block>\n";
3675 $allowed = 1;
3676 }
3677 # Check the post-context.
3678 if (defined $chunks[1]) {
3679 my ($cond, $block) = @{$chunks[1]};
3680 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003681 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003682 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003683 if ($block =~ /^\s*\{/) {
3684 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3685 $allowed = 1;
3686 }
3687 }
3688 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003689 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003690 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003691
Andy Whitcroftf0556632008-10-15 22:02:23 -07003692 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003693 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003694 }
3695
Joe Perches000d1cc12011-07-25 17:13:25 -07003696 WARN("BRACES",
3697 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003698 }
3699 }
3700
Joe Perches0979ae62012-12-17 16:01:59 -08003701# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003702 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003703 CHK("BRACES",
3704 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3705 }
Joe Perches77b9a532013-07-03 15:05:29 -07003706 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003707 CHK("BRACES",
3708 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3709 }
3710
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003711# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003712 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3713 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003714 WARN("VOLATILE",
3715 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003716 }
3717
Andy Whitcroft00df3442007-06-08 13:47:06 -07003718# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003719 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003720 CHK("REDUNDANT_CODE",
3721 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003722 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003723 }
3724
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003725# check for needless "if (<foo>) fn(<foo>)" uses
3726 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3727 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3728 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3729 WARN('NEEDLESS_IF',
3730 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003731 }
3732 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003733
Joe Perches8716de32013-09-11 14:24:05 -07003734sub string_find_replace {
3735 my ($string, $find, $replace) = @_;
3736
3737 $string =~ s/$find/$replace/g;
3738
3739 return $string;
3740}
3741
3742# check for bad placement of section $InitAttribute (e.g.: __initdata)
3743 if ($line =~ /(\b$InitAttribute\b)/) {
3744 my $attr = $1;
3745 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3746 my $ptr = $1;
3747 my $var = $2;
3748 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3749 ERROR("MISPLACED_INIT",
3750 "$attr should be placed after $var\n" . $herecurr)) ||
3751 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3752 WARN("MISPLACED_INIT",
3753 "$attr should be placed after $var\n" . $herecurr))) &&
3754 $fix) {
3755 $fixed[$linenr - 1] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
3756 }
3757 }
3758 }
3759
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003760# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003761 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003762 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003763 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003764 CHK("USLEEP_RANGE",
3765 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003766 }
3767 }
3768
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003769# warn about unexpectedly long msleep's
3770 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3771 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003772 WARN("MSLEEP",
3773 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003774 }
3775 }
3776
Joe Perches36ec1932013-07-03 15:05:25 -07003777# check for comparisons of jiffies
3778 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3779 WARN("JIFFIES_COMPARISON",
3780 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3781 }
3782
Joe Perches9d7a34a2013-07-03 15:05:26 -07003783# check for comparisons of get_jiffies_64()
3784 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3785 WARN("JIFFIES_COMPARISON",
3786 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3787 }
3788
Andy Whitcroft00df3442007-06-08 13:47:06 -07003789# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003790# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003791# print "#ifdef in C files should be avoided\n";
3792# print "$herecurr";
3793# $clean = 0;
3794# }
3795
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003796# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003797 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003798 if (ERROR("SPACING",
3799 "exactly one space required after that #$1\n" . $herecurr) &&
3800 $fix) {
3801 $fixed[$linenr - 1] =~
3802 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3803 }
3804
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003805 }
3806
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003807# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003808 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3809 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003810 my $which = $1;
3811 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003812 CHK("UNCOMMENTED_DEFINITION",
3813 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003814 }
3815 }
3816# check for memory barriers without a comment.
3817 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3818 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003819 CHK("MEMORY_BARRIER",
3820 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003821 }
3822 }
3823# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003824 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003825 CHK("ARCH_DEFINES",
3826 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003827 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003828
Tobias Klauserd4977c72010-05-24 14:33:30 -07003829# Check that the storage class is at the beginning of a declaration
3830 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003831 WARN("STORAGE_CLASS",
3832 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003833 }
3834
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003835# check the location of the inline attribute, that it is between
3836# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003837 if ($line =~ /\b$Type\s+$Inline\b/ ||
3838 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003839 ERROR("INLINE_LOCATION",
3840 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003841 }
3842
Andy Whitcroft8905a672007-11-28 16:21:06 -08003843# Check for __inline__ and __inline, prefer inline
3844 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003845 if (WARN("INLINE",
3846 "plain inline is preferred over $1\n" . $herecurr) &&
3847 $fix) {
3848 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3849
3850 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003851 }
3852
Joe Perches3d130fd2011-01-12 17:00:00 -08003853# Check for __attribute__ packed, prefer __packed
3854 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003855 WARN("PREFER_PACKED",
3856 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003857 }
3858
Joe Perches39b7e282011-07-25 17:13:24 -07003859# Check for __attribute__ aligned, prefer __aligned
3860 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003861 WARN("PREFER_ALIGNED",
3862 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003863 }
3864
Joe Perches5f14d3b2012-01-10 15:09:52 -08003865# Check for __attribute__ format(printf, prefer __printf
3866 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003867 if (WARN("PREFER_PRINTF",
3868 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3869 $fix) {
3870 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3871
3872 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08003873 }
3874
Joe Perches6061d942012-03-23 15:02:16 -07003875# Check for __attribute__ format(scanf, prefer __scanf
3876 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003877 if (WARN("PREFER_SCANF",
3878 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3879 $fix) {
3880 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3881 }
Joe Perches6061d942012-03-23 15:02:16 -07003882 }
3883
Joe Perches8f53a9b2010-03-05 13:43:48 -08003884# check for sizeof(&)
3885 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003886 WARN("SIZEOF_ADDRESS",
3887 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003888 }
3889
Joe Perches66c80b62012-07-30 14:41:22 -07003890# check for sizeof without parenthesis
3891 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003892 if (WARN("SIZEOF_PARENTHESIS",
3893 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3894 $fix) {
3895 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3896 }
Joe Perches66c80b62012-07-30 14:41:22 -07003897 }
3898
Joe Perches428e2fd2011-05-24 17:13:39 -07003899# check for line continuations in quoted strings with odd counts of "
3900 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003901 WARN("LINE_CONTINUATIONS",
3902 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003903 }
3904
Joe Perches88982fe2012-12-17 16:02:00 -08003905# check for struct spinlock declarations
3906 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3907 WARN("USE_SPINLOCK_T",
3908 "struct spinlock should be spinlock_t\n" . $herecurr);
3909 }
3910
Joe Perchesa6962d72013-04-29 16:18:13 -07003911# check for seq_printf uses that could be seq_puts
3912 if ($line =~ /\bseq_printf\s*\(/) {
3913 my $fmt = get_quoted_string($line, $rawline);
3914 if ($fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003915 if (WARN("PREFER_SEQ_PUTS",
3916 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3917 $fix) {
3918 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3919 }
Joe Perchesa6962d72013-04-29 16:18:13 -07003920 }
3921 }
3922
Andy Whitcroft554e1652012-01-10 15:09:57 -08003923# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003924 if ($^V && $^V ge 5.10.0 &&
3925 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003926 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003927
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003928 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003929 my $ms_val = $7;
3930 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003931
Andy Whitcroft554e1652012-01-10 15:09:57 -08003932 if ($ms_size =~ /^(0x|)0$/i) {
3933 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003934 "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 -08003935 } elsif ($ms_size =~ /^(0x|)1$/i) {
3936 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003937 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3938 }
3939 }
3940
3941# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003942 if ($^V && $^V ge 5.10.0 &&
3943 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003944 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003945 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003946 my $call = $1;
3947 my $cast1 = deparenthesize($2);
3948 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003949 my $cast2 = deparenthesize($7);
3950 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003951 my $cast;
3952
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003953 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003954 $cast = "$cast1 or $cast2";
3955 } elsif ($cast1 ne "") {
3956 $cast = $cast1;
3957 } else {
3958 $cast = $cast2;
3959 }
3960 WARN("MINMAX",
3961 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003962 }
3963 }
3964
Joe Perches4a273192012-07-30 14:41:20 -07003965# check usleep_range arguments
3966 if ($^V && $^V ge 5.10.0 &&
3967 defined $stat &&
3968 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3969 my $min = $1;
3970 my $max = $7;
3971 if ($min eq $max) {
3972 WARN("USLEEP_RANGE",
3973 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3974 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3975 $min > $max) {
3976 WARN("USLEEP_RANGE",
3977 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3978 }
3979 }
3980
Joe Perches70dc8a42013-09-11 14:23:58 -07003981# check for new externs in .h files.
3982 if ($realfile =~ /\.h$/ &&
3983 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07003984 if (CHK("AVOID_EXTERNS",
3985 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07003986 $fix) {
3987 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3988 }
3989 }
3990
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003991# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003992 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003993 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003994 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003995 my $function_name = $1;
3996 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003997
3998 my $s = $stat;
3999 if (defined $cond) {
4000 substr($s, 0, length($cond), '');
4001 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004002 if ($s =~ /^\s*;/ &&
4003 $function_name ne 'uninitialized_var')
4004 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004005 WARN("AVOID_EXTERNS",
4006 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004007 }
4008
4009 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004010 WARN("FUNCTION_ARGUMENTS",
4011 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004012 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004013
4014 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4015 $stat =~ /^.\s*extern\s+/)
4016 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004017 WARN("AVOID_EXTERNS",
4018 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004019 }
4020
4021# checks for new __setup's
4022 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4023 my $name = $1;
4024
4025 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004026 CHK("UNDOCUMENTED_SETUP",
4027 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004028 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004029 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004030
4031# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004032 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004033 WARN("UNNECESSARY_CASTS",
4034 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004035 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004036
Joe Perchesa640d252013-07-03 15:05:21 -07004037# alloc style
4038# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4039 if ($^V && $^V ge 5.10.0 &&
4040 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4041 CHK("ALLOC_SIZEOF_STRUCT",
4042 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4043 }
4044
Joe Perches972fdea2013-04-29 16:18:12 -07004045# check for krealloc arg reuse
4046 if ($^V && $^V ge 5.10.0 &&
4047 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4048 WARN("KREALLOC_ARG_REUSE",
4049 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4050 }
4051
Joe Perches5ce59ae2013-02-21 16:44:18 -08004052# check for alloc argument mismatch
4053 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4054 WARN("ALLOC_ARRAY_ARGS",
4055 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4056 }
4057
Joe Perchescaf2a542011-01-12 16:59:56 -08004058# check for multiple semicolons
4059 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004060 if (WARN("ONE_SEMICOLON",
4061 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4062 $fix) {
4063 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4064 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004065 }
4066
4067# check for switch/default statements without a break;
4068 if ($^V && $^V ge 5.10.0 &&
4069 defined $stat &&
4070 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4071 my $ctx = '';
4072 my $herectx = $here . "\n";
4073 my $cnt = statement_rawlines($stat);
4074 for (my $n = 0; $n < $cnt; $n++) {
4075 $herectx .= raw_line($linenr, $n) . "\n";
4076 }
4077 WARN("DEFAULT_NO_BREAK",
4078 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004079 }
4080
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004081# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004082 if ($line =~ /\b__FUNCTION__\b/) {
4083 if (WARN("USE_FUNC",
4084 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4085 $fix) {
4086 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4087 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004088 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004089
Joe Perches2c924882012-03-23 15:02:20 -07004090# check for use of yield()
4091 if ($line =~ /\byield\s*\(\s*\)/) {
4092 WARN("YIELD",
4093 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4094 }
4095
Joe Perches179f8f42013-07-03 15:05:30 -07004096# check for comparisons against true and false
4097 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4098 my $lead = $1;
4099 my $arg = $2;
4100 my $test = $3;
4101 my $otype = $4;
4102 my $trail = $5;
4103 my $op = "!";
4104
4105 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4106
4107 my $type = lc($otype);
4108 if ($type =~ /^(?:true|false)$/) {
4109 if (("$test" eq "==" && "$type" eq "true") ||
4110 ("$test" eq "!=" && "$type" eq "false")) {
4111 $op = "";
4112 }
4113
4114 CHK("BOOL_COMPARISON",
4115 "Using comparison to $otype is error prone\n" . $herecurr);
4116
4117## maybe suggesting a correct construct would better
4118## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4119
4120 }
4121 }
4122
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004123# check for semaphores initialized locked
4124 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004125 WARN("CONSIDER_COMPLETION",
4126 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004127 }
Joe Perches6712d852012-03-23 15:02:20 -07004128
Joe Perches67d0a072011-10-31 17:13:10 -07004129# recommend kstrto* over simple_strto* and strict_strto*
4130 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004131 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004132 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004133 }
Joe Perches6712d852012-03-23 15:02:20 -07004134
Michael Ellermanf3db6632008-07-23 21:28:57 -07004135# check for __initcall(), use device_initcall() explicitly please
4136 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004137 WARN("USE_DEVICE_INITCALL",
4138 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07004139 }
Joe Perches6712d852012-03-23 15:02:20 -07004140
Emese Revfy79404842010-03-05 13:43:53 -08004141# check for various ops structs, ensure they are const.
4142 my $struct_ops = qr{acpi_dock_ops|
4143 address_space_operations|
4144 backlight_ops|
4145 block_device_operations|
4146 dentry_operations|
4147 dev_pm_ops|
4148 dma_map_ops|
4149 extent_io_ops|
4150 file_lock_operations|
4151 file_operations|
4152 hv_ops|
4153 ide_dma_ops|
4154 intel_dvo_dev_ops|
4155 item_operations|
4156 iwl_ops|
4157 kgdb_arch|
4158 kgdb_io|
4159 kset_uevent_ops|
4160 lock_manager_operations|
4161 microcode_ops|
4162 mtrr_ops|
4163 neigh_ops|
4164 nlmsvc_binding|
4165 pci_raw_ops|
4166 pipe_buf_operations|
4167 platform_hibernation_ops|
4168 platform_suspend_ops|
4169 proto_ops|
4170 rpc_pipe_ops|
4171 seq_operations|
4172 snd_ac97_build_ops|
4173 soc_pcmcia_socket_ops|
4174 stacktrace_ops|
4175 sysfs_ops|
4176 tty_operations|
4177 usb_mon_operations|
4178 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004179 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004180 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004181 WARN("CONST_STRUCT",
4182 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004183 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004184 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004185
4186# use of NR_CPUS is usually wrong
4187# ignore definitions of NR_CPUS and usage to define arrays as likely right
4188 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004189 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4190 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004191 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4192 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4193 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004194 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004195 WARN("NR_CPUS",
4196 "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 -07004197 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004198
4199# check for %L{u,d,i} in strings
4200 my $string;
4201 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4202 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004203 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004204 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004205 WARN("PRINTF_L",
4206 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004207 last;
4208 }
4209 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004210
4211# whine mightly about in_atomic
4212 if ($line =~ /\bin_atomic\s*\(/) {
4213 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004214 ERROR("IN_ATOMIC",
4215 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004216 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004217 WARN("IN_ATOMIC",
4218 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004219 }
4220 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004221
4222# check for lockdep_set_novalidate_class
4223 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4224 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4225 if ($realfile !~ m@^kernel/lockdep@ &&
4226 $realfile !~ m@^include/linux/lockdep@ &&
4227 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004228 ERROR("LOCKDEP",
4229 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004230 }
4231 }
Dave Jones88f88312011-01-12 16:59:59 -08004232
4233 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4234 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004235 WARN("EXPORTED_WORLD_WRITABLE",
4236 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004237 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004238 }
4239
4240 # If we have no input at all, then there is nothing to report on
4241 # so just keep quiet.
4242 if ($#rawlines == -1) {
4243 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004244 }
4245
Andy Whitcroft8905a672007-11-28 16:21:06 -08004246 # In mailback mode only produce a report in the negative, for
4247 # things that appear to be patches.
4248 if ($mailback && ($clean == 1 || !$is_patch)) {
4249 exit(0);
4250 }
4251
4252 # This is not a patch, and we are are in 'no-patch' mode so
4253 # just keep quiet.
4254 if (!$chk_patch && !$is_patch) {
4255 exit(0);
4256 }
4257
4258 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004259 ERROR("NOT_UNIFIED_DIFF",
4260 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004261 }
4262 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004263 ERROR("MISSING_SIGN_OFF",
4264 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004265 }
4266
Andy Whitcroft8905a672007-11-28 16:21:06 -08004267 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004268 if ($summary && !($clean == 1 && $quiet == 1)) {
4269 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004270 print "total: $cnt_error errors, $cnt_warn warnings, " .
4271 (($check)? "$cnt_chk checks, " : "") .
4272 "$cnt_lines lines checked\n";
4273 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004274 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004275
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004276 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004277
4278 if ($^V lt 5.10.0) {
4279 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4280 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4281 }
4282
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004283 # If there were whitespace errors which cleanpatch can fix
4284 # then suggest that.
4285 if ($rpt_cleaners) {
4286 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4287 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004288 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004289 }
4290 }
4291
Joe Perches91bfe482013-09-11 14:23:59 -07004292 hash_show_words(\%use_type, "Used");
4293 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07004294
Joe Perches3705ce52013-07-03 15:05:31 -07004295 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4296 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4297 my $linecount = 0;
4298 my $f;
4299
4300 open($f, '>', $newfile)
4301 or die "$P: Can't open $newfile for write\n";
4302 foreach my $fixed_line (@fixed) {
4303 $linecount++;
4304 if ($file) {
4305 if ($linecount > 3) {
4306 $fixed_line =~ s/^\+//;
4307 print $f $fixed_line. "\n";
4308 }
4309 } else {
4310 print $f $fixed_line . "\n";
4311 }
4312 }
4313 close($f);
4314
4315 if (!$quiet) {
4316 print << "EOM";
4317Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4318
4319Do _NOT_ trust the results written to this file.
4320Do _NOT_ submit these changes without inspecting them for correctness.
4321
4322This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4323No warranties, expressed or implied...
4324
4325EOM
4326 }
4327 }
4328
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004329 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004330 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004331 }
4332 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004333 print << "EOM";
4334$vname has style problems, please review.
4335
4336If any of these errors are false positives, please report
4337them to the maintainer, see CHECKPATCH in MAINTAINERS.
4338EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004339 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004340
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004341 return $clean;
4342}