blob: db7778a8f4148b93879cd5b0596e3764d030330b [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070010
11my $P = $0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -070012$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070013
Joe Perches000d1cc12011-07-25 17:13:25 -070014my $V = '0.32';
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -0700115GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700116 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -0700152if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700153 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-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
177 if ($quiet == 0 && keys $hashRef) {
178 print "NOTE: $prefix message types:";
179 foreach my $word (sort keys $hashRef) {
180 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 Whitcroft0a920b5b2007-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
245# Notes to $Attribute:
246# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700247our $Attribute = qr{
248 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700249 __percpu|
250 __nocast|
251 __safe|
252 __bitwise__|
253 __packed__|
254 __packed2__|
255 __naked|
256 __maybe_unused|
257 __always_unused|
258 __noreturn|
259 __used|
260 __cold|
261 __noclone|
262 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700263 __read_mostly|
264 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800265 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700266 ____cacheline_aligned|
267 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800268 ____cacheline_internodealigned_in_smp|
269 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700270 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700271our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700272our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700273our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
274our $Lval = qr{$Ident(?:$Member)*};
275
Joe Perches95e2c602013-07-03 15:05:20 -0700276our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
277our $Binary = qr{(?i)0b[01]+$Int_type?};
278our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
279our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800280our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
281our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
282our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800283our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700284our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800285our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800286our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700287our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700288our $Operators = qr{
289 <=|>=|==|!=|
290 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700291 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292 }x;
293
Andy Whitcroft8905a672007-11-28 16:21:06 -0800294our $NonptrType;
295our $Type;
296our $Declare;
297
Joe Perches15662b32011-10-31 17:13:12 -0700298our $NON_ASCII_UTF8 = qr{
299 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700300 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
301 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
302 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
303 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
304 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
305 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
306}x;
307
Joe Perches15662b32011-10-31 17:13:12 -0700308our $UTF8 = qr{
309 [\x09\x0A\x0D\x20-\x7E] # ASCII
310 | $NON_ASCII_UTF8
311}x;
312
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700313our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700314 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700315 atomic_t
316)};
317
Joe Perches691e6692010-03-05 13:43:51 -0800318our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700319 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700320 (?:[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 -0700321 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700322 panic|
323 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800324)};
325
Joe Perches20112472011-07-25 17:13:23 -0700326our $signature_tags = qr{(?xi:
327 Signed-off-by:|
328 Acked-by:|
329 Tested-by:|
330 Reviewed-by:|
331 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700332 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700333 To:|
334 Cc:
335)};
336
Andy Whitcroft8905a672007-11-28 16:21:06 -0800337our @typeList = (
338 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700339 qr{(?:unsigned\s+)?char},
340 qr{(?:unsigned\s+)?short},
341 qr{(?:unsigned\s+)?int},
342 qr{(?:unsigned\s+)?long},
343 qr{(?:unsigned\s+)?long\s+int},
344 qr{(?:unsigned\s+)?long\s+long},
345 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800346 qr{unsigned},
347 qr{float},
348 qr{double},
349 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800350 qr{struct\s+$Ident},
351 qr{union\s+$Ident},
352 qr{enum\s+$Ident},
353 qr{${Ident}_t},
354 qr{${Ident}_handler},
355 qr{${Ident}_handler_fn},
356);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700357our @modifierList = (
358 qr{fastcall},
359);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800360
Wolfram Sang7840a942010-08-09 17:20:57 -0700361our $allowed_asm_includes = qr{(?x:
362 irq|
363 memory
364)};
365# memory.h: ARM has a custom one
366
Andy Whitcroft8905a672007-11-28 16:21:06 -0800367sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700368 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
369 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700370 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800371 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700372 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800373 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800374 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700375 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700376 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800377 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700378 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800379 }x;
380 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700381 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700382 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700383 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800384 }x;
385 $Declare = qr{(?:$Storage\s+)?$Type};
386}
387build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700388
Joe Perches7d2367a2011-07-25 17:13:22 -0700389our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700390
391# Using $balanced_parens, $LvalOrFunc, or $FuncArg
392# requires at least perl version v5.10.0
393# Any use must be runtime checked with $^V
394
395our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
396our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800397our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700398
399sub deparenthesize {
400 my ($string) = @_;
401 return "" if (!defined($string));
402 $string =~ s@^\s*\(\s*@@g;
403 $string =~ s@\s*\)\s*$@@g;
404 $string =~ s@\s+@ @g;
405 return $string;
406}
407
Joe Perches34456862013-07-03 15:05:34 -0700408sub seed_camelcase_file {
409 my ($file) = @_;
410
411 return if (!(-f $file));
412
413 local $/;
414
415 open(my $include_file, '<', "$file")
416 or warn "$P: Can't read '$file' $!\n";
417 my $text = <$include_file>;
418 close($include_file);
419
420 my @lines = split('\n', $text);
421
422 foreach my $line (@lines) {
423 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
424 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
425 $camelcase{$1} = 1;
426 }
427 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
428 $camelcase{$1} = 1;
429 }
430 }
431}
432
433my $camelcase_seeded = 0;
434sub seed_camelcase_includes {
435 return if ($camelcase_seeded);
436
437 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700438 my $camelcase_cache = "";
439 my @include_files = ();
440
441 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700442
Joe Perches34456862013-07-03 15:05:34 -0700443 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700444 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
445 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700446 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700447 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700448 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700449 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700450 @include_files = split('\n', $files);
451 foreach my $file (@include_files) {
452 my $date = POSIX::strftime("%Y%m%d%H%M",
453 localtime((stat $file)[9]));
454 $last_mod_date = $date if ($last_mod_date < $date);
455 }
456 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700457 }
Joe Perchesc707a812013-07-08 16:00:43 -0700458
459 if ($camelcase_cache ne "" && -f $camelcase_cache) {
460 open(my $camelcase_file, '<', "$camelcase_cache")
461 or warn "$P: Can't read '$camelcase_cache' $!\n";
462 while (<$camelcase_file>) {
463 chomp;
464 $camelcase{$_} = 1;
465 }
466 close($camelcase_file);
467
468 return;
469 }
470
471 if (-d ".git") {
472 $files = `git ls-files "include/*.h"`;
473 @include_files = split('\n', $files);
474 }
475
Joe Perches34456862013-07-03 15:05:34 -0700476 foreach my $file (@include_files) {
477 seed_camelcase_file($file);
478 }
Joe Perches351b2a12013-07-03 15:05:36 -0700479
Joe Perchesc707a812013-07-08 16:00:43 -0700480 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700481 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700482 open(my $camelcase_file, '>', "$camelcase_cache")
483 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700484 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
485 print $camelcase_file ("$_\n");
486 }
487 close($camelcase_file);
488 }
Joe Perches34456862013-07-03 15:05:34 -0700489}
490
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700491$chk_signoff = 0 if ($file);
492
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700493my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800494my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700495my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800496my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700497for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800498 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700499 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800500 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700501 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800502 } elsif ($filename eq '-') {
503 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700504 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800505 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700506 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700507 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800508 if ($filename eq '-') {
509 $vname = 'Your patch';
510 } else {
511 $vname = $filename;
512 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800513 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700514 chomp;
515 push(@rawlines, $_);
516 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800517 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800518 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700519 $exit = 1;
520 }
521 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800522 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700523 @fixed = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700524}
525
526exit($exit);
527
528sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700529 my ($root) = @_;
530
531 my @tree_check = (
532 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
533 "README", "Documentation", "arch", "include", "drivers",
534 "fs", "init", "ipc", "kernel", "lib", "scripts",
535 );
536
537 foreach my $check (@tree_check) {
538 if (! -e $root . '/' . $check) {
539 return 0;
540 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700541 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700542 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700543}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700544
Joe Perches20112472011-07-25 17:13:23 -0700545sub parse_email {
546 my ($formatted_email) = @_;
547
548 my $name = "";
549 my $address = "";
550 my $comment = "";
551
552 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
553 $name = $1;
554 $address = $2;
555 $comment = $3 if defined $3;
556 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
557 $address = $1;
558 $comment = $2 if defined $2;
559 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
560 $address = $1;
561 $comment = $2 if defined $2;
562 $formatted_email =~ s/$address.*$//;
563 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700564 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700565 $name =~ s/^\"|\"$//g;
566 # If there's a name left after stripping spaces and
567 # leading quotes, and the address doesn't have both
568 # leading and trailing angle brackets, the address
569 # is invalid. ie:
570 # "joe smith joe@smith.com" bad
571 # "joe smith <joe@smith.com" bad
572 if ($name ne "" && $address !~ /^<[^>]+>$/) {
573 $name = "";
574 $address = "";
575 $comment = "";
576 }
577 }
578
Joe Perches3705ce52013-07-03 15:05:31 -0700579 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700580 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700581 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700582 $address =~ s/^\<|\>$//g;
583
584 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
585 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
586 $name = "\"$name\"";
587 }
588
589 return ($name, $address, $comment);
590}
591
592sub format_email {
593 my ($name, $address) = @_;
594
595 my $formatted_email;
596
Joe Perches3705ce52013-07-03 15:05:31 -0700597 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700598 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700599 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700600
601 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
602 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
603 $name = "\"$name\"";
604 }
605
606 if ("$name" eq "") {
607 $formatted_email = "$address";
608 } else {
609 $formatted_email = "$name <$address>";
610 }
611
612 return $formatted_email;
613}
614
Joe Perches000d1cc12011-07-25 17:13:25 -0700615sub which_conf {
616 my ($conf) = @_;
617
618 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
619 if (-e "$path/$conf") {
620 return "$path/$conf";
621 }
622 }
623
624 return "";
625}
626
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700627sub expand_tabs {
628 my ($str) = @_;
629
630 my $res = '';
631 my $n = 0;
632 for my $c (split(//, $str)) {
633 if ($c eq "\t") {
634 $res .= ' ';
635 $n++;
636 for (; ($n % 8) != 0; $n++) {
637 $res .= ' ';
638 }
639 next;
640 }
641 $res .= $c;
642 $n++;
643 }
644
645 return $res;
646}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700647sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700648 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700649 return $res;
650}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700651
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700652sub line_stats {
653 my ($line) = @_;
654
655 # Drop the diff line leader and expand tabs
656 $line =~ s/^.//;
657 $line = expand_tabs($line);
658
659 # Pick the indent from the front of the line.
660 my ($white) = ($line =~ /^(\s*)/);
661
662 return (length($line), length($white));
663}
664
Andy Whitcroft773647a2008-03-28 14:15:58 -0700665my $sanitise_quote = '';
666
667sub sanitise_line_reset {
668 my ($in_comment) = @_;
669
670 if ($in_comment) {
671 $sanitise_quote = '*/';
672 } else {
673 $sanitise_quote = '';
674 }
675}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700676sub sanitise_line {
677 my ($line) = @_;
678
679 my $res = '';
680 my $l = '';
681
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800682 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700683 my $off = 0;
684 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700685
Andy Whitcroft773647a2008-03-28 14:15:58 -0700686 # Always copy over the diff marker.
687 $res = substr($line, 0, 1);
688
689 for ($off = 1; $off < length($line); $off++) {
690 $c = substr($line, $off, 1);
691
692 # Comments we are wacking completly including the begin
693 # and end, all to $;.
694 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
695 $sanitise_quote = '*/';
696
697 substr($res, $off, 2, "$;$;");
698 $off++;
699 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800700 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700701 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700702 $sanitise_quote = '';
703 substr($res, $off, 2, "$;$;");
704 $off++;
705 next;
706 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700707 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
708 $sanitise_quote = '//';
709
710 substr($res, $off, 2, $sanitise_quote);
711 $off++;
712 next;
713 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700714
715 # A \ in a string means ignore the next character.
716 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
717 $c eq "\\") {
718 substr($res, $off, 2, 'XX');
719 $off++;
720 next;
721 }
722 # Regular quotes.
723 if ($c eq "'" || $c eq '"') {
724 if ($sanitise_quote eq '') {
725 $sanitise_quote = $c;
726
727 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700728 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700729 } elsif ($sanitise_quote eq $c) {
730 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700731 }
732 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700733
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800734 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700735 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
736 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700737 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
738 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700739 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
740 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700741 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700742 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700743 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800744 }
745
Daniel Walker113f04a2009-09-21 17:04:35 -0700746 if ($sanitise_quote eq '//') {
747 $sanitise_quote = '';
748 }
749
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800750 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700751 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800752 my $clean = 'X' x length($1);
753 $res =~ s@\<.*\>@<$clean>@;
754
755 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700756 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800757 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700758 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800759 }
760
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700761 return $res;
762}
763
Joe Perchesa6962d72013-04-29 16:18:13 -0700764sub get_quoted_string {
765 my ($line, $rawline) = @_;
766
767 return "" if ($line !~ m/(\"[X]+\")/g);
768 return substr($rawline, $-[0], $+[0] - $-[0]);
769}
770
Andy Whitcroft8905a672007-11-28 16:21:06 -0800771sub ctx_statement_block {
772 my ($linenr, $remain, $off) = @_;
773 my $line = $linenr - 1;
774 my $blk = '';
775 my $soff = $off;
776 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700777 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800778
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800779 my $loff = 0;
780
Andy Whitcroft8905a672007-11-28 16:21:06 -0800781 my $type = '';
782 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800783 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800784 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800785 my $c;
786 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800787
788 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800789 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800790 @stack = (['', 0]) if ($#stack == -1);
791
Andy Whitcroft773647a2008-03-28 14:15:58 -0700792 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800793 # If we are about to drop off the end, pull in more
794 # context.
795 if ($off >= $len) {
796 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700797 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800798 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800799 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800800 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800801 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800802 $len = length($blk);
803 $line++;
804 last;
805 }
806 # Bail if there is no further context.
807 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800808 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800809 last;
810 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800811 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
812 $level++;
813 $type = '#';
814 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800815 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800816 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800817 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800818 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800819
Andy Whitcroft773647a2008-03-28 14:15:58 -0700820 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800821
822 # Handle nested #if/#else.
823 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
824 push(@stack, [ $type, $level ]);
825 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
826 ($type, $level) = @{$stack[$#stack - 1]};
827 } elsif ($remainder =~ /^#\s*endif\b/) {
828 ($type, $level) = @{pop(@stack)};
829 }
830
Andy Whitcroft8905a672007-11-28 16:21:06 -0800831 # Statement ends at the ';' or a close '}' at the
832 # outermost level.
833 if ($level == 0 && $c eq ';') {
834 last;
835 }
836
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800837 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700838 if ($level == 0 && $coff_set == 0 &&
839 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
840 $remainder =~ /^(else)(?:\s|{)/ &&
841 $remainder !~ /^else\s+if\b/) {
842 $coff = $off + length($1) - 1;
843 $coff_set = 1;
844 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
845 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800846 }
847
Andy Whitcroft8905a672007-11-28 16:21:06 -0800848 if (($type eq '' || $type eq '(') && $c eq '(') {
849 $level++;
850 $type = '(';
851 }
852 if ($type eq '(' && $c eq ')') {
853 $level--;
854 $type = ($level != 0)? '(' : '';
855
856 if ($level == 0 && $coff < $soff) {
857 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700858 $coff_set = 1;
859 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800860 }
861 }
862 if (($type eq '' || $type eq '{') && $c eq '{') {
863 $level++;
864 $type = '{';
865 }
866 if ($type eq '{' && $c eq '}') {
867 $level--;
868 $type = ($level != 0)? '{' : '';
869
870 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700871 if (substr($blk, $off + 1, 1) eq ';') {
872 $off++;
873 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800874 last;
875 }
876 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800877 # Preprocessor commands end at the newline unless escaped.
878 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
879 $level--;
880 $type = '';
881 $off++;
882 last;
883 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800884 $off++;
885 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700886 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800887 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700888 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800889 $line++;
890 $remain--;
891 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800892
893 my $statement = substr($blk, $soff, $off - $soff + 1);
894 my $condition = substr($blk, $soff, $coff - $soff + 1);
895
896 #warn "STATEMENT<$statement>\n";
897 #warn "CONDITION<$condition>\n";
898
Andy Whitcroft773647a2008-03-28 14:15:58 -0700899 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800900
901 return ($statement, $condition,
902 $line, $remain + 1, $off - $loff + 1, $level);
903}
904
Andy Whitcroftcf655042008-03-04 14:28:20 -0800905sub statement_lines {
906 my ($stmt) = @_;
907
908 # Strip the diff line prefixes and rip blank lines at start and end.
909 $stmt =~ s/(^|\n)./$1/g;
910 $stmt =~ s/^\s*//;
911 $stmt =~ s/\s*$//;
912
913 my @stmt_lines = ($stmt =~ /\n/g);
914
915 return $#stmt_lines + 2;
916}
917
918sub statement_rawlines {
919 my ($stmt) = @_;
920
921 my @stmt_lines = ($stmt =~ /\n/g);
922
923 return $#stmt_lines + 2;
924}
925
926sub statement_block_size {
927 my ($stmt) = @_;
928
929 $stmt =~ s/(^|\n)./$1/g;
930 $stmt =~ s/^\s*{//;
931 $stmt =~ s/}\s*$//;
932 $stmt =~ s/^\s*//;
933 $stmt =~ s/\s*$//;
934
935 my @stmt_lines = ($stmt =~ /\n/g);
936 my @stmt_statements = ($stmt =~ /;/g);
937
938 my $stmt_lines = $#stmt_lines + 2;
939 my $stmt_statements = $#stmt_statements + 1;
940
941 if ($stmt_lines > $stmt_statements) {
942 return $stmt_lines;
943 } else {
944 return $stmt_statements;
945 }
946}
947
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800948sub ctx_statement_full {
949 my ($linenr, $remain, $off) = @_;
950 my ($statement, $condition, $level);
951
952 my (@chunks);
953
Andy Whitcroftcf655042008-03-04 14:28:20 -0800954 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800955 ($statement, $condition, $linenr, $remain, $off, $level) =
956 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700957 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800958 push(@chunks, [ $condition, $statement ]);
959 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
960 return ($level, $linenr, @chunks);
961 }
962
963 # Pull in the following conditional/block pairs and see if they
964 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800965 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800966 ($statement, $condition, $linenr, $remain, $off, $level) =
967 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800968 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700969 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800970 #print "C: push\n";
971 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800972 }
973
974 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800975}
976
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700977sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700978 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700979 my $line;
980 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700981 my $blk = '';
982 my @o;
983 my @c;
984 my @res = ();
985
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700986 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800987 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700988 for ($line = $start; $remain > 0; $line++) {
989 next if ($rawlines[$line] =~ /^-/);
990 $remain--;
991
992 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800993
994 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700995 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800996 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700997 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800998 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700999 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001000 $level = pop(@stack);
1001 }
1002
Andy Whitcroft01464f32010-10-26 14:23:19 -07001003 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001004 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1005 if ($off > 0) {
1006 $off--;
1007 next;
1008 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001009
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001010 if ($c eq $close && $level > 0) {
1011 $level--;
1012 last if ($level == 0);
1013 } elsif ($c eq $open) {
1014 $level++;
1015 }
1016 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001017
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001018 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001019 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001020 }
1021
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001022 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001023 }
1024
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001025 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001026}
1027sub ctx_block_outer {
1028 my ($linenr, $remain) = @_;
1029
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001030 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1031 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001032}
1033sub ctx_block {
1034 my ($linenr, $remain) = @_;
1035
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001036 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1037 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001038}
1039sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001040 my ($linenr, $remain, $off) = @_;
1041
1042 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1043 return @r;
1044}
1045sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001046 my ($linenr, $remain) = @_;
1047
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001048 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001049}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001050sub ctx_statement_level {
1051 my ($linenr, $remain, $off) = @_;
1052
1053 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1054}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001055
1056sub ctx_locate_comment {
1057 my ($first_line, $end_line) = @_;
1058
1059 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001060 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001061 return $current_comment if (defined $current_comment);
1062
1063 # Look through the context and try and figure out if there is a
1064 # comment.
1065 my $in_comment = 0;
1066 $current_comment = '';
1067 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001068 my $line = $rawlines[$linenr - 1];
1069 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001070 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1071 $in_comment = 1;
1072 }
1073 if ($line =~ m@/\*@) {
1074 $in_comment = 1;
1075 }
1076 if (!$in_comment && $current_comment ne '') {
1077 $current_comment = '';
1078 }
1079 $current_comment .= $line . "\n" if ($in_comment);
1080 if ($line =~ m@\*/@) {
1081 $in_comment = 0;
1082 }
1083 }
1084
1085 chomp($current_comment);
1086 return($current_comment);
1087}
1088sub ctx_has_comment {
1089 my ($first_line, $end_line) = @_;
1090 my $cmt = ctx_locate_comment($first_line, $end_line);
1091
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001092 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001093 ##print "CMMT: $cmt\n";
1094
1095 return ($cmt ne '');
1096}
1097
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001098sub raw_line {
1099 my ($linenr, $cnt) = @_;
1100
1101 my $offset = $linenr - 1;
1102 $cnt++;
1103
1104 my $line;
1105 while ($cnt) {
1106 $line = $rawlines[$offset++];
1107 next if (defined($line) && $line =~ /^-/);
1108 $cnt--;
1109 }
1110
1111 return $line;
1112}
1113
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001114sub cat_vet {
1115 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001116 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001117
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001118 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001119 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1120 $res .= $1;
1121 if ($2 ne '') {
1122 $coded = sprintf("^%c", unpack('C', $2) + 64);
1123 $res .= $coded;
1124 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001125 }
1126 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001127
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001128 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001129}
1130
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001131my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001132my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001133my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001134my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001135
1136sub annotate_reset {
1137 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001138 $av_pending = '_';
1139 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001140 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001141}
1142
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001143sub annotate_values {
1144 my ($stream, $type) = @_;
1145
1146 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001147 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001148 my $cur = $stream;
1149
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001150 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001151
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001152 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001153 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001154 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001155 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001156 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001157 print "WS($1)\n" if ($dbg_values > 1);
1158 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001159 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001160 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001161 }
1162
Florian Micklerc023e4732011-01-12 16:59:58 -08001163 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001164 print "CAST($1)\n" if ($dbg_values > 1);
1165 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001166 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001167
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001168 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001169 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001170 $type = 'T';
1171
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001172 } elsif ($cur =~ /^($Modifier)\s*/) {
1173 print "MODIFIER($1)\n" if ($dbg_values > 1);
1174 $type = 'T';
1175
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001176 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001177 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001178 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001179 push(@av_paren_type, $type);
1180 if ($2 ne '') {
1181 $av_pending = 'N';
1182 }
1183 $type = 'E';
1184
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001185 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001186 print "UNDEF($1)\n" if ($dbg_values > 1);
1187 $av_preprocessor = 1;
1188 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001189
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001190 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001191 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001192 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001193
1194 push(@av_paren_type, $type);
1195 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001196 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001197
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001198 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001199 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1200 $av_preprocessor = 1;
1201
1202 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1203
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001204 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001205
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001206 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001207 print "PRE_END($1)\n" if ($dbg_values > 1);
1208
1209 $av_preprocessor = 1;
1210
1211 # Assume all arms of the conditional end as this
1212 # one does, and continue as if the #endif was not here.
1213 pop(@av_paren_type);
1214 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001215 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001216
1217 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001218 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001219
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001220 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1221 print "ATTR($1)\n" if ($dbg_values > 1);
1222 $av_pending = $type;
1223 $type = 'N';
1224
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001225 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001226 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001227 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001228 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001229 }
1230 $type = 'N';
1231
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001232 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001233 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001234 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001235 $type = 'N';
1236
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001237 } elsif ($cur =~/^(case)/o) {
1238 print "CASE($1)\n" if ($dbg_values > 1);
1239 $av_pend_colon = 'C';
1240 $type = 'N';
1241
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001242 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001243 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001244 $type = 'N';
1245
1246 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001247 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001248 push(@av_paren_type, $av_pending);
1249 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001250 $type = 'N';
1251
1252 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001253 my $new_type = pop(@av_paren_type);
1254 if ($new_type ne '_') {
1255 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001256 print "PAREN('$1') -> $type\n"
1257 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001258 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001259 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001260 }
1261
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001262 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001263 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001264 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001265 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001266
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001267 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1268 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001269 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001270 } elsif ($type eq 'E') {
1271 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001272 }
1273 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1274 $type = 'V';
1275
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001276 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001277 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001278 $type = 'V';
1279
1280 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001281 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001282 $type = 'N';
1283
Andy Whitcroftcf655042008-03-04 14:28:20 -08001284 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001285 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001286 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001287 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001288
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001289 } elsif ($cur =~/^(,)/) {
1290 print "COMMA($1)\n" if ($dbg_values > 1);
1291 $type = 'C';
1292
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001293 } elsif ($cur =~ /^(\?)/o) {
1294 print "QUESTION($1)\n" if ($dbg_values > 1);
1295 $type = 'N';
1296
1297 } elsif ($cur =~ /^(:)/o) {
1298 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1299
1300 substr($var, length($res), 1, $av_pend_colon);
1301 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1302 $type = 'E';
1303 } else {
1304 $type = 'N';
1305 }
1306 $av_pend_colon = 'O';
1307
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001308 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001309 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001310 $type = 'N';
1311
Andy Whitcroft0d413862008-10-15 22:02:16 -07001312 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001313 my $variant;
1314
1315 print "OPV($1)\n" if ($dbg_values > 1);
1316 if ($type eq 'V') {
1317 $variant = 'B';
1318 } else {
1319 $variant = 'U';
1320 }
1321
1322 substr($var, length($res), 1, $variant);
1323 $type = 'N';
1324
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001325 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001326 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001327 if ($1 ne '++' && $1 ne '--') {
1328 $type = 'N';
1329 }
1330
1331 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001332 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001333 }
1334 if (defined $1) {
1335 $cur = substr($cur, length($1));
1336 $res .= $type x length($1);
1337 }
1338 }
1339
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001340 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001341}
1342
Andy Whitcroft8905a672007-11-28 16:21:06 -08001343sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001344 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001345 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001346 ^(?:
1347 $Modifier|
1348 $Storage|
1349 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001350 DEFINE_\S+
1351 )$|
1352 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001353 goto|
1354 return|
1355 case|
1356 else|
1357 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001358 do|
1359 \#|
1360 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001361 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001362 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001363 )}x;
1364 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1365 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001366 # Check for modifiers.
1367 $possible =~ s/\s*$Storage\s*//g;
1368 $possible =~ s/\s*$Sparse\s*//g;
1369 if ($possible =~ /^\s*$/) {
1370
1371 } elsif ($possible =~ /\s/) {
1372 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001373 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001374 if ($modifier !~ $notPermitted) {
1375 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1376 push(@modifierList, $modifier);
1377 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001378 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001379
1380 } else {
1381 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1382 push(@typeList, $possible);
1383 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001384 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001385 } else {
1386 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001387 }
1388}
1389
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001390my $prefix = '';
1391
Joe Perches000d1cc12011-07-25 17:13:25 -07001392sub show_type {
Joe Perches91bfe482013-09-11 14:23:59 -07001393 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1394
1395 return !defined $ignore_type{$_[0]};
Joe Perches000d1cc12011-07-25 17:13:25 -07001396}
1397
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001398sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001399 if (!show_type($_[1]) ||
1400 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001401 return 0;
1402 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001403 my $line;
1404 if ($show_types) {
1405 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1406 } else {
1407 $line = "$prefix$_[0]: $_[2]\n";
1408 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001409 $line = (split('\n', $line))[0] . "\n" if ($terse);
1410
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001411 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001412
1413 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001414}
1415sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001416 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001417}
Joe Perches000d1cc12011-07-25 17:13:25 -07001418
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001419sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001420 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421 our $clean = 0;
1422 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001423 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001424 }
Joe Perches3705ce52013-07-03 15:05:31 -07001425 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001426}
1427sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001428 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001429 our $clean = 0;
1430 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001431 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432 }
Joe Perches3705ce52013-07-03 15:05:31 -07001433 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001434}
1435sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001436 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001437 our $clean = 0;
1438 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001439 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001440 }
Joe Perches3705ce52013-07-03 15:05:31 -07001441 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001442}
1443
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001444sub check_absolute_file {
1445 my ($absolute, $herecurr) = @_;
1446 my $file = $absolute;
1447
1448 ##print "absolute<$absolute>\n";
1449
1450 # See if any suffix of this path is a path within the tree.
1451 while ($file =~ s@^[^/]*/@@) {
1452 if (-f "$root/$file") {
1453 ##print "file<$file>\n";
1454 last;
1455 }
1456 }
1457 if (! -f _) {
1458 return 0;
1459 }
1460
1461 # It is, so see if the prefix is acceptable.
1462 my $prefix = $absolute;
1463 substr($prefix, -length($file)) = '';
1464
1465 ##print "prefix<$prefix>\n";
1466 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001467 WARN("USE_RELATIVE_PATH",
1468 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001469 }
1470}
1471
Joe Perches3705ce52013-07-03 15:05:31 -07001472sub trim {
1473 my ($string) = @_;
1474
1475 $string =~ s/(^\s+|\s+$)//g;
1476
1477 return $string;
1478}
1479
1480sub tabify {
1481 my ($leading) = @_;
1482
1483 my $source_indent = 8;
1484 my $max_spaces_before_tab = $source_indent - 1;
1485 my $spaces_to_tab = " " x $source_indent;
1486
1487 #convert leading spaces to tabs
1488 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1489 #Remove spaces before a tab
1490 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1491
1492 return "$leading";
1493}
1494
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001495sub pos_last_openparen {
1496 my ($line) = @_;
1497
1498 my $pos = 0;
1499
1500 my $opens = $line =~ tr/\(/\(/;
1501 my $closes = $line =~ tr/\)/\)/;
1502
1503 my $last_openparen = 0;
1504
1505 if (($opens == 0) || ($closes >= $opens)) {
1506 return -1;
1507 }
1508
1509 my $len = length($line);
1510
1511 for ($pos = 0; $pos < $len; $pos++) {
1512 my $string = substr($line, $pos);
1513 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1514 $pos += length($1) - 1;
1515 } elsif (substr($line, $pos, 1) eq '(') {
1516 $last_openparen = $pos;
1517 } elsif (index($string, '(') == -1) {
1518 last;
1519 }
1520 }
1521
1522 return $last_openparen + 1;
1523}
1524
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001525sub process {
1526 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001527
1528 my $linenr=0;
1529 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001530 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001531 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001532 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001533
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001534 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001535 my $indent;
1536 my $previndent=0;
1537 my $stashindent=0;
1538
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001539 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001540 my $signoff = 0;
1541 my $is_patch = 0;
1542
Joe Perches15662b32011-10-31 17:13:12 -07001543 my $in_header_lines = 1;
1544 my $in_commit_log = 0; #Scanning lines before patch
1545
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001546 my $non_utf8_charset = 0;
1547
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001548 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001549 our $cnt_lines = 0;
1550 our $cnt_error = 0;
1551 our $cnt_warn = 0;
1552 our $cnt_chk = 0;
1553
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001554 # Trace the real file/line as we go.
1555 my $realfile = '';
1556 my $realline = 0;
1557 my $realcnt = 0;
1558 my $here = '';
1559 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001560 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001561 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001562 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001563
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001564 my $prev_values = 'E';
1565
1566 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001567 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001568 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001569 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001570 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001571
Joe Perches7e51f192013-09-11 14:23:57 -07001572 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001573
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001574 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001575 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001576 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001577 my @setup_docs = ();
1578 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001579
1580 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001581 my $line;
1582 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001583 $linenr++;
1584 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001585
Joe Perches3705ce52013-07-03 15:05:31 -07001586 push(@fixed, $rawline) if ($fix);
1587
Andy Whitcroft773647a2008-03-28 14:15:58 -07001588 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001589 $setup_docs = 0;
1590 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1591 $setup_docs = 1;
1592 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001593 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001594 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001595 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1596 $realline=$1-1;
1597 if (defined $2) {
1598 $realcnt=$3+1;
1599 } else {
1600 $realcnt=1+1;
1601 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001602 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001603
1604 # Guestimate if this is a continuing comment. Run
1605 # the context looking for a comment "edge". If this
1606 # edge is a close comment then we must be in a comment
1607 # at context start.
1608 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001609 my $cnt = $realcnt;
1610 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1611 next if (defined $rawlines[$ln - 1] &&
1612 $rawlines[$ln - 1] =~ /^-/);
1613 $cnt--;
1614 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001615 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001616 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1617 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1618 ($edge) = $1;
1619 last;
1620 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 }
1622 if (defined $edge && $edge eq '*/') {
1623 $in_comment = 1;
1624 }
1625
1626 # Guestimate if this is a continuing comment. If this
1627 # is the start of a diff block and this line starts
1628 # ' *' then it is very likely a comment.
1629 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001630 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001631 {
1632 $in_comment = 1;
1633 }
1634
1635 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1636 sanitise_line_reset($in_comment);
1637
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001638 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001639 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001640 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001641 $line = sanitise_line($rawline);
1642 }
1643 push(@lines, $line);
1644
1645 if ($realcnt > 1) {
1646 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1647 } else {
1648 $realcnt = 0;
1649 }
1650
1651 #print "==>$rawline\n";
1652 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001653
1654 if ($setup_docs && $line =~ /^\+/) {
1655 push(@setup_docs, $line);
1656 }
1657 }
1658
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001659 $prefix = '';
1660
Andy Whitcroft773647a2008-03-28 14:15:58 -07001661 $realcnt = 0;
1662 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001663 foreach my $line (@lines) {
1664 $linenr++;
1665
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001666 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001667
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001668#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001669 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001670 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001671 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001672 $realline=$1-1;
1673 if (defined $2) {
1674 $realcnt=$3+1;
1675 } else {
1676 $realcnt=1+1;
1677 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001678 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001679 $prev_values = 'E';
1680
Andy Whitcroft773647a2008-03-28 14:15:58 -07001681 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001682 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001683 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001684 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001685 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001686
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001687# track the line number as we move through the hunk, note that
1688# new versions of GNU diff omit the leading space on completely
1689# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001690 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001691 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001692 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001693
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001694 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001695 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001696
1697 # Track the previous line.
1698 ($prevline, $stashline) = ($stashline, $line);
1699 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001700 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1701
Andy Whitcroft773647a2008-03-28 14:15:58 -07001702 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001703
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001704 } elsif ($realcnt == 1) {
1705 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001706 }
1707
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001708 my $hunk_line = ($realcnt != 0);
1709
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001710#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001711 $prefix = "$filename:$realline: " if ($emacs && $file);
1712 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1713
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001714 $here = "#$linenr: " if (!$file);
1715 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001716
1717 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001718 if ($line =~ /^diff --git.*?(\S+)$/) {
1719 $realfile = $1;
1720 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001721 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001722 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001723 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001724 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001725 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001726
1727 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001728 if (!$file && $tree && $p1_prefix ne '' &&
1729 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001730 WARN("PATCH_PREFIX",
1731 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001732 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001733
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001734 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001735 ERROR("MODIFIED_INCLUDE_ASM",
1736 "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 -07001737 }
1738 next;
1739 }
1740
Randy Dunlap389834b2007-06-08 13:47:03 -07001741 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001742
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001743 my $hereline = "$here\n$rawline\n";
1744 my $herecurr = "$here\n$rawline\n";
1745 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001746
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001747 $cnt_lines++ if ($realcnt != 0);
1748
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001749# Check for incorrect file permissions
1750 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1751 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001752 if ($realfile !~ m@scripts/@ &&
1753 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001754 ERROR("EXECUTE_PERMISSIONS",
1755 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001756 }
1757 }
1758
Joe Perches20112472011-07-25 17:13:23 -07001759# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001760 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001761 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001762 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001763 }
1764
1765# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001766 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001767 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001768 my $space_before = $1;
1769 my $sign_off = $2;
1770 my $space_after = $3;
1771 my $email = $4;
1772 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1773
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001774 if ($sign_off !~ /$signature_tags/) {
1775 WARN("BAD_SIGN_OFF",
1776 "Non-standard signature: $sign_off\n" . $herecurr);
1777 }
Joe Perches20112472011-07-25 17:13:23 -07001778 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001779 if (WARN("BAD_SIGN_OFF",
1780 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1781 $fix) {
1782 $fixed[$linenr - 1] =
1783 "$ucfirst_sign_off $email";
1784 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001785 }
Joe Perches20112472011-07-25 17:13:23 -07001786 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001787 if (WARN("BAD_SIGN_OFF",
1788 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1789 $fix) {
1790 $fixed[$linenr - 1] =
1791 "$ucfirst_sign_off $email";
1792 }
1793
Joe Perches20112472011-07-25 17:13:23 -07001794 }
1795 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001796 if (WARN("BAD_SIGN_OFF",
1797 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1798 $fix) {
1799 $fixed[$linenr - 1] =
1800 "$ucfirst_sign_off $email";
1801 }
Joe Perches20112472011-07-25 17:13:23 -07001802 }
1803
1804 my ($email_name, $email_address, $comment) = parse_email($email);
1805 my $suggested_email = format_email(($email_name, $email_address));
1806 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001807 ERROR("BAD_SIGN_OFF",
1808 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001809 } else {
1810 my $dequoted = $suggested_email;
1811 $dequoted =~ s/^"//;
1812 $dequoted =~ s/" </ </;
1813 # Don't force email to have quotes
1814 # Allow just an angle bracketed address
1815 if ("$dequoted$comment" ne $email &&
1816 "<$email_address>$comment" ne $email &&
1817 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001818 WARN("BAD_SIGN_OFF",
1819 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001820 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001821 }
Joe Perches7e51f192013-09-11 14:23:57 -07001822
1823# Check for duplicate signatures
1824 my $sig_nospace = $line;
1825 $sig_nospace =~ s/\s//g;
1826 $sig_nospace = lc($sig_nospace);
1827 if (defined $signatures{$sig_nospace}) {
1828 WARN("BAD_SIGN_OFF",
1829 "Duplicate signature\n" . $herecurr);
1830 } else {
1831 $signatures{$sig_nospace} = 1;
1832 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001833 }
1834
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001835# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001836 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001837 ERROR("CORRUPTED_PATCH",
1838 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001839 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001840 }
1841
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001842# Check for absolute kernel paths.
1843 if ($tree) {
1844 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1845 my $file = $1;
1846
1847 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1848 check_absolute_file($1, $herecurr)) {
1849 #
1850 } else {
1851 check_absolute_file($file, $herecurr);
1852 }
1853 }
1854 }
1855
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001856# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1857 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001858 $rawline !~ m/^$UTF8*$/) {
1859 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1860
1861 my $blank = copy_spacing($rawline);
1862 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1863 my $hereptr = "$hereline$ptr\n";
1864
Joe Perches34d99212011-07-25 17:13:26 -07001865 CHK("INVALID_UTF8",
1866 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001867 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001868
Joe Perches15662b32011-10-31 17:13:12 -07001869# Check if it's the start of a commit log
1870# (not a header line and we haven't seen the patch filename)
1871 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001872 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001873 $in_header_lines = 0;
1874 $in_commit_log = 1;
1875 }
1876
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001877# Check if there is UTF-8 in a commit log when a mail header has explicitly
1878# declined it, i.e defined some charset where it is missing.
1879 if ($in_header_lines &&
1880 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1881 $1 !~ /utf-8/i) {
1882 $non_utf8_charset = 1;
1883 }
1884
1885 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001886 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001887 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001888 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1889 }
1890
Andy Whitcroft306708542008-10-15 22:02:28 -07001891# ignore non-hunk lines and lines being removed
1892 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001893
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001894#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001895 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001896 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07001897 if (ERROR("DOS_LINE_ENDINGS",
1898 "DOS line endings\n" . $herevet) &&
1899 $fix) {
1900 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1901 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001902 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1903 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001904 if (ERROR("TRAILING_WHITESPACE",
1905 "trailing whitespace\n" . $herevet) &&
1906 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07001907 $fixed[$linenr - 1] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001908 }
1909
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001910 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001911 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001912
Andi Kleen33549572010-05-24 14:33:29 -07001913# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001914# Only applies when adding the entry originally, after that we do not have
1915# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001916 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001917 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001918 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001919 my $cnt = $realcnt;
1920 my $ln = $linenr + 1;
1921 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001922 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001923 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001924 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001925 $f = $lines[$ln - 1];
1926 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1927 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001928
1929 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001930
1931 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1932 $is_start = 1;
1933 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1934 $length = -1;
1935 }
1936
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001937 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001938 $f =~ s/#.*//;
1939 $f =~ s/^\s+//;
1940 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001941 if ($f =~ /^\s*config\s/) {
1942 $is_end = 1;
1943 last;
1944 }
Andi Kleen33549572010-05-24 14:33:29 -07001945 $length++;
1946 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001947 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001948 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1949 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001950 }
1951
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001952# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1953 if ($realfile =~ /Kconfig/ &&
1954 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1955 WARN("CONFIG_EXPERIMENTAL",
1956 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1957 }
1958
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001959 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1960 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1961 my $flag = $1;
1962 my $replacement = {
1963 'EXTRA_AFLAGS' => 'asflags-y',
1964 'EXTRA_CFLAGS' => 'ccflags-y',
1965 'EXTRA_CPPFLAGS' => 'cppflags-y',
1966 'EXTRA_LDFLAGS' => 'ldflags-y',
1967 };
1968
1969 WARN("DEPRECATED_VARIABLE",
1970 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1971 }
1972
Andy Whitcroft5368df202008-10-15 22:02:27 -07001973# check we are in a valid source file if not then ignore this hunk
1974 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1975
Joe Perches6cd7f382012-12-17 16:01:54 -08001976#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001977 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001978 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001979 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001980 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001981 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001982 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001983 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001984 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001985 }
1986
Josh Triplettca56dc02012-03-23 15:02:21 -07001987# Check for user-visible strings broken across lines, which breaks the ability
1988# to grep for the string. Limited to strings used as parameters (those
1989# following an open parenthesis), which almost completely eliminates false
1990# positives, as well as warning only once per parameter rather than once per
1991# line of the string. Make an exception when the previous string ends in a
1992# newline (multiple lines in one string constant) or \n\t (common in inline
1993# assembly to indent the instruction on the following line).
1994 if ($line =~ /^\+\s*"/ &&
1995 $prevline =~ /"\s*$/ &&
1996 $prevline =~ /\(/ &&
1997 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1998 WARN("SPLIT_STRING",
1999 "quoted string split across lines\n" . $hereprev);
2000 }
2001
Joe Perches5e79d962010-03-05 13:43:55 -08002002# check for spaces before a quoted newline
2003 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002004 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2005 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2006 $fix) {
2007 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2008 }
2009
Joe Perches5e79d962010-03-05 13:43:55 -08002010 }
2011
Andy Whitcroft8905a672007-11-28 16:21:06 -08002012# check for adding lines without a newline.
2013 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002014 WARN("MISSING_EOF_NEWLINE",
2015 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002016 }
2017
Mike Frysinger42e41c52009-09-21 17:04:40 -07002018# Blackfin: use hi/lo macros
2019 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2020 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2021 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002022 ERROR("LO_MACRO",
2023 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002024 }
2025 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2026 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002027 ERROR("HI_MACRO",
2028 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002029 }
2030 }
2031
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002032# check we are in a valid source file C or perl if not then ignore this hunk
2033 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002034
2035# at the beginning of a line any tabs must come first and anything
2036# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002037 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2038 $rawline =~ /^\+\s* \s*/) {
2039 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002040 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002041 if (ERROR("CODE_INDENT",
2042 "code indent should use tabs where possible\n" . $herevet) &&
2043 $fix) {
2044 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2045 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002046 }
2047
Alberto Panizzo08e44362010-03-05 13:43:54 -08002048# check for space before tabs.
2049 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2050 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002051 if (WARN("SPACE_BEFORE_TAB",
2052 "please, no space before tabs\n" . $herevet) &&
2053 $fix) {
2054 $fixed[$linenr - 1] =~
2055 s/(^\+.*) +\t/$1\t/;
2056 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002057 }
2058
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002059# check for && or || at the start of a line
2060 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2061 CHK("LOGICAL_CONTINUATIONS",
2062 "Logical continuations should be on the previous line\n" . $hereprev);
2063 }
2064
2065# check multi-line statement indentation matches previous line
2066 if ($^V && $^V ge 5.10.0 &&
2067 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2068 $prevline =~ /^\+(\t*)(.*)$/;
2069 my $oldindent = $1;
2070 my $rest = $2;
2071
2072 my $pos = pos_last_openparen($rest);
2073 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002074 $line =~ /^(\+| )([ \t]*)/;
2075 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002076
2077 my $goodtabindent = $oldindent .
2078 "\t" x ($pos / 8) .
2079 " " x ($pos % 8);
2080 my $goodspaceindent = $oldindent . " " x $pos;
2081
2082 if ($newindent ne $goodtabindent &&
2083 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002084
2085 if (CHK("PARENTHESIS_ALIGNMENT",
2086 "Alignment should match open parenthesis\n" . $hereprev) &&
2087 $fix && $line =~ /^\+/) {
2088 $fixed[$linenr - 1] =~
2089 s/^\+[ \t]*/\+$goodtabindent/;
2090 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002091 }
2092 }
2093 }
2094
Joe Perches23f780c2013-07-03 15:05:31 -07002095 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002096 if (CHK("SPACING",
2097 "No space is necessary after a cast\n" . $hereprev) &&
2098 $fix) {
2099 $fixed[$linenr - 1] =~
2100 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2101 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002102 }
2103
Joe Perches05880602012-10-04 17:13:35 -07002104 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002105 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2106 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002107 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2108 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2109 }
2110
2111 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002112 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2113 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002114 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002115 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2116 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2117 "networking block comments start with * on subsequent lines\n" . $hereprev);
2118 }
2119
2120 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002121 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2122 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2123 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2124 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002125 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2126 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2127 }
2128
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002129# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002130# Exceptions:
2131# 1) within comments
2132# 2) indented preprocessor commands
2133# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002134 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002135 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002136 if (WARN("LEADING_SPACE",
2137 "please, no spaces at the start of a line\n" . $herevet) &&
2138 $fix) {
2139 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2140 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002141 }
2142
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002143# check we are in a valid C source file if not then ignore this hunk
2144 next if ($realfile !~ /\.(h|c)$/);
2145
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002146# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2147 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2148 WARN("CONFIG_EXPERIMENTAL",
2149 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2150 }
2151
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002152# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002153 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002154 WARN("CVS_KEYWORD",
2155 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002156 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002157
Mike Frysinger42e41c52009-09-21 17:04:40 -07002158# Blackfin: don't use __builtin_bfin_[cs]sync
2159 if ($line =~ /__builtin_bfin_csync/) {
2160 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002161 ERROR("CSYNC",
2162 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002163 }
2164 if ($line =~ /__builtin_bfin_ssync/) {
2165 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002166 ERROR("SSYNC",
2167 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002168 }
2169
Joe Perches56e77d72013-02-21 16:44:14 -08002170# check for old HOTPLUG __dev<foo> section markings
2171 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2172 WARN("HOTPLUG_SECTION",
2173 "Using $1 is unnecessary\n" . $herecurr);
2174 }
2175
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002176# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002177 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2178 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002179#print "LINE<$line>\n";
2180 if ($linenr >= $suppress_statement &&
2181 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002182 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002183 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002184 $stat =~ s/\n./\n /g;
2185 $cond =~ s/\n./\n /g;
2186
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002187#print "linenr<$linenr> <$stat>\n";
2188 # If this statement has no statement boundaries within
2189 # it there is no point in retrying a statement scan
2190 # until we hit end of it.
2191 my $frag = $stat; $frag =~ s/;+\s*$//;
2192 if ($frag !~ /(?:{|;)/) {
2193#print "skip<$line_nr_next>\n";
2194 $suppress_statement = $line_nr_next;
2195 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002196
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002197 # Find the real next line.
2198 $realline_next = $line_nr_next;
2199 if (defined $realline_next &&
2200 (!defined $lines[$realline_next - 1] ||
2201 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2202 $realline_next++;
2203 }
2204
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002205 my $s = $stat;
2206 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002207
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002208 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002209 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002210
2211 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002212 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002213
Andy Whitcroft463f2862009-09-21 17:04:34 -07002214 } elsif ($s =~ /^.\s*else\b/s) {
2215
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002216 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002217 } 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 -07002218 my $type = $1;
2219 $type =~ s/\s+/ /g;
2220 possible($type, "A:" . $s);
2221
Andy Whitcroft8905a672007-11-28 16:21:06 -08002222 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002223 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002224 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002225 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002226
2227 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002228 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002229 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002230 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002231
2232 # Check for any sort of function declaration.
2233 # int foo(something bar, other baz);
2234 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002235 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 -08002236 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002237
Andy Whitcroftcf655042008-03-04 14:28:20 -08002238 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002240 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002241
Andy Whitcroft8905a672007-11-28 16:21:06 -08002242 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002243 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002244
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002245 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002246 }
2247 }
2248 }
2249
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002250 }
2251
Andy Whitcroft653d4872007-06-23 17:16:34 -07002252#
2253# Checks which may be anchored in the context.
2254#
2255
2256# Check for switch () and associated case and default
2257# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002258 if ($line=~/\bswitch\s*\(.*\)/) {
2259 my $err = '';
2260 my $sep = '';
2261 my @ctx = ctx_block_outer($linenr, $realcnt);
2262 shift(@ctx);
2263 for my $ctx (@ctx) {
2264 my ($clen, $cindent) = line_stats($ctx);
2265 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2266 $indent != $cindent) {
2267 $err .= "$sep$ctx\n";
2268 $sep = '';
2269 } else {
2270 $sep = "[...]\n";
2271 }
2272 }
2273 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002274 ERROR("SWITCH_CASE_INDENT_LEVEL",
2275 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002276 }
2277 }
2278
2279# if/while/etc brace do not go on next line, unless defining a do while loop,
2280# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002281 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002282 my $pre_ctx = "$1$2";
2283
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002284 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002285
2286 if ($line =~ /^\+\t{6,}/) {
2287 WARN("DEEP_INDENTATION",
2288 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2289 }
2290
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002291 my $ctx_cnt = $realcnt - $#ctx - 1;
2292 my $ctx = join("\n", @ctx);
2293
Andy Whitcroft548596d2008-07-23 21:29:01 -07002294 my $ctx_ln = $linenr;
2295 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002296
Andy Whitcroft548596d2008-07-23 21:29:01 -07002297 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2298 defined $lines[$ctx_ln - 1] &&
2299 $lines[$ctx_ln - 1] =~ /^-/)) {
2300 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2301 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002302 $ctx_ln++;
2303 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002304
Andy Whitcroft53210162008-07-23 21:29:03 -07002305 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2306 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002307
2308 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002309 ERROR("OPEN_BRACE",
2310 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002311 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002312 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002313 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2314 $ctx =~ /\)\s*\;\s*$/ &&
2315 defined $lines[$ctx_ln - 1])
2316 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002317 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2318 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002319 WARN("TRAILING_SEMICOLON",
2320 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002321 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002322 }
2323 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002324 }
2325
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002326# Check relative indent for conditionals and blocks.
2327 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002328 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2329 ctx_statement_block($linenr, $realcnt, 0)
2330 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002331 my ($s, $c) = ($stat, $cond);
2332
2333 substr($s, 0, length($c), '');
2334
2335 # Make sure we remove the line prefixes as we have
2336 # none on the first line, and are going to readd them
2337 # where necessary.
2338 $s =~ s/\n./\n/gs;
2339
2340 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002341 my @newlines = ($c =~ /\n/gs);
2342 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002343
2344 # We want to check the first line inside the block
2345 # starting at the end of the conditional, so remove:
2346 # 1) any blank line termination
2347 # 2) any opening brace { on end of the line
2348 # 3) any do (...) {
2349 my $continuation = 0;
2350 my $check = 0;
2351 $s =~ s/^.*\bdo\b//;
2352 $s =~ s/^\s*{//;
2353 if ($s =~ s/^\s*\\//) {
2354 $continuation = 1;
2355 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002356 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002357 $check = 1;
2358 $cond_lines++;
2359 }
2360
2361 # Also ignore a loop construct at the end of a
2362 # preprocessor statement.
2363 if (($prevline =~ /^.\s*#\s*define\s/ ||
2364 $prevline =~ /\\\s*$/) && $continuation == 0) {
2365 $check = 0;
2366 }
2367
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002368 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002369 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002370 while ($cond_ptr != $cond_lines) {
2371 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002372
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002373 # If we see an #else/#elif then the code
2374 # is not linear.
2375 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2376 $check = 0;
2377 }
2378
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002379 # Ignore:
2380 # 1) blank lines, they should be at 0,
2381 # 2) preprocessor lines, and
2382 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002383 if ($continuation ||
2384 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002385 $s =~ /^\s*#\s*?/ ||
2386 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002387 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002388 if ($s =~ s/^.*?\n//) {
2389 $cond_lines++;
2390 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002391 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002392 }
2393
2394 my (undef, $sindent) = line_stats("+" . $s);
2395 my $stat_real = raw_line($linenr, $cond_lines);
2396
2397 # Check if either of these lines are modified, else
2398 # this is not this patch's fault.
2399 if (!defined($stat_real) ||
2400 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2401 $check = 0;
2402 }
2403 if (defined($stat_real) && $cond_lines > 1) {
2404 $stat_real = "[...]\n$stat_real";
2405 }
2406
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002407 #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 -07002408
2409 if ($check && (($sindent % 8) != 0 ||
2410 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002411 WARN("SUSPECT_CODE_INDENT",
2412 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002413 }
2414 }
2415
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002416 # Track the 'values' across context and added lines.
2417 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002418 my ($curr_values, $curr_vars) =
2419 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002420 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002421 if ($dbg_values) {
2422 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002423 print "$linenr > .$outline\n";
2424 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002425 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002426 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002427 $prev_values = substr($curr_values, -1);
2428
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002429#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002430 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002431
Andy Whitcroft653d4872007-06-23 17:16:34 -07002432# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002433 if ($dbg_type) {
2434 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002435 ERROR("TEST_TYPE",
2436 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002437 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002438 ERROR("TEST_NOT_TYPE",
2439 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002440 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002441 next;
2442 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002443# TEST: allow direct testing of the attribute matcher.
2444 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002445 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002446 ERROR("TEST_ATTR",
2447 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002448 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002449 ERROR("TEST_NOT_ATTR",
2450 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002451 }
2452 next;
2453 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002454
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002455# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002456 if ($line =~ /^.\s*{/ &&
2457 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002458 ERROR("OPEN_BRACE",
2459 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002460 }
2461
Andy Whitcroft653d4872007-06-23 17:16:34 -07002462#
2463# Checks which are anchored on the added line.
2464#
2465
2466# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002467 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002468 my $path = $1;
2469 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002470 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002471 "malformed #include filename\n" . $herecurr);
2472 }
2473 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2474 ERROR("UAPI_INCLUDE",
2475 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002476 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002477 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002478
2479# no C99 // comments
2480 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002481 if (ERROR("C99_COMMENTS",
2482 "do not use C99 // comments\n" . $herecurr) &&
2483 $fix) {
2484 my $line = $fixed[$linenr - 1];
2485 if ($line =~ /\/\/(.*)$/) {
2486 my $comment = trim($1);
2487 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2488 }
2489 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002490 }
2491 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002492 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002493 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002494
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002495# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2496# the whole statement.
2497#print "APW <$lines[$realline_next - 1]>\n";
2498 if (defined $realline_next &&
2499 exists $lines[$realline_next - 1] &&
2500 !defined $suppress_export{$realline_next} &&
2501 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2502 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002503 # Handle definitions which produce identifiers with
2504 # a prefix:
2505 # XXX(foo);
2506 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002507 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002508 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002509 $name =~ /^${Ident}_$2/) {
2510#print "FOO C name<$name>\n";
2511 $suppress_export{$realline_next} = 1;
2512
2513 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002514 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002515 ^.DEFINE_$Ident\(\Q$name\E\)|
2516 ^.DECLARE_$Ident\(\Q$name\E\)|
2517 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002518 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2519 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002520 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002521#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2522 $suppress_export{$realline_next} = 2;
2523 } else {
2524 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002525 }
2526 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002527 if (!defined $suppress_export{$linenr} &&
2528 $prevline =~ /^.\s*$/ &&
2529 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2530 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2531#print "FOO B <$lines[$linenr - 1]>\n";
2532 $suppress_export{$linenr} = 2;
2533 }
2534 if (defined $suppress_export{$linenr} &&
2535 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002536 WARN("EXPORT_SYMBOL",
2537 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002538 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002539
Joe Eloff5150bda2010-08-09 17:21:00 -07002540# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002541 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2542 if (ERROR("GLOBAL_INITIALISERS",
2543 "do not initialise globals to 0 or NULL\n" .
2544 $herecurr) &&
2545 $fix) {
2546 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2547 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002548 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002549# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002550 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2551 if (ERROR("INITIALISED_STATIC",
2552 "do not initialise statics to 0 or NULL\n" .
2553 $herecurr) &&
2554 $fix) {
2555 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2556 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002557 }
2558
Joe Perchescb710ec2010-10-26 14:23:20 -07002559# check for static const char * arrays.
2560 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002561 WARN("STATIC_CONST_CHAR_ARRAY",
2562 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002563 $herecurr);
2564 }
2565
2566# check for static char foo[] = "bar" declarations.
2567 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002568 WARN("STATIC_CONST_CHAR_ARRAY",
2569 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002570 $herecurr);
2571 }
2572
Joe Perches93ed0e22010-10-26 14:23:21 -07002573# check for declarations of struct pci_device_id
2574 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002575 WARN("DEFINE_PCI_DEVICE_TABLE",
2576 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002577 }
2578
Andy Whitcroft653d4872007-06-23 17:16:34 -07002579# check for new typedefs, only function parameters and sparse annotations
2580# make sense.
2581 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002582 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002583 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002584 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002585 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002586 WARN("NEW_TYPEDEFS",
2587 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002588 }
2589
2590# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002591 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002592 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2593 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002594 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002595
Andy Whitcroft65863862009-01-06 14:41:21 -08002596 # Should start with a space.
2597 $to =~ s/^(\S)/ $1/;
2598 # Should not end with a space.
2599 $to =~ s/\s+$//;
2600 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002601 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002602 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002603
Joe Perches3705ce52013-07-03 15:05:31 -07002604## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002605 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002606 if (ERROR("POINTER_LOCATION",
2607 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2608 $fix) {
2609 my $sub_from = $ident;
2610 my $sub_to = $ident;
2611 $sub_to =~ s/\Q$from\E/$to/;
2612 $fixed[$linenr - 1] =~
2613 s@\Q$sub_from\E@$sub_to@;
2614 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002615 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002616 }
2617 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2618 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002619 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002620
Andy Whitcroft65863862009-01-06 14:41:21 -08002621 # Should start with a space.
2622 $to =~ s/^(\S)/ $1/;
2623 # Should not end with a space.
2624 $to =~ s/\s+$//;
2625 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002626 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002627 }
2628 # Modifiers should have spaces.
2629 $to =~ s/(\b$Modifier$)/$1 /;
2630
Joe Perches3705ce52013-07-03 15:05:31 -07002631## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002632 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002633 if (ERROR("POINTER_LOCATION",
2634 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2635 $fix) {
2636
2637 my $sub_from = $match;
2638 my $sub_to = $match;
2639 $sub_to =~ s/\Q$from\E/$to/;
2640 $fixed[$linenr - 1] =~
2641 s@\Q$sub_from\E@$sub_to@;
2642 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002643 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002644 }
2645
2646# # no BUG() or BUG_ON()
2647# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2648# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2649# print "$herecurr";
2650# $clean = 0;
2651# }
2652
Andy Whitcroft8905a672007-11-28 16:21:06 -08002653 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002654 WARN("LINUX_VERSION_CODE",
2655 "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 -08002656 }
2657
Joe Perches17441222011-06-15 15:08:17 -07002658# check for uses of printk_ratelimit
2659 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002660 WARN("PRINTK_RATELIMITED",
2661"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002662 }
2663
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002664# printk should use KERN_* levels. Note that follow on printk's on the
2665# same line do not need a level, so we use the current block context
2666# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002667# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002668# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002669 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002670 my $ok = 0;
2671 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2672 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002673 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002674 # with "\n" ignore it, else it is to blame
2675 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2676 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2677 $ok = 1;
2678 }
2679 last;
2680 }
2681 }
2682 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002683 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2684 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002685 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002686 }
2687
Joe Perches243f3802012-05-31 16:26:09 -07002688 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2689 my $orig = $1;
2690 my $level = lc($orig);
2691 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002692 my $level2 = $level;
2693 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002694 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002695 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002696 }
2697
2698 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07002699 if (WARN("PREFER_PR_LEVEL",
2700 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2701 $fix) {
2702 $fixed[$linenr - 1] =~
2703 s/\bpr_warning\b/pr_warn/;
2704 }
Joe Perches243f3802012-05-31 16:26:09 -07002705 }
2706
Joe Perchesdc139312013-02-21 16:44:13 -08002707 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2708 my $orig = $1;
2709 my $level = lc($orig);
2710 $level = "warn" if ($level eq "warning");
2711 $level = "dbg" if ($level eq "debug");
2712 WARN("PREFER_DEV_LEVEL",
2713 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2714 }
2715
Andy Whitcroft653d4872007-06-23 17:16:34 -07002716# function brace can't be on same line, except for #defines of do while,
2717# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002718 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2719 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002720 ERROR("OPEN_BRACE",
2721 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002722 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002723
Andy Whitcroft8905a672007-11-28 16:21:06 -08002724# open braces for enum, union and struct go on the same line.
2725 if ($line =~ /^.\s*{/ &&
2726 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002727 ERROR("OPEN_BRACE",
2728 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002729 }
2730
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002731# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002732 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2733 if (WARN("SPACING",
2734 "missing space after $1 definition\n" . $herecurr) &&
2735 $fix) {
2736 $fixed[$linenr - 1] =~
2737 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2738 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002739 }
2740
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002741# check for spacing round square brackets; allowed:
2742# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002743# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2744# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002745 while ($line =~ /(.*?\s)\[/g) {
2746 my ($where, $prefix) = ($-[1], $1);
2747 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002748 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002749 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002750 if (ERROR("BRACKET_SPACE",
2751 "space prohibited before open square bracket '['\n" . $herecurr) &&
2752 $fix) {
2753 $fixed[$linenr - 1] =~
2754 s/^(\+.*?)\s+\[/$1\[/;
2755 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002756 }
2757 }
2758
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002759# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002760 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002761 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002762 my $ctx_before = substr($line, 0, $-[1]);
2763 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002764
2765 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002766 if ($name =~ /^(?:
2767 if|for|while|switch|return|case|
2768 volatile|__volatile__|
2769 __attribute__|format|__extension__|
2770 asm|__asm__)$/x)
2771 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002772 # cpp #define statements have non-optional spaces, ie
2773 # if there is a space between the name and the open
2774 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002775 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002776
2777 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002778 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002779
2780 # If this whole things ends with a type its most
2781 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002782 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002783
2784 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002785 if (WARN("SPACING",
2786 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2787 $fix) {
2788 $fixed[$linenr - 1] =~
2789 s/\b$name\s+\(/$name\(/;
2790 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002791 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002792 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002793
Andy Whitcroft653d4872007-06-23 17:16:34 -07002794# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002795 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002796 my $fixed_line = "";
2797 my $line_fixed = 0;
2798
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002799 my $ops = qr{
2800 <<=|>>=|<=|>=|==|!=|
2801 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2802 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002803 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2804 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002805 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002806 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002807
2808## print("element count: <" . $#elements . ">\n");
2809## foreach my $el (@elements) {
2810## print("el: <$el>\n");
2811## }
2812
2813 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002814 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002815
Joe Perches3705ce52013-07-03 15:05:31 -07002816 foreach my $el (@elements) {
2817 push(@fix_elements, substr($rawline, $off, length($el)));
2818 $off += length($el);
2819 }
2820
2821 $off = 0;
2822
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002823 my $blank = copy_spacing($opline);
2824
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002825 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002826
2827 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2828
2829## print("n: <$n> good: <$good>\n");
2830
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002831 $off += length($elements[$n]);
2832
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002833 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002834 my $ca = substr($opline, 0, $off);
2835 my $cc = '';
2836 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2837 $cc = substr($opline, $off + length($elements[$n + 1]));
2838 }
2839 my $cb = "$ca$;$cc";
2840
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002841 my $a = '';
2842 $a = 'V' if ($elements[$n] ne '');
2843 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002844 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002845 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2846 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002847 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002848
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002849 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002850
2851 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002852 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002853 $c = 'V' if ($elements[$n + 2] ne '');
2854 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002855 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002856 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2857 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002858 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002859 } else {
2860 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002861 }
2862
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002863 my $ctx = "${a}x${c}";
2864
2865 my $at = "(ctx:$ctx)";
2866
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002867 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002868 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002869
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002870 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002871 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002872
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002873 # Get the full operator variant.
2874 my $opv = $op . substr($curr_vars, $off, 1);
2875
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002876 # Ignore operators passed as parameters.
2877 if ($op_type ne 'V' &&
2878 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2879
Andy Whitcroftcf655042008-03-04 14:28:20 -08002880# # Ignore comments
2881# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002882
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002883 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002884 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002885 if ($ctx !~ /.x[WEBC]/ &&
2886 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002887 if (ERROR("SPACING",
2888 "space required after that '$op' $at\n" . $hereptr)) {
2889 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2890 $line_fixed = 1;
2891 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002892 }
2893
2894 # // is a comment
2895 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002896
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002897 # No spaces for:
2898 # ->
2899 # : when part of a bitfield
2900 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002901 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002902 if (ERROR("SPACING",
2903 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2904 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2905 $line_fixed = 1;
2906 if (defined $fix_elements[$n + 2]) {
2907 $fix_elements[$n + 2] =~ s/^\s+//;
2908 }
2909 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002910 }
2911
2912 # , must have a space on the right.
2913 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002914 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002915 if (ERROR("SPACING",
2916 "space required after that '$op' $at\n" . $hereptr)) {
2917 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2918 $line_fixed = 1;
2919 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002920 }
2921
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002922 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002923 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002924 #warn "'*' is part of type\n";
2925
2926 # unary operators should have a space before and
2927 # none after. May be left adjacent to another
2928 # unary operator, or a cast
2929 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002930 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002931 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002932 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002933 if (ERROR("SPACING",
2934 "space required before that '$op' $at\n" . $hereptr)) {
2935 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2936 $line_fixed = 1;
2937 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002938 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002939 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002940 # A unary '*' may be const
2941
2942 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002943 if (ERROR("SPACING",
2944 "space prohibited after that '$op' $at\n" . $hereptr)) {
2945 $fixed_line =~ s/\s+$//;
2946 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2947 $line_fixed = 1;
2948 if (defined $fix_elements[$n + 2]) {
2949 $fix_elements[$n + 2] =~ s/^\s+//;
2950 }
2951 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002952 }
2953
2954 # unary ++ and unary -- are allowed no space on one side.
2955 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002956 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002957 if (ERROR("SPACING",
2958 "space required one side of that '$op' $at\n" . $hereptr)) {
2959 $fixed_line =~ s/\s+$//;
2960 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2961 $line_fixed = 1;
2962 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002963 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002964 if ($ctx =~ /Wx[BE]/ ||
2965 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002966 if (ERROR("SPACING",
2967 "space prohibited before that '$op' $at\n" . $hereptr)) {
2968 $fixed_line =~ s/\s+$//;
2969 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2970 $line_fixed = 1;
2971 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002972 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002973 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002974 if (ERROR("SPACING",
2975 "space prohibited after that '$op' $at\n" . $hereptr)) {
2976 $fixed_line =~ s/\s+$//;
2977 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2978 $line_fixed = 1;
2979 if (defined $fix_elements[$n + 2]) {
2980 $fix_elements[$n + 2] =~ s/^\s+//;
2981 }
2982 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002983 }
2984
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002985 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002986 } elsif ($op eq '<<' or $op eq '>>' or
2987 $op eq '&' or $op eq '^' or $op eq '|' or
2988 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002989 $op eq '*' or $op eq '/' or
2990 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002991 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002992 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002993 if (ERROR("SPACING",
2994 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2995 $fixed_line =~ s/\s+$//;
2996 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2997 $line_fixed = 1;
2998 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002999 }
3000
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003001 # A colon needs no spaces before when it is
3002 # terminating a case value or a label.
3003 } elsif ($opv eq ':C' || $opv eq ':L') {
3004 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003005 if (ERROR("SPACING",
3006 "space prohibited before that '$op' $at\n" . $hereptr)) {
3007 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3008 $line_fixed = 1;
3009 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003010 }
3011
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003012 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003013 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003014 my $ok = 0;
3015
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003016 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003017 if (($op eq '<' &&
3018 $cc =~ /^\S+\@\S+>/) ||
3019 ($op eq '>' &&
3020 $ca =~ /<\S+\@\S+$/))
3021 {
3022 $ok = 1;
3023 }
3024
3025 # Ignore ?:
3026 if (($opv eq ':O' && $ca =~ /\?$/) ||
3027 ($op eq '?' && $cc =~ /^:/)) {
3028 $ok = 1;
3029 }
3030
3031 if ($ok == 0) {
Joe Perches3705ce52013-07-03 15:05:31 -07003032 if (ERROR("SPACING",
3033 "spaces required around that '$op' $at\n" . $hereptr)) {
3034 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3035 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
3036 $line_fixed = 1;
3037 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003038 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003039 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003040 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003041
3042## print("n: <$n> GOOD: <$good>\n");
3043
3044 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003045 }
Joe Perches3705ce52013-07-03 15:05:31 -07003046
3047 if (($#elements % 2) == 0) {
3048 $fixed_line = $fixed_line . $fix_elements[$#elements];
3049 }
3050
3051 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3052 $fixed[$linenr - 1] = $fixed_line;
3053 }
3054
3055
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003056 }
3057
Joe Perches786b6322013-07-03 15:05:32 -07003058# check for whitespace before a non-naked semicolon
3059 if ($line =~ /^\+.*\S\s+;/) {
3060 if (WARN("SPACING",
3061 "space prohibited before semicolon\n" . $herecurr) &&
3062 $fix) {
3063 1 while $fixed[$linenr - 1] =~
3064 s/^(\+.*\S)\s+;/$1;/;
3065 }
3066 }
3067
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003068# check for multiple assignments
3069 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003070 CHK("MULTIPLE_ASSIGNMENTS",
3071 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003072 }
3073
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003074## # check for multiple declarations, allowing for a function declaration
3075## # continuation.
3076## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3077## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3078##
3079## # Remove any bracketed sections to ensure we do not
3080## # falsly report the parameters of functions.
3081## my $ln = $line;
3082## while ($ln =~ s/\([^\(\)]*\)//g) {
3083## }
3084## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003085## WARN("MULTIPLE_DECLARATION",
3086## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003087## }
3088## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003089
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003090#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003091 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3092 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003093 if (ERROR("SPACING",
3094 "space required before the open brace '{'\n" . $herecurr) &&
3095 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003096 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003097 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003098 }
3099
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003100## # check for blank lines before declarations
3101## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3102## $prevrawline =~ /^.\s*$/) {
3103## WARN("SPACING",
3104## "No blank lines before declarations\n" . $hereprev);
3105## }
3106##
3107
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003108# closing brace should have a space following it when it has anything
3109# on the line
3110 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003111 if (ERROR("SPACING",
3112 "space required after that close brace '}'\n" . $herecurr) &&
3113 $fix) {
3114 $fixed[$linenr - 1] =~
3115 s/}((?!(?:,|;|\)))\S)/} $1/;
3116 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003117 }
3118
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003119# check spacing on square brackets
3120 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003121 if (ERROR("SPACING",
3122 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3123 $fix) {
3124 $fixed[$linenr - 1] =~
3125 s/\[\s+/\[/;
3126 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003127 }
3128 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003129 if (ERROR("SPACING",
3130 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3131 $fix) {
3132 $fixed[$linenr - 1] =~
3133 s/\s+\]/\]/;
3134 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003135 }
3136
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003137# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003138 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3139 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003140 if (ERROR("SPACING",
3141 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3142 $fix) {
3143 $fixed[$linenr - 1] =~
3144 s/\(\s+/\(/;
3145 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003146 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003147 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003148 $line !~ /for\s*\(.*;\s+\)/ &&
3149 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003150 if (ERROR("SPACING",
3151 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3152 $fix) {
3153 $fixed[$linenr - 1] =~
3154 s/\s+\)/\)/;
3155 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003156 }
3157
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003158#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003159 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003160 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003161 if (WARN("INDENTED_LABEL",
3162 "labels should not be indented\n" . $herecurr) &&
3163 $fix) {
3164 $fixed[$linenr - 1] =~
3165 s/^(.)\s+/$1/;
3166 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003167 }
3168
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003169# Return is not a function.
3170 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3171 my $spacing = $1;
3172 my $value = $2;
3173
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003174 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003175 $value =~ s/\(/ \(/g;
3176 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003177 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003178 $value !~ /(?:$Ident|-?$Constant)\s*
3179 $Compare\s*
3180 (?:$Ident|-?$Constant)/x &&
3181 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003182 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003183#print "value<$value>\n";
3184 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003185 ERROR("RETURN_PARENTHESES",
3186 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003187
3188 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003189 ERROR("SPACING",
3190 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003191 }
3192 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003193# Return of what appears to be an errno should normally be -'ve
3194 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3195 my $name = $1;
3196 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003197 WARN("USE_NEGATIVE_ERRNO",
3198 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003199 }
3200 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003201
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003202# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003203 if ($line =~ /\b(if|while|for|switch)\(/) {
3204 if (ERROR("SPACING",
3205 "space required before the open parenthesis '('\n" . $herecurr) &&
3206 $fix) {
3207 $fixed[$linenr - 1] =~
3208 s/\b(if|while|for|switch)\(/$1 \(/;
3209 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003210 }
3211
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003212# Check for illegal assignment in if conditional -- and check for trailing
3213# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003214 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003215 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3216 ctx_statement_block($linenr, $realcnt, 0)
3217 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003218 my ($stat_next) = ctx_statement_block($line_nr_next,
3219 $remain_next, $off_next);
3220 $stat_next =~ s/\n./\n /g;
3221 ##print "stat<$stat> stat_next<$stat_next>\n";
3222
3223 if ($stat_next =~ /^\s*while\b/) {
3224 # If the statement carries leading newlines,
3225 # then count those as offsets.
3226 my ($whitespace) =
3227 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3228 my $offset =
3229 statement_rawlines($whitespace) - 1;
3230
3231 $suppress_whiletrailers{$line_nr_next +
3232 $offset} = 1;
3233 }
3234 }
3235 if (!defined $suppress_whiletrailers{$linenr} &&
3236 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003237 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003238
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003239 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003240 ERROR("ASSIGN_IN_IF",
3241 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003242 }
3243
3244 # Find out what is on the end of the line after the
3245 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003246 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003247 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003248 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003249 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3250 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003251 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003252 # Find out how long the conditional actually is.
3253 my @newlines = ($c =~ /\n/gs);
3254 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003255 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003256
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003257 $stat_real = raw_line($linenr, $cond_lines)
3258 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003259 if (defined($stat_real) && $cond_lines > 1) {
3260 $stat_real = "[...]\n$stat_real";
3261 }
3262
Joe Perches000d1cc12011-07-25 17:13:25 -07003263 ERROR("TRAILING_STATEMENTS",
3264 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003265 }
3266 }
3267
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003268# Check for bitwise tests written as boolean
3269 if ($line =~ /
3270 (?:
3271 (?:\[|\(|\&\&|\|\|)
3272 \s*0[xX][0-9]+\s*
3273 (?:\&\&|\|\|)
3274 |
3275 (?:\&\&|\|\|)
3276 \s*0[xX][0-9]+\s*
3277 (?:\&\&|\|\||\)|\])
3278 )/x)
3279 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003280 WARN("HEXADECIMAL_BOOLEAN_TEST",
3281 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003282 }
3283
Andy Whitcroft8905a672007-11-28 16:21:06 -08003284# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003285 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3286 my $s = $1;
3287 $s =~ s/$;//g; # Remove any comments
3288 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003289 ERROR("TRAILING_STATEMENTS",
3290 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003291 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003292 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003293# if should not continue a brace
3294 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003295 ERROR("TRAILING_STATEMENTS",
3296 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003297 $herecurr);
3298 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003299# case and default should not have general statements after them
3300 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3301 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003302 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003303 \s*return\s+
3304 )/xg)
3305 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003306 ERROR("TRAILING_STATEMENTS",
3307 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003308 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003309
3310 # Check for }<nl>else {, these must be at the same
3311 # indent level to be relevant to each other.
3312 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3313 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003314 ERROR("ELSE_AFTER_BRACE",
3315 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003316 }
3317
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003318 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3319 $previndent == $indent) {
3320 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3321
3322 # Find out what is on the end of the line after the
3323 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003324 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003325 $s =~ s/\n.*//g;
3326
3327 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003328 ERROR("WHILE_AFTER_BRACE",
3329 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003330 }
3331 }
3332
Joe Perches95e2c602013-07-03 15:05:20 -07003333#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003334 while ($line =~ m{($Constant|$Lval)}g) {
3335 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003336
3337#gcc binary extension
3338 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003339 if (WARN("GCC_BINARY_CONSTANT",
3340 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3341 $fix) {
3342 my $hexval = sprintf("0x%x", oct($var));
3343 $fixed[$linenr - 1] =~
3344 s/\b$var\b/$hexval/;
3345 }
Joe Perches95e2c602013-07-03 15:05:20 -07003346 }
3347
3348#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003349 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003350 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003351#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003352 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003353#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003354 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003355 while ($var =~ m{($Ident)}g) {
3356 my $word = $1;
3357 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3358 seed_camelcase_includes() if ($check);
3359 if (!defined $camelcase{$word}) {
3360 $camelcase{$word} = 1;
3361 CHK("CAMELCASE",
3362 "Avoid CamelCase: <$word>\n" . $herecurr);
3363 }
Joe Perches34456862013-07-03 15:05:34 -07003364 }
Joe Perches323c1262012-12-17 16:02:07 -08003365 }
3366 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003367
3368#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003369 if ($line =~ /\#\s*define.*\\\s+$/) {
3370 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3371 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3372 $fix) {
3373 $fixed[$linenr - 1] =~ s/\s+$//;
3374 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003375 }
3376
Andy Whitcroft653d4872007-06-23 17:16:34 -07003377#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003378 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003379 my $file = "$1.h";
3380 my $checkfile = "include/linux/$file";
3381 if (-f "$root/$checkfile" &&
3382 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003383 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003384 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003385 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003386 CHK("ARCH_INCLUDE_LINUX",
3387 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003388 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003389 WARN("INCLUDE_LINUX",
3390 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003391 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003392 }
3393 }
3394
Andy Whitcroft653d4872007-06-23 17:16:34 -07003395# multi-statement macros should be enclosed in a do while loop, grab the
3396# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003397# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07003398 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3399 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003400 my $ln = $linenr;
3401 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003402 my ($off, $dstat, $dcond, $rest);
3403 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003404 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003405 ctx_statement_block($linenr, $realcnt, 0);
3406 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003407 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003408 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003409
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003410 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003411 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003412 $dstat =~ s/\\\n.//g;
3413 $dstat =~ s/^\s*//s;
3414 $dstat =~ s/\s*$//s;
3415
3416 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003417 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3418 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003419 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003420 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003421 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003422
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003423 # Flatten any obvious string concatentation.
3424 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3425 $dstat =~ s/$Ident\s*("X*")/$1/)
3426 {
3427 }
3428
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003429 my $exceptions = qr{
3430 $Declare|
3431 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003432 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003433 DECLARE_PER_CPU|
3434 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003435 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003436 union|
3437 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003438 \.$Ident\s*=\s*|
3439 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003440 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003441 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003442 if ($dstat ne '' &&
3443 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3444 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003445 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003446 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003447 $dstat !~ /$exceptions/ &&
3448 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003449 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003450 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003451 $dstat !~ /^for\s*$Constant$/ && # for (...)
3452 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3453 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07003454 $dstat !~ /^\({/ && # ({...
3455 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003456 {
3457 $ctx =~ s/\n*$//;
3458 my $herectx = $here . "\n";
3459 my $cnt = statement_rawlines($ctx);
3460
3461 for (my $n = 0; $n < $cnt; $n++) {
3462 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003463 }
3464
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003465 if ($dstat =~ /;/) {
3466 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3467 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3468 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003469 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003470 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003471 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003472 }
Joe Perches5023d342012-12-17 16:01:47 -08003473
Joe Perches481eb482012-12-17 16:01:56 -08003474# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003475
3476 } else {
3477 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003478 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3479 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003480 $line =~ /^\+.*\\$/) {
3481 WARN("LINE_CONTINUATIONS",
3482 "Avoid unnecessary line continuations\n" . $herecurr);
3483 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003484 }
3485
Joe Perchesb13edf72012-07-30 14:41:24 -07003486# do {} while (0) macro tests:
3487# single-statement macros do not need to be enclosed in do while (0) loop,
3488# macro should not end with a semicolon
3489 if ($^V && $^V ge 5.10.0 &&
3490 $realfile !~ m@/vmlinux.lds.h$@ &&
3491 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3492 my $ln = $linenr;
3493 my $cnt = $realcnt;
3494 my ($off, $dstat, $dcond, $rest);
3495 my $ctx = '';
3496 ($dstat, $dcond, $ln, $cnt, $off) =
3497 ctx_statement_block($linenr, $realcnt, 0);
3498 $ctx = $dstat;
3499
3500 $dstat =~ s/\\\n.//g;
3501
3502 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3503 my $stmts = $2;
3504 my $semis = $3;
3505
3506 $ctx =~ s/\n*$//;
3507 my $cnt = statement_rawlines($ctx);
3508 my $herectx = $here . "\n";
3509
3510 for (my $n = 0; $n < $cnt; $n++) {
3511 $herectx .= raw_line($linenr, $n) . "\n";
3512 }
3513
Joe Perchesac8e97f2012-08-21 16:15:53 -07003514 if (($stmts =~ tr/;/;/) == 1 &&
3515 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003516 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3517 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3518 }
3519 if (defined $semis && $semis ne "") {
3520 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3521 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3522 }
3523 }
3524 }
3525
Mike Frysinger080ba922009-01-06 14:41:25 -08003526# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3527# all assignments may have only one of the following with an assignment:
3528# .
3529# ALIGN(...)
3530# VMLINUX_SYMBOL(...)
3531 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003532 WARN("MISSING_VMLINUX_SYMBOL",
3533 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003534 }
3535
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003536# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003537 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3538 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003539 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003540 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003541 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3542 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003543 my @allowed = ();
3544 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003545 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003546 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003547 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003548 for my $chunk (@chunks) {
3549 my ($cond, $block) = @{$chunk};
3550
Andy Whitcroft773647a2008-03-28 14:15:58 -07003551 # If the condition carries leading newlines, then count those as offsets.
3552 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3553 my $offset = statement_rawlines($whitespace) - 1;
3554
Joe Perchesaad4f612012-03-23 15:02:19 -07003555 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003556 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3557
3558 # We have looked at and allowed this specific line.
3559 $suppress_ifbraces{$ln + $offset} = 1;
3560
3561 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003562 $ln += statement_rawlines($block) - 1;
3563
Andy Whitcroft773647a2008-03-28 14:15:58 -07003564 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003565
3566 $seen++ if ($block =~ /^\s*{/);
3567
Joe Perchesaad4f612012-03-23 15:02:19 -07003568 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003569 if (statement_lines($cond) > 1) {
3570 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003571 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003572 }
3573 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003574 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003575 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003576 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003577 if (statement_block_size($block) > 1) {
3578 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003579 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003580 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003581 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003582 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003583 if ($seen) {
3584 my $sum_allowed = 0;
3585 foreach (@allowed) {
3586 $sum_allowed += $_;
3587 }
3588 if ($sum_allowed == 0) {
3589 WARN("BRACES",
3590 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3591 } elsif ($sum_allowed != $allow &&
3592 $seen != $allow) {
3593 CHK("BRACES",
3594 "braces {} should be used on all arms of this statement\n" . $herectx);
3595 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003596 }
3597 }
3598 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003599 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003600 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003601 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003602
Andy Whitcroftcf655042008-03-04 14:28:20 -08003603 # Check the pre-context.
3604 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3605 #print "APW: ALLOWED: pre<$1>\n";
3606 $allowed = 1;
3607 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003608
3609 my ($level, $endln, @chunks) =
3610 ctx_statement_full($linenr, $realcnt, $-[0]);
3611
Andy Whitcroftcf655042008-03-04 14:28:20 -08003612 # Check the condition.
3613 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003614 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003615 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003616 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003617 }
3618 if (statement_lines($cond) > 1) {
3619 #print "APW: ALLOWED: cond<$cond>\n";
3620 $allowed = 1;
3621 }
3622 if ($block =~/\b(?:if|for|while)\b/) {
3623 #print "APW: ALLOWED: block<$block>\n";
3624 $allowed = 1;
3625 }
3626 if (statement_block_size($block) > 1) {
3627 #print "APW: ALLOWED: lines block<$block>\n";
3628 $allowed = 1;
3629 }
3630 # Check the post-context.
3631 if (defined $chunks[1]) {
3632 my ($cond, $block) = @{$chunks[1]};
3633 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003634 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003635 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003636 if ($block =~ /^\s*\{/) {
3637 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3638 $allowed = 1;
3639 }
3640 }
3641 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003642 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003643 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003644
Andy Whitcroftf0556632008-10-15 22:02:23 -07003645 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003646 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003647 }
3648
Joe Perches000d1cc12011-07-25 17:13:25 -07003649 WARN("BRACES",
3650 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003651 }
3652 }
3653
Joe Perches0979ae62012-12-17 16:01:59 -08003654# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003655 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003656 CHK("BRACES",
3657 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3658 }
Joe Perches77b9a532013-07-03 15:05:29 -07003659 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003660 CHK("BRACES",
3661 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3662 }
3663
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003664# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003665 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3666 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003667 WARN("VOLATILE",
3668 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003669 }
3670
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003671# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003672 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003673 CHK("REDUNDANT_CODE",
3674 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003675 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003676 }
3677
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003678# check for needless "if (<foo>) fn(<foo>)" uses
3679 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3680 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3681 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3682 WARN('NEEDLESS_IF',
3683 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003684 }
3685 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003686
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003687# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003688 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003689 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003690 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003691 CHK("USLEEP_RANGE",
3692 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003693 }
3694 }
3695
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003696# warn about unexpectedly long msleep's
3697 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3698 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003699 WARN("MSLEEP",
3700 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003701 }
3702 }
3703
Joe Perches36ec1932013-07-03 15:05:25 -07003704# check for comparisons of jiffies
3705 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3706 WARN("JIFFIES_COMPARISON",
3707 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3708 }
3709
Joe Perches9d7a34a2013-07-03 15:05:26 -07003710# check for comparisons of get_jiffies_64()
3711 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3712 WARN("JIFFIES_COMPARISON",
3713 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3714 }
3715
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003716# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003717# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003718# print "#ifdef in C files should be avoided\n";
3719# print "$herecurr";
3720# $clean = 0;
3721# }
3722
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003723# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003724 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003725 if (ERROR("SPACING",
3726 "exactly one space required after that #$1\n" . $herecurr) &&
3727 $fix) {
3728 $fixed[$linenr - 1] =~
3729 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3730 }
3731
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003732 }
3733
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003734# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003735 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3736 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003737 my $which = $1;
3738 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003739 CHK("UNCOMMENTED_DEFINITION",
3740 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003741 }
3742 }
3743# check for memory barriers without a comment.
3744 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3745 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003746 CHK("MEMORY_BARRIER",
3747 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003748 }
3749 }
3750# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003751 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003752 CHK("ARCH_DEFINES",
3753 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003754 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003755
Tobias Klauserd4977c72010-05-24 14:33:30 -07003756# Check that the storage class is at the beginning of a declaration
3757 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003758 WARN("STORAGE_CLASS",
3759 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003760 }
3761
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003762# check the location of the inline attribute, that it is between
3763# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003764 if ($line =~ /\b$Type\s+$Inline\b/ ||
3765 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003766 ERROR("INLINE_LOCATION",
3767 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003768 }
3769
Andy Whitcroft8905a672007-11-28 16:21:06 -08003770# Check for __inline__ and __inline, prefer inline
3771 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003772 if (WARN("INLINE",
3773 "plain inline is preferred over $1\n" . $herecurr) &&
3774 $fix) {
3775 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3776
3777 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003778 }
3779
Joe Perches3d130fd2011-01-12 17:00:00 -08003780# Check for __attribute__ packed, prefer __packed
3781 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003782 WARN("PREFER_PACKED",
3783 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003784 }
3785
Joe Perches39b7e282011-07-25 17:13:24 -07003786# Check for __attribute__ aligned, prefer __aligned
3787 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003788 WARN("PREFER_ALIGNED",
3789 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003790 }
3791
Joe Perches5f14d3b2012-01-10 15:09:52 -08003792# Check for __attribute__ format(printf, prefer __printf
3793 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003794 if (WARN("PREFER_PRINTF",
3795 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3796 $fix) {
3797 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3798
3799 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08003800 }
3801
Joe Perches6061d942012-03-23 15:02:16 -07003802# Check for __attribute__ format(scanf, prefer __scanf
3803 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003804 if (WARN("PREFER_SCANF",
3805 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3806 $fix) {
3807 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3808 }
Joe Perches6061d942012-03-23 15:02:16 -07003809 }
3810
Joe Perches8f53a9b2010-03-05 13:43:48 -08003811# check for sizeof(&)
3812 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003813 WARN("SIZEOF_ADDRESS",
3814 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003815 }
3816
Joe Perches66c80b62012-07-30 14:41:22 -07003817# check for sizeof without parenthesis
3818 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003819 if (WARN("SIZEOF_PARENTHESIS",
3820 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3821 $fix) {
3822 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3823 }
Joe Perches66c80b62012-07-30 14:41:22 -07003824 }
3825
Joe Perches428e2fd2011-05-24 17:13:39 -07003826# check for line continuations in quoted strings with odd counts of "
3827 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003828 WARN("LINE_CONTINUATIONS",
3829 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003830 }
3831
Joe Perches88982fe2012-12-17 16:02:00 -08003832# check for struct spinlock declarations
3833 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3834 WARN("USE_SPINLOCK_T",
3835 "struct spinlock should be spinlock_t\n" . $herecurr);
3836 }
3837
Joe Perchesa6962d72013-04-29 16:18:13 -07003838# check for seq_printf uses that could be seq_puts
3839 if ($line =~ /\bseq_printf\s*\(/) {
3840 my $fmt = get_quoted_string($line, $rawline);
3841 if ($fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003842 if (WARN("PREFER_SEQ_PUTS",
3843 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3844 $fix) {
3845 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3846 }
Joe Perchesa6962d72013-04-29 16:18:13 -07003847 }
3848 }
3849
Andy Whitcroft554e1652012-01-10 15:09:57 -08003850# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003851 if ($^V && $^V ge 5.10.0 &&
3852 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003853 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003854
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003855 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003856 my $ms_val = $7;
3857 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003858
Andy Whitcroft554e1652012-01-10 15:09:57 -08003859 if ($ms_size =~ /^(0x|)0$/i) {
3860 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003861 "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 -08003862 } elsif ($ms_size =~ /^(0x|)1$/i) {
3863 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003864 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3865 }
3866 }
3867
3868# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003869 if ($^V && $^V ge 5.10.0 &&
3870 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003871 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003872 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003873 my $call = $1;
3874 my $cast1 = deparenthesize($2);
3875 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003876 my $cast2 = deparenthesize($7);
3877 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003878 my $cast;
3879
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003880 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003881 $cast = "$cast1 or $cast2";
3882 } elsif ($cast1 ne "") {
3883 $cast = $cast1;
3884 } else {
3885 $cast = $cast2;
3886 }
3887 WARN("MINMAX",
3888 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003889 }
3890 }
3891
Joe Perches4a273192012-07-30 14:41:20 -07003892# check usleep_range arguments
3893 if ($^V && $^V ge 5.10.0 &&
3894 defined $stat &&
3895 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3896 my $min = $1;
3897 my $max = $7;
3898 if ($min eq $max) {
3899 WARN("USLEEP_RANGE",
3900 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3901 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3902 $min > $max) {
3903 WARN("USLEEP_RANGE",
3904 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3905 }
3906 }
3907
Joe Perches70dc8a42013-09-11 14:23:58 -07003908# check for new externs in .h files.
3909 if ($realfile =~ /\.h$/ &&
3910 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3911 if (WARN("AVOID_EXTERNS",
3912 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
3913 $fix) {
3914 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3915 }
3916 }
3917
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003918# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003919 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003920 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003921 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003922 my $function_name = $1;
3923 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003924
3925 my $s = $stat;
3926 if (defined $cond) {
3927 substr($s, 0, length($cond), '');
3928 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003929 if ($s =~ /^\s*;/ &&
3930 $function_name ne 'uninitialized_var')
3931 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003932 WARN("AVOID_EXTERNS",
3933 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003934 }
3935
3936 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003937 WARN("FUNCTION_ARGUMENTS",
3938 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003939 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003940
3941 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3942 $stat =~ /^.\s*extern\s+/)
3943 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003944 WARN("AVOID_EXTERNS",
3945 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003946 }
3947
3948# checks for new __setup's
3949 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3950 my $name = $1;
3951
3952 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003953 CHK("UNDOCUMENTED_SETUP",
3954 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003955 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003956 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003957
3958# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003959 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003960 WARN("UNNECESSARY_CASTS",
3961 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003962 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003963
Joe Perchesa640d252013-07-03 15:05:21 -07003964# alloc style
3965# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3966 if ($^V && $^V ge 5.10.0 &&
3967 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3968 CHK("ALLOC_SIZEOF_STRUCT",
3969 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3970 }
3971
Joe Perches972fdea2013-04-29 16:18:12 -07003972# check for krealloc arg reuse
3973 if ($^V && $^V ge 5.10.0 &&
3974 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3975 WARN("KREALLOC_ARG_REUSE",
3976 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3977 }
3978
Joe Perches5ce59ae2013-02-21 16:44:18 -08003979# check for alloc argument mismatch
3980 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3981 WARN("ALLOC_ARRAY_ARGS",
3982 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3983 }
3984
Joe Perchescaf2a542011-01-12 16:59:56 -08003985# check for multiple semicolons
3986 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003987 if (WARN("ONE_SEMICOLON",
3988 "Statements terminations use 1 semicolon\n" . $herecurr) &&
3989 $fix) {
3990 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
3991 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08003992 }
3993
3994# check for switch/default statements without a break;
3995 if ($^V && $^V ge 5.10.0 &&
3996 defined $stat &&
3997 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3998 my $ctx = '';
3999 my $herectx = $here . "\n";
4000 my $cnt = statement_rawlines($stat);
4001 for (my $n = 0; $n < $cnt; $n++) {
4002 $herectx .= raw_line($linenr, $n) . "\n";
4003 }
4004 WARN("DEFAULT_NO_BREAK",
4005 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004006 }
4007
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004008# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004009 if ($line =~ /\b__FUNCTION__\b/) {
4010 if (WARN("USE_FUNC",
4011 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4012 $fix) {
4013 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4014 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004015 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004016
Joe Perches2c924882012-03-23 15:02:20 -07004017# check for use of yield()
4018 if ($line =~ /\byield\s*\(\s*\)/) {
4019 WARN("YIELD",
4020 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4021 }
4022
Joe Perches179f8f42013-07-03 15:05:30 -07004023# check for comparisons against true and false
4024 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4025 my $lead = $1;
4026 my $arg = $2;
4027 my $test = $3;
4028 my $otype = $4;
4029 my $trail = $5;
4030 my $op = "!";
4031
4032 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4033
4034 my $type = lc($otype);
4035 if ($type =~ /^(?:true|false)$/) {
4036 if (("$test" eq "==" && "$type" eq "true") ||
4037 ("$test" eq "!=" && "$type" eq "false")) {
4038 $op = "";
4039 }
4040
4041 CHK("BOOL_COMPARISON",
4042 "Using comparison to $otype is error prone\n" . $herecurr);
4043
4044## maybe suggesting a correct construct would better
4045## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4046
4047 }
4048 }
4049
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004050# check for semaphores initialized locked
4051 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004052 WARN("CONSIDER_COMPLETION",
4053 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004054 }
Joe Perches6712d852012-03-23 15:02:20 -07004055
Joe Perches67d0a072011-10-31 17:13:10 -07004056# recommend kstrto* over simple_strto* and strict_strto*
4057 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004058 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004059 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004060 }
Joe Perches6712d852012-03-23 15:02:20 -07004061
Michael Ellermanf3db6632008-07-23 21:28:57 -07004062# check for __initcall(), use device_initcall() explicitly please
4063 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004064 WARN("USE_DEVICE_INITCALL",
4065 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07004066 }
Joe Perches6712d852012-03-23 15:02:20 -07004067
Emese Revfy79404842010-03-05 13:43:53 -08004068# check for various ops structs, ensure they are const.
4069 my $struct_ops = qr{acpi_dock_ops|
4070 address_space_operations|
4071 backlight_ops|
4072 block_device_operations|
4073 dentry_operations|
4074 dev_pm_ops|
4075 dma_map_ops|
4076 extent_io_ops|
4077 file_lock_operations|
4078 file_operations|
4079 hv_ops|
4080 ide_dma_ops|
4081 intel_dvo_dev_ops|
4082 item_operations|
4083 iwl_ops|
4084 kgdb_arch|
4085 kgdb_io|
4086 kset_uevent_ops|
4087 lock_manager_operations|
4088 microcode_ops|
4089 mtrr_ops|
4090 neigh_ops|
4091 nlmsvc_binding|
4092 pci_raw_ops|
4093 pipe_buf_operations|
4094 platform_hibernation_ops|
4095 platform_suspend_ops|
4096 proto_ops|
4097 rpc_pipe_ops|
4098 seq_operations|
4099 snd_ac97_build_ops|
4100 soc_pcmcia_socket_ops|
4101 stacktrace_ops|
4102 sysfs_ops|
4103 tty_operations|
4104 usb_mon_operations|
4105 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004106 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004107 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004108 WARN("CONST_STRUCT",
4109 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004110 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004111 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004112
4113# use of NR_CPUS is usually wrong
4114# ignore definitions of NR_CPUS and usage to define arrays as likely right
4115 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004116 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4117 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004118 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4119 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4120 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004121 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004122 WARN("NR_CPUS",
4123 "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 -07004124 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004125
4126# check for %L{u,d,i} in strings
4127 my $string;
4128 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4129 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004130 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004131 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004132 WARN("PRINTF_L",
4133 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004134 last;
4135 }
4136 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004137
4138# whine mightly about in_atomic
4139 if ($line =~ /\bin_atomic\s*\(/) {
4140 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004141 ERROR("IN_ATOMIC",
4142 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004143 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004144 WARN("IN_ATOMIC",
4145 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004146 }
4147 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004148
4149# check for lockdep_set_novalidate_class
4150 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4151 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4152 if ($realfile !~ m@^kernel/lockdep@ &&
4153 $realfile !~ m@^include/linux/lockdep@ &&
4154 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004155 ERROR("LOCKDEP",
4156 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004157 }
4158 }
Dave Jones88f88312011-01-12 16:59:59 -08004159
4160 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4161 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004162 WARN("EXPORTED_WORLD_WRITABLE",
4163 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004164 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004165 }
4166
4167 # If we have no input at all, then there is nothing to report on
4168 # so just keep quiet.
4169 if ($#rawlines == -1) {
4170 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004171 }
4172
Andy Whitcroft8905a672007-11-28 16:21:06 -08004173 # In mailback mode only produce a report in the negative, for
4174 # things that appear to be patches.
4175 if ($mailback && ($clean == 1 || !$is_patch)) {
4176 exit(0);
4177 }
4178
4179 # This is not a patch, and we are are in 'no-patch' mode so
4180 # just keep quiet.
4181 if (!$chk_patch && !$is_patch) {
4182 exit(0);
4183 }
4184
4185 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004186 ERROR("NOT_UNIFIED_DIFF",
4187 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004188 }
4189 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004190 ERROR("MISSING_SIGN_OFF",
4191 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004192 }
4193
Andy Whitcroft8905a672007-11-28 16:21:06 -08004194 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004195 if ($summary && !($clean == 1 && $quiet == 1)) {
4196 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004197 print "total: $cnt_error errors, $cnt_warn warnings, " .
4198 (($check)? "$cnt_chk checks, " : "") .
4199 "$cnt_lines lines checked\n";
4200 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004201 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004202
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004203 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004204
4205 if ($^V lt 5.10.0) {
4206 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4207 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4208 }
4209
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004210 # If there were whitespace errors which cleanpatch can fix
4211 # then suggest that.
4212 if ($rpt_cleaners) {
4213 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4214 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004215 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004216 }
4217 }
4218
Joe Perches91bfe482013-09-11 14:23:59 -07004219 hash_show_words(\%use_type, "Used");
4220 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07004221
Joe Perches3705ce52013-07-03 15:05:31 -07004222 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4223 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4224 my $linecount = 0;
4225 my $f;
4226
4227 open($f, '>', $newfile)
4228 or die "$P: Can't open $newfile for write\n";
4229 foreach my $fixed_line (@fixed) {
4230 $linecount++;
4231 if ($file) {
4232 if ($linecount > 3) {
4233 $fixed_line =~ s/^\+//;
4234 print $f $fixed_line. "\n";
4235 }
4236 } else {
4237 print $f $fixed_line . "\n";
4238 }
4239 }
4240 close($f);
4241
4242 if (!$quiet) {
4243 print << "EOM";
4244Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4245
4246Do _NOT_ trust the results written to this file.
4247Do _NOT_ submit these changes without inspecting them for correctness.
4248
4249This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4250No warranties, expressed or implied...
4251
4252EOM
4253 }
4254 }
4255
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004256 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004257 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004258 }
4259 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004260 print << "EOM";
4261$vname has style problems, please review.
4262
4263If any of these errors are false positives, please report
4264them to the maintainer, see CHECKPATCH in MAINTAINERS.
4265EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004266 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004267
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004268 return $clean;
4269}