blob: 70e7f7cdb452ee405b6d1b6213a8c3576b6336d3 [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;
9
10my $P = $0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -070011$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
Joe Perches000d1cc12011-07-25 17:13:25 -070013my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070014
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070029my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070030my $fix = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070031my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080032my %debug;
Joe Perches000d1cc12011-07-25 17:13:25 -070033my %ignore_type = ();
Joe Perches34456862013-07-03 15:05:34 -070034my %camelcase = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070035my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070036my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070037my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080038my $max_line_length = 80;
Hannes Eder77f5b102009-09-21 17:04:37 -070039
40sub help {
41 my ($exitcode) = @_;
42
43 print << "EOM";
44Usage: $P [OPTION]... [FILE]...
45Version: $V
46
47Options:
48 -q, --quiet quiet
49 --no-tree run without a kernel tree
50 --no-signoff do not check for 'Signed-off-by' line
51 --patch treat FILE as patchfile (default)
52 --emacs emacs compile window format
53 --terse one line per report
54 -f, --file treat FILE as regular source file
55 --subjective, --strict enable more subjective tests
Joe Perches000d1cc12011-07-25 17:13:25 -070056 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080057 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070058 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070059 --root=PATH PATH to the kernel tree root
60 --no-summary suppress the per-file summary
61 --mailback only produce a report in case of warnings/errors
62 --summary-file include the filename in summary
63 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
64 'values', 'possible', 'type', and 'attr' (default
65 is all off)
66 --test-only=WORD report only warnings/errors containing WORD
67 literally
Joe Perches3705ce52013-07-03 15:05:31 -070068 --fix EXPERIMENTAL - may create horrible results
69 If correctable single-line errors exist, create
70 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
71 with potential errors corrected to the preferred
72 checkpatch style
Hannes Eder77f5b102009-09-21 17:04:37 -070073 -h, --help, --version display this help and exit
74
75When FILE is - read standard input.
76EOM
77
78 exit($exitcode);
79}
80
Joe Perches000d1cc12011-07-25 17:13:25 -070081my $conf = which_conf($configuration_file);
82if (-f $conf) {
83 my @conf_args;
84 open(my $conffile, '<', "$conf")
85 or warn "$P: Can't find a readable $configuration_file file $!\n";
86
87 while (<$conffile>) {
88 my $line = $_;
89
90 $line =~ s/\s*\n?$//g;
91 $line =~ s/^\s*//g;
92 $line =~ s/\s+/ /g;
93
94 next if ($line =~ m/^\s*#/);
95 next if ($line =~ m/^\s*$/);
96
97 my @words = split(" ", $line);
98 foreach my $word (@words) {
99 last if ($word =~ m/^#/);
100 push (@conf_args, $word);
101 }
102 }
103 close($conffile);
104 unshift(@ARGV, @conf_args) if @conf_args;
105}
106
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700107GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700108 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700109 'tree!' => \$tree,
110 'signoff!' => \$chk_signoff,
111 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700112 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800113 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700114 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700115 'subjective!' => \$check,
116 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700117 'ignore=s' => \@ignore,
118 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800119 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700120 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800121 'summary!' => \$summary,
122 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800123 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700124 'fix!' => \$fix,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800125 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700126 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700127 'h|help' => \$help,
128 'version' => \$help
129) or help(1);
130
131help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700132
133my $exit = 0;
134
135if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700136 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700137 exit(1);
138}
139
Joe Perches000d1cc12011-07-25 17:13:25 -0700140@ignore = split(/,/, join(',',@ignore));
141foreach my $word (@ignore) {
142 $word =~ s/\s*\n?$//g;
143 $word =~ s/^\s*//g;
144 $word =~ s/\s+/ /g;
145 $word =~ tr/[a-z]/[A-Z]/;
146
147 next if ($word =~ m/^\s*#/);
148 next if ($word =~ m/^\s*$/);
149
150 $ignore_type{$word}++;
151}
152
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800153my $dbg_values = 0;
154my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700155my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700156my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800157for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800158 ## no critic
159 eval "\${dbg_$key} = '$debug{$key}';";
160 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800161}
162
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700163my $rpt_cleaners = 0;
164
Andy Whitcroft8905a672007-11-28 16:21:06 -0800165if ($terse) {
166 $emacs = 1;
167 $quiet++;
168}
169
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700170if ($tree) {
171 if (defined $root) {
172 if (!top_of_kernel_tree($root)) {
173 die "$P: $root: --root does not point at a valid tree\n";
174 }
175 } else {
176 if (top_of_kernel_tree('.')) {
177 $root = '.';
178 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
179 top_of_kernel_tree($1)) {
180 $root = $1;
181 }
182 }
183
184 if (!defined $root) {
185 print "Must be run from the top-level dir. of a kernel tree\n";
186 exit(2);
187 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700188}
189
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700190my $emitted_corrupt = 0;
191
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700192our $Ident = qr{
193 [A-Za-z_][A-Za-z\d_]*
194 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
195 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700196our $Storage = qr{extern|static|asmlinkage};
197our $Sparse = qr{
198 __user|
199 __kernel|
200 __force|
201 __iomem|
202 __must_check|
203 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800204 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700205 __ref|
206 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800208
209# Notes to $Attribute:
210# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700211our $Attribute = qr{
212 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700213 __percpu|
214 __nocast|
215 __safe|
216 __bitwise__|
217 __packed__|
218 __packed2__|
219 __naked|
220 __maybe_unused|
221 __always_unused|
222 __noreturn|
223 __used|
224 __cold|
225 __noclone|
226 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700227 __read_mostly|
228 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800229 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700230 ____cacheline_aligned|
231 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800232 ____cacheline_internodealigned_in_smp|
233 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700234 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700235our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700236our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700237our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
238our $Lval = qr{$Ident(?:$Member)*};
239
Joe Perches95e2c602013-07-03 15:05:20 -0700240our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
241our $Binary = qr{(?i)0b[01]+$Int_type?};
242our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
243our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800244our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
245our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
246our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800247our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700248our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800249our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800250our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700251our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700252our $Operators = qr{
253 <=|>=|==|!=|
254 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700255 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700256 }x;
257
Andy Whitcroft8905a672007-11-28 16:21:06 -0800258our $NonptrType;
259our $Type;
260our $Declare;
261
Joe Perches15662b32011-10-31 17:13:12 -0700262our $NON_ASCII_UTF8 = qr{
263 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700264 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
265 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
266 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
267 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
268 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
269 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
270}x;
271
Joe Perches15662b32011-10-31 17:13:12 -0700272our $UTF8 = qr{
273 [\x09\x0A\x0D\x20-\x7E] # ASCII
274 | $NON_ASCII_UTF8
275}x;
276
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700277our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700278 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700279 atomic_t
280)};
281
Joe Perches691e6692010-03-05 13:43:51 -0800282our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700283 printk(?:_ratelimited|_once|)|
284 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
285 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700286 panic|
287 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800288)};
289
Joe Perches20112472011-07-25 17:13:23 -0700290our $signature_tags = qr{(?xi:
291 Signed-off-by:|
292 Acked-by:|
293 Tested-by:|
294 Reviewed-by:|
295 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700296 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700297 To:|
298 Cc:
299)};
300
Andy Whitcroft8905a672007-11-28 16:21:06 -0800301our @typeList = (
302 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700303 qr{(?:unsigned\s+)?char},
304 qr{(?:unsigned\s+)?short},
305 qr{(?:unsigned\s+)?int},
306 qr{(?:unsigned\s+)?long},
307 qr{(?:unsigned\s+)?long\s+int},
308 qr{(?:unsigned\s+)?long\s+long},
309 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800310 qr{unsigned},
311 qr{float},
312 qr{double},
313 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314 qr{struct\s+$Ident},
315 qr{union\s+$Ident},
316 qr{enum\s+$Ident},
317 qr{${Ident}_t},
318 qr{${Ident}_handler},
319 qr{${Ident}_handler_fn},
320);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700321our @modifierList = (
322 qr{fastcall},
323);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800324
Wolfram Sang7840a942010-08-09 17:20:57 -0700325our $allowed_asm_includes = qr{(?x:
326 irq|
327 memory
328)};
329# memory.h: ARM has a custom one
330
Andy Whitcroft8905a672007-11-28 16:21:06 -0800331sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700332 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
333 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700334 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800335 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700336 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800337 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800338 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700339 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700340 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800341 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700342 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800343 }x;
344 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700345 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700346 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700347 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800348 }x;
349 $Declare = qr{(?:$Storage\s+)?$Type};
350}
351build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700352
Joe Perches7d2367a2011-07-25 17:13:22 -0700353our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700354
355# Using $balanced_parens, $LvalOrFunc, or $FuncArg
356# requires at least perl version v5.10.0
357# Any use must be runtime checked with $^V
358
359our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
360our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800361our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700362
363sub deparenthesize {
364 my ($string) = @_;
365 return "" if (!defined($string));
366 $string =~ s@^\s*\(\s*@@g;
367 $string =~ s@\s*\)\s*$@@g;
368 $string =~ s@\s+@ @g;
369 return $string;
370}
371
Joe Perches34456862013-07-03 15:05:34 -0700372sub seed_camelcase_file {
373 my ($file) = @_;
374
375 return if (!(-f $file));
376
377 local $/;
378
379 open(my $include_file, '<', "$file")
380 or warn "$P: Can't read '$file' $!\n";
381 my $text = <$include_file>;
382 close($include_file);
383
384 my @lines = split('\n', $text);
385
386 foreach my $line (@lines) {
387 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
388 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
389 $camelcase{$1} = 1;
390 }
391 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
392 $camelcase{$1} = 1;
393 }
394 }
395}
396
397my $camelcase_seeded = 0;
398sub seed_camelcase_includes {
399 return if ($camelcase_seeded);
400
401 my $files;
402 if (-d ".git") {
403 $files = `git ls-files include`;
404 } else {
405 $files = `find $root/include -name "*.h"`;
406 }
407 my @include_files = split('\n', $files);
408 foreach my $file (@include_files) {
409 seed_camelcase_file($file);
410 }
411 $camelcase_seeded = 1;
412}
413
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700414$chk_signoff = 0 if ($file);
415
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700416my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800417my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700418my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800419my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700420for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800421 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700422 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800423 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700424 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800425 } elsif ($filename eq '-') {
426 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700427 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800428 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700429 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700430 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800431 if ($filename eq '-') {
432 $vname = 'Your patch';
433 } else {
434 $vname = $filename;
435 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800436 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700437 chomp;
438 push(@rawlines, $_);
439 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800440 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800441 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700442 $exit = 1;
443 }
444 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800445 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700446 @fixed = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700447}
448
449exit($exit);
450
451sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700452 my ($root) = @_;
453
454 my @tree_check = (
455 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
456 "README", "Documentation", "arch", "include", "drivers",
457 "fs", "init", "ipc", "kernel", "lib", "scripts",
458 );
459
460 foreach my $check (@tree_check) {
461 if (! -e $root . '/' . $check) {
462 return 0;
463 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700464 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700465 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700466}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700467
Joe Perches20112472011-07-25 17:13:23 -0700468sub parse_email {
469 my ($formatted_email) = @_;
470
471 my $name = "";
472 my $address = "";
473 my $comment = "";
474
475 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
476 $name = $1;
477 $address = $2;
478 $comment = $3 if defined $3;
479 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
480 $address = $1;
481 $comment = $2 if defined $2;
482 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
483 $address = $1;
484 $comment = $2 if defined $2;
485 $formatted_email =~ s/$address.*$//;
486 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700487 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700488 $name =~ s/^\"|\"$//g;
489 # If there's a name left after stripping spaces and
490 # leading quotes, and the address doesn't have both
491 # leading and trailing angle brackets, the address
492 # is invalid. ie:
493 # "joe smith joe@smith.com" bad
494 # "joe smith <joe@smith.com" bad
495 if ($name ne "" && $address !~ /^<[^>]+>$/) {
496 $name = "";
497 $address = "";
498 $comment = "";
499 }
500 }
501
Joe Perches3705ce52013-07-03 15:05:31 -0700502 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700503 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700504 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700505 $address =~ s/^\<|\>$//g;
506
507 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
508 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
509 $name = "\"$name\"";
510 }
511
512 return ($name, $address, $comment);
513}
514
515sub format_email {
516 my ($name, $address) = @_;
517
518 my $formatted_email;
519
Joe Perches3705ce52013-07-03 15:05:31 -0700520 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700521 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700522 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700523
524 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
525 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
526 $name = "\"$name\"";
527 }
528
529 if ("$name" eq "") {
530 $formatted_email = "$address";
531 } else {
532 $formatted_email = "$name <$address>";
533 }
534
535 return $formatted_email;
536}
537
Joe Perches000d1cc12011-07-25 17:13:25 -0700538sub which_conf {
539 my ($conf) = @_;
540
541 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
542 if (-e "$path/$conf") {
543 return "$path/$conf";
544 }
545 }
546
547 return "";
548}
549
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700550sub expand_tabs {
551 my ($str) = @_;
552
553 my $res = '';
554 my $n = 0;
555 for my $c (split(//, $str)) {
556 if ($c eq "\t") {
557 $res .= ' ';
558 $n++;
559 for (; ($n % 8) != 0; $n++) {
560 $res .= ' ';
561 }
562 next;
563 }
564 $res .= $c;
565 $n++;
566 }
567
568 return $res;
569}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700570sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700571 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700572 return $res;
573}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700574
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700575sub line_stats {
576 my ($line) = @_;
577
578 # Drop the diff line leader and expand tabs
579 $line =~ s/^.//;
580 $line = expand_tabs($line);
581
582 # Pick the indent from the front of the line.
583 my ($white) = ($line =~ /^(\s*)/);
584
585 return (length($line), length($white));
586}
587
Andy Whitcroft773647a2008-03-28 14:15:58 -0700588my $sanitise_quote = '';
589
590sub sanitise_line_reset {
591 my ($in_comment) = @_;
592
593 if ($in_comment) {
594 $sanitise_quote = '*/';
595 } else {
596 $sanitise_quote = '';
597 }
598}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700599sub sanitise_line {
600 my ($line) = @_;
601
602 my $res = '';
603 my $l = '';
604
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800605 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700606 my $off = 0;
607 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700608
Andy Whitcroft773647a2008-03-28 14:15:58 -0700609 # Always copy over the diff marker.
610 $res = substr($line, 0, 1);
611
612 for ($off = 1; $off < length($line); $off++) {
613 $c = substr($line, $off, 1);
614
615 # Comments we are wacking completly including the begin
616 # and end, all to $;.
617 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
618 $sanitise_quote = '*/';
619
620 substr($res, $off, 2, "$;$;");
621 $off++;
622 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800623 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700624 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700625 $sanitise_quote = '';
626 substr($res, $off, 2, "$;$;");
627 $off++;
628 next;
629 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700630 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
631 $sanitise_quote = '//';
632
633 substr($res, $off, 2, $sanitise_quote);
634 $off++;
635 next;
636 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700637
638 # A \ in a string means ignore the next character.
639 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
640 $c eq "\\") {
641 substr($res, $off, 2, 'XX');
642 $off++;
643 next;
644 }
645 # Regular quotes.
646 if ($c eq "'" || $c eq '"') {
647 if ($sanitise_quote eq '') {
648 $sanitise_quote = $c;
649
650 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700651 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700652 } elsif ($sanitise_quote eq $c) {
653 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700654 }
655 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700656
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800657 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700658 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
659 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700660 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
661 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700662 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
663 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700664 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700665 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700666 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800667 }
668
Daniel Walker113f04a2009-09-21 17:04:35 -0700669 if ($sanitise_quote eq '//') {
670 $sanitise_quote = '';
671 }
672
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800673 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700674 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800675 my $clean = 'X' x length($1);
676 $res =~ s@\<.*\>@<$clean>@;
677
678 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700679 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800680 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700681 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800682 }
683
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700684 return $res;
685}
686
Joe Perchesa6962d72013-04-29 16:18:13 -0700687sub get_quoted_string {
688 my ($line, $rawline) = @_;
689
690 return "" if ($line !~ m/(\"[X]+\")/g);
691 return substr($rawline, $-[0], $+[0] - $-[0]);
692}
693
Andy Whitcroft8905a672007-11-28 16:21:06 -0800694sub ctx_statement_block {
695 my ($linenr, $remain, $off) = @_;
696 my $line = $linenr - 1;
697 my $blk = '';
698 my $soff = $off;
699 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700700 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800701
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800702 my $loff = 0;
703
Andy Whitcroft8905a672007-11-28 16:21:06 -0800704 my $type = '';
705 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800706 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800707 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800708 my $c;
709 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800710
711 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800712 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800713 @stack = (['', 0]) if ($#stack == -1);
714
Andy Whitcroft773647a2008-03-28 14:15:58 -0700715 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800716 # If we are about to drop off the end, pull in more
717 # context.
718 if ($off >= $len) {
719 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700720 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800721 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800722 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800723 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800724 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800725 $len = length($blk);
726 $line++;
727 last;
728 }
729 # Bail if there is no further context.
730 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800731 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800732 last;
733 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800734 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
735 $level++;
736 $type = '#';
737 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800738 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800739 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800740 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800741 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800742
Andy Whitcroft773647a2008-03-28 14:15:58 -0700743 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800744
745 # Handle nested #if/#else.
746 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
747 push(@stack, [ $type, $level ]);
748 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
749 ($type, $level) = @{$stack[$#stack - 1]};
750 } elsif ($remainder =~ /^#\s*endif\b/) {
751 ($type, $level) = @{pop(@stack)};
752 }
753
Andy Whitcroft8905a672007-11-28 16:21:06 -0800754 # Statement ends at the ';' or a close '}' at the
755 # outermost level.
756 if ($level == 0 && $c eq ';') {
757 last;
758 }
759
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800760 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700761 if ($level == 0 && $coff_set == 0 &&
762 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
763 $remainder =~ /^(else)(?:\s|{)/ &&
764 $remainder !~ /^else\s+if\b/) {
765 $coff = $off + length($1) - 1;
766 $coff_set = 1;
767 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
768 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800769 }
770
Andy Whitcroft8905a672007-11-28 16:21:06 -0800771 if (($type eq '' || $type eq '(') && $c eq '(') {
772 $level++;
773 $type = '(';
774 }
775 if ($type eq '(' && $c eq ')') {
776 $level--;
777 $type = ($level != 0)? '(' : '';
778
779 if ($level == 0 && $coff < $soff) {
780 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700781 $coff_set = 1;
782 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800783 }
784 }
785 if (($type eq '' || $type eq '{') && $c eq '{') {
786 $level++;
787 $type = '{';
788 }
789 if ($type eq '{' && $c eq '}') {
790 $level--;
791 $type = ($level != 0)? '{' : '';
792
793 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700794 if (substr($blk, $off + 1, 1) eq ';') {
795 $off++;
796 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800797 last;
798 }
799 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800800 # Preprocessor commands end at the newline unless escaped.
801 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
802 $level--;
803 $type = '';
804 $off++;
805 last;
806 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800807 $off++;
808 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700809 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800810 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700811 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800812 $line++;
813 $remain--;
814 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800815
816 my $statement = substr($blk, $soff, $off - $soff + 1);
817 my $condition = substr($blk, $soff, $coff - $soff + 1);
818
819 #warn "STATEMENT<$statement>\n";
820 #warn "CONDITION<$condition>\n";
821
Andy Whitcroft773647a2008-03-28 14:15:58 -0700822 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800823
824 return ($statement, $condition,
825 $line, $remain + 1, $off - $loff + 1, $level);
826}
827
Andy Whitcroftcf655042008-03-04 14:28:20 -0800828sub statement_lines {
829 my ($stmt) = @_;
830
831 # Strip the diff line prefixes and rip blank lines at start and end.
832 $stmt =~ s/(^|\n)./$1/g;
833 $stmt =~ s/^\s*//;
834 $stmt =~ s/\s*$//;
835
836 my @stmt_lines = ($stmt =~ /\n/g);
837
838 return $#stmt_lines + 2;
839}
840
841sub statement_rawlines {
842 my ($stmt) = @_;
843
844 my @stmt_lines = ($stmt =~ /\n/g);
845
846 return $#stmt_lines + 2;
847}
848
849sub statement_block_size {
850 my ($stmt) = @_;
851
852 $stmt =~ s/(^|\n)./$1/g;
853 $stmt =~ s/^\s*{//;
854 $stmt =~ s/}\s*$//;
855 $stmt =~ s/^\s*//;
856 $stmt =~ s/\s*$//;
857
858 my @stmt_lines = ($stmt =~ /\n/g);
859 my @stmt_statements = ($stmt =~ /;/g);
860
861 my $stmt_lines = $#stmt_lines + 2;
862 my $stmt_statements = $#stmt_statements + 1;
863
864 if ($stmt_lines > $stmt_statements) {
865 return $stmt_lines;
866 } else {
867 return $stmt_statements;
868 }
869}
870
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800871sub ctx_statement_full {
872 my ($linenr, $remain, $off) = @_;
873 my ($statement, $condition, $level);
874
875 my (@chunks);
876
Andy Whitcroftcf655042008-03-04 14:28:20 -0800877 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800878 ($statement, $condition, $linenr, $remain, $off, $level) =
879 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700880 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800881 push(@chunks, [ $condition, $statement ]);
882 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
883 return ($level, $linenr, @chunks);
884 }
885
886 # Pull in the following conditional/block pairs and see if they
887 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800888 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800889 ($statement, $condition, $linenr, $remain, $off, $level) =
890 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800891 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700892 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800893 #print "C: push\n";
894 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800895 }
896
897 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800898}
899
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700900sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700901 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700902 my $line;
903 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700904 my $blk = '';
905 my @o;
906 my @c;
907 my @res = ();
908
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700909 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800910 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700911 for ($line = $start; $remain > 0; $line++) {
912 next if ($rawlines[$line] =~ /^-/);
913 $remain--;
914
915 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800916
917 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700918 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800919 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700920 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800921 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700922 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800923 $level = pop(@stack);
924 }
925
Andy Whitcroft01464f32010-10-26 14:23:19 -0700926 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700927 ##print "C<$c>L<$level><$open$close>O<$off>\n";
928 if ($off > 0) {
929 $off--;
930 next;
931 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700932
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700933 if ($c eq $close && $level > 0) {
934 $level--;
935 last if ($level == 0);
936 } elsif ($c eq $open) {
937 $level++;
938 }
939 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700940
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700941 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700942 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700943 }
944
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700945 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700946 }
947
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700948 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700949}
950sub ctx_block_outer {
951 my ($linenr, $remain) = @_;
952
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700953 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
954 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700955}
956sub ctx_block {
957 my ($linenr, $remain) = @_;
958
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700959 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
960 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700961}
962sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700963 my ($linenr, $remain, $off) = @_;
964
965 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
966 return @r;
967}
968sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700969 my ($linenr, $remain) = @_;
970
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700971 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700972}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700973sub ctx_statement_level {
974 my ($linenr, $remain, $off) = @_;
975
976 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
977}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700978
979sub ctx_locate_comment {
980 my ($first_line, $end_line) = @_;
981
982 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700983 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700984 return $current_comment if (defined $current_comment);
985
986 # Look through the context and try and figure out if there is a
987 # comment.
988 my $in_comment = 0;
989 $current_comment = '';
990 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700991 my $line = $rawlines[$linenr - 1];
992 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700993 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
994 $in_comment = 1;
995 }
996 if ($line =~ m@/\*@) {
997 $in_comment = 1;
998 }
999 if (!$in_comment && $current_comment ne '') {
1000 $current_comment = '';
1001 }
1002 $current_comment .= $line . "\n" if ($in_comment);
1003 if ($line =~ m@\*/@) {
1004 $in_comment = 0;
1005 }
1006 }
1007
1008 chomp($current_comment);
1009 return($current_comment);
1010}
1011sub ctx_has_comment {
1012 my ($first_line, $end_line) = @_;
1013 my $cmt = ctx_locate_comment($first_line, $end_line);
1014
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001015 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001016 ##print "CMMT: $cmt\n";
1017
1018 return ($cmt ne '');
1019}
1020
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001021sub raw_line {
1022 my ($linenr, $cnt) = @_;
1023
1024 my $offset = $linenr - 1;
1025 $cnt++;
1026
1027 my $line;
1028 while ($cnt) {
1029 $line = $rawlines[$offset++];
1030 next if (defined($line) && $line =~ /^-/);
1031 $cnt--;
1032 }
1033
1034 return $line;
1035}
1036
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001037sub cat_vet {
1038 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001039 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001040
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001041 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001042 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1043 $res .= $1;
1044 if ($2 ne '') {
1045 $coded = sprintf("^%c", unpack('C', $2) + 64);
1046 $res .= $coded;
1047 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001048 }
1049 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001050
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001051 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001052}
1053
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001054my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001055my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001056my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001057my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001058
1059sub annotate_reset {
1060 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001061 $av_pending = '_';
1062 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001063 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001064}
1065
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001066sub annotate_values {
1067 my ($stream, $type) = @_;
1068
1069 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001070 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001071 my $cur = $stream;
1072
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001073 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001074
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001075 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001076 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001077 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001078 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001079 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001080 print "WS($1)\n" if ($dbg_values > 1);
1081 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001082 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001083 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001084 }
1085
Florian Micklerc023e4732011-01-12 16:59:58 -08001086 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001087 print "CAST($1)\n" if ($dbg_values > 1);
1088 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001089 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001090
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001091 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001092 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001093 $type = 'T';
1094
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001095 } elsif ($cur =~ /^($Modifier)\s*/) {
1096 print "MODIFIER($1)\n" if ($dbg_values > 1);
1097 $type = 'T';
1098
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001099 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001100 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001101 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001102 push(@av_paren_type, $type);
1103 if ($2 ne '') {
1104 $av_pending = 'N';
1105 }
1106 $type = 'E';
1107
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001108 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001109 print "UNDEF($1)\n" if ($dbg_values > 1);
1110 $av_preprocessor = 1;
1111 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001112
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001113 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001114 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001115 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001116
1117 push(@av_paren_type, $type);
1118 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001119 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001120
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001121 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001122 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1123 $av_preprocessor = 1;
1124
1125 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1126
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001127 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001128
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001129 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001130 print "PRE_END($1)\n" if ($dbg_values > 1);
1131
1132 $av_preprocessor = 1;
1133
1134 # Assume all arms of the conditional end as this
1135 # one does, and continue as if the #endif was not here.
1136 pop(@av_paren_type);
1137 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001138 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001139
1140 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001141 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001142
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001143 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1144 print "ATTR($1)\n" if ($dbg_values > 1);
1145 $av_pending = $type;
1146 $type = 'N';
1147
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001148 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001149 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001150 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001151 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001152 }
1153 $type = 'N';
1154
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001155 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001156 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001157 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001158 $type = 'N';
1159
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001160 } elsif ($cur =~/^(case)/o) {
1161 print "CASE($1)\n" if ($dbg_values > 1);
1162 $av_pend_colon = 'C';
1163 $type = 'N';
1164
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001165 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001166 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001167 $type = 'N';
1168
1169 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001170 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001171 push(@av_paren_type, $av_pending);
1172 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001173 $type = 'N';
1174
1175 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001176 my $new_type = pop(@av_paren_type);
1177 if ($new_type ne '_') {
1178 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001179 print "PAREN('$1') -> $type\n"
1180 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001181 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001182 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001183 }
1184
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001185 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001186 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001187 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001188 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001189
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001190 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1191 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001192 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001193 } elsif ($type eq 'E') {
1194 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001195 }
1196 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1197 $type = 'V';
1198
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001199 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001200 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001201 $type = 'V';
1202
1203 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001204 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001205 $type = 'N';
1206
Andy Whitcroftcf655042008-03-04 14:28:20 -08001207 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001208 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001209 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001210 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001211
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001212 } elsif ($cur =~/^(,)/) {
1213 print "COMMA($1)\n" if ($dbg_values > 1);
1214 $type = 'C';
1215
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001216 } elsif ($cur =~ /^(\?)/o) {
1217 print "QUESTION($1)\n" if ($dbg_values > 1);
1218 $type = 'N';
1219
1220 } elsif ($cur =~ /^(:)/o) {
1221 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1222
1223 substr($var, length($res), 1, $av_pend_colon);
1224 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1225 $type = 'E';
1226 } else {
1227 $type = 'N';
1228 }
1229 $av_pend_colon = 'O';
1230
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001231 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001232 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001233 $type = 'N';
1234
Andy Whitcroft0d413862008-10-15 22:02:16 -07001235 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001236 my $variant;
1237
1238 print "OPV($1)\n" if ($dbg_values > 1);
1239 if ($type eq 'V') {
1240 $variant = 'B';
1241 } else {
1242 $variant = 'U';
1243 }
1244
1245 substr($var, length($res), 1, $variant);
1246 $type = 'N';
1247
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001248 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001249 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001250 if ($1 ne '++' && $1 ne '--') {
1251 $type = 'N';
1252 }
1253
1254 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001255 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001256 }
1257 if (defined $1) {
1258 $cur = substr($cur, length($1));
1259 $res .= $type x length($1);
1260 }
1261 }
1262
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001263 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001264}
1265
Andy Whitcroft8905a672007-11-28 16:21:06 -08001266sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001267 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001268 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001269 ^(?:
1270 $Modifier|
1271 $Storage|
1272 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001273 DEFINE_\S+
1274 )$|
1275 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001276 goto|
1277 return|
1278 case|
1279 else|
1280 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001281 do|
1282 \#|
1283 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001284 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001285 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001286 )}x;
1287 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1288 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001289 # Check for modifiers.
1290 $possible =~ s/\s*$Storage\s*//g;
1291 $possible =~ s/\s*$Sparse\s*//g;
1292 if ($possible =~ /^\s*$/) {
1293
1294 } elsif ($possible =~ /\s/) {
1295 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001296 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001297 if ($modifier !~ $notPermitted) {
1298 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1299 push(@modifierList, $modifier);
1300 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001301 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001302
1303 } else {
1304 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1305 push(@typeList, $possible);
1306 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001307 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001308 } else {
1309 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001310 }
1311}
1312
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001313my $prefix = '';
1314
Joe Perches000d1cc12011-07-25 17:13:25 -07001315sub show_type {
1316 return !defined $ignore_type{$_[0]};
1317}
1318
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001319sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001320 if (!show_type($_[1]) ||
1321 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001322 return 0;
1323 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001324 my $line;
1325 if ($show_types) {
1326 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1327 } else {
1328 $line = "$prefix$_[0]: $_[2]\n";
1329 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001330 $line = (split('\n', $line))[0] . "\n" if ($terse);
1331
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001332 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001333
1334 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001335}
1336sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001337 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001338}
Joe Perches000d1cc12011-07-25 17:13:25 -07001339
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001340sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001341 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001342 our $clean = 0;
1343 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001344 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001345 }
Joe Perches3705ce52013-07-03 15:05:31 -07001346 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001347}
1348sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001349 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001350 our $clean = 0;
1351 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001352 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001353 }
Joe Perches3705ce52013-07-03 15:05:31 -07001354 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001355}
1356sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001357 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001358 our $clean = 0;
1359 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001360 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001361 }
Joe Perches3705ce52013-07-03 15:05:31 -07001362 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001363}
1364
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001365sub check_absolute_file {
1366 my ($absolute, $herecurr) = @_;
1367 my $file = $absolute;
1368
1369 ##print "absolute<$absolute>\n";
1370
1371 # See if any suffix of this path is a path within the tree.
1372 while ($file =~ s@^[^/]*/@@) {
1373 if (-f "$root/$file") {
1374 ##print "file<$file>\n";
1375 last;
1376 }
1377 }
1378 if (! -f _) {
1379 return 0;
1380 }
1381
1382 # It is, so see if the prefix is acceptable.
1383 my $prefix = $absolute;
1384 substr($prefix, -length($file)) = '';
1385
1386 ##print "prefix<$prefix>\n";
1387 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001388 WARN("USE_RELATIVE_PATH",
1389 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001390 }
1391}
1392
Joe Perches3705ce52013-07-03 15:05:31 -07001393sub trim {
1394 my ($string) = @_;
1395
1396 $string =~ s/(^\s+|\s+$)//g;
1397
1398 return $string;
1399}
1400
1401sub tabify {
1402 my ($leading) = @_;
1403
1404 my $source_indent = 8;
1405 my $max_spaces_before_tab = $source_indent - 1;
1406 my $spaces_to_tab = " " x $source_indent;
1407
1408 #convert leading spaces to tabs
1409 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1410 #Remove spaces before a tab
1411 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1412
1413 return "$leading";
1414}
1415
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001416sub pos_last_openparen {
1417 my ($line) = @_;
1418
1419 my $pos = 0;
1420
1421 my $opens = $line =~ tr/\(/\(/;
1422 my $closes = $line =~ tr/\)/\)/;
1423
1424 my $last_openparen = 0;
1425
1426 if (($opens == 0) || ($closes >= $opens)) {
1427 return -1;
1428 }
1429
1430 my $len = length($line);
1431
1432 for ($pos = 0; $pos < $len; $pos++) {
1433 my $string = substr($line, $pos);
1434 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1435 $pos += length($1) - 1;
1436 } elsif (substr($line, $pos, 1) eq '(') {
1437 $last_openparen = $pos;
1438 } elsif (index($string, '(') == -1) {
1439 last;
1440 }
1441 }
1442
1443 return $last_openparen + 1;
1444}
1445
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001446sub process {
1447 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001448
1449 my $linenr=0;
1450 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001451 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001452 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001453 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001454
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001455 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001456 my $indent;
1457 my $previndent=0;
1458 my $stashindent=0;
1459
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001460 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001461 my $signoff = 0;
1462 my $is_patch = 0;
1463
Joe Perches15662b32011-10-31 17:13:12 -07001464 my $in_header_lines = 1;
1465 my $in_commit_log = 0; #Scanning lines before patch
1466
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001467 my $non_utf8_charset = 0;
1468
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001469 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001470 our $cnt_lines = 0;
1471 our $cnt_error = 0;
1472 our $cnt_warn = 0;
1473 our $cnt_chk = 0;
1474
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001475 # Trace the real file/line as we go.
1476 my $realfile = '';
1477 my $realline = 0;
1478 my $realcnt = 0;
1479 my $here = '';
1480 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001481 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001482 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001483 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001484
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001485 my $prev_values = 'E';
1486
1487 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001488 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001489 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001490 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001491 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001492
Joe Perches323c1262012-12-17 16:02:07 -08001493
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001494 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001495 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001496 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001497 my @setup_docs = ();
1498 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001499
1500 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001501 my $line;
1502 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001503 $linenr++;
1504 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001505
Joe Perches3705ce52013-07-03 15:05:31 -07001506 push(@fixed, $rawline) if ($fix);
1507
Andy Whitcroft773647a2008-03-28 14:15:58 -07001508 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001509 $setup_docs = 0;
1510 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1511 $setup_docs = 1;
1512 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001513 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001514 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001515 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1516 $realline=$1-1;
1517 if (defined $2) {
1518 $realcnt=$3+1;
1519 } else {
1520 $realcnt=1+1;
1521 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001522 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001523
1524 # Guestimate if this is a continuing comment. Run
1525 # the context looking for a comment "edge". If this
1526 # edge is a close comment then we must be in a comment
1527 # at context start.
1528 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001529 my $cnt = $realcnt;
1530 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1531 next if (defined $rawlines[$ln - 1] &&
1532 $rawlines[$ln - 1] =~ /^-/);
1533 $cnt--;
1534 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001535 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001536 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1537 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1538 ($edge) = $1;
1539 last;
1540 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001541 }
1542 if (defined $edge && $edge eq '*/') {
1543 $in_comment = 1;
1544 }
1545
1546 # Guestimate if this is a continuing comment. If this
1547 # is the start of a diff block and this line starts
1548 # ' *' then it is very likely a comment.
1549 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001550 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001551 {
1552 $in_comment = 1;
1553 }
1554
1555 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1556 sanitise_line_reset($in_comment);
1557
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001558 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001559 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001560 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001561 $line = sanitise_line($rawline);
1562 }
1563 push(@lines, $line);
1564
1565 if ($realcnt > 1) {
1566 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1567 } else {
1568 $realcnt = 0;
1569 }
1570
1571 #print "==>$rawline\n";
1572 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001573
1574 if ($setup_docs && $line =~ /^\+/) {
1575 push(@setup_docs, $line);
1576 }
1577 }
1578
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001579 $prefix = '';
1580
Andy Whitcroft773647a2008-03-28 14:15:58 -07001581 $realcnt = 0;
1582 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001583 foreach my $line (@lines) {
1584 $linenr++;
1585
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001586 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001587
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001588#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001589 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001590 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001591 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001592 $realline=$1-1;
1593 if (defined $2) {
1594 $realcnt=$3+1;
1595 } else {
1596 $realcnt=1+1;
1597 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001598 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001599 $prev_values = 'E';
1600
Andy Whitcroft773647a2008-03-28 14:15:58 -07001601 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001602 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001603 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001604 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001605 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001606
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001607# track the line number as we move through the hunk, note that
1608# new versions of GNU diff omit the leading space on completely
1609# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001610 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001611 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001612 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001613
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001614 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001615 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001616
1617 # Track the previous line.
1618 ($prevline, $stashline) = ($stashline, $line);
1619 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001620 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1621
Andy Whitcroft773647a2008-03-28 14:15:58 -07001622 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001623
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001624 } elsif ($realcnt == 1) {
1625 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001626 }
1627
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001628 my $hunk_line = ($realcnt != 0);
1629
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001630#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001631 $prefix = "$filename:$realline: " if ($emacs && $file);
1632 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1633
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001634 $here = "#$linenr: " if (!$file);
1635 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001636
1637 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001638 if ($line =~ /^diff --git.*?(\S+)$/) {
1639 $realfile = $1;
1640 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001641 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001642 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001643 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001644 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001645 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001646
1647 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001648 if (!$file && $tree && $p1_prefix ne '' &&
1649 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001650 WARN("PATCH_PREFIX",
1651 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001652 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001653
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001654 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001655 ERROR("MODIFIED_INCLUDE_ASM",
1656 "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 -07001657 }
1658 next;
1659 }
1660
Randy Dunlap389834b2007-06-08 13:47:03 -07001661 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001662
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001663 my $hereline = "$here\n$rawline\n";
1664 my $herecurr = "$here\n$rawline\n";
1665 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001666
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001667 $cnt_lines++ if ($realcnt != 0);
1668
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001669# Check for incorrect file permissions
1670 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1671 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001672 if ($realfile !~ m@scripts/@ &&
1673 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001674 ERROR("EXECUTE_PERMISSIONS",
1675 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001676 }
1677 }
1678
Joe Perches20112472011-07-25 17:13:23 -07001679# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001680 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001681 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001682 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001683 }
1684
1685# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001686 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001687 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001688 my $space_before = $1;
1689 my $sign_off = $2;
1690 my $space_after = $3;
1691 my $email = $4;
1692 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1693
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001694 if ($sign_off !~ /$signature_tags/) {
1695 WARN("BAD_SIGN_OFF",
1696 "Non-standard signature: $sign_off\n" . $herecurr);
1697 }
Joe Perches20112472011-07-25 17:13:23 -07001698 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001699 if (WARN("BAD_SIGN_OFF",
1700 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1701 $fix) {
1702 $fixed[$linenr - 1] =
1703 "$ucfirst_sign_off $email";
1704 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001705 }
Joe Perches20112472011-07-25 17:13:23 -07001706 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001707 if (WARN("BAD_SIGN_OFF",
1708 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1709 $fix) {
1710 $fixed[$linenr - 1] =
1711 "$ucfirst_sign_off $email";
1712 }
1713
Joe Perches20112472011-07-25 17:13:23 -07001714 }
1715 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001716 if (WARN("BAD_SIGN_OFF",
1717 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1718 $fix) {
1719 $fixed[$linenr - 1] =
1720 "$ucfirst_sign_off $email";
1721 }
Joe Perches20112472011-07-25 17:13:23 -07001722 }
1723
1724 my ($email_name, $email_address, $comment) = parse_email($email);
1725 my $suggested_email = format_email(($email_name, $email_address));
1726 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001727 ERROR("BAD_SIGN_OFF",
1728 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001729 } else {
1730 my $dequoted = $suggested_email;
1731 $dequoted =~ s/^"//;
1732 $dequoted =~ s/" </ </;
1733 # Don't force email to have quotes
1734 # Allow just an angle bracketed address
1735 if ("$dequoted$comment" ne $email &&
1736 "<$email_address>$comment" ne $email &&
1737 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001738 WARN("BAD_SIGN_OFF",
1739 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001740 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001741 }
1742 }
1743
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001744# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001745 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001746 ERROR("CORRUPTED_PATCH",
1747 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001748 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001749 }
1750
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001751# Check for absolute kernel paths.
1752 if ($tree) {
1753 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1754 my $file = $1;
1755
1756 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1757 check_absolute_file($1, $herecurr)) {
1758 #
1759 } else {
1760 check_absolute_file($file, $herecurr);
1761 }
1762 }
1763 }
1764
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001765# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1766 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001767 $rawline !~ m/^$UTF8*$/) {
1768 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1769
1770 my $blank = copy_spacing($rawline);
1771 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1772 my $hereptr = "$hereline$ptr\n";
1773
Joe Perches34d99212011-07-25 17:13:26 -07001774 CHK("INVALID_UTF8",
1775 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001776 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001777
Joe Perches15662b32011-10-31 17:13:12 -07001778# Check if it's the start of a commit log
1779# (not a header line and we haven't seen the patch filename)
1780 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001781 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001782 $in_header_lines = 0;
1783 $in_commit_log = 1;
1784 }
1785
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001786# Check if there is UTF-8 in a commit log when a mail header has explicitly
1787# declined it, i.e defined some charset where it is missing.
1788 if ($in_header_lines &&
1789 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1790 $1 !~ /utf-8/i) {
1791 $non_utf8_charset = 1;
1792 }
1793
1794 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001795 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001796 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001797 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1798 }
1799
Andy Whitcroft306708542008-10-15 22:02:28 -07001800# ignore non-hunk lines and lines being removed
1801 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001802
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001803#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001804 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001805 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001806 ERROR("DOS_LINE_ENDINGS",
1807 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001808
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001809 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1810 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001811 if (ERROR("TRAILING_WHITESPACE",
1812 "trailing whitespace\n" . $herevet) &&
1813 $fix) {
1814 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1815 }
1816
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001817 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001818 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001819
Andi Kleen33549572010-05-24 14:33:29 -07001820# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001821# Only applies when adding the entry originally, after that we do not have
1822# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001823 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001824 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001825 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001826 my $cnt = $realcnt;
1827 my $ln = $linenr + 1;
1828 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001829 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001830 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001831 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001832 $f = $lines[$ln - 1];
1833 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1834 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001835
1836 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001837
1838 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1839 $is_start = 1;
1840 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1841 $length = -1;
1842 }
1843
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001844 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001845 $f =~ s/#.*//;
1846 $f =~ s/^\s+//;
1847 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001848 if ($f =~ /^\s*config\s/) {
1849 $is_end = 1;
1850 last;
1851 }
Andi Kleen33549572010-05-24 14:33:29 -07001852 $length++;
1853 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001854 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001855 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1856 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001857 }
1858
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001859# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1860 if ($realfile =~ /Kconfig/ &&
1861 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1862 WARN("CONFIG_EXPERIMENTAL",
1863 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1864 }
1865
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001866 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1867 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1868 my $flag = $1;
1869 my $replacement = {
1870 'EXTRA_AFLAGS' => 'asflags-y',
1871 'EXTRA_CFLAGS' => 'ccflags-y',
1872 'EXTRA_CPPFLAGS' => 'cppflags-y',
1873 'EXTRA_LDFLAGS' => 'ldflags-y',
1874 };
1875
1876 WARN("DEPRECATED_VARIABLE",
1877 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1878 }
1879
Andy Whitcroft5368df202008-10-15 22:02:27 -07001880# check we are in a valid source file if not then ignore this hunk
1881 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1882
Joe Perches6cd7f382012-12-17 16:01:54 -08001883#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001884 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001885 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001886 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001887 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001888 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001889 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001890 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001891 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001892 }
1893
Josh Triplettca56dc02012-03-23 15:02:21 -07001894# Check for user-visible strings broken across lines, which breaks the ability
1895# to grep for the string. Limited to strings used as parameters (those
1896# following an open parenthesis), which almost completely eliminates false
1897# positives, as well as warning only once per parameter rather than once per
1898# line of the string. Make an exception when the previous string ends in a
1899# newline (multiple lines in one string constant) or \n\t (common in inline
1900# assembly to indent the instruction on the following line).
1901 if ($line =~ /^\+\s*"/ &&
1902 $prevline =~ /"\s*$/ &&
1903 $prevline =~ /\(/ &&
1904 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1905 WARN("SPLIT_STRING",
1906 "quoted string split across lines\n" . $hereprev);
1907 }
1908
Joe Perches5e79d962010-03-05 13:43:55 -08001909# check for spaces before a quoted newline
1910 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07001911 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1912 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1913 $fix) {
1914 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1915 }
1916
Joe Perches5e79d962010-03-05 13:43:55 -08001917 }
1918
Andy Whitcroft8905a672007-11-28 16:21:06 -08001919# check for adding lines without a newline.
1920 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001921 WARN("MISSING_EOF_NEWLINE",
1922 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001923 }
1924
Mike Frysinger42e41c52009-09-21 17:04:40 -07001925# Blackfin: use hi/lo macros
1926 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1927 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1928 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001929 ERROR("LO_MACRO",
1930 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001931 }
1932 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1933 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001934 ERROR("HI_MACRO",
1935 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001936 }
1937 }
1938
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001939# check we are in a valid source file C or perl if not then ignore this hunk
1940 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001941
1942# at the beginning of a line any tabs must come first and anything
1943# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001944 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1945 $rawline =~ /^\+\s* \s*/) {
1946 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001947 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07001948 if (ERROR("CODE_INDENT",
1949 "code indent should use tabs where possible\n" . $herevet) &&
1950 $fix) {
1951 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
1952 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001953 }
1954
Alberto Panizzo08e44362010-03-05 13:43:54 -08001955# check for space before tabs.
1956 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1957 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001958 if (WARN("SPACE_BEFORE_TAB",
1959 "please, no space before tabs\n" . $herevet) &&
1960 $fix) {
1961 $fixed[$linenr - 1] =~
1962 s/(^\+.*) +\t/$1\t/;
1963 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08001964 }
1965
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001966# check for && or || at the start of a line
1967 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1968 CHK("LOGICAL_CONTINUATIONS",
1969 "Logical continuations should be on the previous line\n" . $hereprev);
1970 }
1971
1972# check multi-line statement indentation matches previous line
1973 if ($^V && $^V ge 5.10.0 &&
1974 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1975 $prevline =~ /^\+(\t*)(.*)$/;
1976 my $oldindent = $1;
1977 my $rest = $2;
1978
1979 my $pos = pos_last_openparen($rest);
1980 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07001981 $line =~ /^(\+| )([ \t]*)/;
1982 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001983
1984 my $goodtabindent = $oldindent .
1985 "\t" x ($pos / 8) .
1986 " " x ($pos % 8);
1987 my $goodspaceindent = $oldindent . " " x $pos;
1988
1989 if ($newindent ne $goodtabindent &&
1990 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07001991
1992 if (CHK("PARENTHESIS_ALIGNMENT",
1993 "Alignment should match open parenthesis\n" . $hereprev) &&
1994 $fix && $line =~ /^\+/) {
1995 $fixed[$linenr - 1] =~
1996 s/^\+[ \t]*/\+$goodtabindent/;
1997 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001998 }
1999 }
2000 }
2001
Joe Perches23f780c2013-07-03 15:05:31 -07002002 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002003 if (CHK("SPACING",
2004 "No space is necessary after a cast\n" . $hereprev) &&
2005 $fix) {
2006 $fixed[$linenr - 1] =~
2007 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2008 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002009 }
2010
Joe Perches05880602012-10-04 17:13:35 -07002011 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002012 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2013 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002014 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2015 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2016 }
2017
2018 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002019 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2020 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2021 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2022 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2023 "networking block comments start with * on subsequent lines\n" . $hereprev);
2024 }
2025
2026 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002027 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2028 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2029 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2030 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002031 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2032 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2033 }
2034
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002035# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002036# Exceptions:
2037# 1) within comments
2038# 2) indented preprocessor commands
2039# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002040 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002041 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002042 if (WARN("LEADING_SPACE",
2043 "please, no spaces at the start of a line\n" . $herevet) &&
2044 $fix) {
2045 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2046 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002047 }
2048
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002049# check we are in a valid C source file if not then ignore this hunk
2050 next if ($realfile !~ /\.(h|c)$/);
2051
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002052# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2053 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2054 WARN("CONFIG_EXPERIMENTAL",
2055 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2056 }
2057
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002058# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002059 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002060 WARN("CVS_KEYWORD",
2061 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002062 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002063
Mike Frysinger42e41c52009-09-21 17:04:40 -07002064# Blackfin: don't use __builtin_bfin_[cs]sync
2065 if ($line =~ /__builtin_bfin_csync/) {
2066 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002067 ERROR("CSYNC",
2068 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002069 }
2070 if ($line =~ /__builtin_bfin_ssync/) {
2071 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002072 ERROR("SSYNC",
2073 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002074 }
2075
Joe Perches56e77d72013-02-21 16:44:14 -08002076# check for old HOTPLUG __dev<foo> section markings
2077 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2078 WARN("HOTPLUG_SECTION",
2079 "Using $1 is unnecessary\n" . $herecurr);
2080 }
2081
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002082# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002083 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2084 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002085#print "LINE<$line>\n";
2086 if ($linenr >= $suppress_statement &&
2087 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002088 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002089 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002090 $stat =~ s/\n./\n /g;
2091 $cond =~ s/\n./\n /g;
2092
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002093#print "linenr<$linenr> <$stat>\n";
2094 # If this statement has no statement boundaries within
2095 # it there is no point in retrying a statement scan
2096 # until we hit end of it.
2097 my $frag = $stat; $frag =~ s/;+\s*$//;
2098 if ($frag !~ /(?:{|;)/) {
2099#print "skip<$line_nr_next>\n";
2100 $suppress_statement = $line_nr_next;
2101 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002102
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002103 # Find the real next line.
2104 $realline_next = $line_nr_next;
2105 if (defined $realline_next &&
2106 (!defined $lines[$realline_next - 1] ||
2107 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2108 $realline_next++;
2109 }
2110
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002111 my $s = $stat;
2112 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002113
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002114 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002115 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002116
2117 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002118 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002119
Andy Whitcroft463f2862009-09-21 17:04:34 -07002120 } elsif ($s =~ /^.\s*else\b/s) {
2121
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002122 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002123 } 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 -07002124 my $type = $1;
2125 $type =~ s/\s+/ /g;
2126 possible($type, "A:" . $s);
2127
Andy Whitcroft8905a672007-11-28 16:21:06 -08002128 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002129 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002130 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002131 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002132
2133 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002134 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002135 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002136 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002137
2138 # Check for any sort of function declaration.
2139 # int foo(something bar, other baz);
2140 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002141 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 -08002142 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002143
Andy Whitcroftcf655042008-03-04 14:28:20 -08002144 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002145 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002146 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002147
Andy Whitcroft8905a672007-11-28 16:21:06 -08002148 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002149 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002150
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002151 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002152 }
2153 }
2154 }
2155
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002156 }
2157
Andy Whitcroft653d4872007-06-23 17:16:34 -07002158#
2159# Checks which may be anchored in the context.
2160#
2161
2162# Check for switch () and associated case and default
2163# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002164 if ($line=~/\bswitch\s*\(.*\)/) {
2165 my $err = '';
2166 my $sep = '';
2167 my @ctx = ctx_block_outer($linenr, $realcnt);
2168 shift(@ctx);
2169 for my $ctx (@ctx) {
2170 my ($clen, $cindent) = line_stats($ctx);
2171 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2172 $indent != $cindent) {
2173 $err .= "$sep$ctx\n";
2174 $sep = '';
2175 } else {
2176 $sep = "[...]\n";
2177 }
2178 }
2179 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002180 ERROR("SWITCH_CASE_INDENT_LEVEL",
2181 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002182 }
2183 }
2184
2185# if/while/etc brace do not go on next line, unless defining a do while loop,
2186# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002187 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002188 my $pre_ctx = "$1$2";
2189
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002190 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002191
2192 if ($line =~ /^\+\t{6,}/) {
2193 WARN("DEEP_INDENTATION",
2194 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2195 }
2196
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002197 my $ctx_cnt = $realcnt - $#ctx - 1;
2198 my $ctx = join("\n", @ctx);
2199
Andy Whitcroft548596d2008-07-23 21:29:01 -07002200 my $ctx_ln = $linenr;
2201 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002202
Andy Whitcroft548596d2008-07-23 21:29:01 -07002203 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2204 defined $lines[$ctx_ln - 1] &&
2205 $lines[$ctx_ln - 1] =~ /^-/)) {
2206 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2207 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002208 $ctx_ln++;
2209 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002210
Andy Whitcroft53210162008-07-23 21:29:03 -07002211 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2212 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002213
2214 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002215 ERROR("OPEN_BRACE",
2216 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002217 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002218 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002219 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2220 $ctx =~ /\)\s*\;\s*$/ &&
2221 defined $lines[$ctx_ln - 1])
2222 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002223 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2224 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002225 WARN("TRAILING_SEMICOLON",
2226 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002227 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002228 }
2229 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002230 }
2231
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002232# Check relative indent for conditionals and blocks.
2233 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002234 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2235 ctx_statement_block($linenr, $realcnt, 0)
2236 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002237 my ($s, $c) = ($stat, $cond);
2238
2239 substr($s, 0, length($c), '');
2240
2241 # Make sure we remove the line prefixes as we have
2242 # none on the first line, and are going to readd them
2243 # where necessary.
2244 $s =~ s/\n./\n/gs;
2245
2246 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002247 my @newlines = ($c =~ /\n/gs);
2248 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002249
2250 # We want to check the first line inside the block
2251 # starting at the end of the conditional, so remove:
2252 # 1) any blank line termination
2253 # 2) any opening brace { on end of the line
2254 # 3) any do (...) {
2255 my $continuation = 0;
2256 my $check = 0;
2257 $s =~ s/^.*\bdo\b//;
2258 $s =~ s/^\s*{//;
2259 if ($s =~ s/^\s*\\//) {
2260 $continuation = 1;
2261 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002262 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002263 $check = 1;
2264 $cond_lines++;
2265 }
2266
2267 # Also ignore a loop construct at the end of a
2268 # preprocessor statement.
2269 if (($prevline =~ /^.\s*#\s*define\s/ ||
2270 $prevline =~ /\\\s*$/) && $continuation == 0) {
2271 $check = 0;
2272 }
2273
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002274 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002275 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002276 while ($cond_ptr != $cond_lines) {
2277 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002278
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002279 # If we see an #else/#elif then the code
2280 # is not linear.
2281 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2282 $check = 0;
2283 }
2284
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002285 # Ignore:
2286 # 1) blank lines, they should be at 0,
2287 # 2) preprocessor lines, and
2288 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002289 if ($continuation ||
2290 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002291 $s =~ /^\s*#\s*?/ ||
2292 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002293 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002294 if ($s =~ s/^.*?\n//) {
2295 $cond_lines++;
2296 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002297 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002298 }
2299
2300 my (undef, $sindent) = line_stats("+" . $s);
2301 my $stat_real = raw_line($linenr, $cond_lines);
2302
2303 # Check if either of these lines are modified, else
2304 # this is not this patch's fault.
2305 if (!defined($stat_real) ||
2306 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2307 $check = 0;
2308 }
2309 if (defined($stat_real) && $cond_lines > 1) {
2310 $stat_real = "[...]\n$stat_real";
2311 }
2312
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002313 #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 -07002314
2315 if ($check && (($sindent % 8) != 0 ||
2316 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002317 WARN("SUSPECT_CODE_INDENT",
2318 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002319 }
2320 }
2321
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002322 # Track the 'values' across context and added lines.
2323 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002324 my ($curr_values, $curr_vars) =
2325 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002326 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002327 if ($dbg_values) {
2328 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002329 print "$linenr > .$outline\n";
2330 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002331 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002332 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002333 $prev_values = substr($curr_values, -1);
2334
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002335#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002336 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002337
Andy Whitcroft653d4872007-06-23 17:16:34 -07002338# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002339 if ($dbg_type) {
2340 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002341 ERROR("TEST_TYPE",
2342 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002343 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002344 ERROR("TEST_NOT_TYPE",
2345 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002346 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002347 next;
2348 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002349# TEST: allow direct testing of the attribute matcher.
2350 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002351 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002352 ERROR("TEST_ATTR",
2353 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002354 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002355 ERROR("TEST_NOT_ATTR",
2356 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002357 }
2358 next;
2359 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002360
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002361# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002362 if ($line =~ /^.\s*{/ &&
2363 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002364 ERROR("OPEN_BRACE",
2365 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002366 }
2367
Andy Whitcroft653d4872007-06-23 17:16:34 -07002368#
2369# Checks which are anchored on the added line.
2370#
2371
2372# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002373 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002374 my $path = $1;
2375 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002376 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002377 "malformed #include filename\n" . $herecurr);
2378 }
2379 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2380 ERROR("UAPI_INCLUDE",
2381 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002382 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002383 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002384
2385# no C99 // comments
2386 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002387 if (ERROR("C99_COMMENTS",
2388 "do not use C99 // comments\n" . $herecurr) &&
2389 $fix) {
2390 my $line = $fixed[$linenr - 1];
2391 if ($line =~ /\/\/(.*)$/) {
2392 my $comment = trim($1);
2393 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2394 }
2395 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002396 }
2397 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002398 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002399 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002400
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002401# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2402# the whole statement.
2403#print "APW <$lines[$realline_next - 1]>\n";
2404 if (defined $realline_next &&
2405 exists $lines[$realline_next - 1] &&
2406 !defined $suppress_export{$realline_next} &&
2407 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2408 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002409 # Handle definitions which produce identifiers with
2410 # a prefix:
2411 # XXX(foo);
2412 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002413 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002414 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002415 $name =~ /^${Ident}_$2/) {
2416#print "FOO C name<$name>\n";
2417 $suppress_export{$realline_next} = 1;
2418
2419 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002420 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002421 ^.DEFINE_$Ident\(\Q$name\E\)|
2422 ^.DECLARE_$Ident\(\Q$name\E\)|
2423 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002424 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2425 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002426 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002427#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2428 $suppress_export{$realline_next} = 2;
2429 } else {
2430 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002431 }
2432 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002433 if (!defined $suppress_export{$linenr} &&
2434 $prevline =~ /^.\s*$/ &&
2435 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2436 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2437#print "FOO B <$lines[$linenr - 1]>\n";
2438 $suppress_export{$linenr} = 2;
2439 }
2440 if (defined $suppress_export{$linenr} &&
2441 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002442 WARN("EXPORT_SYMBOL",
2443 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002444 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002445
Joe Eloff5150bda2010-08-09 17:21:00 -07002446# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002447 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002448 ERROR("GLOBAL_INITIALISERS",
2449 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002450 $herecurr);
2451 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002452# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002453 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002454 ERROR("INITIALISED_STATIC",
2455 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002456 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002457 }
2458
Joe Perchescb710ec2010-10-26 14:23:20 -07002459# check for static const char * arrays.
2460 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002461 WARN("STATIC_CONST_CHAR_ARRAY",
2462 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002463 $herecurr);
2464 }
2465
2466# check for static char foo[] = "bar" declarations.
2467 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002468 WARN("STATIC_CONST_CHAR_ARRAY",
2469 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002470 $herecurr);
2471 }
2472
Joe Perches93ed0e22010-10-26 14:23:21 -07002473# check for declarations of struct pci_device_id
2474 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002475 WARN("DEFINE_PCI_DEVICE_TABLE",
2476 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002477 }
2478
Andy Whitcroft653d4872007-06-23 17:16:34 -07002479# check for new typedefs, only function parameters and sparse annotations
2480# make sense.
2481 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002482 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002483 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002484 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002485 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002486 WARN("NEW_TYPEDEFS",
2487 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002488 }
2489
2490# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002491 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002492 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2493 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002494 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002495
Andy Whitcroft65863862009-01-06 14:41:21 -08002496 # Should start with a space.
2497 $to =~ s/^(\S)/ $1/;
2498 # Should not end with a space.
2499 $to =~ s/\s+$//;
2500 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002501 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002502 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002503
Joe Perches3705ce52013-07-03 15:05:31 -07002504## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002505 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002506 if (ERROR("POINTER_LOCATION",
2507 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2508 $fix) {
2509 my $sub_from = $ident;
2510 my $sub_to = $ident;
2511 $sub_to =~ s/\Q$from\E/$to/;
2512 $fixed[$linenr - 1] =~
2513 s@\Q$sub_from\E@$sub_to@;
2514 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002515 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002516 }
2517 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2518 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002519 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002520
Andy Whitcroft65863862009-01-06 14:41:21 -08002521 # Should start with a space.
2522 $to =~ s/^(\S)/ $1/;
2523 # Should not end with a space.
2524 $to =~ s/\s+$//;
2525 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002526 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002527 }
2528 # Modifiers should have spaces.
2529 $to =~ s/(\b$Modifier$)/$1 /;
2530
Joe Perches3705ce52013-07-03 15:05:31 -07002531## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002532 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002533 if (ERROR("POINTER_LOCATION",
2534 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2535 $fix) {
2536
2537 my $sub_from = $match;
2538 my $sub_to = $match;
2539 $sub_to =~ s/\Q$from\E/$to/;
2540 $fixed[$linenr - 1] =~
2541 s@\Q$sub_from\E@$sub_to@;
2542 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002543 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002544 }
2545
2546# # no BUG() or BUG_ON()
2547# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2548# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2549# print "$herecurr";
2550# $clean = 0;
2551# }
2552
Andy Whitcroft8905a672007-11-28 16:21:06 -08002553 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002554 WARN("LINUX_VERSION_CODE",
2555 "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 -08002556 }
2557
Joe Perches17441222011-06-15 15:08:17 -07002558# check for uses of printk_ratelimit
2559 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002560 WARN("PRINTK_RATELIMITED",
2561"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002562 }
2563
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002564# printk should use KERN_* levels. Note that follow on printk's on the
2565# same line do not need a level, so we use the current block context
2566# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002567# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002568# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002569 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002570 my $ok = 0;
2571 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2572 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002573 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002574 # with "\n" ignore it, else it is to blame
2575 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2576 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2577 $ok = 1;
2578 }
2579 last;
2580 }
2581 }
2582 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002583 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2584 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002585 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002586 }
2587
Joe Perches243f3802012-05-31 16:26:09 -07002588 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2589 my $orig = $1;
2590 my $level = lc($orig);
2591 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002592 my $level2 = $level;
2593 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002594 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002595 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002596 }
2597
2598 if ($line =~ /\bpr_warning\s*\(/) {
2599 WARN("PREFER_PR_LEVEL",
2600 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2601 }
2602
Joe Perchesdc139312013-02-21 16:44:13 -08002603 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2604 my $orig = $1;
2605 my $level = lc($orig);
2606 $level = "warn" if ($level eq "warning");
2607 $level = "dbg" if ($level eq "debug");
2608 WARN("PREFER_DEV_LEVEL",
2609 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2610 }
2611
Andy Whitcroft653d4872007-06-23 17:16:34 -07002612# function brace can't be on same line, except for #defines of do while,
2613# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002614 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2615 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002616 ERROR("OPEN_BRACE",
2617 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002618 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002619
Andy Whitcroft8905a672007-11-28 16:21:06 -08002620# open braces for enum, union and struct go on the same line.
2621 if ($line =~ /^.\s*{/ &&
2622 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002623 ERROR("OPEN_BRACE",
2624 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002625 }
2626
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002627# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002628 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2629 if (WARN("SPACING",
2630 "missing space after $1 definition\n" . $herecurr) &&
2631 $fix) {
2632 $fixed[$linenr - 1] =~
2633 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2634 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002635 }
2636
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002637# check for spacing round square brackets; allowed:
2638# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002639# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2640# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002641 while ($line =~ /(.*?\s)\[/g) {
2642 my ($where, $prefix) = ($-[1], $1);
2643 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002644 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002645 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002646 if (ERROR("BRACKET_SPACE",
2647 "space prohibited before open square bracket '['\n" . $herecurr) &&
2648 $fix) {
2649 $fixed[$linenr - 1] =~
2650 s/^(\+.*?)\s+\[/$1\[/;
2651 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002652 }
2653 }
2654
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002655# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002656 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002657 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002658 my $ctx_before = substr($line, 0, $-[1]);
2659 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002660
2661 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002662 if ($name =~ /^(?:
2663 if|for|while|switch|return|case|
2664 volatile|__volatile__|
2665 __attribute__|format|__extension__|
2666 asm|__asm__)$/x)
2667 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002668 # cpp #define statements have non-optional spaces, ie
2669 # if there is a space between the name and the open
2670 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002671 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002672
2673 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002674 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002675
2676 # If this whole things ends with a type its most
2677 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002678 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002679
2680 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002681 if (WARN("SPACING",
2682 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2683 $fix) {
2684 $fixed[$linenr - 1] =~
2685 s/\b$name\s+\(/$name\(/;
2686 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002687 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002688 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002689
Andy Whitcroft653d4872007-06-23 17:16:34 -07002690# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002691 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002692 my $fixed_line = "";
2693 my $line_fixed = 0;
2694
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002695 my $ops = qr{
2696 <<=|>>=|<=|>=|==|!=|
2697 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2698 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002699 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2700 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002701 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002702 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002703
2704## print("element count: <" . $#elements . ">\n");
2705## foreach my $el (@elements) {
2706## print("el: <$el>\n");
2707## }
2708
2709 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002710 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002711
Joe Perches3705ce52013-07-03 15:05:31 -07002712 foreach my $el (@elements) {
2713 push(@fix_elements, substr($rawline, $off, length($el)));
2714 $off += length($el);
2715 }
2716
2717 $off = 0;
2718
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002719 my $blank = copy_spacing($opline);
2720
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002721 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002722
2723 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2724
2725## print("n: <$n> good: <$good>\n");
2726
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002727 $off += length($elements[$n]);
2728
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002729 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002730 my $ca = substr($opline, 0, $off);
2731 my $cc = '';
2732 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2733 $cc = substr($opline, $off + length($elements[$n + 1]));
2734 }
2735 my $cb = "$ca$;$cc";
2736
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002737 my $a = '';
2738 $a = 'V' if ($elements[$n] ne '');
2739 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002740 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002741 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2742 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002743 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002744
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002745 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002746
2747 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002748 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002749 $c = 'V' if ($elements[$n + 2] ne '');
2750 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002751 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002752 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2753 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002754 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002755 } else {
2756 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002757 }
2758
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002759 my $ctx = "${a}x${c}";
2760
2761 my $at = "(ctx:$ctx)";
2762
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002763 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002764 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002765
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002766 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002767 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002768
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002769 # Get the full operator variant.
2770 my $opv = $op . substr($curr_vars, $off, 1);
2771
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002772 # Ignore operators passed as parameters.
2773 if ($op_type ne 'V' &&
2774 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2775
Andy Whitcroftcf655042008-03-04 14:28:20 -08002776# # Ignore comments
2777# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002778
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002779 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002780 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002781 if ($ctx !~ /.x[WEBC]/ &&
2782 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002783 if (ERROR("SPACING",
2784 "space required after that '$op' $at\n" . $hereptr)) {
2785 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2786 $line_fixed = 1;
2787 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002788 }
2789
2790 # // is a comment
2791 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002792
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002793 # No spaces for:
2794 # ->
2795 # : when part of a bitfield
2796 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002797 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002798 if (ERROR("SPACING",
2799 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2800 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2801 $line_fixed = 1;
2802 if (defined $fix_elements[$n + 2]) {
2803 $fix_elements[$n + 2] =~ s/^\s+//;
2804 }
2805 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002806 }
2807
2808 # , must have a space on the right.
2809 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002810 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002811 if (ERROR("SPACING",
2812 "space required after that '$op' $at\n" . $hereptr)) {
2813 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2814 $line_fixed = 1;
2815 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002816 }
2817
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002818 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002819 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002820 #warn "'*' is part of type\n";
2821
2822 # unary operators should have a space before and
2823 # none after. May be left adjacent to another
2824 # unary operator, or a cast
2825 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002826 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002827 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002828 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002829 if (ERROR("SPACING",
2830 "space required before that '$op' $at\n" . $hereptr)) {
2831 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2832 $line_fixed = 1;
2833 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002834 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002835 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002836 # A unary '*' may be const
2837
2838 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002839 if (ERROR("SPACING",
2840 "space prohibited after that '$op' $at\n" . $hereptr)) {
2841 $fixed_line =~ s/\s+$//;
2842 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2843 $line_fixed = 1;
2844 if (defined $fix_elements[$n + 2]) {
2845 $fix_elements[$n + 2] =~ s/^\s+//;
2846 }
2847 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002848 }
2849
2850 # unary ++ and unary -- are allowed no space on one side.
2851 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002852 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002853 if (ERROR("SPACING",
2854 "space required one side of that '$op' $at\n" . $hereptr)) {
2855 $fixed_line =~ s/\s+$//;
2856 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2857 $line_fixed = 1;
2858 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002859 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002860 if ($ctx =~ /Wx[BE]/ ||
2861 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002862 if (ERROR("SPACING",
2863 "space prohibited before that '$op' $at\n" . $hereptr)) {
2864 $fixed_line =~ s/\s+$//;
2865 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2866 $line_fixed = 1;
2867 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002868 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002869 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002870 if (ERROR("SPACING",
2871 "space prohibited after that '$op' $at\n" . $hereptr)) {
2872 $fixed_line =~ s/\s+$//;
2873 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2874 $line_fixed = 1;
2875 if (defined $fix_elements[$n + 2]) {
2876 $fix_elements[$n + 2] =~ s/^\s+//;
2877 }
2878 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002879 }
2880
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002881 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002882 } elsif ($op eq '<<' or $op eq '>>' or
2883 $op eq '&' or $op eq '^' or $op eq '|' or
2884 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002885 $op eq '*' or $op eq '/' or
2886 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002887 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002888 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002889 if (ERROR("SPACING",
2890 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2891 $fixed_line =~ s/\s+$//;
2892 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2893 $line_fixed = 1;
2894 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002895 }
2896
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002897 # A colon needs no spaces before when it is
2898 # terminating a case value or a label.
2899 } elsif ($opv eq ':C' || $opv eq ':L') {
2900 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07002901 if (ERROR("SPACING",
2902 "space prohibited before that '$op' $at\n" . $hereptr)) {
2903 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2904 $line_fixed = 1;
2905 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002906 }
2907
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002908 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002909 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002910 my $ok = 0;
2911
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002912 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002913 if (($op eq '<' &&
2914 $cc =~ /^\S+\@\S+>/) ||
2915 ($op eq '>' &&
2916 $ca =~ /<\S+\@\S+$/))
2917 {
2918 $ok = 1;
2919 }
2920
2921 # Ignore ?:
2922 if (($opv eq ':O' && $ca =~ /\?$/) ||
2923 ($op eq '?' && $cc =~ /^:/)) {
2924 $ok = 1;
2925 }
2926
2927 if ($ok == 0) {
Joe Perches3705ce52013-07-03 15:05:31 -07002928 if (ERROR("SPACING",
2929 "spaces required around that '$op' $at\n" . $hereptr)) {
2930 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2931 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
2932 $line_fixed = 1;
2933 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002934 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002935 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002936 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002937
2938## print("n: <$n> GOOD: <$good>\n");
2939
2940 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002941 }
Joe Perches3705ce52013-07-03 15:05:31 -07002942
2943 if (($#elements % 2) == 0) {
2944 $fixed_line = $fixed_line . $fix_elements[$#elements];
2945 }
2946
2947 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2948 $fixed[$linenr - 1] = $fixed_line;
2949 }
2950
2951
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002952 }
2953
Joe Perches786b6322013-07-03 15:05:32 -07002954# check for whitespace before a non-naked semicolon
2955 if ($line =~ /^\+.*\S\s+;/) {
2956 if (WARN("SPACING",
2957 "space prohibited before semicolon\n" . $herecurr) &&
2958 $fix) {
2959 1 while $fixed[$linenr - 1] =~
2960 s/^(\+.*\S)\s+;/$1;/;
2961 }
2962 }
2963
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002964# check for multiple assignments
2965 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002966 CHK("MULTIPLE_ASSIGNMENTS",
2967 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002968 }
2969
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002970## # check for multiple declarations, allowing for a function declaration
2971## # continuation.
2972## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2973## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2974##
2975## # Remove any bracketed sections to ensure we do not
2976## # falsly report the parameters of functions.
2977## my $ln = $line;
2978## while ($ln =~ s/\([^\(\)]*\)//g) {
2979## }
2980## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002981## WARN("MULTIPLE_DECLARATION",
2982## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002983## }
2984## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002985
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002986#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002987 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2988 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002989 if (ERROR("SPACING",
2990 "space required before the open brace '{'\n" . $herecurr) &&
2991 $fix) {
2992 $fixed[$linenr - 1] =~
2993 s/^(\+.*(?:do|\))){/$1 {/;
2994 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002995 }
2996
Joe Perchesc4a62ef2013-07-03 15:05:28 -07002997## # check for blank lines before declarations
2998## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
2999## $prevrawline =~ /^.\s*$/) {
3000## WARN("SPACING",
3001## "No blank lines before declarations\n" . $hereprev);
3002## }
3003##
3004
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003005# closing brace should have a space following it when it has anything
3006# on the line
3007 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003008 ERROR("SPACING",
3009 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003010 }
3011
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003012# check spacing on square brackets
3013 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003014 if (ERROR("SPACING",
3015 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3016 $fix) {
3017 $fixed[$linenr - 1] =~
3018 s/\[\s+/\[/;
3019 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003020 }
3021 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003022 if (ERROR("SPACING",
3023 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3024 $fix) {
3025 $fixed[$linenr - 1] =~
3026 s/\s+\]/\]/;
3027 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003028 }
3029
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003030# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003031 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3032 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003033 if (ERROR("SPACING",
3034 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3035 $fix) {
3036 $fixed[$linenr - 1] =~
3037 s/\(\s+/\(/;
3038 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003039 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003040 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003041 $line !~ /for\s*\(.*;\s+\)/ &&
3042 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003043 if (ERROR("SPACING",
3044 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3045 $fix) {
3046 $fixed[$linenr - 1] =~
3047 s/\s+\)/\)/;
3048 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003049 }
3050
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003051#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003052 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003053 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003054 if (WARN("INDENTED_LABEL",
3055 "labels should not be indented\n" . $herecurr) &&
3056 $fix) {
3057 $fixed[$linenr - 1] =~
3058 s/^(.)\s+/$1/;
3059 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003060 }
3061
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003062# Return is not a function.
3063 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3064 my $spacing = $1;
3065 my $value = $2;
3066
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003067 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003068 $value =~ s/\(/ \(/g;
3069 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003070 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003071 $value !~ /(?:$Ident|-?$Constant)\s*
3072 $Compare\s*
3073 (?:$Ident|-?$Constant)/x &&
3074 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003075 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003076#print "value<$value>\n";
3077 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003078 ERROR("RETURN_PARENTHESES",
3079 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003080
3081 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003082 ERROR("SPACING",
3083 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003084 }
3085 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003086# Return of what appears to be an errno should normally be -'ve
3087 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3088 my $name = $1;
3089 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003090 WARN("USE_NEGATIVE_ERRNO",
3091 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003092 }
3093 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003094
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003095# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003096 if ($line =~ /\b(if|while|for|switch)\(/) {
3097 if (ERROR("SPACING",
3098 "space required before the open parenthesis '('\n" . $herecurr) &&
3099 $fix) {
3100 $fixed[$linenr - 1] =~
3101 s/\b(if|while|for|switch)\(/$1 \(/;
3102 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003103 }
3104
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003105# Check for illegal assignment in if conditional -- and check for trailing
3106# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003107 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003108 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3109 ctx_statement_block($linenr, $realcnt, 0)
3110 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003111 my ($stat_next) = ctx_statement_block($line_nr_next,
3112 $remain_next, $off_next);
3113 $stat_next =~ s/\n./\n /g;
3114 ##print "stat<$stat> stat_next<$stat_next>\n";
3115
3116 if ($stat_next =~ /^\s*while\b/) {
3117 # If the statement carries leading newlines,
3118 # then count those as offsets.
3119 my ($whitespace) =
3120 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3121 my $offset =
3122 statement_rawlines($whitespace) - 1;
3123
3124 $suppress_whiletrailers{$line_nr_next +
3125 $offset} = 1;
3126 }
3127 }
3128 if (!defined $suppress_whiletrailers{$linenr} &&
3129 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003130 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003131
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003132 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003133 ERROR("ASSIGN_IN_IF",
3134 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003135 }
3136
3137 # Find out what is on the end of the line after the
3138 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003139 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003140 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003141 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003142 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3143 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003144 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003145 # Find out how long the conditional actually is.
3146 my @newlines = ($c =~ /\n/gs);
3147 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003148 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003149
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003150 $stat_real = raw_line($linenr, $cond_lines)
3151 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003152 if (defined($stat_real) && $cond_lines > 1) {
3153 $stat_real = "[...]\n$stat_real";
3154 }
3155
Joe Perches000d1cc12011-07-25 17:13:25 -07003156 ERROR("TRAILING_STATEMENTS",
3157 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003158 }
3159 }
3160
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003161# Check for bitwise tests written as boolean
3162 if ($line =~ /
3163 (?:
3164 (?:\[|\(|\&\&|\|\|)
3165 \s*0[xX][0-9]+\s*
3166 (?:\&\&|\|\|)
3167 |
3168 (?:\&\&|\|\|)
3169 \s*0[xX][0-9]+\s*
3170 (?:\&\&|\|\||\)|\])
3171 )/x)
3172 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003173 WARN("HEXADECIMAL_BOOLEAN_TEST",
3174 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003175 }
3176
Andy Whitcroft8905a672007-11-28 16:21:06 -08003177# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003178 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3179 my $s = $1;
3180 $s =~ s/$;//g; # Remove any comments
3181 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003182 ERROR("TRAILING_STATEMENTS",
3183 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003184 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003185 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003186# if should not continue a brace
3187 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003188 ERROR("TRAILING_STATEMENTS",
3189 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003190 $herecurr);
3191 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003192# case and default should not have general statements after them
3193 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3194 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003195 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003196 \s*return\s+
3197 )/xg)
3198 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003199 ERROR("TRAILING_STATEMENTS",
3200 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003201 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003202
3203 # Check for }<nl>else {, these must be at the same
3204 # indent level to be relevant to each other.
3205 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3206 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003207 ERROR("ELSE_AFTER_BRACE",
3208 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003209 }
3210
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003211 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3212 $previndent == $indent) {
3213 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3214
3215 # Find out what is on the end of the line after the
3216 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003217 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003218 $s =~ s/\n.*//g;
3219
3220 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003221 ERROR("WHILE_AFTER_BRACE",
3222 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003223 }
3224 }
3225
Joe Perches95e2c602013-07-03 15:05:20 -07003226#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003227 while ($line =~ m{($Constant|$Lval)}g) {
3228 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003229
3230#gcc binary extension
3231 if ($var =~ /^$Binary$/) {
3232 WARN("GCC_BINARY_CONSTANT",
3233 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
3234 }
3235
3236#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003237 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003238 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003239#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003240 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003241#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003242 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3243 seed_camelcase_includes() if ($check);
3244 if (!defined $camelcase{$var}) {
3245 $camelcase{$var} = 1;
3246 CHK("CAMELCASE",
3247 "Avoid CamelCase: <$var>\n" . $herecurr);
3248 }
Joe Perches323c1262012-12-17 16:02:07 -08003249 }
3250 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003251
3252#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003253 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003254 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3255 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003256 }
3257
Andy Whitcroft653d4872007-06-23 17:16:34 -07003258#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003259 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003260 my $file = "$1.h";
3261 my $checkfile = "include/linux/$file";
3262 if (-f "$root/$checkfile" &&
3263 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003264 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003265 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003266 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003267 CHK("ARCH_INCLUDE_LINUX",
3268 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003269 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003270 WARN("INCLUDE_LINUX",
3271 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003272 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003273 }
3274 }
3275
Andy Whitcroft653d4872007-06-23 17:16:34 -07003276# multi-statement macros should be enclosed in a do while loop, grab the
3277# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003278# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07003279 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3280 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003281 my $ln = $linenr;
3282 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003283 my ($off, $dstat, $dcond, $rest);
3284 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003285 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003286 ctx_statement_block($linenr, $realcnt, 0);
3287 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003288 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003289 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003290
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003291 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003292 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003293 $dstat =~ s/\\\n.//g;
3294 $dstat =~ s/^\s*//s;
3295 $dstat =~ s/\s*$//s;
3296
3297 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003298 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3299 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003300 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003301 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003302 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003303
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003304 # Flatten any obvious string concatentation.
3305 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3306 $dstat =~ s/$Ident\s*("X*")/$1/)
3307 {
3308 }
3309
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003310 my $exceptions = qr{
3311 $Declare|
3312 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003313 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003314 DECLARE_PER_CPU|
3315 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003316 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003317 union|
3318 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003319 \.$Ident\s*=\s*|
3320 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003321 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003322 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003323 if ($dstat ne '' &&
3324 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3325 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003326 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003327 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003328 $dstat !~ /$exceptions/ &&
3329 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003330 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003331 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003332 $dstat !~ /^for\s*$Constant$/ && # for (...)
3333 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3334 $dstat !~ /^do\s*{/ && # do {...
3335 $dstat !~ /^\({/) # ({...
3336 {
3337 $ctx =~ s/\n*$//;
3338 my $herectx = $here . "\n";
3339 my $cnt = statement_rawlines($ctx);
3340
3341 for (my $n = 0; $n < $cnt; $n++) {
3342 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003343 }
3344
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003345 if ($dstat =~ /;/) {
3346 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3347 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3348 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003349 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003350 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003351 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003352 }
Joe Perches5023d342012-12-17 16:01:47 -08003353
Joe Perches481eb482012-12-17 16:01:56 -08003354# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003355
3356 } else {
3357 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003358 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3359 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003360 $line =~ /^\+.*\\$/) {
3361 WARN("LINE_CONTINUATIONS",
3362 "Avoid unnecessary line continuations\n" . $herecurr);
3363 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003364 }
3365
Joe Perchesb13edf72012-07-30 14:41:24 -07003366# do {} while (0) macro tests:
3367# single-statement macros do not need to be enclosed in do while (0) loop,
3368# macro should not end with a semicolon
3369 if ($^V && $^V ge 5.10.0 &&
3370 $realfile !~ m@/vmlinux.lds.h$@ &&
3371 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3372 my $ln = $linenr;
3373 my $cnt = $realcnt;
3374 my ($off, $dstat, $dcond, $rest);
3375 my $ctx = '';
3376 ($dstat, $dcond, $ln, $cnt, $off) =
3377 ctx_statement_block($linenr, $realcnt, 0);
3378 $ctx = $dstat;
3379
3380 $dstat =~ s/\\\n.//g;
3381
3382 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3383 my $stmts = $2;
3384 my $semis = $3;
3385
3386 $ctx =~ s/\n*$//;
3387 my $cnt = statement_rawlines($ctx);
3388 my $herectx = $here . "\n";
3389
3390 for (my $n = 0; $n < $cnt; $n++) {
3391 $herectx .= raw_line($linenr, $n) . "\n";
3392 }
3393
Joe Perchesac8e97f2012-08-21 16:15:53 -07003394 if (($stmts =~ tr/;/;/) == 1 &&
3395 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003396 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3397 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3398 }
3399 if (defined $semis && $semis ne "") {
3400 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3401 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3402 }
3403 }
3404 }
3405
Mike Frysinger080ba922009-01-06 14:41:25 -08003406# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3407# all assignments may have only one of the following with an assignment:
3408# .
3409# ALIGN(...)
3410# VMLINUX_SYMBOL(...)
3411 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003412 WARN("MISSING_VMLINUX_SYMBOL",
3413 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003414 }
3415
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003416# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003417 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3418 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003419 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003420 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003421 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3422 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003423 my @allowed = ();
3424 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003425 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003426 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003427 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003428 for my $chunk (@chunks) {
3429 my ($cond, $block) = @{$chunk};
3430
Andy Whitcroft773647a2008-03-28 14:15:58 -07003431 # If the condition carries leading newlines, then count those as offsets.
3432 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3433 my $offset = statement_rawlines($whitespace) - 1;
3434
Joe Perchesaad4f612012-03-23 15:02:19 -07003435 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003436 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3437
3438 # We have looked at and allowed this specific line.
3439 $suppress_ifbraces{$ln + $offset} = 1;
3440
3441 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003442 $ln += statement_rawlines($block) - 1;
3443
Andy Whitcroft773647a2008-03-28 14:15:58 -07003444 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003445
3446 $seen++ if ($block =~ /^\s*{/);
3447
Joe Perchesaad4f612012-03-23 15:02:19 -07003448 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003449 if (statement_lines($cond) > 1) {
3450 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003451 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003452 }
3453 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003454 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003455 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003456 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003457 if (statement_block_size($block) > 1) {
3458 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003459 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003460 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003461 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003462 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003463 if ($seen) {
3464 my $sum_allowed = 0;
3465 foreach (@allowed) {
3466 $sum_allowed += $_;
3467 }
3468 if ($sum_allowed == 0) {
3469 WARN("BRACES",
3470 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3471 } elsif ($sum_allowed != $allow &&
3472 $seen != $allow) {
3473 CHK("BRACES",
3474 "braces {} should be used on all arms of this statement\n" . $herectx);
3475 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003476 }
3477 }
3478 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003479 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003480 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003481 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003482
Andy Whitcroftcf655042008-03-04 14:28:20 -08003483 # Check the pre-context.
3484 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3485 #print "APW: ALLOWED: pre<$1>\n";
3486 $allowed = 1;
3487 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003488
3489 my ($level, $endln, @chunks) =
3490 ctx_statement_full($linenr, $realcnt, $-[0]);
3491
Andy Whitcroftcf655042008-03-04 14:28:20 -08003492 # Check the condition.
3493 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003494 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003495 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003496 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003497 }
3498 if (statement_lines($cond) > 1) {
3499 #print "APW: ALLOWED: cond<$cond>\n";
3500 $allowed = 1;
3501 }
3502 if ($block =~/\b(?:if|for|while)\b/) {
3503 #print "APW: ALLOWED: block<$block>\n";
3504 $allowed = 1;
3505 }
3506 if (statement_block_size($block) > 1) {
3507 #print "APW: ALLOWED: lines block<$block>\n";
3508 $allowed = 1;
3509 }
3510 # Check the post-context.
3511 if (defined $chunks[1]) {
3512 my ($cond, $block) = @{$chunks[1]};
3513 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003514 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003515 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003516 if ($block =~ /^\s*\{/) {
3517 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3518 $allowed = 1;
3519 }
3520 }
3521 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003522 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003523 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003524
Andy Whitcroftf0556632008-10-15 22:02:23 -07003525 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003526 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003527 }
3528
Joe Perches000d1cc12011-07-25 17:13:25 -07003529 WARN("BRACES",
3530 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003531 }
3532 }
3533
Joe Perches0979ae62012-12-17 16:01:59 -08003534# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003535 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003536 CHK("BRACES",
3537 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3538 }
Joe Perches77b9a532013-07-03 15:05:29 -07003539 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003540 CHK("BRACES",
3541 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3542 }
3543
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003544# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003545 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3546 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003547 WARN("VOLATILE",
3548 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003549 }
3550
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003551# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003552 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003553 CHK("REDUNDANT_CODE",
3554 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003555 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003556 }
3557
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003558# check for needless "if (<foo>) fn(<foo>)" uses
3559 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3560 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3561 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3562 WARN('NEEDLESS_IF',
3563 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003564 }
3565 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003566
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003567# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003568 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003569 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003570 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003571 CHK("USLEEP_RANGE",
3572 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003573 }
3574 }
3575
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003576# warn about unexpectedly long msleep's
3577 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3578 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003579 WARN("MSLEEP",
3580 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003581 }
3582 }
3583
Joe Perches36ec1932013-07-03 15:05:25 -07003584# check for comparisons of jiffies
3585 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3586 WARN("JIFFIES_COMPARISON",
3587 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3588 }
3589
Joe Perches9d7a34a2013-07-03 15:05:26 -07003590# check for comparisons of get_jiffies_64()
3591 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3592 WARN("JIFFIES_COMPARISON",
3593 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3594 }
3595
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003596# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003597# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003598# print "#ifdef in C files should be avoided\n";
3599# print "$herecurr";
3600# $clean = 0;
3601# }
3602
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003603# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003604 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003605 if (ERROR("SPACING",
3606 "exactly one space required after that #$1\n" . $herecurr) &&
3607 $fix) {
3608 $fixed[$linenr - 1] =~
3609 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3610 }
3611
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003612 }
3613
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003614# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003615 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3616 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003617 my $which = $1;
3618 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003619 CHK("UNCOMMENTED_DEFINITION",
3620 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003621 }
3622 }
3623# check for memory barriers without a comment.
3624 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3625 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003626 CHK("MEMORY_BARRIER",
3627 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003628 }
3629 }
3630# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003631 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003632 CHK("ARCH_DEFINES",
3633 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003634 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003635
Tobias Klauserd4977c72010-05-24 14:33:30 -07003636# Check that the storage class is at the beginning of a declaration
3637 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003638 WARN("STORAGE_CLASS",
3639 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003640 }
3641
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003642# check the location of the inline attribute, that it is between
3643# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003644 if ($line =~ /\b$Type\s+$Inline\b/ ||
3645 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003646 ERROR("INLINE_LOCATION",
3647 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003648 }
3649
Andy Whitcroft8905a672007-11-28 16:21:06 -08003650# Check for __inline__ and __inline, prefer inline
3651 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003652 WARN("INLINE",
3653 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003654 }
3655
Joe Perches3d130fd2011-01-12 17:00:00 -08003656# Check for __attribute__ packed, prefer __packed
3657 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003658 WARN("PREFER_PACKED",
3659 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003660 }
3661
Joe Perches39b7e282011-07-25 17:13:24 -07003662# Check for __attribute__ aligned, prefer __aligned
3663 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003664 WARN("PREFER_ALIGNED",
3665 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003666 }
3667
Joe Perches5f14d3b2012-01-10 15:09:52 -08003668# Check for __attribute__ format(printf, prefer __printf
3669 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3670 WARN("PREFER_PRINTF",
3671 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3672 }
3673
Joe Perches6061d942012-03-23 15:02:16 -07003674# Check for __attribute__ format(scanf, prefer __scanf
3675 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3676 WARN("PREFER_SCANF",
3677 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3678 }
3679
Joe Perches8f53a9b2010-03-05 13:43:48 -08003680# check for sizeof(&)
3681 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003682 WARN("SIZEOF_ADDRESS",
3683 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003684 }
3685
Joe Perches66c80b62012-07-30 14:41:22 -07003686# check for sizeof without parenthesis
3687 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3688 WARN("SIZEOF_PARENTHESIS",
3689 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3690 }
3691
Joe Perches428e2fd2011-05-24 17:13:39 -07003692# check for line continuations in quoted strings with odd counts of "
3693 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003694 WARN("LINE_CONTINUATIONS",
3695 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003696 }
3697
Joe Perches88982fe2012-12-17 16:02:00 -08003698# check for struct spinlock declarations
3699 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3700 WARN("USE_SPINLOCK_T",
3701 "struct spinlock should be spinlock_t\n" . $herecurr);
3702 }
3703
Joe Perchesa6962d72013-04-29 16:18:13 -07003704# check for seq_printf uses that could be seq_puts
3705 if ($line =~ /\bseq_printf\s*\(/) {
3706 my $fmt = get_quoted_string($line, $rawline);
3707 if ($fmt !~ /[^\\]\%/) {
3708 WARN("PREFER_SEQ_PUTS",
3709 "Prefer seq_puts to seq_printf\n" . $herecurr);
3710 }
3711 }
3712
Andy Whitcroft554e1652012-01-10 15:09:57 -08003713# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003714 if ($^V && $^V ge 5.10.0 &&
3715 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003716 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003717
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003718 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003719 my $ms_val = $7;
3720 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003721
Andy Whitcroft554e1652012-01-10 15:09:57 -08003722 if ($ms_size =~ /^(0x|)0$/i) {
3723 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003724 "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 -08003725 } elsif ($ms_size =~ /^(0x|)1$/i) {
3726 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003727 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3728 }
3729 }
3730
3731# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003732 if ($^V && $^V ge 5.10.0 &&
3733 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003734 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003735 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003736 my $call = $1;
3737 my $cast1 = deparenthesize($2);
3738 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003739 my $cast2 = deparenthesize($7);
3740 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003741 my $cast;
3742
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003743 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003744 $cast = "$cast1 or $cast2";
3745 } elsif ($cast1 ne "") {
3746 $cast = $cast1;
3747 } else {
3748 $cast = $cast2;
3749 }
3750 WARN("MINMAX",
3751 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003752 }
3753 }
3754
Joe Perches4a273192012-07-30 14:41:20 -07003755# check usleep_range arguments
3756 if ($^V && $^V ge 5.10.0 &&
3757 defined $stat &&
3758 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3759 my $min = $1;
3760 my $max = $7;
3761 if ($min eq $max) {
3762 WARN("USLEEP_RANGE",
3763 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3764 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3765 $min > $max) {
3766 WARN("USLEEP_RANGE",
3767 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3768 }
3769 }
3770
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003771# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003772 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003773 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003774 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003775 my $function_name = $1;
3776 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003777
3778 my $s = $stat;
3779 if (defined $cond) {
3780 substr($s, 0, length($cond), '');
3781 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003782 if ($s =~ /^\s*;/ &&
3783 $function_name ne 'uninitialized_var')
3784 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003785 WARN("AVOID_EXTERNS",
3786 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003787 }
3788
3789 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003790 WARN("FUNCTION_ARGUMENTS",
3791 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003792 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003793
3794 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3795 $stat =~ /^.\s*extern\s+/)
3796 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003797 WARN("AVOID_EXTERNS",
3798 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003799 }
3800
3801# checks for new __setup's
3802 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3803 my $name = $1;
3804
3805 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003806 CHK("UNDOCUMENTED_SETUP",
3807 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003808 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003809 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003810
3811# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003812 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003813 WARN("UNNECESSARY_CASTS",
3814 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003815 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003816
Joe Perchesa640d252013-07-03 15:05:21 -07003817# alloc style
3818# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3819 if ($^V && $^V ge 5.10.0 &&
3820 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3821 CHK("ALLOC_SIZEOF_STRUCT",
3822 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3823 }
3824
Joe Perches972fdea2013-04-29 16:18:12 -07003825# check for krealloc arg reuse
3826 if ($^V && $^V ge 5.10.0 &&
3827 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3828 WARN("KREALLOC_ARG_REUSE",
3829 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3830 }
3831
Joe Perches5ce59ae2013-02-21 16:44:18 -08003832# check for alloc argument mismatch
3833 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3834 WARN("ALLOC_ARRAY_ARGS",
3835 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3836 }
3837
Joe Perchescaf2a542011-01-12 16:59:56 -08003838# check for multiple semicolons
3839 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08003840 WARN("ONE_SEMICOLON",
3841 "Statements terminations use 1 semicolon\n" . $herecurr);
3842 }
3843
3844# check for switch/default statements without a break;
3845 if ($^V && $^V ge 5.10.0 &&
3846 defined $stat &&
3847 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3848 my $ctx = '';
3849 my $herectx = $here . "\n";
3850 my $cnt = statement_rawlines($stat);
3851 for (my $n = 0; $n < $cnt; $n++) {
3852 $herectx .= raw_line($linenr, $n) . "\n";
3853 }
3854 WARN("DEFAULT_NO_BREAK",
3855 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08003856 }
3857
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003858# check for gcc specific __FUNCTION__
3859 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003860 WARN("USE_FUNC",
3861 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003862 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003863
Joe Perches2c924882012-03-23 15:02:20 -07003864# check for use of yield()
3865 if ($line =~ /\byield\s*\(\s*\)/) {
3866 WARN("YIELD",
3867 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3868 }
3869
Joe Perches179f8f42013-07-03 15:05:30 -07003870# check for comparisons against true and false
3871 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3872 my $lead = $1;
3873 my $arg = $2;
3874 my $test = $3;
3875 my $otype = $4;
3876 my $trail = $5;
3877 my $op = "!";
3878
3879 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3880
3881 my $type = lc($otype);
3882 if ($type =~ /^(?:true|false)$/) {
3883 if (("$test" eq "==" && "$type" eq "true") ||
3884 ("$test" eq "!=" && "$type" eq "false")) {
3885 $op = "";
3886 }
3887
3888 CHK("BOOL_COMPARISON",
3889 "Using comparison to $otype is error prone\n" . $herecurr);
3890
3891## maybe suggesting a correct construct would better
3892## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3893
3894 }
3895 }
3896
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003897# check for semaphores initialized locked
3898 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003899 WARN("CONSIDER_COMPLETION",
3900 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003901 }
Joe Perches6712d852012-03-23 15:02:20 -07003902
Joe Perches67d0a072011-10-31 17:13:10 -07003903# recommend kstrto* over simple_strto* and strict_strto*
3904 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003905 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003906 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003907 }
Joe Perches6712d852012-03-23 15:02:20 -07003908
Michael Ellermanf3db6632008-07-23 21:28:57 -07003909# check for __initcall(), use device_initcall() explicitly please
3910 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003911 WARN("USE_DEVICE_INITCALL",
3912 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003913 }
Joe Perches6712d852012-03-23 15:02:20 -07003914
Emese Revfy79404842010-03-05 13:43:53 -08003915# check for various ops structs, ensure they are const.
3916 my $struct_ops = qr{acpi_dock_ops|
3917 address_space_operations|
3918 backlight_ops|
3919 block_device_operations|
3920 dentry_operations|
3921 dev_pm_ops|
3922 dma_map_ops|
3923 extent_io_ops|
3924 file_lock_operations|
3925 file_operations|
3926 hv_ops|
3927 ide_dma_ops|
3928 intel_dvo_dev_ops|
3929 item_operations|
3930 iwl_ops|
3931 kgdb_arch|
3932 kgdb_io|
3933 kset_uevent_ops|
3934 lock_manager_operations|
3935 microcode_ops|
3936 mtrr_ops|
3937 neigh_ops|
3938 nlmsvc_binding|
3939 pci_raw_ops|
3940 pipe_buf_operations|
3941 platform_hibernation_ops|
3942 platform_suspend_ops|
3943 proto_ops|
3944 rpc_pipe_ops|
3945 seq_operations|
3946 snd_ac97_build_ops|
3947 soc_pcmcia_socket_ops|
3948 stacktrace_ops|
3949 sysfs_ops|
3950 tty_operations|
3951 usb_mon_operations|
3952 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003953 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003954 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003955 WARN("CONST_STRUCT",
3956 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003957 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003958 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003959
3960# use of NR_CPUS is usually wrong
3961# ignore definitions of NR_CPUS and usage to define arrays as likely right
3962 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003963 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3964 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003965 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3966 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3967 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003968 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003969 WARN("NR_CPUS",
3970 "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 -07003971 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003972
3973# check for %L{u,d,i} in strings
3974 my $string;
3975 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3976 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003977 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003978 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003979 WARN("PRINTF_L",
3980 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003981 last;
3982 }
3983 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003984
3985# whine mightly about in_atomic
3986 if ($line =~ /\bin_atomic\s*\(/) {
3987 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003988 ERROR("IN_ATOMIC",
3989 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003990 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003991 WARN("IN_ATOMIC",
3992 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003993 }
3994 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003995
3996# check for lockdep_set_novalidate_class
3997 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3998 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3999 if ($realfile !~ m@^kernel/lockdep@ &&
4000 $realfile !~ m@^include/linux/lockdep@ &&
4001 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004002 ERROR("LOCKDEP",
4003 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004004 }
4005 }
Dave Jones88f88312011-01-12 16:59:59 -08004006
4007 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4008 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004009 WARN("EXPORTED_WORLD_WRITABLE",
4010 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004011 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004012 }
4013
4014 # If we have no input at all, then there is nothing to report on
4015 # so just keep quiet.
4016 if ($#rawlines == -1) {
4017 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004018 }
4019
Andy Whitcroft8905a672007-11-28 16:21:06 -08004020 # In mailback mode only produce a report in the negative, for
4021 # things that appear to be patches.
4022 if ($mailback && ($clean == 1 || !$is_patch)) {
4023 exit(0);
4024 }
4025
4026 # This is not a patch, and we are are in 'no-patch' mode so
4027 # just keep quiet.
4028 if (!$chk_patch && !$is_patch) {
4029 exit(0);
4030 }
4031
4032 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004033 ERROR("NOT_UNIFIED_DIFF",
4034 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004035 }
4036 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004037 ERROR("MISSING_SIGN_OFF",
4038 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004039 }
4040
Andy Whitcroft8905a672007-11-28 16:21:06 -08004041 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004042 if ($summary && !($clean == 1 && $quiet == 1)) {
4043 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004044 print "total: $cnt_error errors, $cnt_warn warnings, " .
4045 (($check)? "$cnt_chk checks, " : "") .
4046 "$cnt_lines lines checked\n";
4047 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004048 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004049
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004050 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004051
4052 if ($^V lt 5.10.0) {
4053 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4054 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4055 }
4056
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004057 # If there were whitespace errors which cleanpatch can fix
4058 # then suggest that.
4059 if ($rpt_cleaners) {
4060 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4061 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004062 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004063 }
4064 }
4065
Artem Bityutskiy11232682012-03-23 15:02:17 -07004066 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004067 print "NOTE: Ignored message types:";
4068 foreach my $ignore (sort keys %ignore_type) {
4069 print " $ignore";
4070 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07004071 print "\n\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07004072 }
4073
Joe Perches3705ce52013-07-03 15:05:31 -07004074 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4075 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4076 my $linecount = 0;
4077 my $f;
4078
4079 open($f, '>', $newfile)
4080 or die "$P: Can't open $newfile for write\n";
4081 foreach my $fixed_line (@fixed) {
4082 $linecount++;
4083 if ($file) {
4084 if ($linecount > 3) {
4085 $fixed_line =~ s/^\+//;
4086 print $f $fixed_line. "\n";
4087 }
4088 } else {
4089 print $f $fixed_line . "\n";
4090 }
4091 }
4092 close($f);
4093
4094 if (!$quiet) {
4095 print << "EOM";
4096Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4097
4098Do _NOT_ trust the results written to this file.
4099Do _NOT_ submit these changes without inspecting them for correctness.
4100
4101This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4102No warranties, expressed or implied...
4103
4104EOM
4105 }
4106 }
4107
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004108 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004109 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004110 }
4111 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004112 print << "EOM";
4113$vname has style problems, please review.
4114
4115If any of these errors are false positives, please report
4116them to the maintainer, see CHECKPATCH in MAINTAINERS.
4117EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004118 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004119
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004120 return $clean;
4121}