blob: 5989415985ae4b93773b662a2847170f12e1f87d [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 Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy 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 Whitcroft00df3442007-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 = ();
34my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070035my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070036my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080037my $max_line_length = 80;
Hannes Eder77f5b102009-09-21 17:04:37 -070038
39sub help {
40 my ($exitcode) = @_;
41
42 print << "EOM";
43Usage: $P [OPTION]... [FILE]...
44Version: $V
45
46Options:
47 -q, --quiet quiet
48 --no-tree run without a kernel tree
49 --no-signoff do not check for 'Signed-off-by' line
50 --patch treat FILE as patchfile (default)
51 --emacs emacs compile window format
52 --terse one line per report
53 -f, --file treat FILE as regular source file
54 --subjective, --strict enable more subjective tests
Joe Perches000d1cc12011-07-25 17:13:25 -070055 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080056 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070057 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070058 --root=PATH PATH to the kernel tree root
59 --no-summary suppress the per-file summary
60 --mailback only produce a report in case of warnings/errors
61 --summary-file include the filename in summary
62 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
63 'values', 'possible', 'type', and 'attr' (default
64 is all off)
65 --test-only=WORD report only warnings/errors containing WORD
66 literally
Joe Perches3705ce52013-07-03 15:05:31 -070067 --fix EXPERIMENTAL - may create horrible results
68 If correctable single-line errors exist, create
69 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
70 with potential errors corrected to the preferred
71 checkpatch style
Hannes Eder77f5b102009-09-21 17:04:37 -070072 -h, --help, --version display this help and exit
73
74When FILE is - read standard input.
75EOM
76
77 exit($exitcode);
78}
79
Joe Perches000d1cc12011-07-25 17:13:25 -070080my $conf = which_conf($configuration_file);
81if (-f $conf) {
82 my @conf_args;
83 open(my $conffile, '<', "$conf")
84 or warn "$P: Can't find a readable $configuration_file file $!\n";
85
86 while (<$conffile>) {
87 my $line = $_;
88
89 $line =~ s/\s*\n?$//g;
90 $line =~ s/^\s*//g;
91 $line =~ s/\s+/ /g;
92
93 next if ($line =~ m/^\s*#/);
94 next if ($line =~ m/^\s*$/);
95
96 my @words = split(" ", $line);
97 foreach my $word (@words) {
98 last if ($word =~ m/^#/);
99 push (@conf_args, $word);
100 }
101 }
102 close($conffile);
103 unshift(@ARGV, @conf_args) if @conf_args;
104}
105
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700106GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700107 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700108 'tree!' => \$tree,
109 'signoff!' => \$chk_signoff,
110 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700111 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800112 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700113 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700114 'subjective!' => \$check,
115 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700116 'ignore=s' => \@ignore,
117 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800118 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700119 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800120 'summary!' => \$summary,
121 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800122 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700123 'fix!' => \$fix,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800124 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700125 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700126 'h|help' => \$help,
127 'version' => \$help
128) or help(1);
129
130help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700131
132my $exit = 0;
133
134if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700135 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700136 exit(1);
137}
138
Joe Perches000d1cc12011-07-25 17:13:25 -0700139@ignore = split(/,/, join(',',@ignore));
140foreach my $word (@ignore) {
141 $word =~ s/\s*\n?$//g;
142 $word =~ s/^\s*//g;
143 $word =~ s/\s+/ /g;
144 $word =~ tr/[a-z]/[A-Z]/;
145
146 next if ($word =~ m/^\s*#/);
147 next if ($word =~ m/^\s*$/);
148
149 $ignore_type{$word}++;
150}
151
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800152my $dbg_values = 0;
153my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700154my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700155my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800156for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800157 ## no critic
158 eval "\${dbg_$key} = '$debug{$key}';";
159 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800160}
161
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700162my $rpt_cleaners = 0;
163
Andy Whitcroft8905a672007-11-28 16:21:06 -0800164if ($terse) {
165 $emacs = 1;
166 $quiet++;
167}
168
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700169if ($tree) {
170 if (defined $root) {
171 if (!top_of_kernel_tree($root)) {
172 die "$P: $root: --root does not point at a valid tree\n";
173 }
174 } else {
175 if (top_of_kernel_tree('.')) {
176 $root = '.';
177 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
178 top_of_kernel_tree($1)) {
179 $root = $1;
180 }
181 }
182
183 if (!defined $root) {
184 print "Must be run from the top-level dir. of a kernel tree\n";
185 exit(2);
186 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700187}
188
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189my $emitted_corrupt = 0;
190
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700191our $Ident = qr{
192 [A-Za-z_][A-Za-z\d_]*
193 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
194 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195our $Storage = qr{extern|static|asmlinkage};
196our $Sparse = qr{
197 __user|
198 __kernel|
199 __force|
200 __iomem|
201 __must_check|
202 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800203 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700204 __ref|
205 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700206 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800207
208# Notes to $Attribute:
209# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210our $Attribute = qr{
211 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700212 __percpu|
213 __nocast|
214 __safe|
215 __bitwise__|
216 __packed__|
217 __packed2__|
218 __naked|
219 __maybe_unused|
220 __always_unused|
221 __noreturn|
222 __used|
223 __cold|
224 __noclone|
225 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700226 __read_mostly|
227 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800228 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700229 ____cacheline_aligned|
230 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800231 ____cacheline_internodealigned_in_smp|
232 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700233 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700234our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700235our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700236our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
237our $Lval = qr{$Ident(?:$Member)*};
238
Joe Perches95e2c602013-07-03 15:05:20 -0700239our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
240our $Binary = qr{(?i)0b[01]+$Int_type?};
241our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
242our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800243our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
244our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
245our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800246our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700247our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800248our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800249our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700250our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700251our $Operators = qr{
252 <=|>=|==|!=|
253 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700254 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700255 }x;
256
Andy Whitcroft8905a672007-11-28 16:21:06 -0800257our $NonptrType;
258our $Type;
259our $Declare;
260
Joe Perches15662b32011-10-31 17:13:12 -0700261our $NON_ASCII_UTF8 = qr{
262 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700263 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
264 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
265 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
266 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
267 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
268 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
269}x;
270
Joe Perches15662b32011-10-31 17:13:12 -0700271our $UTF8 = qr{
272 [\x09\x0A\x0D\x20-\x7E] # ASCII
273 | $NON_ASCII_UTF8
274}x;
275
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700276our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700277 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700278 atomic_t
279)};
280
Joe Perches691e6692010-03-05 13:43:51 -0800281our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700282 printk(?:_ratelimited|_once|)|
283 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
284 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700285 panic|
286 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800287)};
288
Joe Perches20112472011-07-25 17:13:23 -0700289our $signature_tags = qr{(?xi:
290 Signed-off-by:|
291 Acked-by:|
292 Tested-by:|
293 Reviewed-by:|
294 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700295 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700296 To:|
297 Cc:
298)};
299
Andy Whitcroft8905a672007-11-28 16:21:06 -0800300our @typeList = (
301 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700302 qr{(?:unsigned\s+)?char},
303 qr{(?:unsigned\s+)?short},
304 qr{(?:unsigned\s+)?int},
305 qr{(?:unsigned\s+)?long},
306 qr{(?:unsigned\s+)?long\s+int},
307 qr{(?:unsigned\s+)?long\s+long},
308 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800309 qr{unsigned},
310 qr{float},
311 qr{double},
312 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800313 qr{struct\s+$Ident},
314 qr{union\s+$Ident},
315 qr{enum\s+$Ident},
316 qr{${Ident}_t},
317 qr{${Ident}_handler},
318 qr{${Ident}_handler_fn},
319);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700320our @modifierList = (
321 qr{fastcall},
322);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800323
Wolfram Sang7840a942010-08-09 17:20:57 -0700324our $allowed_asm_includes = qr{(?x:
325 irq|
326 memory
327)};
328# memory.h: ARM has a custom one
329
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700331 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
332 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700333 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800334 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700335 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800336 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800337 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700338 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700339 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800340 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700341 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800342 }x;
343 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700344 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700345 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700346 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800347 }x;
348 $Declare = qr{(?:$Storage\s+)?$Type};
349}
350build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700351
Joe Perches7d2367a2011-07-25 17:13:22 -0700352
353our $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
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372$chk_signoff = 0 if ($file);
373
Andy Whitcroft00df3442007-06-08 13:47:06 -0700374my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800375my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700376my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800377my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700378for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800379 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700380 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800381 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700382 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800383 } elsif ($filename eq '-') {
384 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700385 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800386 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700387 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700388 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800389 if ($filename eq '-') {
390 $vname = 'Your patch';
391 } else {
392 $vname = $filename;
393 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800394 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700395 chomp;
396 push(@rawlines, $_);
397 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800398 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800399 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700400 $exit = 1;
401 }
402 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800403 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700404 @fixed = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700405}
406
407exit($exit);
408
409sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700410 my ($root) = @_;
411
412 my @tree_check = (
413 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
414 "README", "Documentation", "arch", "include", "drivers",
415 "fs", "init", "ipc", "kernel", "lib", "scripts",
416 );
417
418 foreach my $check (@tree_check) {
419 if (! -e $root . '/' . $check) {
420 return 0;
421 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700422 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700423 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700424}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700425
Joe Perches20112472011-07-25 17:13:23 -0700426sub parse_email {
427 my ($formatted_email) = @_;
428
429 my $name = "";
430 my $address = "";
431 my $comment = "";
432
433 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
434 $name = $1;
435 $address = $2;
436 $comment = $3 if defined $3;
437 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
438 $address = $1;
439 $comment = $2 if defined $2;
440 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
441 $address = $1;
442 $comment = $2 if defined $2;
443 $formatted_email =~ s/$address.*$//;
444 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700445 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700446 $name =~ s/^\"|\"$//g;
447 # If there's a name left after stripping spaces and
448 # leading quotes, and the address doesn't have both
449 # leading and trailing angle brackets, the address
450 # is invalid. ie:
451 # "joe smith joe@smith.com" bad
452 # "joe smith <joe@smith.com" bad
453 if ($name ne "" && $address !~ /^<[^>]+>$/) {
454 $name = "";
455 $address = "";
456 $comment = "";
457 }
458 }
459
Joe Perches3705ce52013-07-03 15:05:31 -0700460 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700461 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700462 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700463 $address =~ s/^\<|\>$//g;
464
465 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
466 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
467 $name = "\"$name\"";
468 }
469
470 return ($name, $address, $comment);
471}
472
473sub format_email {
474 my ($name, $address) = @_;
475
476 my $formatted_email;
477
Joe Perches3705ce52013-07-03 15:05:31 -0700478 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700479 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700480 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700481
482 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
483 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
484 $name = "\"$name\"";
485 }
486
487 if ("$name" eq "") {
488 $formatted_email = "$address";
489 } else {
490 $formatted_email = "$name <$address>";
491 }
492
493 return $formatted_email;
494}
495
Joe Perches000d1cc12011-07-25 17:13:25 -0700496sub which_conf {
497 my ($conf) = @_;
498
499 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
500 if (-e "$path/$conf") {
501 return "$path/$conf";
502 }
503 }
504
505 return "";
506}
507
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700508sub expand_tabs {
509 my ($str) = @_;
510
511 my $res = '';
512 my $n = 0;
513 for my $c (split(//, $str)) {
514 if ($c eq "\t") {
515 $res .= ' ';
516 $n++;
517 for (; ($n % 8) != 0; $n++) {
518 $res .= ' ';
519 }
520 next;
521 }
522 $res .= $c;
523 $n++;
524 }
525
526 return $res;
527}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700528sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700529 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700530 return $res;
531}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700532
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700533sub line_stats {
534 my ($line) = @_;
535
536 # Drop the diff line leader and expand tabs
537 $line =~ s/^.//;
538 $line = expand_tabs($line);
539
540 # Pick the indent from the front of the line.
541 my ($white) = ($line =~ /^(\s*)/);
542
543 return (length($line), length($white));
544}
545
Andy Whitcroft773647a2008-03-28 14:15:58 -0700546my $sanitise_quote = '';
547
548sub sanitise_line_reset {
549 my ($in_comment) = @_;
550
551 if ($in_comment) {
552 $sanitise_quote = '*/';
553 } else {
554 $sanitise_quote = '';
555 }
556}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700557sub sanitise_line {
558 my ($line) = @_;
559
560 my $res = '';
561 my $l = '';
562
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800563 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700564 my $off = 0;
565 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700566
Andy Whitcroft773647a2008-03-28 14:15:58 -0700567 # Always copy over the diff marker.
568 $res = substr($line, 0, 1);
569
570 for ($off = 1; $off < length($line); $off++) {
571 $c = substr($line, $off, 1);
572
573 # Comments we are wacking completly including the begin
574 # and end, all to $;.
575 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
576 $sanitise_quote = '*/';
577
578 substr($res, $off, 2, "$;$;");
579 $off++;
580 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800581 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700582 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700583 $sanitise_quote = '';
584 substr($res, $off, 2, "$;$;");
585 $off++;
586 next;
587 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700588 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
589 $sanitise_quote = '//';
590
591 substr($res, $off, 2, $sanitise_quote);
592 $off++;
593 next;
594 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700595
596 # A \ in a string means ignore the next character.
597 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
598 $c eq "\\") {
599 substr($res, $off, 2, 'XX');
600 $off++;
601 next;
602 }
603 # Regular quotes.
604 if ($c eq "'" || $c eq '"') {
605 if ($sanitise_quote eq '') {
606 $sanitise_quote = $c;
607
608 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700609 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700610 } elsif ($sanitise_quote eq $c) {
611 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700612 }
613 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700614
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800615 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700616 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
617 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700618 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
619 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700620 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
621 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700622 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700623 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700624 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800625 }
626
Daniel Walker113f04a2009-09-21 17:04:35 -0700627 if ($sanitise_quote eq '//') {
628 $sanitise_quote = '';
629 }
630
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800631 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700632 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800633 my $clean = 'X' x length($1);
634 $res =~ s@\<.*\>@<$clean>@;
635
636 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700637 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800638 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700639 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800640 }
641
Andy Whitcroft00df3442007-06-08 13:47:06 -0700642 return $res;
643}
644
Joe Perchesa6962d72013-04-29 16:18:13 -0700645sub get_quoted_string {
646 my ($line, $rawline) = @_;
647
648 return "" if ($line !~ m/(\"[X]+\")/g);
649 return substr($rawline, $-[0], $+[0] - $-[0]);
650}
651
Andy Whitcroft8905a672007-11-28 16:21:06 -0800652sub ctx_statement_block {
653 my ($linenr, $remain, $off) = @_;
654 my $line = $linenr - 1;
655 my $blk = '';
656 my $soff = $off;
657 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700658 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800659
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800660 my $loff = 0;
661
Andy Whitcroft8905a672007-11-28 16:21:06 -0800662 my $type = '';
663 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800664 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800665 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800666 my $c;
667 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800668
669 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800670 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800671 @stack = (['', 0]) if ($#stack == -1);
672
Andy Whitcroft773647a2008-03-28 14:15:58 -0700673 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800674 # If we are about to drop off the end, pull in more
675 # context.
676 if ($off >= $len) {
677 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700678 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800679 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800680 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800681 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800682 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800683 $len = length($blk);
684 $line++;
685 last;
686 }
687 # Bail if there is no further context.
688 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800689 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800690 last;
691 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800692 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
693 $level++;
694 $type = '#';
695 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800696 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800697 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800698 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800699 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800700
Andy Whitcroft773647a2008-03-28 14:15:58 -0700701 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800702
703 # Handle nested #if/#else.
704 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
705 push(@stack, [ $type, $level ]);
706 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
707 ($type, $level) = @{$stack[$#stack - 1]};
708 } elsif ($remainder =~ /^#\s*endif\b/) {
709 ($type, $level) = @{pop(@stack)};
710 }
711
Andy Whitcroft8905a672007-11-28 16:21:06 -0800712 # Statement ends at the ';' or a close '}' at the
713 # outermost level.
714 if ($level == 0 && $c eq ';') {
715 last;
716 }
717
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800718 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700719 if ($level == 0 && $coff_set == 0 &&
720 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
721 $remainder =~ /^(else)(?:\s|{)/ &&
722 $remainder !~ /^else\s+if\b/) {
723 $coff = $off + length($1) - 1;
724 $coff_set = 1;
725 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
726 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800727 }
728
Andy Whitcroft8905a672007-11-28 16:21:06 -0800729 if (($type eq '' || $type eq '(') && $c eq '(') {
730 $level++;
731 $type = '(';
732 }
733 if ($type eq '(' && $c eq ')') {
734 $level--;
735 $type = ($level != 0)? '(' : '';
736
737 if ($level == 0 && $coff < $soff) {
738 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700739 $coff_set = 1;
740 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800741 }
742 }
743 if (($type eq '' || $type eq '{') && $c eq '{') {
744 $level++;
745 $type = '{';
746 }
747 if ($type eq '{' && $c eq '}') {
748 $level--;
749 $type = ($level != 0)? '{' : '';
750
751 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700752 if (substr($blk, $off + 1, 1) eq ';') {
753 $off++;
754 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800755 last;
756 }
757 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800758 # Preprocessor commands end at the newline unless escaped.
759 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
760 $level--;
761 $type = '';
762 $off++;
763 last;
764 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800765 $off++;
766 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700767 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800768 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700769 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800770 $line++;
771 $remain--;
772 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800773
774 my $statement = substr($blk, $soff, $off - $soff + 1);
775 my $condition = substr($blk, $soff, $coff - $soff + 1);
776
777 #warn "STATEMENT<$statement>\n";
778 #warn "CONDITION<$condition>\n";
779
Andy Whitcroft773647a2008-03-28 14:15:58 -0700780 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800781
782 return ($statement, $condition,
783 $line, $remain + 1, $off - $loff + 1, $level);
784}
785
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786sub statement_lines {
787 my ($stmt) = @_;
788
789 # Strip the diff line prefixes and rip blank lines at start and end.
790 $stmt =~ s/(^|\n)./$1/g;
791 $stmt =~ s/^\s*//;
792 $stmt =~ s/\s*$//;
793
794 my @stmt_lines = ($stmt =~ /\n/g);
795
796 return $#stmt_lines + 2;
797}
798
799sub statement_rawlines {
800 my ($stmt) = @_;
801
802 my @stmt_lines = ($stmt =~ /\n/g);
803
804 return $#stmt_lines + 2;
805}
806
807sub statement_block_size {
808 my ($stmt) = @_;
809
810 $stmt =~ s/(^|\n)./$1/g;
811 $stmt =~ s/^\s*{//;
812 $stmt =~ s/}\s*$//;
813 $stmt =~ s/^\s*//;
814 $stmt =~ s/\s*$//;
815
816 my @stmt_lines = ($stmt =~ /\n/g);
817 my @stmt_statements = ($stmt =~ /;/g);
818
819 my $stmt_lines = $#stmt_lines + 2;
820 my $stmt_statements = $#stmt_statements + 1;
821
822 if ($stmt_lines > $stmt_statements) {
823 return $stmt_lines;
824 } else {
825 return $stmt_statements;
826 }
827}
828
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800829sub ctx_statement_full {
830 my ($linenr, $remain, $off) = @_;
831 my ($statement, $condition, $level);
832
833 my (@chunks);
834
Andy Whitcroftcf655042008-03-04 14:28:20 -0800835 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800836 ($statement, $condition, $linenr, $remain, $off, $level) =
837 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700838 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800839 push(@chunks, [ $condition, $statement ]);
840 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
841 return ($level, $linenr, @chunks);
842 }
843
844 # Pull in the following conditional/block pairs and see if they
845 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800846 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800847 ($statement, $condition, $linenr, $remain, $off, $level) =
848 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800849 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700850 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800851 #print "C: push\n";
852 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800853 }
854
855 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800856}
857
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700858sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700859 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700860 my $line;
861 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700862 my $blk = '';
863 my @o;
864 my @c;
865 my @res = ();
866
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700867 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800868 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700869 for ($line = $start; $remain > 0; $line++) {
870 next if ($rawlines[$line] =~ /^-/);
871 $remain--;
872
873 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800874
875 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700876 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800877 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700878 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800879 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700880 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800881 $level = pop(@stack);
882 }
883
Andy Whitcroft01464f32010-10-26 14:23:19 -0700884 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700885 ##print "C<$c>L<$level><$open$close>O<$off>\n";
886 if ($off > 0) {
887 $off--;
888 next;
889 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700890
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700891 if ($c eq $close && $level > 0) {
892 $level--;
893 last if ($level == 0);
894 } elsif ($c eq $open) {
895 $level++;
896 }
897 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700898
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700899 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700900 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700901 }
902
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700903 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700904 }
905
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700906 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700907}
908sub ctx_block_outer {
909 my ($linenr, $remain) = @_;
910
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700911 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
912 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700913}
914sub ctx_block {
915 my ($linenr, $remain) = @_;
916
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700917 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
918 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700919}
920sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700921 my ($linenr, $remain, $off) = @_;
922
923 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
924 return @r;
925}
926sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700927 my ($linenr, $remain) = @_;
928
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700929 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700930}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700931sub ctx_statement_level {
932 my ($linenr, $remain, $off) = @_;
933
934 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
935}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700936
937sub ctx_locate_comment {
938 my ($first_line, $end_line) = @_;
939
940 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700941 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700942 return $current_comment if (defined $current_comment);
943
944 # Look through the context and try and figure out if there is a
945 # comment.
946 my $in_comment = 0;
947 $current_comment = '';
948 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700949 my $line = $rawlines[$linenr - 1];
950 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700951 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
952 $in_comment = 1;
953 }
954 if ($line =~ m@/\*@) {
955 $in_comment = 1;
956 }
957 if (!$in_comment && $current_comment ne '') {
958 $current_comment = '';
959 }
960 $current_comment .= $line . "\n" if ($in_comment);
961 if ($line =~ m@\*/@) {
962 $in_comment = 0;
963 }
964 }
965
966 chomp($current_comment);
967 return($current_comment);
968}
969sub ctx_has_comment {
970 my ($first_line, $end_line) = @_;
971 my $cmt = ctx_locate_comment($first_line, $end_line);
972
Andy Whitcroft00df3442007-06-08 13:47:06 -0700973 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700974 ##print "CMMT: $cmt\n";
975
976 return ($cmt ne '');
977}
978
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700979sub raw_line {
980 my ($linenr, $cnt) = @_;
981
982 my $offset = $linenr - 1;
983 $cnt++;
984
985 my $line;
986 while ($cnt) {
987 $line = $rawlines[$offset++];
988 next if (defined($line) && $line =~ /^-/);
989 $cnt--;
990 }
991
992 return $line;
993}
994
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700995sub cat_vet {
996 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700997 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700998
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700999 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1001 $res .= $1;
1002 if ($2 ne '') {
1003 $coded = sprintf("^%c", unpack('C', $2) + 64);
1004 $res .= $coded;
1005 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001006 }
1007 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001008
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001009 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001010}
1011
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001013my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001014my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001015my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001016
1017sub annotate_reset {
1018 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001019 $av_pending = '_';
1020 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001021 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001022}
1023
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024sub annotate_values {
1025 my ($stream, $type) = @_;
1026
1027 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001028 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001029 my $cur = $stream;
1030
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001032
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001033 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001034 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001035 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001036 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001037 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038 print "WS($1)\n" if ($dbg_values > 1);
1039 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001040 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001041 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001042 }
1043
Florian Micklerc023e4732011-01-12 16:59:58 -08001044 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001045 print "CAST($1)\n" if ($dbg_values > 1);
1046 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001047 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001048
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001049 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001050 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001051 $type = 'T';
1052
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001053 } elsif ($cur =~ /^($Modifier)\s*/) {
1054 print "MODIFIER($1)\n" if ($dbg_values > 1);
1055 $type = 'T';
1056
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001057 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001058 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001059 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001060 push(@av_paren_type, $type);
1061 if ($2 ne '') {
1062 $av_pending = 'N';
1063 }
1064 $type = 'E';
1065
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001066 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001067 print "UNDEF($1)\n" if ($dbg_values > 1);
1068 $av_preprocessor = 1;
1069 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001071 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001072 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001073 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001074
1075 push(@av_paren_type, $type);
1076 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001077 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001078
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001079 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001080 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1081 $av_preprocessor = 1;
1082
1083 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1084
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001085 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001086
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001087 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001088 print "PRE_END($1)\n" if ($dbg_values > 1);
1089
1090 $av_preprocessor = 1;
1091
1092 # Assume all arms of the conditional end as this
1093 # one does, and continue as if the #endif was not here.
1094 pop(@av_paren_type);
1095 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001096 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001097
1098 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001099 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001100
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001101 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1102 print "ATTR($1)\n" if ($dbg_values > 1);
1103 $av_pending = $type;
1104 $type = 'N';
1105
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001106 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001107 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001108 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001109 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110 }
1111 $type = 'N';
1112
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001113 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001114 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001115 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001116 $type = 'N';
1117
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001118 } elsif ($cur =~/^(case)/o) {
1119 print "CASE($1)\n" if ($dbg_values > 1);
1120 $av_pend_colon = 'C';
1121 $type = 'N';
1122
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001123 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001124 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125 $type = 'N';
1126
1127 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001128 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001129 push(@av_paren_type, $av_pending);
1130 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001131 $type = 'N';
1132
1133 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001134 my $new_type = pop(@av_paren_type);
1135 if ($new_type ne '_') {
1136 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001137 print "PAREN('$1') -> $type\n"
1138 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001139 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001140 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001141 }
1142
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001143 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001144 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001145 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001146 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001147
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001148 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1149 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001150 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001151 } elsif ($type eq 'E') {
1152 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001153 }
1154 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1155 $type = 'V';
1156
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001157 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001158 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001159 $type = 'V';
1160
1161 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001162 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001163 $type = 'N';
1164
Andy Whitcroftcf655042008-03-04 14:28:20 -08001165 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001166 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001167 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001168 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001169
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001170 } elsif ($cur =~/^(,)/) {
1171 print "COMMA($1)\n" if ($dbg_values > 1);
1172 $type = 'C';
1173
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001174 } elsif ($cur =~ /^(\?)/o) {
1175 print "QUESTION($1)\n" if ($dbg_values > 1);
1176 $type = 'N';
1177
1178 } elsif ($cur =~ /^(:)/o) {
1179 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1180
1181 substr($var, length($res), 1, $av_pend_colon);
1182 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1183 $type = 'E';
1184 } else {
1185 $type = 'N';
1186 }
1187 $av_pend_colon = 'O';
1188
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001189 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001190 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001191 $type = 'N';
1192
Andy Whitcroft0d413862008-10-15 22:02:16 -07001193 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001194 my $variant;
1195
1196 print "OPV($1)\n" if ($dbg_values > 1);
1197 if ($type eq 'V') {
1198 $variant = 'B';
1199 } else {
1200 $variant = 'U';
1201 }
1202
1203 substr($var, length($res), 1, $variant);
1204 $type = 'N';
1205
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001206 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001207 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001208 if ($1 ne '++' && $1 ne '--') {
1209 $type = 'N';
1210 }
1211
1212 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001214 }
1215 if (defined $1) {
1216 $cur = substr($cur, length($1));
1217 $res .= $type x length($1);
1218 }
1219 }
1220
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001221 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001222}
1223
Andy Whitcroft8905a672007-11-28 16:21:06 -08001224sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001225 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001226 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001227 ^(?:
1228 $Modifier|
1229 $Storage|
1230 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001231 DEFINE_\S+
1232 )$|
1233 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001234 goto|
1235 return|
1236 case|
1237 else|
1238 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001239 do|
1240 \#|
1241 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001242 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001243 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001244 )}x;
1245 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1246 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001247 # Check for modifiers.
1248 $possible =~ s/\s*$Storage\s*//g;
1249 $possible =~ s/\s*$Sparse\s*//g;
1250 if ($possible =~ /^\s*$/) {
1251
1252 } elsif ($possible =~ /\s/) {
1253 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001254 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001255 if ($modifier !~ $notPermitted) {
1256 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1257 push(@modifierList, $modifier);
1258 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001259 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001260
1261 } else {
1262 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1263 push(@typeList, $possible);
1264 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001265 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001266 } else {
1267 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001268 }
1269}
1270
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001271my $prefix = '';
1272
Joe Perches000d1cc12011-07-25 17:13:25 -07001273sub show_type {
1274 return !defined $ignore_type{$_[0]};
1275}
1276
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001277sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001278 if (!show_type($_[1]) ||
1279 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001280 return 0;
1281 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001282 my $line;
1283 if ($show_types) {
1284 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1285 } else {
1286 $line = "$prefix$_[0]: $_[2]\n";
1287 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001288 $line = (split('\n', $line))[0] . "\n" if ($terse);
1289
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001290 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001291
1292 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001293}
1294sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001295 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001296}
Joe Perches000d1cc12011-07-25 17:13:25 -07001297
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001298sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001299 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 our $clean = 0;
1301 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001302 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001303 }
Joe Perches3705ce52013-07-03 15:05:31 -07001304 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001305}
1306sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001307 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001308 our $clean = 0;
1309 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001310 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001311 }
Joe Perches3705ce52013-07-03 15:05:31 -07001312 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001313}
1314sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001315 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001316 our $clean = 0;
1317 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001318 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001319 }
Joe Perches3705ce52013-07-03 15:05:31 -07001320 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001321}
1322
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001323sub check_absolute_file {
1324 my ($absolute, $herecurr) = @_;
1325 my $file = $absolute;
1326
1327 ##print "absolute<$absolute>\n";
1328
1329 # See if any suffix of this path is a path within the tree.
1330 while ($file =~ s@^[^/]*/@@) {
1331 if (-f "$root/$file") {
1332 ##print "file<$file>\n";
1333 last;
1334 }
1335 }
1336 if (! -f _) {
1337 return 0;
1338 }
1339
1340 # It is, so see if the prefix is acceptable.
1341 my $prefix = $absolute;
1342 substr($prefix, -length($file)) = '';
1343
1344 ##print "prefix<$prefix>\n";
1345 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001346 WARN("USE_RELATIVE_PATH",
1347 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001348 }
1349}
1350
Joe Perches3705ce52013-07-03 15:05:31 -07001351sub trim {
1352 my ($string) = @_;
1353
1354 $string =~ s/(^\s+|\s+$)//g;
1355
1356 return $string;
1357}
1358
1359sub tabify {
1360 my ($leading) = @_;
1361
1362 my $source_indent = 8;
1363 my $max_spaces_before_tab = $source_indent - 1;
1364 my $spaces_to_tab = " " x $source_indent;
1365
1366 #convert leading spaces to tabs
1367 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1368 #Remove spaces before a tab
1369 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1370
1371 return "$leading";
1372}
1373
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001374sub pos_last_openparen {
1375 my ($line) = @_;
1376
1377 my $pos = 0;
1378
1379 my $opens = $line =~ tr/\(/\(/;
1380 my $closes = $line =~ tr/\)/\)/;
1381
1382 my $last_openparen = 0;
1383
1384 if (($opens == 0) || ($closes >= $opens)) {
1385 return -1;
1386 }
1387
1388 my $len = length($line);
1389
1390 for ($pos = 0; $pos < $len; $pos++) {
1391 my $string = substr($line, $pos);
1392 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1393 $pos += length($1) - 1;
1394 } elsif (substr($line, $pos, 1) eq '(') {
1395 $last_openparen = $pos;
1396 } elsif (index($string, '(') == -1) {
1397 last;
1398 }
1399 }
1400
1401 return $last_openparen + 1;
1402}
1403
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001404sub process {
1405 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001406
1407 my $linenr=0;
1408 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001410 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001411 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001412
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001413 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001414 my $indent;
1415 my $previndent=0;
1416 my $stashindent=0;
1417
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001418 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001419 my $signoff = 0;
1420 my $is_patch = 0;
1421
Joe Perches15662b32011-10-31 17:13:12 -07001422 my $in_header_lines = 1;
1423 my $in_commit_log = 0; #Scanning lines before patch
1424
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001425 my $non_utf8_charset = 0;
1426
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001427 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001428 our $cnt_lines = 0;
1429 our $cnt_error = 0;
1430 our $cnt_warn = 0;
1431 our $cnt_chk = 0;
1432
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001433 # Trace the real file/line as we go.
1434 my $realfile = '';
1435 my $realline = 0;
1436 my $realcnt = 0;
1437 my $here = '';
1438 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001439 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001440 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001441 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001442
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001443 my $prev_values = 'E';
1444
1445 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001446 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001447 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001448 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001449 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001450
Joe Perches323c1262012-12-17 16:02:07 -08001451 my %camelcase = ();
1452
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001453 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001454 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001455 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001456 my @setup_docs = ();
1457 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001458
1459 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001460 my $line;
1461 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001462 $linenr++;
1463 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001464
Joe Perches3705ce52013-07-03 15:05:31 -07001465 push(@fixed, $rawline) if ($fix);
1466
Andy Whitcroft773647a2008-03-28 14:15:58 -07001467 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001468 $setup_docs = 0;
1469 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1470 $setup_docs = 1;
1471 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001472 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001473 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001474 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1475 $realline=$1-1;
1476 if (defined $2) {
1477 $realcnt=$3+1;
1478 } else {
1479 $realcnt=1+1;
1480 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001481 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001482
1483 # Guestimate if this is a continuing comment. Run
1484 # the context looking for a comment "edge". If this
1485 # edge is a close comment then we must be in a comment
1486 # at context start.
1487 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001488 my $cnt = $realcnt;
1489 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1490 next if (defined $rawlines[$ln - 1] &&
1491 $rawlines[$ln - 1] =~ /^-/);
1492 $cnt--;
1493 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001494 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001495 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1496 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1497 ($edge) = $1;
1498 last;
1499 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001500 }
1501 if (defined $edge && $edge eq '*/') {
1502 $in_comment = 1;
1503 }
1504
1505 # Guestimate if this is a continuing comment. If this
1506 # is the start of a diff block and this line starts
1507 # ' *' then it is very likely a comment.
1508 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001509 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001510 {
1511 $in_comment = 1;
1512 }
1513
1514 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1515 sanitise_line_reset($in_comment);
1516
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001517 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001518 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001519 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001520 $line = sanitise_line($rawline);
1521 }
1522 push(@lines, $line);
1523
1524 if ($realcnt > 1) {
1525 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1526 } else {
1527 $realcnt = 0;
1528 }
1529
1530 #print "==>$rawline\n";
1531 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001532
1533 if ($setup_docs && $line =~ /^\+/) {
1534 push(@setup_docs, $line);
1535 }
1536 }
1537
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001538 $prefix = '';
1539
Andy Whitcroft773647a2008-03-28 14:15:58 -07001540 $realcnt = 0;
1541 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001542 foreach my $line (@lines) {
1543 $linenr++;
1544
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001545 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001546
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001547#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001548 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001549 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001550 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001551 $realline=$1-1;
1552 if (defined $2) {
1553 $realcnt=$3+1;
1554 } else {
1555 $realcnt=1+1;
1556 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001557 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001558 $prev_values = 'E';
1559
Andy Whitcroft773647a2008-03-28 14:15:58 -07001560 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001561 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001562 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001563 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001564 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001565
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001566# track the line number as we move through the hunk, note that
1567# new versions of GNU diff omit the leading space on completely
1568# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001569 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001570 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001571 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001572
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001573 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001574 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001575
1576 # Track the previous line.
1577 ($prevline, $stashline) = ($stashline, $line);
1578 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001579 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1580
Andy Whitcroft773647a2008-03-28 14:15:58 -07001581 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001582
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001583 } elsif ($realcnt == 1) {
1584 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001585 }
1586
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001587 my $hunk_line = ($realcnt != 0);
1588
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001589#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001590 $prefix = "$filename:$realline: " if ($emacs && $file);
1591 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1592
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001593 $here = "#$linenr: " if (!$file);
1594 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001595
1596 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001597 if ($line =~ /^diff --git.*?(\S+)$/) {
1598 $realfile = $1;
1599 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001600 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001601 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001602 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001603 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001604 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001605
1606 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001607 if (!$file && $tree && $p1_prefix ne '' &&
1608 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001609 WARN("PATCH_PREFIX",
1610 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001611 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001612
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001613 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001614 ERROR("MODIFIED_INCLUDE_ASM",
1615 "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 -07001616 }
1617 next;
1618 }
1619
Randy Dunlap389834b2007-06-08 13:47:03 -07001620 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001621
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001622 my $hereline = "$here\n$rawline\n";
1623 my $herecurr = "$here\n$rawline\n";
1624 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001625
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001626 $cnt_lines++ if ($realcnt != 0);
1627
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001628# Check for incorrect file permissions
1629 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1630 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001631 if ($realfile !~ m@scripts/@ &&
1632 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001633 ERROR("EXECUTE_PERMISSIONS",
1634 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001635 }
1636 }
1637
Joe Perches20112472011-07-25 17:13:23 -07001638# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001639 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001640 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001641 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001642 }
1643
1644# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001645 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001646 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001647 my $space_before = $1;
1648 my $sign_off = $2;
1649 my $space_after = $3;
1650 my $email = $4;
1651 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1652
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001653 if ($sign_off !~ /$signature_tags/) {
1654 WARN("BAD_SIGN_OFF",
1655 "Non-standard signature: $sign_off\n" . $herecurr);
1656 }
Joe Perches20112472011-07-25 17:13:23 -07001657 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001658 if (WARN("BAD_SIGN_OFF",
1659 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1660 $fix) {
1661 $fixed[$linenr - 1] =
1662 "$ucfirst_sign_off $email";
1663 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001664 }
Joe Perches20112472011-07-25 17:13:23 -07001665 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001666 if (WARN("BAD_SIGN_OFF",
1667 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1668 $fix) {
1669 $fixed[$linenr - 1] =
1670 "$ucfirst_sign_off $email";
1671 }
1672
Joe Perches20112472011-07-25 17:13:23 -07001673 }
1674 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001675 if (WARN("BAD_SIGN_OFF",
1676 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1677 $fix) {
1678 $fixed[$linenr - 1] =
1679 "$ucfirst_sign_off $email";
1680 }
Joe Perches20112472011-07-25 17:13:23 -07001681 }
1682
1683 my ($email_name, $email_address, $comment) = parse_email($email);
1684 my $suggested_email = format_email(($email_name, $email_address));
1685 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001686 ERROR("BAD_SIGN_OFF",
1687 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001688 } else {
1689 my $dequoted = $suggested_email;
1690 $dequoted =~ s/^"//;
1691 $dequoted =~ s/" </ </;
1692 # Don't force email to have quotes
1693 # Allow just an angle bracketed address
1694 if ("$dequoted$comment" ne $email &&
1695 "<$email_address>$comment" ne $email &&
1696 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001697 WARN("BAD_SIGN_OFF",
1698 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001699 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001700 }
1701 }
1702
Andy Whitcroft00df3442007-06-08 13:47:06 -07001703# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001704 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001705 ERROR("CORRUPTED_PATCH",
1706 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001707 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001708 }
1709
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001710# Check for absolute kernel paths.
1711 if ($tree) {
1712 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1713 my $file = $1;
1714
1715 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1716 check_absolute_file($1, $herecurr)) {
1717 #
1718 } else {
1719 check_absolute_file($file, $herecurr);
1720 }
1721 }
1722 }
1723
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001724# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1725 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001726 $rawline !~ m/^$UTF8*$/) {
1727 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1728
1729 my $blank = copy_spacing($rawline);
1730 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1731 my $hereptr = "$hereline$ptr\n";
1732
Joe Perches34d99212011-07-25 17:13:26 -07001733 CHK("INVALID_UTF8",
1734 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001735 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001736
Joe Perches15662b32011-10-31 17:13:12 -07001737# Check if it's the start of a commit log
1738# (not a header line and we haven't seen the patch filename)
1739 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001740 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001741 $in_header_lines = 0;
1742 $in_commit_log = 1;
1743 }
1744
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001745# Check if there is UTF-8 in a commit log when a mail header has explicitly
1746# declined it, i.e defined some charset where it is missing.
1747 if ($in_header_lines &&
1748 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1749 $1 !~ /utf-8/i) {
1750 $non_utf8_charset = 1;
1751 }
1752
1753 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001754 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001755 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001756 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1757 }
1758
Andy Whitcroft306708542008-10-15 22:02:28 -07001759# ignore non-hunk lines and lines being removed
1760 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001761
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001762#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001763 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001764 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001765 ERROR("DOS_LINE_ENDINGS",
1766 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001767
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001768 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1769 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001770 if (ERROR("TRAILING_WHITESPACE",
1771 "trailing whitespace\n" . $herevet) &&
1772 $fix) {
1773 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1774 }
1775
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001776 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001777 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001778
Andi Kleen33549572010-05-24 14:33:29 -07001779# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001780# Only applies when adding the entry originally, after that we do not have
1781# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001782 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001783 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001784 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001785 my $cnt = $realcnt;
1786 my $ln = $linenr + 1;
1787 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001788 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001789 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001790 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001791 $f = $lines[$ln - 1];
1792 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1793 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001794
1795 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001796
1797 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1798 $is_start = 1;
1799 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1800 $length = -1;
1801 }
1802
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001803 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001804 $f =~ s/#.*//;
1805 $f =~ s/^\s+//;
1806 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001807 if ($f =~ /^\s*config\s/) {
1808 $is_end = 1;
1809 last;
1810 }
Andi Kleen33549572010-05-24 14:33:29 -07001811 $length++;
1812 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001813 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001814 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1815 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001816 }
1817
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001818# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1819 if ($realfile =~ /Kconfig/ &&
1820 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1821 WARN("CONFIG_EXPERIMENTAL",
1822 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1823 }
1824
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001825 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1826 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1827 my $flag = $1;
1828 my $replacement = {
1829 'EXTRA_AFLAGS' => 'asflags-y',
1830 'EXTRA_CFLAGS' => 'ccflags-y',
1831 'EXTRA_CPPFLAGS' => 'cppflags-y',
1832 'EXTRA_LDFLAGS' => 'ldflags-y',
1833 };
1834
1835 WARN("DEPRECATED_VARIABLE",
1836 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1837 }
1838
Andy Whitcroft5368df202008-10-15 22:02:27 -07001839# check we are in a valid source file if not then ignore this hunk
1840 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1841
Joe Perches6cd7f382012-12-17 16:01:54 -08001842#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001843 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001844 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001845 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001846 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001847 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001848 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001849 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001850 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001851 }
1852
Josh Triplettca56dc02012-03-23 15:02:21 -07001853# Check for user-visible strings broken across lines, which breaks the ability
1854# to grep for the string. Limited to strings used as parameters (those
1855# following an open parenthesis), which almost completely eliminates false
1856# positives, as well as warning only once per parameter rather than once per
1857# line of the string. Make an exception when the previous string ends in a
1858# newline (multiple lines in one string constant) or \n\t (common in inline
1859# assembly to indent the instruction on the following line).
1860 if ($line =~ /^\+\s*"/ &&
1861 $prevline =~ /"\s*$/ &&
1862 $prevline =~ /\(/ &&
1863 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1864 WARN("SPLIT_STRING",
1865 "quoted string split across lines\n" . $hereprev);
1866 }
1867
Joe Perches5e79d962010-03-05 13:43:55 -08001868# check for spaces before a quoted newline
1869 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07001870 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1871 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1872 $fix) {
1873 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1874 }
1875
Joe Perches5e79d962010-03-05 13:43:55 -08001876 }
1877
Andy Whitcroft8905a672007-11-28 16:21:06 -08001878# check for adding lines without a newline.
1879 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001880 WARN("MISSING_EOF_NEWLINE",
1881 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001882 }
1883
Mike Frysinger42e41c52009-09-21 17:04:40 -07001884# Blackfin: use hi/lo macros
1885 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1886 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1887 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001888 ERROR("LO_MACRO",
1889 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001890 }
1891 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1892 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001893 ERROR("HI_MACRO",
1894 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001895 }
1896 }
1897
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001898# check we are in a valid source file C or perl if not then ignore this hunk
1899 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001900
1901# at the beginning of a line any tabs must come first and anything
1902# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001903 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1904 $rawline =~ /^\+\s* \s*/) {
1905 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001906 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07001907 if (ERROR("CODE_INDENT",
1908 "code indent should use tabs where possible\n" . $herevet) &&
1909 $fix) {
1910 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
1911 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001912 }
1913
Alberto Panizzo08e44362010-03-05 13:43:54 -08001914# check for space before tabs.
1915 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1916 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001917 if (WARN("SPACE_BEFORE_TAB",
1918 "please, no space before tabs\n" . $herevet) &&
1919 $fix) {
1920 $fixed[$linenr - 1] =~
1921 s/(^\+.*) +\t/$1\t/;
1922 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08001923 }
1924
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001925# check for && or || at the start of a line
1926 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1927 CHK("LOGICAL_CONTINUATIONS",
1928 "Logical continuations should be on the previous line\n" . $hereprev);
1929 }
1930
1931# check multi-line statement indentation matches previous line
1932 if ($^V && $^V ge 5.10.0 &&
1933 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1934 $prevline =~ /^\+(\t*)(.*)$/;
1935 my $oldindent = $1;
1936 my $rest = $2;
1937
1938 my $pos = pos_last_openparen($rest);
1939 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07001940 $line =~ /^(\+| )([ \t]*)/;
1941 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001942
1943 my $goodtabindent = $oldindent .
1944 "\t" x ($pos / 8) .
1945 " " x ($pos % 8);
1946 my $goodspaceindent = $oldindent . " " x $pos;
1947
1948 if ($newindent ne $goodtabindent &&
1949 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07001950
1951 if (CHK("PARENTHESIS_ALIGNMENT",
1952 "Alignment should match open parenthesis\n" . $hereprev) &&
1953 $fix && $line =~ /^\+/) {
1954 $fixed[$linenr - 1] =~
1955 s/^\+[ \t]*/\+$goodtabindent/;
1956 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001957 }
1958 }
1959 }
1960
Joe Perches23f780c2013-07-03 15:05:31 -07001961 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07001962 if (CHK("SPACING",
1963 "No space is necessary after a cast\n" . $hereprev) &&
1964 $fix) {
1965 $fixed[$linenr - 1] =~
1966 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
1967 }
Joe Perchesaad4f612012-03-23 15:02:19 -07001968 }
1969
Joe Perches05880602012-10-04 17:13:35 -07001970 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07001971 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1972 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07001973 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1974 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1975 }
1976
1977 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07001978 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
1979 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1980 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1981 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1982 "networking block comments start with * on subsequent lines\n" . $hereprev);
1983 }
1984
1985 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08001986 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1987 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1988 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1989 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07001990 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1991 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1992 }
1993
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001994# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001995# Exceptions:
1996# 1) within comments
1997# 2) indented preprocessor commands
1998# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07001999 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002000 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002001 if (WARN("LEADING_SPACE",
2002 "please, no spaces at the start of a line\n" . $herevet) &&
2003 $fix) {
2004 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2005 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002006 }
2007
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002008# check we are in a valid C source file if not then ignore this hunk
2009 next if ($realfile !~ /\.(h|c)$/);
2010
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002011# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2012 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2013 WARN("CONFIG_EXPERIMENTAL",
2014 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2015 }
2016
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002017# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002018 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002019 WARN("CVS_KEYWORD",
2020 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002021 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002022
Mike Frysinger42e41c52009-09-21 17:04:40 -07002023# Blackfin: don't use __builtin_bfin_[cs]sync
2024 if ($line =~ /__builtin_bfin_csync/) {
2025 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002026 ERROR("CSYNC",
2027 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002028 }
2029 if ($line =~ /__builtin_bfin_ssync/) {
2030 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002031 ERROR("SSYNC",
2032 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002033 }
2034
Joe Perches56e77d72013-02-21 16:44:14 -08002035# check for old HOTPLUG __dev<foo> section markings
2036 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2037 WARN("HOTPLUG_SECTION",
2038 "Using $1 is unnecessary\n" . $herecurr);
2039 }
2040
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002041# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002042 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2043 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002044#print "LINE<$line>\n";
2045 if ($linenr >= $suppress_statement &&
2046 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002047 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002048 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002049 $stat =~ s/\n./\n /g;
2050 $cond =~ s/\n./\n /g;
2051
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002052#print "linenr<$linenr> <$stat>\n";
2053 # If this statement has no statement boundaries within
2054 # it there is no point in retrying a statement scan
2055 # until we hit end of it.
2056 my $frag = $stat; $frag =~ s/;+\s*$//;
2057 if ($frag !~ /(?:{|;)/) {
2058#print "skip<$line_nr_next>\n";
2059 $suppress_statement = $line_nr_next;
2060 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002061
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002062 # Find the real next line.
2063 $realline_next = $line_nr_next;
2064 if (defined $realline_next &&
2065 (!defined $lines[$realline_next - 1] ||
2066 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2067 $realline_next++;
2068 }
2069
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002070 my $s = $stat;
2071 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002072
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002073 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002074 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002075
2076 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002077 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002078
Andy Whitcroft463f2862009-09-21 17:04:34 -07002079 } elsif ($s =~ /^.\s*else\b/s) {
2080
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002081 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002082 } 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 -07002083 my $type = $1;
2084 $type =~ s/\s+/ /g;
2085 possible($type, "A:" . $s);
2086
Andy Whitcroft8905a672007-11-28 16:21:06 -08002087 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002088 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002089 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002090 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002091
2092 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002093 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002094 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002095 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002096
2097 # Check for any sort of function declaration.
2098 # int foo(something bar, other baz);
2099 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002100 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 -08002101 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002102
Andy Whitcroftcf655042008-03-04 14:28:20 -08002103 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002104 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002105 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002106
Andy Whitcroft8905a672007-11-28 16:21:06 -08002107 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002108 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002109
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002110 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002111 }
2112 }
2113 }
2114
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002115 }
2116
Andy Whitcroft653d4872007-06-23 17:16:34 -07002117#
2118# Checks which may be anchored in the context.
2119#
2120
2121# Check for switch () and associated case and default
2122# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002123 if ($line=~/\bswitch\s*\(.*\)/) {
2124 my $err = '';
2125 my $sep = '';
2126 my @ctx = ctx_block_outer($linenr, $realcnt);
2127 shift(@ctx);
2128 for my $ctx (@ctx) {
2129 my ($clen, $cindent) = line_stats($ctx);
2130 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2131 $indent != $cindent) {
2132 $err .= "$sep$ctx\n";
2133 $sep = '';
2134 } else {
2135 $sep = "[...]\n";
2136 }
2137 }
2138 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002139 ERROR("SWITCH_CASE_INDENT_LEVEL",
2140 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002141 }
2142 }
2143
2144# if/while/etc brace do not go on next line, unless defining a do while loop,
2145# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002146 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002147 my $pre_ctx = "$1$2";
2148
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002149 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002150
2151 if ($line =~ /^\+\t{6,}/) {
2152 WARN("DEEP_INDENTATION",
2153 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2154 }
2155
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002156 my $ctx_cnt = $realcnt - $#ctx - 1;
2157 my $ctx = join("\n", @ctx);
2158
Andy Whitcroft548596d2008-07-23 21:29:01 -07002159 my $ctx_ln = $linenr;
2160 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002161
Andy Whitcroft548596d2008-07-23 21:29:01 -07002162 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2163 defined $lines[$ctx_ln - 1] &&
2164 $lines[$ctx_ln - 1] =~ /^-/)) {
2165 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2166 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002167 $ctx_ln++;
2168 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002169
Andy Whitcroft53210162008-07-23 21:29:03 -07002170 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2171 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002172
2173 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002174 ERROR("OPEN_BRACE",
2175 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002176 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002177 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002178 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2179 $ctx =~ /\)\s*\;\s*$/ &&
2180 defined $lines[$ctx_ln - 1])
2181 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002182 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2183 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002184 WARN("TRAILING_SEMICOLON",
2185 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002186 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002187 }
2188 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002189 }
2190
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002191# Check relative indent for conditionals and blocks.
2192 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002193 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2194 ctx_statement_block($linenr, $realcnt, 0)
2195 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002196 my ($s, $c) = ($stat, $cond);
2197
2198 substr($s, 0, length($c), '');
2199
2200 # Make sure we remove the line prefixes as we have
2201 # none on the first line, and are going to readd them
2202 # where necessary.
2203 $s =~ s/\n./\n/gs;
2204
2205 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002206 my @newlines = ($c =~ /\n/gs);
2207 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002208
2209 # We want to check the first line inside the block
2210 # starting at the end of the conditional, so remove:
2211 # 1) any blank line termination
2212 # 2) any opening brace { on end of the line
2213 # 3) any do (...) {
2214 my $continuation = 0;
2215 my $check = 0;
2216 $s =~ s/^.*\bdo\b//;
2217 $s =~ s/^\s*{//;
2218 if ($s =~ s/^\s*\\//) {
2219 $continuation = 1;
2220 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002221 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002222 $check = 1;
2223 $cond_lines++;
2224 }
2225
2226 # Also ignore a loop construct at the end of a
2227 # preprocessor statement.
2228 if (($prevline =~ /^.\s*#\s*define\s/ ||
2229 $prevline =~ /\\\s*$/) && $continuation == 0) {
2230 $check = 0;
2231 }
2232
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002233 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002234 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002235 while ($cond_ptr != $cond_lines) {
2236 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002237
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002238 # If we see an #else/#elif then the code
2239 # is not linear.
2240 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2241 $check = 0;
2242 }
2243
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002244 # Ignore:
2245 # 1) blank lines, they should be at 0,
2246 # 2) preprocessor lines, and
2247 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002248 if ($continuation ||
2249 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002250 $s =~ /^\s*#\s*?/ ||
2251 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002252 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002253 if ($s =~ s/^.*?\n//) {
2254 $cond_lines++;
2255 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002256 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002257 }
2258
2259 my (undef, $sindent) = line_stats("+" . $s);
2260 my $stat_real = raw_line($linenr, $cond_lines);
2261
2262 # Check if either of these lines are modified, else
2263 # this is not this patch's fault.
2264 if (!defined($stat_real) ||
2265 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2266 $check = 0;
2267 }
2268 if (defined($stat_real) && $cond_lines > 1) {
2269 $stat_real = "[...]\n$stat_real";
2270 }
2271
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002272 #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 -07002273
2274 if ($check && (($sindent % 8) != 0 ||
2275 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002276 WARN("SUSPECT_CODE_INDENT",
2277 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002278 }
2279 }
2280
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002281 # Track the 'values' across context and added lines.
2282 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002283 my ($curr_values, $curr_vars) =
2284 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002285 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002286 if ($dbg_values) {
2287 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002288 print "$linenr > .$outline\n";
2289 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002290 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002291 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002292 $prev_values = substr($curr_values, -1);
2293
Andy Whitcroft00df3442007-06-08 13:47:06 -07002294#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002295 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002296
Andy Whitcroft653d4872007-06-23 17:16:34 -07002297# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002298 if ($dbg_type) {
2299 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002300 ERROR("TEST_TYPE",
2301 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002302 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002303 ERROR("TEST_NOT_TYPE",
2304 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002305 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002306 next;
2307 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002308# TEST: allow direct testing of the attribute matcher.
2309 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002310 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002311 ERROR("TEST_ATTR",
2312 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002313 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002314 ERROR("TEST_NOT_ATTR",
2315 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002316 }
2317 next;
2318 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002319
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002320# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002321 if ($line =~ /^.\s*{/ &&
2322 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002323 ERROR("OPEN_BRACE",
2324 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002325 }
2326
Andy Whitcroft653d4872007-06-23 17:16:34 -07002327#
2328# Checks which are anchored on the added line.
2329#
2330
2331# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002332 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002333 my $path = $1;
2334 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002335 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002336 "malformed #include filename\n" . $herecurr);
2337 }
2338 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2339 ERROR("UAPI_INCLUDE",
2340 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002341 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002342 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002343
2344# no C99 // comments
2345 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002346 if (ERROR("C99_COMMENTS",
2347 "do not use C99 // comments\n" . $herecurr) &&
2348 $fix) {
2349 my $line = $fixed[$linenr - 1];
2350 if ($line =~ /\/\/(.*)$/) {
2351 my $comment = trim($1);
2352 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2353 }
2354 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002355 }
2356 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002357 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002358 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002359
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002360# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2361# the whole statement.
2362#print "APW <$lines[$realline_next - 1]>\n";
2363 if (defined $realline_next &&
2364 exists $lines[$realline_next - 1] &&
2365 !defined $suppress_export{$realline_next} &&
2366 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2367 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002368 # Handle definitions which produce identifiers with
2369 # a prefix:
2370 # XXX(foo);
2371 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002372 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002373 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002374 $name =~ /^${Ident}_$2/) {
2375#print "FOO C name<$name>\n";
2376 $suppress_export{$realline_next} = 1;
2377
2378 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002379 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002380 ^.DEFINE_$Ident\(\Q$name\E\)|
2381 ^.DECLARE_$Ident\(\Q$name\E\)|
2382 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002383 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2384 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002385 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002386#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2387 $suppress_export{$realline_next} = 2;
2388 } else {
2389 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002390 }
2391 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002392 if (!defined $suppress_export{$linenr} &&
2393 $prevline =~ /^.\s*$/ &&
2394 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2395 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2396#print "FOO B <$lines[$linenr - 1]>\n";
2397 $suppress_export{$linenr} = 2;
2398 }
2399 if (defined $suppress_export{$linenr} &&
2400 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002401 WARN("EXPORT_SYMBOL",
2402 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002403 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002404
Joe Eloff5150bda2010-08-09 17:21:00 -07002405# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002406 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002407 ERROR("GLOBAL_INITIALISERS",
2408 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002409 $herecurr);
2410 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002411# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002412 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002413 ERROR("INITIALISED_STATIC",
2414 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002415 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002416 }
2417
Joe Perchescb710ec2010-10-26 14:23:20 -07002418# check for static const char * arrays.
2419 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002420 WARN("STATIC_CONST_CHAR_ARRAY",
2421 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002422 $herecurr);
2423 }
2424
2425# check for static char foo[] = "bar" declarations.
2426 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002427 WARN("STATIC_CONST_CHAR_ARRAY",
2428 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002429 $herecurr);
2430 }
2431
Joe Perches93ed0e22010-10-26 14:23:21 -07002432# check for declarations of struct pci_device_id
2433 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002434 WARN("DEFINE_PCI_DEVICE_TABLE",
2435 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002436 }
2437
Andy Whitcroft653d4872007-06-23 17:16:34 -07002438# check for new typedefs, only function parameters and sparse annotations
2439# make sense.
2440 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002441 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002442 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002443 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002444 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002445 WARN("NEW_TYPEDEFS",
2446 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002447 }
2448
2449# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002450 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002451 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2452 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002453 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002454
Andy Whitcroft65863862009-01-06 14:41:21 -08002455 # Should start with a space.
2456 $to =~ s/^(\S)/ $1/;
2457 # Should not end with a space.
2458 $to =~ s/\s+$//;
2459 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002460 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002461 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002462
Joe Perches3705ce52013-07-03 15:05:31 -07002463## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002464 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002465 if (ERROR("POINTER_LOCATION",
2466 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2467 $fix) {
2468 my $sub_from = $ident;
2469 my $sub_to = $ident;
2470 $sub_to =~ s/\Q$from\E/$to/;
2471 $fixed[$linenr - 1] =~
2472 s@\Q$sub_from\E@$sub_to@;
2473 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002474 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002475 }
2476 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2477 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002478 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002479
Andy Whitcroft65863862009-01-06 14:41:21 -08002480 # Should start with a space.
2481 $to =~ s/^(\S)/ $1/;
2482 # Should not end with a space.
2483 $to =~ s/\s+$//;
2484 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002485 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002486 }
2487 # Modifiers should have spaces.
2488 $to =~ s/(\b$Modifier$)/$1 /;
2489
Joe Perches3705ce52013-07-03 15:05:31 -07002490## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002491 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002492 if (ERROR("POINTER_LOCATION",
2493 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2494 $fix) {
2495
2496 my $sub_from = $match;
2497 my $sub_to = $match;
2498 $sub_to =~ s/\Q$from\E/$to/;
2499 $fixed[$linenr - 1] =~
2500 s@\Q$sub_from\E@$sub_to@;
2501 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002502 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002503 }
2504
2505# # no BUG() or BUG_ON()
2506# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2507# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2508# print "$herecurr";
2509# $clean = 0;
2510# }
2511
Andy Whitcroft8905a672007-11-28 16:21:06 -08002512 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002513 WARN("LINUX_VERSION_CODE",
2514 "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 -08002515 }
2516
Joe Perches17441222011-06-15 15:08:17 -07002517# check for uses of printk_ratelimit
2518 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002519 WARN("PRINTK_RATELIMITED",
2520"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002521 }
2522
Andy Whitcroft00df3442007-06-08 13:47:06 -07002523# printk should use KERN_* levels. Note that follow on printk's on the
2524# same line do not need a level, so we use the current block context
2525# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002526# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002527# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002528 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002529 my $ok = 0;
2530 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2531 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002532 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002533 # with "\n" ignore it, else it is to blame
2534 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2535 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2536 $ok = 1;
2537 }
2538 last;
2539 }
2540 }
2541 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002542 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2543 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002544 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002545 }
2546
Joe Perches243f3802012-05-31 16:26:09 -07002547 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2548 my $orig = $1;
2549 my $level = lc($orig);
2550 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002551 my $level2 = $level;
2552 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002553 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002554 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002555 }
2556
2557 if ($line =~ /\bpr_warning\s*\(/) {
2558 WARN("PREFER_PR_LEVEL",
2559 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2560 }
2561
Joe Perchesdc139312013-02-21 16:44:13 -08002562 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2563 my $orig = $1;
2564 my $level = lc($orig);
2565 $level = "warn" if ($level eq "warning");
2566 $level = "dbg" if ($level eq "debug");
2567 WARN("PREFER_DEV_LEVEL",
2568 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2569 }
2570
Andy Whitcroft653d4872007-06-23 17:16:34 -07002571# function brace can't be on same line, except for #defines of do while,
2572# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002573 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2574 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002575 ERROR("OPEN_BRACE",
2576 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002577 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002578
Andy Whitcroft8905a672007-11-28 16:21:06 -08002579# open braces for enum, union and struct go on the same line.
2580 if ($line =~ /^.\s*{/ &&
2581 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002582 ERROR("OPEN_BRACE",
2583 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002584 }
2585
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002586# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002587 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2588 if (WARN("SPACING",
2589 "missing space after $1 definition\n" . $herecurr) &&
2590 $fix) {
2591 $fixed[$linenr - 1] =~
2592 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2593 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002594 }
2595
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002596# check for spacing round square brackets; allowed:
2597# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002598# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2599# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002600 while ($line =~ /(.*?\s)\[/g) {
2601 my ($where, $prefix) = ($-[1], $1);
2602 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002603 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002604 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002605 if (ERROR("BRACKET_SPACE",
2606 "space prohibited before open square bracket '['\n" . $herecurr) &&
2607 $fix) {
2608 $fixed[$linenr - 1] =~
2609 s/^(\+.*?)\s+\[/$1\[/;
2610 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002611 }
2612 }
2613
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002614# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002615 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002616 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002617 my $ctx_before = substr($line, 0, $-[1]);
2618 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002619
2620 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002621 if ($name =~ /^(?:
2622 if|for|while|switch|return|case|
2623 volatile|__volatile__|
2624 __attribute__|format|__extension__|
2625 asm|__asm__)$/x)
2626 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002627 # cpp #define statements have non-optional spaces, ie
2628 # if there is a space between the name and the open
2629 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002630 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002631
2632 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002633 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002634
2635 # If this whole things ends with a type its most
2636 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002637 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002638
2639 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002640 if (WARN("SPACING",
2641 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2642 $fix) {
2643 $fixed[$linenr - 1] =~
2644 s/\b$name\s+\(/$name\(/;
2645 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002646 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002647 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002648
Andy Whitcroft653d4872007-06-23 17:16:34 -07002649# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002650 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002651 my $fixed_line = "";
2652 my $line_fixed = 0;
2653
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002654 my $ops = qr{
2655 <<=|>>=|<=|>=|==|!=|
2656 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2657 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002658 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2659 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002660 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002661 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002662
2663## print("element count: <" . $#elements . ">\n");
2664## foreach my $el (@elements) {
2665## print("el: <$el>\n");
2666## }
2667
2668 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07002669 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002670
Joe Perches3705ce52013-07-03 15:05:31 -07002671 foreach my $el (@elements) {
2672 push(@fix_elements, substr($rawline, $off, length($el)));
2673 $off += length($el);
2674 }
2675
2676 $off = 0;
2677
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002678 my $blank = copy_spacing($opline);
2679
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002680 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002681
2682 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2683
2684## print("n: <$n> good: <$good>\n");
2685
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002686 $off += length($elements[$n]);
2687
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002688 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002689 my $ca = substr($opline, 0, $off);
2690 my $cc = '';
2691 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2692 $cc = substr($opline, $off + length($elements[$n + 1]));
2693 }
2694 my $cb = "$ca$;$cc";
2695
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002696 my $a = '';
2697 $a = 'V' if ($elements[$n] ne '');
2698 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002699 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002700 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2701 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002702 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002703
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002704 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002705
2706 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002707 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002708 $c = 'V' if ($elements[$n + 2] ne '');
2709 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002710 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002711 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2712 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002713 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002714 } else {
2715 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002716 }
2717
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002718 my $ctx = "${a}x${c}";
2719
2720 my $at = "(ctx:$ctx)";
2721
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002722 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002723 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002724
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002725 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002726 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002727
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002728 # Get the full operator variant.
2729 my $opv = $op . substr($curr_vars, $off, 1);
2730
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002731 # Ignore operators passed as parameters.
2732 if ($op_type ne 'V' &&
2733 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2734
Andy Whitcroftcf655042008-03-04 14:28:20 -08002735# # Ignore comments
2736# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002737
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002738 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002739 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002740 if ($ctx !~ /.x[WEBC]/ &&
2741 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002742 if (ERROR("SPACING",
2743 "space required after that '$op' $at\n" . $hereptr)) {
2744 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2745 $line_fixed = 1;
2746 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002747 }
2748
2749 # // is a comment
2750 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002751
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002752 # No spaces for:
2753 # ->
2754 # : when part of a bitfield
2755 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002756 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002757 if (ERROR("SPACING",
2758 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2759 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2760 $line_fixed = 1;
2761 if (defined $fix_elements[$n + 2]) {
2762 $fix_elements[$n + 2] =~ s/^\s+//;
2763 }
2764 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002765 }
2766
2767 # , must have a space on the right.
2768 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002769 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002770 if (ERROR("SPACING",
2771 "space required after that '$op' $at\n" . $hereptr)) {
2772 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2773 $line_fixed = 1;
2774 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002775 }
2776
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002777 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002778 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002779 #warn "'*' is part of type\n";
2780
2781 # unary operators should have a space before and
2782 # none after. May be left adjacent to another
2783 # unary operator, or a cast
2784 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002785 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002786 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002787 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002788 if (ERROR("SPACING",
2789 "space required before that '$op' $at\n" . $hereptr)) {
2790 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2791 $line_fixed = 1;
2792 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002793 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002794 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002795 # A unary '*' may be const
2796
2797 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002798 if (ERROR("SPACING",
2799 "space prohibited after that '$op' $at\n" . $hereptr)) {
2800 $fixed_line =~ s/\s+$//;
2801 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2802 $line_fixed = 1;
2803 if (defined $fix_elements[$n + 2]) {
2804 $fix_elements[$n + 2] =~ s/^\s+//;
2805 }
2806 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002807 }
2808
2809 # unary ++ and unary -- are allowed no space on one side.
2810 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002811 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002812 if (ERROR("SPACING",
2813 "space required one side of that '$op' $at\n" . $hereptr)) {
2814 $fixed_line =~ s/\s+$//;
2815 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2816 $line_fixed = 1;
2817 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002818 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002819 if ($ctx =~ /Wx[BE]/ ||
2820 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002821 if (ERROR("SPACING",
2822 "space prohibited before that '$op' $at\n" . $hereptr)) {
2823 $fixed_line =~ s/\s+$//;
2824 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2825 $line_fixed = 1;
2826 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002827 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002828 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002829 if (ERROR("SPACING",
2830 "space prohibited after that '$op' $at\n" . $hereptr)) {
2831 $fixed_line =~ s/\s+$//;
2832 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2833 $line_fixed = 1;
2834 if (defined $fix_elements[$n + 2]) {
2835 $fix_elements[$n + 2] =~ s/^\s+//;
2836 }
2837 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002838 }
2839
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002840 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002841 } elsif ($op eq '<<' or $op eq '>>' or
2842 $op eq '&' or $op eq '^' or $op eq '|' or
2843 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002844 $op eq '*' or $op eq '/' or
2845 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002846 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002847 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002848 if (ERROR("SPACING",
2849 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2850 $fixed_line =~ s/\s+$//;
2851 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2852 $line_fixed = 1;
2853 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002854 }
2855
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002856 # A colon needs no spaces before when it is
2857 # terminating a case value or a label.
2858 } elsif ($opv eq ':C' || $opv eq ':L') {
2859 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07002860 if (ERROR("SPACING",
2861 "space prohibited before that '$op' $at\n" . $hereptr)) {
2862 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2863 $line_fixed = 1;
2864 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002865 }
2866
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002867 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002868 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002869 my $ok = 0;
2870
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002871 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002872 if (($op eq '<' &&
2873 $cc =~ /^\S+\@\S+>/) ||
2874 ($op eq '>' &&
2875 $ca =~ /<\S+\@\S+$/))
2876 {
2877 $ok = 1;
2878 }
2879
2880 # Ignore ?:
2881 if (($opv eq ':O' && $ca =~ /\?$/) ||
2882 ($op eq '?' && $cc =~ /^:/)) {
2883 $ok = 1;
2884 }
2885
2886 if ($ok == 0) {
Joe Perches3705ce52013-07-03 15:05:31 -07002887 if (ERROR("SPACING",
2888 "spaces required around that '$op' $at\n" . $hereptr)) {
2889 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2890 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
2891 $line_fixed = 1;
2892 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002893 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002894 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002895 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002896
2897## print("n: <$n> GOOD: <$good>\n");
2898
2899 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002900 }
Joe Perches3705ce52013-07-03 15:05:31 -07002901
2902 if (($#elements % 2) == 0) {
2903 $fixed_line = $fixed_line . $fix_elements[$#elements];
2904 }
2905
2906 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2907 $fixed[$linenr - 1] = $fixed_line;
2908 }
2909
2910
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002911 }
2912
Joe Perches786b6322013-07-03 15:05:32 -07002913# check for whitespace before a non-naked semicolon
2914 if ($line =~ /^\+.*\S\s+;/) {
2915 if (WARN("SPACING",
2916 "space prohibited before semicolon\n" . $herecurr) &&
2917 $fix) {
2918 1 while $fixed[$linenr - 1] =~
2919 s/^(\+.*\S)\s+;/$1;/;
2920 }
2921 }
2922
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002923# check for multiple assignments
2924 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002925 CHK("MULTIPLE_ASSIGNMENTS",
2926 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002927 }
2928
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002929## # check for multiple declarations, allowing for a function declaration
2930## # continuation.
2931## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2932## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2933##
2934## # Remove any bracketed sections to ensure we do not
2935## # falsly report the parameters of functions.
2936## my $ln = $line;
2937## while ($ln =~ s/\([^\(\)]*\)//g) {
2938## }
2939## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002940## WARN("MULTIPLE_DECLARATION",
2941## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002942## }
2943## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002944
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002945#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002946 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2947 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002948 if (ERROR("SPACING",
2949 "space required before the open brace '{'\n" . $herecurr) &&
2950 $fix) {
2951 $fixed[$linenr - 1] =~
2952 s/^(\+.*(?:do|\))){/$1 {/;
2953 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002954 }
2955
Joe Perchesc4a62ef2013-07-03 15:05:28 -07002956## # check for blank lines before declarations
2957## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
2958## $prevrawline =~ /^.\s*$/) {
2959## WARN("SPACING",
2960## "No blank lines before declarations\n" . $hereprev);
2961## }
2962##
2963
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002964# closing brace should have a space following it when it has anything
2965# on the line
2966 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002967 ERROR("SPACING",
2968 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002969 }
2970
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002971# check spacing on square brackets
2972 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002973 if (ERROR("SPACING",
2974 "space prohibited after that open square bracket '['\n" . $herecurr) &&
2975 $fix) {
2976 $fixed[$linenr - 1] =~
2977 s/\[\s+/\[/;
2978 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002979 }
2980 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002981 if (ERROR("SPACING",
2982 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
2983 $fix) {
2984 $fixed[$linenr - 1] =~
2985 s/\s+\]/\]/;
2986 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002987 }
2988
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002989# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002990 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2991 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002992 if (ERROR("SPACING",
2993 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
2994 $fix) {
2995 $fixed[$linenr - 1] =~
2996 s/\(\s+/\(/;
2997 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002998 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002999 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003000 $line !~ /for\s*\(.*;\s+\)/ &&
3001 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003002 if (ERROR("SPACING",
3003 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3004 $fix) {
3005 $fixed[$linenr - 1] =~
3006 s/\s+\)/\)/;
3007 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003008 }
3009
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003010#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003011 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003012 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003013 if (WARN("INDENTED_LABEL",
3014 "labels should not be indented\n" . $herecurr) &&
3015 $fix) {
3016 $fixed[$linenr - 1] =~
3017 s/^(.)\s+/$1/;
3018 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003019 }
3020
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003021# Return is not a function.
3022 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3023 my $spacing = $1;
3024 my $value = $2;
3025
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003026 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003027 $value =~ s/\(/ \(/g;
3028 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003029 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003030 $value !~ /(?:$Ident|-?$Constant)\s*
3031 $Compare\s*
3032 (?:$Ident|-?$Constant)/x &&
3033 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003034 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003035#print "value<$value>\n";
3036 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003037 ERROR("RETURN_PARENTHESES",
3038 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003039
3040 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003041 ERROR("SPACING",
3042 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003043 }
3044 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003045# Return of what appears to be an errno should normally be -'ve
3046 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3047 my $name = $1;
3048 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003049 WARN("USE_NEGATIVE_ERRNO",
3050 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003051 }
3052 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003053
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003054# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003055 if ($line =~ /\b(if|while|for|switch)\(/) {
3056 if (ERROR("SPACING",
3057 "space required before the open parenthesis '('\n" . $herecurr) &&
3058 $fix) {
3059 $fixed[$linenr - 1] =~
3060 s/\b(if|while|for|switch)\(/$1 \(/;
3061 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003062 }
3063
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003064# Check for illegal assignment in if conditional -- and check for trailing
3065# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003066 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003067 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3068 ctx_statement_block($linenr, $realcnt, 0)
3069 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003070 my ($stat_next) = ctx_statement_block($line_nr_next,
3071 $remain_next, $off_next);
3072 $stat_next =~ s/\n./\n /g;
3073 ##print "stat<$stat> stat_next<$stat_next>\n";
3074
3075 if ($stat_next =~ /^\s*while\b/) {
3076 # If the statement carries leading newlines,
3077 # then count those as offsets.
3078 my ($whitespace) =
3079 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3080 my $offset =
3081 statement_rawlines($whitespace) - 1;
3082
3083 $suppress_whiletrailers{$line_nr_next +
3084 $offset} = 1;
3085 }
3086 }
3087 if (!defined $suppress_whiletrailers{$linenr} &&
3088 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003089 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003090
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003091 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003092 ERROR("ASSIGN_IN_IF",
3093 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003094 }
3095
3096 # Find out what is on the end of the line after the
3097 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003098 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003099 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003100 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003101 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3102 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003103 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003104 # Find out how long the conditional actually is.
3105 my @newlines = ($c =~ /\n/gs);
3106 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003107 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003108
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003109 $stat_real = raw_line($linenr, $cond_lines)
3110 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003111 if (defined($stat_real) && $cond_lines > 1) {
3112 $stat_real = "[...]\n$stat_real";
3113 }
3114
Joe Perches000d1cc12011-07-25 17:13:25 -07003115 ERROR("TRAILING_STATEMENTS",
3116 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003117 }
3118 }
3119
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003120# Check for bitwise tests written as boolean
3121 if ($line =~ /
3122 (?:
3123 (?:\[|\(|\&\&|\|\|)
3124 \s*0[xX][0-9]+\s*
3125 (?:\&\&|\|\|)
3126 |
3127 (?:\&\&|\|\|)
3128 \s*0[xX][0-9]+\s*
3129 (?:\&\&|\|\||\)|\])
3130 )/x)
3131 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003132 WARN("HEXADECIMAL_BOOLEAN_TEST",
3133 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003134 }
3135
Andy Whitcroft8905a672007-11-28 16:21:06 -08003136# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003137 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3138 my $s = $1;
3139 $s =~ s/$;//g; # Remove any comments
3140 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003141 ERROR("TRAILING_STATEMENTS",
3142 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003143 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003144 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003145# if should not continue a brace
3146 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003147 ERROR("TRAILING_STATEMENTS",
3148 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003149 $herecurr);
3150 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003151# case and default should not have general statements after them
3152 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3153 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003154 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003155 \s*return\s+
3156 )/xg)
3157 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003158 ERROR("TRAILING_STATEMENTS",
3159 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003160 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003161
3162 # Check for }<nl>else {, these must be at the same
3163 # indent level to be relevant to each other.
3164 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3165 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003166 ERROR("ELSE_AFTER_BRACE",
3167 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003168 }
3169
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003170 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3171 $previndent == $indent) {
3172 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3173
3174 # Find out what is on the end of the line after the
3175 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003176 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003177 $s =~ s/\n.*//g;
3178
3179 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003180 ERROR("WHILE_AFTER_BRACE",
3181 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003182 }
3183 }
3184
Joe Perches95e2c602013-07-03 15:05:20 -07003185#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003186 while ($line =~ m{($Constant|$Lval)}g) {
3187 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003188
3189#gcc binary extension
3190 if ($var =~ /^$Binary$/) {
3191 WARN("GCC_BINARY_CONSTANT",
3192 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
3193 }
3194
3195#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003196 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003197 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches807bd262013-07-03 15:05:22 -07003198 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches323c1262012-12-17 16:02:07 -08003199 !defined $camelcase{$var}) {
3200 $camelcase{$var} = 1;
Joe Perchesbe797942013-07-03 15:05:20 -07003201 CHK("CAMELCASE",
3202 "Avoid CamelCase: <$var>\n" . $herecurr);
Joe Perches323c1262012-12-17 16:02:07 -08003203 }
3204 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003205
3206#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003207 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003208 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3209 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003210 }
3211
Andy Whitcroft653d4872007-06-23 17:16:34 -07003212#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003213 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003214 my $file = "$1.h";
3215 my $checkfile = "include/linux/$file";
3216 if (-f "$root/$checkfile" &&
3217 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003218 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003219 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003220 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003221 CHK("ARCH_INCLUDE_LINUX",
3222 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003223 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003224 WARN("INCLUDE_LINUX",
3225 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003226 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003227 }
3228 }
3229
Andy Whitcroft653d4872007-06-23 17:16:34 -07003230# multi-statement macros should be enclosed in a do while loop, grab the
3231# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003232# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07003233 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3234 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003235 my $ln = $linenr;
3236 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003237 my ($off, $dstat, $dcond, $rest);
3238 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003239 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003240 ctx_statement_block($linenr, $realcnt, 0);
3241 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003242 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003243 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003244
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003245 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003246 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003247 $dstat =~ s/\\\n.//g;
3248 $dstat =~ s/^\s*//s;
3249 $dstat =~ s/\s*$//s;
3250
3251 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003252 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3253 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003254 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003255 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003256 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003257
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003258 # Flatten any obvious string concatentation.
3259 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3260 $dstat =~ s/$Ident\s*("X*")/$1/)
3261 {
3262 }
3263
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003264 my $exceptions = qr{
3265 $Declare|
3266 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003267 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003268 DECLARE_PER_CPU|
3269 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003270 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003271 union|
3272 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003273 \.$Ident\s*=\s*|
3274 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003275 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003276 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003277 if ($dstat ne '' &&
3278 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3279 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003280 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003281 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003282 $dstat !~ /$exceptions/ &&
3283 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003284 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003285 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003286 $dstat !~ /^for\s*$Constant$/ && # for (...)
3287 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3288 $dstat !~ /^do\s*{/ && # do {...
3289 $dstat !~ /^\({/) # ({...
3290 {
3291 $ctx =~ s/\n*$//;
3292 my $herectx = $here . "\n";
3293 my $cnt = statement_rawlines($ctx);
3294
3295 for (my $n = 0; $n < $cnt; $n++) {
3296 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003297 }
3298
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003299 if ($dstat =~ /;/) {
3300 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3301 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3302 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003303 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003304 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003305 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003306 }
Joe Perches5023d342012-12-17 16:01:47 -08003307
Joe Perches481eb482012-12-17 16:01:56 -08003308# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003309
3310 } else {
3311 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003312 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3313 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003314 $line =~ /^\+.*\\$/) {
3315 WARN("LINE_CONTINUATIONS",
3316 "Avoid unnecessary line continuations\n" . $herecurr);
3317 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003318 }
3319
Joe Perchesb13edf72012-07-30 14:41:24 -07003320# do {} while (0) macro tests:
3321# single-statement macros do not need to be enclosed in do while (0) loop,
3322# macro should not end with a semicolon
3323 if ($^V && $^V ge 5.10.0 &&
3324 $realfile !~ m@/vmlinux.lds.h$@ &&
3325 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3326 my $ln = $linenr;
3327 my $cnt = $realcnt;
3328 my ($off, $dstat, $dcond, $rest);
3329 my $ctx = '';
3330 ($dstat, $dcond, $ln, $cnt, $off) =
3331 ctx_statement_block($linenr, $realcnt, 0);
3332 $ctx = $dstat;
3333
3334 $dstat =~ s/\\\n.//g;
3335
3336 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3337 my $stmts = $2;
3338 my $semis = $3;
3339
3340 $ctx =~ s/\n*$//;
3341 my $cnt = statement_rawlines($ctx);
3342 my $herectx = $here . "\n";
3343
3344 for (my $n = 0; $n < $cnt; $n++) {
3345 $herectx .= raw_line($linenr, $n) . "\n";
3346 }
3347
Joe Perchesac8e97f2012-08-21 16:15:53 -07003348 if (($stmts =~ tr/;/;/) == 1 &&
3349 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003350 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3351 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3352 }
3353 if (defined $semis && $semis ne "") {
3354 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3355 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3356 }
3357 }
3358 }
3359
Mike Frysinger080ba922009-01-06 14:41:25 -08003360# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3361# all assignments may have only one of the following with an assignment:
3362# .
3363# ALIGN(...)
3364# VMLINUX_SYMBOL(...)
3365 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003366 WARN("MISSING_VMLINUX_SYMBOL",
3367 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003368 }
3369
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003370# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003371 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3372 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003373 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003374 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003375 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3376 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003377 my @allowed = ();
3378 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003379 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003380 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003381 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003382 for my $chunk (@chunks) {
3383 my ($cond, $block) = @{$chunk};
3384
Andy Whitcroft773647a2008-03-28 14:15:58 -07003385 # If the condition carries leading newlines, then count those as offsets.
3386 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3387 my $offset = statement_rawlines($whitespace) - 1;
3388
Joe Perchesaad4f612012-03-23 15:02:19 -07003389 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003390 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3391
3392 # We have looked at and allowed this specific line.
3393 $suppress_ifbraces{$ln + $offset} = 1;
3394
3395 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003396 $ln += statement_rawlines($block) - 1;
3397
Andy Whitcroft773647a2008-03-28 14:15:58 -07003398 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003399
3400 $seen++ if ($block =~ /^\s*{/);
3401
Joe Perchesaad4f612012-03-23 15:02:19 -07003402 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003403 if (statement_lines($cond) > 1) {
3404 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003405 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003406 }
3407 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003408 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003409 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003410 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003411 if (statement_block_size($block) > 1) {
3412 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003413 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003414 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003415 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003416 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003417 if ($seen) {
3418 my $sum_allowed = 0;
3419 foreach (@allowed) {
3420 $sum_allowed += $_;
3421 }
3422 if ($sum_allowed == 0) {
3423 WARN("BRACES",
3424 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3425 } elsif ($sum_allowed != $allow &&
3426 $seen != $allow) {
3427 CHK("BRACES",
3428 "braces {} should be used on all arms of this statement\n" . $herectx);
3429 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003430 }
3431 }
3432 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003433 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003434 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003435 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003436
Andy Whitcroftcf655042008-03-04 14:28:20 -08003437 # Check the pre-context.
3438 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3439 #print "APW: ALLOWED: pre<$1>\n";
3440 $allowed = 1;
3441 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003442
3443 my ($level, $endln, @chunks) =
3444 ctx_statement_full($linenr, $realcnt, $-[0]);
3445
Andy Whitcroftcf655042008-03-04 14:28:20 -08003446 # Check the condition.
3447 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003448 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003449 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003450 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003451 }
3452 if (statement_lines($cond) > 1) {
3453 #print "APW: ALLOWED: cond<$cond>\n";
3454 $allowed = 1;
3455 }
3456 if ($block =~/\b(?:if|for|while)\b/) {
3457 #print "APW: ALLOWED: block<$block>\n";
3458 $allowed = 1;
3459 }
3460 if (statement_block_size($block) > 1) {
3461 #print "APW: ALLOWED: lines block<$block>\n";
3462 $allowed = 1;
3463 }
3464 # Check the post-context.
3465 if (defined $chunks[1]) {
3466 my ($cond, $block) = @{$chunks[1]};
3467 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003468 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003469 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003470 if ($block =~ /^\s*\{/) {
3471 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3472 $allowed = 1;
3473 }
3474 }
3475 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003476 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003477 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003478
Andy Whitcroftf0556632008-10-15 22:02:23 -07003479 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003480 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003481 }
3482
Joe Perches000d1cc12011-07-25 17:13:25 -07003483 WARN("BRACES",
3484 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003485 }
3486 }
3487
Joe Perches0979ae62012-12-17 16:01:59 -08003488# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003489 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003490 CHK("BRACES",
3491 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3492 }
Joe Perches77b9a532013-07-03 15:05:29 -07003493 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003494 CHK("BRACES",
3495 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3496 }
3497
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003498# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003499 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3500 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003501 WARN("VOLATILE",
3502 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003503 }
3504
Andy Whitcroft00df3442007-06-08 13:47:06 -07003505# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003506 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003507 CHK("REDUNDANT_CODE",
3508 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003509 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003510 }
3511
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003512# check for needless "if (<foo>) fn(<foo>)" uses
3513 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3514 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3515 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3516 WARN('NEEDLESS_IF',
3517 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003518 }
3519 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003520
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003521# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003522 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003523 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003524 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003525 CHK("USLEEP_RANGE",
3526 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003527 }
3528 }
3529
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003530# warn about unexpectedly long msleep's
3531 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3532 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003533 WARN("MSLEEP",
3534 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003535 }
3536 }
3537
Joe Perches36ec1932013-07-03 15:05:25 -07003538# check for comparisons of jiffies
3539 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3540 WARN("JIFFIES_COMPARISON",
3541 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3542 }
3543
Joe Perches9d7a34a2013-07-03 15:05:26 -07003544# check for comparisons of get_jiffies_64()
3545 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3546 WARN("JIFFIES_COMPARISON",
3547 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3548 }
3549
Andy Whitcroft00df3442007-06-08 13:47:06 -07003550# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003551# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003552# print "#ifdef in C files should be avoided\n";
3553# print "$herecurr";
3554# $clean = 0;
3555# }
3556
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003557# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003558 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003559 if (ERROR("SPACING",
3560 "exactly one space required after that #$1\n" . $herecurr) &&
3561 $fix) {
3562 $fixed[$linenr - 1] =~
3563 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3564 }
3565
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003566 }
3567
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003568# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003569 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3570 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003571 my $which = $1;
3572 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003573 CHK("UNCOMMENTED_DEFINITION",
3574 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003575 }
3576 }
3577# check for memory barriers without a comment.
3578 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3579 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003580 CHK("MEMORY_BARRIER",
3581 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003582 }
3583 }
3584# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003585 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003586 CHK("ARCH_DEFINES",
3587 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003588 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003589
Tobias Klauserd4977c72010-05-24 14:33:30 -07003590# Check that the storage class is at the beginning of a declaration
3591 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003592 WARN("STORAGE_CLASS",
3593 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003594 }
3595
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003596# check the location of the inline attribute, that it is between
3597# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003598 if ($line =~ /\b$Type\s+$Inline\b/ ||
3599 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003600 ERROR("INLINE_LOCATION",
3601 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003602 }
3603
Andy Whitcroft8905a672007-11-28 16:21:06 -08003604# Check for __inline__ and __inline, prefer inline
3605 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003606 WARN("INLINE",
3607 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003608 }
3609
Joe Perches3d130fd2011-01-12 17:00:00 -08003610# Check for __attribute__ packed, prefer __packed
3611 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003612 WARN("PREFER_PACKED",
3613 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003614 }
3615
Joe Perches39b7e282011-07-25 17:13:24 -07003616# Check for __attribute__ aligned, prefer __aligned
3617 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003618 WARN("PREFER_ALIGNED",
3619 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003620 }
3621
Joe Perches5f14d3b2012-01-10 15:09:52 -08003622# Check for __attribute__ format(printf, prefer __printf
3623 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3624 WARN("PREFER_PRINTF",
3625 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3626 }
3627
Joe Perches6061d942012-03-23 15:02:16 -07003628# Check for __attribute__ format(scanf, prefer __scanf
3629 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3630 WARN("PREFER_SCANF",
3631 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3632 }
3633
Joe Perches8f53a9b2010-03-05 13:43:48 -08003634# check for sizeof(&)
3635 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003636 WARN("SIZEOF_ADDRESS",
3637 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003638 }
3639
Joe Perches66c80b62012-07-30 14:41:22 -07003640# check for sizeof without parenthesis
3641 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3642 WARN("SIZEOF_PARENTHESIS",
3643 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3644 }
3645
Joe Perches428e2fd2011-05-24 17:13:39 -07003646# check for line continuations in quoted strings with odd counts of "
3647 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003648 WARN("LINE_CONTINUATIONS",
3649 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003650 }
3651
Joe Perches88982fe2012-12-17 16:02:00 -08003652# check for struct spinlock declarations
3653 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3654 WARN("USE_SPINLOCK_T",
3655 "struct spinlock should be spinlock_t\n" . $herecurr);
3656 }
3657
Joe Perchesa6962d72013-04-29 16:18:13 -07003658# check for seq_printf uses that could be seq_puts
3659 if ($line =~ /\bseq_printf\s*\(/) {
3660 my $fmt = get_quoted_string($line, $rawline);
3661 if ($fmt !~ /[^\\]\%/) {
3662 WARN("PREFER_SEQ_PUTS",
3663 "Prefer seq_puts to seq_printf\n" . $herecurr);
3664 }
3665 }
3666
Andy Whitcroft554e1652012-01-10 15:09:57 -08003667# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003668 if ($^V && $^V ge 5.10.0 &&
3669 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003670 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003671
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003672 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003673 my $ms_val = $7;
3674 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003675
Andy Whitcroft554e1652012-01-10 15:09:57 -08003676 if ($ms_size =~ /^(0x|)0$/i) {
3677 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003678 "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 -08003679 } elsif ($ms_size =~ /^(0x|)1$/i) {
3680 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003681 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3682 }
3683 }
3684
3685# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003686 if ($^V && $^V ge 5.10.0 &&
3687 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003688 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003689 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003690 my $call = $1;
3691 my $cast1 = deparenthesize($2);
3692 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003693 my $cast2 = deparenthesize($7);
3694 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003695 my $cast;
3696
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003697 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003698 $cast = "$cast1 or $cast2";
3699 } elsif ($cast1 ne "") {
3700 $cast = $cast1;
3701 } else {
3702 $cast = $cast2;
3703 }
3704 WARN("MINMAX",
3705 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003706 }
3707 }
3708
Joe Perches4a273192012-07-30 14:41:20 -07003709# check usleep_range arguments
3710 if ($^V && $^V ge 5.10.0 &&
3711 defined $stat &&
3712 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3713 my $min = $1;
3714 my $max = $7;
3715 if ($min eq $max) {
3716 WARN("USLEEP_RANGE",
3717 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3718 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3719 $min > $max) {
3720 WARN("USLEEP_RANGE",
3721 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3722 }
3723 }
3724
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003725# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003726 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003727 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003728 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003729 my $function_name = $1;
3730 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003731
3732 my $s = $stat;
3733 if (defined $cond) {
3734 substr($s, 0, length($cond), '');
3735 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003736 if ($s =~ /^\s*;/ &&
3737 $function_name ne 'uninitialized_var')
3738 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003739 WARN("AVOID_EXTERNS",
3740 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003741 }
3742
3743 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003744 WARN("FUNCTION_ARGUMENTS",
3745 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003746 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003747
3748 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3749 $stat =~ /^.\s*extern\s+/)
3750 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003751 WARN("AVOID_EXTERNS",
3752 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003753 }
3754
3755# checks for new __setup's
3756 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3757 my $name = $1;
3758
3759 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003760 CHK("UNDOCUMENTED_SETUP",
3761 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003762 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003763 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003764
3765# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003766 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003767 WARN("UNNECESSARY_CASTS",
3768 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003769 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003770
Joe Perchesa640d252013-07-03 15:05:21 -07003771# alloc style
3772# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3773 if ($^V && $^V ge 5.10.0 &&
3774 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3775 CHK("ALLOC_SIZEOF_STRUCT",
3776 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3777 }
3778
Joe Perches972fdea2013-04-29 16:18:12 -07003779# check for krealloc arg reuse
3780 if ($^V && $^V ge 5.10.0 &&
3781 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3782 WARN("KREALLOC_ARG_REUSE",
3783 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3784 }
3785
Joe Perches5ce59ae2013-02-21 16:44:18 -08003786# check for alloc argument mismatch
3787 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3788 WARN("ALLOC_ARRAY_ARGS",
3789 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3790 }
3791
Joe Perchescaf2a542011-01-12 16:59:56 -08003792# check for multiple semicolons
3793 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08003794 WARN("ONE_SEMICOLON",
3795 "Statements terminations use 1 semicolon\n" . $herecurr);
3796 }
3797
3798# check for switch/default statements without a break;
3799 if ($^V && $^V ge 5.10.0 &&
3800 defined $stat &&
3801 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3802 my $ctx = '';
3803 my $herectx = $here . "\n";
3804 my $cnt = statement_rawlines($stat);
3805 for (my $n = 0; $n < $cnt; $n++) {
3806 $herectx .= raw_line($linenr, $n) . "\n";
3807 }
3808 WARN("DEFAULT_NO_BREAK",
3809 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08003810 }
3811
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003812# check for gcc specific __FUNCTION__
3813 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003814 WARN("USE_FUNC",
3815 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003816 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003817
Joe Perches2c924882012-03-23 15:02:20 -07003818# check for use of yield()
3819 if ($line =~ /\byield\s*\(\s*\)/) {
3820 WARN("YIELD",
3821 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3822 }
3823
Joe Perches179f8f42013-07-03 15:05:30 -07003824# check for comparisons against true and false
3825 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3826 my $lead = $1;
3827 my $arg = $2;
3828 my $test = $3;
3829 my $otype = $4;
3830 my $trail = $5;
3831 my $op = "!";
3832
3833 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3834
3835 my $type = lc($otype);
3836 if ($type =~ /^(?:true|false)$/) {
3837 if (("$test" eq "==" && "$type" eq "true") ||
3838 ("$test" eq "!=" && "$type" eq "false")) {
3839 $op = "";
3840 }
3841
3842 CHK("BOOL_COMPARISON",
3843 "Using comparison to $otype is error prone\n" . $herecurr);
3844
3845## maybe suggesting a correct construct would better
3846## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3847
3848 }
3849 }
3850
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003851# check for semaphores initialized locked
3852 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003853 WARN("CONSIDER_COMPLETION",
3854 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003855 }
Joe Perches6712d852012-03-23 15:02:20 -07003856
Joe Perches67d0a072011-10-31 17:13:10 -07003857# recommend kstrto* over simple_strto* and strict_strto*
3858 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003859 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003860 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003861 }
Joe Perches6712d852012-03-23 15:02:20 -07003862
Michael Ellermanf3db6632008-07-23 21:28:57 -07003863# check for __initcall(), use device_initcall() explicitly please
3864 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003865 WARN("USE_DEVICE_INITCALL",
3866 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003867 }
Joe Perches6712d852012-03-23 15:02:20 -07003868
Emese Revfy79404842010-03-05 13:43:53 -08003869# check for various ops structs, ensure they are const.
3870 my $struct_ops = qr{acpi_dock_ops|
3871 address_space_operations|
3872 backlight_ops|
3873 block_device_operations|
3874 dentry_operations|
3875 dev_pm_ops|
3876 dma_map_ops|
3877 extent_io_ops|
3878 file_lock_operations|
3879 file_operations|
3880 hv_ops|
3881 ide_dma_ops|
3882 intel_dvo_dev_ops|
3883 item_operations|
3884 iwl_ops|
3885 kgdb_arch|
3886 kgdb_io|
3887 kset_uevent_ops|
3888 lock_manager_operations|
3889 microcode_ops|
3890 mtrr_ops|
3891 neigh_ops|
3892 nlmsvc_binding|
3893 pci_raw_ops|
3894 pipe_buf_operations|
3895 platform_hibernation_ops|
3896 platform_suspend_ops|
3897 proto_ops|
3898 rpc_pipe_ops|
3899 seq_operations|
3900 snd_ac97_build_ops|
3901 soc_pcmcia_socket_ops|
3902 stacktrace_ops|
3903 sysfs_ops|
3904 tty_operations|
3905 usb_mon_operations|
3906 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003907 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003908 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003909 WARN("CONST_STRUCT",
3910 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003911 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003912 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003913
3914# use of NR_CPUS is usually wrong
3915# ignore definitions of NR_CPUS and usage to define arrays as likely right
3916 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003917 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3918 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003919 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3920 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3921 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003922 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003923 WARN("NR_CPUS",
3924 "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 -07003925 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003926
3927# check for %L{u,d,i} in strings
3928 my $string;
3929 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3930 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003931 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003932 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003933 WARN("PRINTF_L",
3934 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003935 last;
3936 }
3937 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003938
3939# whine mightly about in_atomic
3940 if ($line =~ /\bin_atomic\s*\(/) {
3941 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003942 ERROR("IN_ATOMIC",
3943 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003944 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003945 WARN("IN_ATOMIC",
3946 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003947 }
3948 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003949
3950# check for lockdep_set_novalidate_class
3951 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3952 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3953 if ($realfile !~ m@^kernel/lockdep@ &&
3954 $realfile !~ m@^include/linux/lockdep@ &&
3955 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003956 ERROR("LOCKDEP",
3957 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003958 }
3959 }
Dave Jones88f88312011-01-12 16:59:59 -08003960
3961 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3962 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003963 WARN("EXPORTED_WORLD_WRITABLE",
3964 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08003965 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003966 }
3967
3968 # If we have no input at all, then there is nothing to report on
3969 # so just keep quiet.
3970 if ($#rawlines == -1) {
3971 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003972 }
3973
Andy Whitcroft8905a672007-11-28 16:21:06 -08003974 # In mailback mode only produce a report in the negative, for
3975 # things that appear to be patches.
3976 if ($mailback && ($clean == 1 || !$is_patch)) {
3977 exit(0);
3978 }
3979
3980 # This is not a patch, and we are are in 'no-patch' mode so
3981 # just keep quiet.
3982 if (!$chk_patch && !$is_patch) {
3983 exit(0);
3984 }
3985
3986 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003987 ERROR("NOT_UNIFIED_DIFF",
3988 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003989 }
3990 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003991 ERROR("MISSING_SIGN_OFF",
3992 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003993 }
3994
Andy Whitcroft8905a672007-11-28 16:21:06 -08003995 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003996 if ($summary && !($clean == 1 && $quiet == 1)) {
3997 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003998 print "total: $cnt_error errors, $cnt_warn warnings, " .
3999 (($check)? "$cnt_chk checks, " : "") .
4000 "$cnt_lines lines checked\n";
4001 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004002 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004003
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004004 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004005
4006 if ($^V lt 5.10.0) {
4007 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4008 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4009 }
4010
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004011 # If there were whitespace errors which cleanpatch can fix
4012 # then suggest that.
4013 if ($rpt_cleaners) {
4014 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4015 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004016 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004017 }
4018 }
4019
Artem Bityutskiy11232682012-03-23 15:02:17 -07004020 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004021 print "NOTE: Ignored message types:";
4022 foreach my $ignore (sort keys %ignore_type) {
4023 print " $ignore";
4024 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07004025 print "\n\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07004026 }
4027
Joe Perches3705ce52013-07-03 15:05:31 -07004028 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4029 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4030 my $linecount = 0;
4031 my $f;
4032
4033 open($f, '>', $newfile)
4034 or die "$P: Can't open $newfile for write\n";
4035 foreach my $fixed_line (@fixed) {
4036 $linecount++;
4037 if ($file) {
4038 if ($linecount > 3) {
4039 $fixed_line =~ s/^\+//;
4040 print $f $fixed_line. "\n";
4041 }
4042 } else {
4043 print $f $fixed_line . "\n";
4044 }
4045 }
4046 close($f);
4047
4048 if (!$quiet) {
4049 print << "EOM";
4050Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4051
4052Do _NOT_ trust the results written to this file.
4053Do _NOT_ submit these changes without inspecting them for correctness.
4054
4055This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4056No warranties, expressed or implied...
4057
4058EOM
4059 }
4060 }
4061
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004062 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004063 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004064 }
4065 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004066 print << "EOM";
4067$vname has style problems, please review.
4068
4069If any of these errors are false positives, please report
4070them to the maintainer, see CHECKPATCH in MAINTAINERS.
4071EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004072 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004073
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004074 return $clean;
4075}