blob: 5748c35d034263f44645e42df7a6a7b831dac6d3 [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070012
13my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080014my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070015
Joe Perches000d1cc12011-07-25 17:13:25 -070016my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070017
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $quiet = 0;
21my $tree = 1;
22my $chk_signoff = 1;
23my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070024my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $file = 0;
28my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070029my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $summary = 1;
31my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080032my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070033my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070034my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080035my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Joe Perches34456862013-07-03 15:05:34 -070038my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070039my %use_type = ();
40my @use = ();
41my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070042my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070043my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080045my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070046my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070048my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070049my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070050my $codespell = 0;
51my $codespellfile = "/usr/local/share/codespell/dictionary.txt";
Hannes Eder77f5b102009-09-21 17:04:37 -070052
53sub help {
54 my ($exitcode) = @_;
55
56 print << "EOM";
57Usage: $P [OPTION]... [FILE]...
58Version: $V
59
60Options:
61 -q, --quiet quiet
62 --no-tree run without a kernel tree
63 --no-signoff do not check for 'Signed-off-by' line
64 --patch treat FILE as patchfile (default)
65 --emacs emacs compile window format
66 --terse one line per report
67 -f, --file treat FILE as regular source file
68 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070069 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070070 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080071 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070072 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070073 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070074 --root=PATH PATH to the kernel tree root
75 --no-summary suppress the per-file summary
76 --mailback only produce a report in case of warnings/errors
77 --summary-file include the filename in summary
78 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
79 'values', 'possible', 'type', and 'attr' (default
80 is all off)
81 --test-only=WORD report only warnings/errors containing WORD
82 literally
Joe Perches3705ce52013-07-03 15:05:31 -070083 --fix EXPERIMENTAL - may create horrible results
84 If correctable single-line errors exist, create
85 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
86 with potential errors corrected to the preferred
87 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080088 --fix-inplace EXPERIMENTAL - may create horrible results
89 Is the same as --fix, but overwrites the input
90 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070091 --ignore-perl-version override checking of perl version. expect
92 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -070093 --codespell Use the codespell dictionary for spelling/typos
94 (default:/usr/local/share/codespell/dictionary.txt)
95 --codespellfile Use this codespell dictionary
Hannes Eder77f5b102009-09-21 17:04:37 -070096 -h, --help, --version display this help and exit
97
98When FILE is - read standard input.
99EOM
100
101 exit($exitcode);
102}
103
Joe Perches000d1cc12011-07-25 17:13:25 -0700104my $conf = which_conf($configuration_file);
105if (-f $conf) {
106 my @conf_args;
107 open(my $conffile, '<', "$conf")
108 or warn "$P: Can't find a readable $configuration_file file $!\n";
109
110 while (<$conffile>) {
111 my $line = $_;
112
113 $line =~ s/\s*\n?$//g;
114 $line =~ s/^\s*//g;
115 $line =~ s/\s+/ /g;
116
117 next if ($line =~ m/^\s*#/);
118 next if ($line =~ m/^\s*$/);
119
120 my @words = split(" ", $line);
121 foreach my $word (@words) {
122 last if ($word =~ m/^#/);
123 push (@conf_args, $word);
124 }
125 }
126 close($conffile);
127 unshift(@ARGV, @conf_args) if @conf_args;
128}
129
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700130GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700132 'tree!' => \$tree,
133 'signoff!' => \$chk_signoff,
134 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700135 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800136 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700137 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700138 'subjective!' => \$check,
139 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700140 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700141 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700142 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800143 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700144 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700145 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800146 'summary!' => \$summary,
147 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800148 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700149 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800150 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700151 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800152 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700153 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700154 'codespell!' => \$codespell,
155 'codespellfile=s' => \$codespellfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700156 'h|help' => \$help,
157 'version' => \$help
158) or help(1);
159
160help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700161
Joe Perches9624b8d2014-01-23 15:54:44 -0800162$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700163$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800164
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700165my $exit = 0;
166
Dave Hansend62a2012013-09-11 14:23:56 -0700167if ($^V && $^V lt $minimum_perl_version) {
168 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
169 if (!$ignore_perl_version) {
170 exit(1);
171 }
172}
173
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700174if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700175 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700176 exit(1);
177}
178
Joe Perches91bfe482013-09-11 14:23:59 -0700179sub hash_save_array_words {
180 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700181
Joe Perches91bfe482013-09-11 14:23:59 -0700182 my @array = split(/,/, join(',', @$arrayRef));
183 foreach my $word (@array) {
184 $word =~ s/\s*\n?$//g;
185 $word =~ s/^\s*//g;
186 $word =~ s/\s+/ /g;
187 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700188
Joe Perches91bfe482013-09-11 14:23:59 -0700189 next if ($word =~ m/^\s*#/);
190 next if ($word =~ m/^\s*$/);
191
192 $hashRef->{$word}++;
193 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700194}
195
Joe Perches91bfe482013-09-11 14:23:59 -0700196sub hash_show_words {
197 my ($hashRef, $prefix) = @_;
198
Joe Perches58cb3cf2013-09-11 14:24:04 -0700199 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700200 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700201 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700202 print " $word";
203 }
204 print "\n\n";
205 }
206}
207
208hash_save_array_words(\%ignore_type, \@ignore);
209hash_save_array_words(\%use_type, \@use);
210
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800211my $dbg_values = 0;
212my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700213my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700214my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800215for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800216 ## no critic
217 eval "\${dbg_$key} = '$debug{$key}';";
218 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219}
220
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700221my $rpt_cleaners = 0;
222
Andy Whitcroft8905a672007-11-28 16:21:06 -0800223if ($terse) {
224 $emacs = 1;
225 $quiet++;
226}
227
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228if ($tree) {
229 if (defined $root) {
230 if (!top_of_kernel_tree($root)) {
231 die "$P: $root: --root does not point at a valid tree\n";
232 }
233 } else {
234 if (top_of_kernel_tree('.')) {
235 $root = '.';
236 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
237 top_of_kernel_tree($1)) {
238 $root = $1;
239 }
240 }
241
242 if (!defined $root) {
243 print "Must be run from the top-level dir. of a kernel tree\n";
244 exit(2);
245 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700246}
247
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700248my $emitted_corrupt = 0;
249
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700250our $Ident = qr{
251 [A-Za-z_][A-Za-z\d_]*
252 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
253 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700254our $Storage = qr{extern|static|asmlinkage};
255our $Sparse = qr{
256 __user|
257 __kernel|
258 __force|
259 __iomem|
260 __must_check|
261 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800262 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700263 __ref|
264 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800266our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
267our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
268our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
269our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
270our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700271
Wolfram Sang52131292010-03-05 13:43:51 -0800272# Notes to $Attribute:
273# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700274our $Attribute = qr{
275 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700276 __percpu|
277 __nocast|
278 __safe|
279 __bitwise__|
280 __packed__|
281 __packed2__|
282 __naked|
283 __maybe_unused|
284 __always_unused|
285 __noreturn|
286 __used|
287 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800288 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700289 __noclone|
290 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700291 __read_mostly|
292 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700293 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700294 ____cacheline_aligned|
295 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800296 ____cacheline_internodealigned_in_smp|
297 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700298 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700299our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700300our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700301our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
302our $Lval = qr{$Ident(?:$Member)*};
303
Joe Perches95e2c602013-07-03 15:05:20 -0700304our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
305our $Binary = qr{(?i)0b[01]+$Int_type?};
306our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
307our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700308our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800309our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800310our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
311our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
312our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800313our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700314our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800315our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700316our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700317our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700318our $Operators = qr{
319 <=|>=|==|!=|
320 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700321 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700322 }x;
323
Joe Perches91cb5192014-04-03 14:49:32 -0700324our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
325
Andy Whitcroft8905a672007-11-28 16:21:06 -0800326our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700327our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700328our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800329our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700330our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800331our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700332our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800333
Joe Perches15662b32011-10-31 17:13:12 -0700334our $NON_ASCII_UTF8 = qr{
335 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700336 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
337 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
338 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
339 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
340 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
341 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
342}x;
343
Joe Perches15662b32011-10-31 17:13:12 -0700344our $UTF8 = qr{
345 [\x09\x0A\x0D\x20-\x7E] # ASCII
346 | $NON_ASCII_UTF8
347}x;
348
Joe Perches021158b2015-02-13 14:38:43 -0800349our $typeOtherOSTypedefs = qr{(?x:
350 u_(?:char|short|int|long) | # bsd
351 u(?:nchar|short|int|long) # sysv
352)};
353
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700354our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700355 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700356 atomic_t
357)};
358
Joe Perches691e6692010-03-05 13:43:51 -0800359our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700360 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700361 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700362 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700363 panic|
Joe Perches06668722013-11-12 15:10:07 -0800364 MODULE_[A-Z_]+|
365 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800366)};
367
Joe Perches20112472011-07-25 17:13:23 -0700368our $signature_tags = qr{(?xi:
369 Signed-off-by:|
370 Acked-by:|
371 Tested-by:|
372 Reviewed-by:|
373 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700374 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700375 To:|
376 Cc:
377)};
378
Joe Perches18130872014-08-06 16:11:22 -0700379our @typeListMisordered = (
380 qr{char\s+(?:un)?signed},
381 qr{int\s+(?:(?:un)?signed\s+)?short\s},
382 qr{int\s+short(?:\s+(?:un)?signed)},
383 qr{short\s+int(?:\s+(?:un)?signed)},
384 qr{(?:un)?signed\s+int\s+short},
385 qr{short\s+(?:un)?signed},
386 qr{long\s+int\s+(?:un)?signed},
387 qr{int\s+long\s+(?:un)?signed},
388 qr{long\s+(?:un)?signed\s+int},
389 qr{int\s+(?:un)?signed\s+long},
390 qr{int\s+(?:un)?signed},
391 qr{int\s+long\s+long\s+(?:un)?signed},
392 qr{long\s+long\s+int\s+(?:un)?signed},
393 qr{long\s+long\s+(?:un)?signed\s+int},
394 qr{long\s+long\s+(?:un)?signed},
395 qr{long\s+(?:un)?signed},
396);
397
Andy Whitcroft8905a672007-11-28 16:21:06 -0800398our @typeList = (
399 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700400 qr{(?:(?:un)?signed\s+)?char},
401 qr{(?:(?:un)?signed\s+)?short\s+int},
402 qr{(?:(?:un)?signed\s+)?short},
403 qr{(?:(?:un)?signed\s+)?int},
404 qr{(?:(?:un)?signed\s+)?long\s+int},
405 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
406 qr{(?:(?:un)?signed\s+)?long\s+long},
407 qr{(?:(?:un)?signed\s+)?long},
408 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800409 qr{float},
410 qr{double},
411 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800412 qr{struct\s+$Ident},
413 qr{union\s+$Ident},
414 qr{enum\s+$Ident},
415 qr{${Ident}_t},
416 qr{${Ident}_handler},
417 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700418 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800419);
Joe Perches8716de32013-09-11 14:24:05 -0700420our @typeListWithAttr = (
421 @typeList,
422 qr{struct\s+$InitAttribute\s+$Ident},
423 qr{union\s+$InitAttribute\s+$Ident},
424);
425
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700426our @modifierList = (
427 qr{fastcall},
428);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800429
Joe Perches24358802014-04-03 14:49:13 -0700430our @mode_permission_funcs = (
431 ["module_param", 3],
432 ["module_param_(?:array|named|string)", 4],
433 ["module_param_array_named", 5],
434 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
435 ["proc_create(?:_data|)", 2],
436 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
437);
438
Joe Perches515a2352014-04-03 14:49:24 -0700439#Create a search pattern for all these functions to speed up a loop below
440our $mode_perms_search = "";
441foreach my $entry (@mode_permission_funcs) {
442 $mode_perms_search .= '|' if ($mode_perms_search ne "");
443 $mode_perms_search .= $entry->[0];
444}
445
Joe Perchesb392c642015-04-16 12:44:16 -0700446our $mode_perms_world_writable = qr{
447 S_IWUGO |
448 S_IWOTH |
449 S_IRWXUGO |
450 S_IALLUGO |
451 0[0-7][0-7][2367]
452}x;
453
Wolfram Sang7840a942010-08-09 17:20:57 -0700454our $allowed_asm_includes = qr{(?x:
455 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700456 memory|
457 time|
458 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700459)};
460# memory.h: ARM has a custom one
461
Kees Cook66b47b42014-10-13 15:51:57 -0700462# Load common spelling mistakes and build regular expression list.
463my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700464my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700465
Joe Perches36061e32014-12-10 15:51:43 -0800466if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800467 while (<$spelling>) {
468 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700469
Joe Perches36061e32014-12-10 15:51:43 -0800470 $line =~ s/\s*\n?$//g;
471 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700472
Joe Perches36061e32014-12-10 15:51:43 -0800473 next if ($line =~ m/^\s*#/);
474 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700475
Joe Perches36061e32014-12-10 15:51:43 -0800476 my ($suspect, $fix) = split(/\|\|/, $line);
477
Joe Perches36061e32014-12-10 15:51:43 -0800478 $spelling_fix{$suspect} = $fix;
479 }
480 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800481} else {
482 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700483}
Kees Cook66b47b42014-10-13 15:51:57 -0700484
Joe Perchesebfd7d62015-04-16 12:44:14 -0700485if ($codespell) {
486 if (open(my $spelling, '<', $codespellfile)) {
487 while (<$spelling>) {
488 my $line = $_;
489
490 $line =~ s/\s*\n?$//g;
491 $line =~ s/^\s*//g;
492
493 next if ($line =~ m/^\s*#/);
494 next if ($line =~ m/^\s*$/);
495 next if ($line =~ m/, disabled/i);
496
497 $line =~ s/,.*$//;
498
499 my ($suspect, $fix) = split(/->/, $line);
500
501 $spelling_fix{$suspect} = $fix;
502 }
503 close($spelling);
504 } else {
505 warn "No codespell typos will be found - file '$codespellfile': $!\n";
506 }
507}
508
509$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
510
Andy Whitcroft8905a672007-11-28 16:21:06 -0800511sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700512 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
513 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700514 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700515 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700516 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800517 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700518 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800519 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800520 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Perches021158b2015-02-13 14:38:43 -0800521 (?:$typeOtherOSTypedefs\b)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700522 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700523 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800524 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700525 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526 }x;
Joe Perches18130872014-08-06 16:11:22 -0700527 $NonptrTypeMisordered = qr{
528 (?:$Modifier\s+|const\s+)*
529 (?:
530 (?:${Misordered}\b)
531 )
532 (?:\s+$Modifier|\s+const)*
533 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700534 $NonptrTypeWithAttr = qr{
535 (?:$Modifier\s+|const\s+)*
536 (?:
537 (?:typeof|__typeof__)\s*\([^\)]*\)|
538 (?:$typeTypedefs\b)|
Joe Perches021158b2015-02-13 14:38:43 -0800539 (?:$typeOtherOSTypedefs\b)|
Joe Perches8716de32013-09-11 14:24:05 -0700540 (?:${allWithAttr}\b)
541 )
542 (?:\s+$Modifier|\s+const)*
543 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800544 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700545 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700546 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700547 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800548 }x;
Joe Perches18130872014-08-06 16:11:22 -0700549 $TypeMisordered = qr{
550 $NonptrTypeMisordered
551 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
552 (?:\s+$Inline|\s+$Modifier)*
553 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700554 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700555 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800556}
557build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700558
Joe Perches7d2367a2011-07-25 17:13:22 -0700559our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700560
561# Using $balanced_parens, $LvalOrFunc, or $FuncArg
562# requires at least perl version v5.10.0
563# Any use must be runtime checked with $^V
564
565our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700566our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800567our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700568
Joe Perchesf8422302014-08-06 16:11:31 -0700569our $declaration_macros = qr{(?x:
570 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
571 (?:$Storage\s+)?LIST_HEAD\s*\(|
572 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
573)};
574
Joe Perches7d2367a2011-07-25 17:13:22 -0700575sub deparenthesize {
576 my ($string) = @_;
577 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700578
579 while ($string =~ /^\s*\(.*\)\s*$/) {
580 $string =~ s@^\s*\(\s*@@;
581 $string =~ s@\s*\)\s*$@@;
582 }
583
Joe Perches7d2367a2011-07-25 17:13:22 -0700584 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700585
Joe Perches7d2367a2011-07-25 17:13:22 -0700586 return $string;
587}
588
Joe Perches34456862013-07-03 15:05:34 -0700589sub seed_camelcase_file {
590 my ($file) = @_;
591
592 return if (!(-f $file));
593
594 local $/;
595
596 open(my $include_file, '<', "$file")
597 or warn "$P: Can't read '$file' $!\n";
598 my $text = <$include_file>;
599 close($include_file);
600
601 my @lines = split('\n', $text);
602
603 foreach my $line (@lines) {
604 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
605 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
606 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800607 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
608 $camelcase{$1} = 1;
609 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700610 $camelcase{$1} = 1;
611 }
612 }
613}
614
615my $camelcase_seeded = 0;
616sub seed_camelcase_includes {
617 return if ($camelcase_seeded);
618
619 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700620 my $camelcase_cache = "";
621 my @include_files = ();
622
623 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700624
Richard Genoud3645e322014-02-10 14:25:32 -0800625 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700626 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
627 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700628 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700629 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700630 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700631 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700632 @include_files = split('\n', $files);
633 foreach my $file (@include_files) {
634 my $date = POSIX::strftime("%Y%m%d%H%M",
635 localtime((stat $file)[9]));
636 $last_mod_date = $date if ($last_mod_date < $date);
637 }
638 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700639 }
Joe Perchesc707a812013-07-08 16:00:43 -0700640
641 if ($camelcase_cache ne "" && -f $camelcase_cache) {
642 open(my $camelcase_file, '<', "$camelcase_cache")
643 or warn "$P: Can't read '$camelcase_cache' $!\n";
644 while (<$camelcase_file>) {
645 chomp;
646 $camelcase{$_} = 1;
647 }
648 close($camelcase_file);
649
650 return;
651 }
652
Richard Genoud3645e322014-02-10 14:25:32 -0800653 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700654 $files = `git ls-files "include/*.h"`;
655 @include_files = split('\n', $files);
656 }
657
Joe Perches34456862013-07-03 15:05:34 -0700658 foreach my $file (@include_files) {
659 seed_camelcase_file($file);
660 }
Joe Perches351b2a12013-07-03 15:05:36 -0700661
Joe Perchesc707a812013-07-08 16:00:43 -0700662 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700663 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700664 open(my $camelcase_file, '>', "$camelcase_cache")
665 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700666 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
667 print $camelcase_file ("$_\n");
668 }
669 close($camelcase_file);
670 }
Joe Perches34456862013-07-03 15:05:34 -0700671}
672
Joe Perchesd311cd42014-08-06 16:10:57 -0700673sub git_commit_info {
674 my ($commit, $id, $desc) = @_;
675
676 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
677
678 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
679 $output =~ s/^\s*//gm;
680 my @lines = split("\n", $output);
681
Joe Perches0d7835f2015-02-13 14:38:35 -0800682 return ($id, $desc) if ($#lines < 0);
683
Joe Perchesd311cd42014-08-06 16:10:57 -0700684 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
685# Maybe one day convert this block of bash into something that returns
686# all matching commit ids, but it's very slow...
687#
688# echo "checking commits $1..."
689# git rev-list --remotes | grep -i "^$1" |
690# while read line ; do
691# git log --format='%H %s' -1 $line |
692# echo "commit $(cut -c 1-12,41-)"
693# done
694 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
695 } else {
696 $id = substr($lines[0], 0, 12);
697 $desc = substr($lines[0], 41);
698 }
699
700 return ($id, $desc);
701}
702
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700703$chk_signoff = 0 if ($file);
704
Andy Whitcroft00df3442007-06-08 13:47:06 -0700705my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800706my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700707my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700708my @fixed_inserted = ();
709my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700710my $fixlinenr = -1;
711
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800712my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700713for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800714 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700715 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800716 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700717 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800718 } elsif ($filename eq '-') {
719 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700720 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800721 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700722 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700723 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800724 if ($filename eq '-') {
725 $vname = 'Your patch';
726 } else {
727 $vname = $filename;
728 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800729 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700730 chomp;
731 push(@rawlines, $_);
732 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800733 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800734 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700735 $exit = 1;
736 }
737 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800738 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700739 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700740 @fixed_inserted = ();
741 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700742 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700743}
744
745exit($exit);
746
747sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700748 my ($root) = @_;
749
750 my @tree_check = (
751 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
752 "README", "Documentation", "arch", "include", "drivers",
753 "fs", "init", "ipc", "kernel", "lib", "scripts",
754 );
755
756 foreach my $check (@tree_check) {
757 if (! -e $root . '/' . $check) {
758 return 0;
759 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700760 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700761 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700762}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700763
Joe Perches20112472011-07-25 17:13:23 -0700764sub parse_email {
765 my ($formatted_email) = @_;
766
767 my $name = "";
768 my $address = "";
769 my $comment = "";
770
771 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
772 $name = $1;
773 $address = $2;
774 $comment = $3 if defined $3;
775 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
776 $address = $1;
777 $comment = $2 if defined $2;
778 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
779 $address = $1;
780 $comment = $2 if defined $2;
781 $formatted_email =~ s/$address.*$//;
782 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700783 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700784 $name =~ s/^\"|\"$//g;
785 # If there's a name left after stripping spaces and
786 # leading quotes, and the address doesn't have both
787 # leading and trailing angle brackets, the address
788 # is invalid. ie:
789 # "joe smith joe@smith.com" bad
790 # "joe smith <joe@smith.com" bad
791 if ($name ne "" && $address !~ /^<[^>]+>$/) {
792 $name = "";
793 $address = "";
794 $comment = "";
795 }
796 }
797
Joe Perches3705ce52013-07-03 15:05:31 -0700798 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700799 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700800 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700801 $address =~ s/^\<|\>$//g;
802
803 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
804 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
805 $name = "\"$name\"";
806 }
807
808 return ($name, $address, $comment);
809}
810
811sub format_email {
812 my ($name, $address) = @_;
813
814 my $formatted_email;
815
Joe Perches3705ce52013-07-03 15:05:31 -0700816 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700817 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700818 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700819
820 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
821 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
822 $name = "\"$name\"";
823 }
824
825 if ("$name" eq "") {
826 $formatted_email = "$address";
827 } else {
828 $formatted_email = "$name <$address>";
829 }
830
831 return $formatted_email;
832}
833
Joe Perchesd311cd42014-08-06 16:10:57 -0700834sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700835 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700836
Joe Perchesbd474ca2014-08-06 16:11:10 -0700837 foreach my $path (split(/:/, $ENV{PATH})) {
838 if (-e "$path/$bin") {
839 return "$path/$bin";
840 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700841 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700842
Joe Perchesbd474ca2014-08-06 16:11:10 -0700843 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700844}
845
Joe Perches000d1cc12011-07-25 17:13:25 -0700846sub which_conf {
847 my ($conf) = @_;
848
849 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
850 if (-e "$path/$conf") {
851 return "$path/$conf";
852 }
853 }
854
855 return "";
856}
857
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700858sub expand_tabs {
859 my ($str) = @_;
860
861 my $res = '';
862 my $n = 0;
863 for my $c (split(//, $str)) {
864 if ($c eq "\t") {
865 $res .= ' ';
866 $n++;
867 for (; ($n % 8) != 0; $n++) {
868 $res .= ' ';
869 }
870 next;
871 }
872 $res .= $c;
873 $n++;
874 }
875
876 return $res;
877}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700878sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700879 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700880 return $res;
881}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700882
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700883sub line_stats {
884 my ($line) = @_;
885
886 # Drop the diff line leader and expand tabs
887 $line =~ s/^.//;
888 $line = expand_tabs($line);
889
890 # Pick the indent from the front of the line.
891 my ($white) = ($line =~ /^(\s*)/);
892
893 return (length($line), length($white));
894}
895
Andy Whitcroft773647a2008-03-28 14:15:58 -0700896my $sanitise_quote = '';
897
898sub sanitise_line_reset {
899 my ($in_comment) = @_;
900
901 if ($in_comment) {
902 $sanitise_quote = '*/';
903 } else {
904 $sanitise_quote = '';
905 }
906}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700907sub sanitise_line {
908 my ($line) = @_;
909
910 my $res = '';
911 my $l = '';
912
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800913 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700914 my $off = 0;
915 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700916
Andy Whitcroft773647a2008-03-28 14:15:58 -0700917 # Always copy over the diff marker.
918 $res = substr($line, 0, 1);
919
920 for ($off = 1; $off < length($line); $off++) {
921 $c = substr($line, $off, 1);
922
923 # Comments we are wacking completly including the begin
924 # and end, all to $;.
925 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
926 $sanitise_quote = '*/';
927
928 substr($res, $off, 2, "$;$;");
929 $off++;
930 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800931 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700932 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700933 $sanitise_quote = '';
934 substr($res, $off, 2, "$;$;");
935 $off++;
936 next;
937 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700938 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
939 $sanitise_quote = '//';
940
941 substr($res, $off, 2, $sanitise_quote);
942 $off++;
943 next;
944 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700945
946 # A \ in a string means ignore the next character.
947 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
948 $c eq "\\") {
949 substr($res, $off, 2, 'XX');
950 $off++;
951 next;
952 }
953 # Regular quotes.
954 if ($c eq "'" || $c eq '"') {
955 if ($sanitise_quote eq '') {
956 $sanitise_quote = $c;
957
958 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700959 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700960 } elsif ($sanitise_quote eq $c) {
961 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700962 }
963 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700964
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800965 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700966 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
967 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700968 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
969 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700970 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
971 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700972 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700973 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700974 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800975 }
976
Daniel Walker113f04a2009-09-21 17:04:35 -0700977 if ($sanitise_quote eq '//') {
978 $sanitise_quote = '';
979 }
980
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800981 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700982 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800983 my $clean = 'X' x length($1);
984 $res =~ s@\<.*\>@<$clean>@;
985
986 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700987 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800988 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700989 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800990 }
991
Andy Whitcroft00df3442007-06-08 13:47:06 -0700992 return $res;
993}
994
Joe Perchesa6962d72013-04-29 16:18:13 -0700995sub get_quoted_string {
996 my ($line, $rawline) = @_;
997
Joe Perches5e4f6ba2014-12-10 15:52:05 -0800998 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -0700999 return substr($rawline, $-[0], $+[0] - $-[0]);
1000}
1001
Andy Whitcroft8905a672007-11-28 16:21:06 -08001002sub ctx_statement_block {
1003 my ($linenr, $remain, $off) = @_;
1004 my $line = $linenr - 1;
1005 my $blk = '';
1006 my $soff = $off;
1007 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001008 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001009
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001010 my $loff = 0;
1011
Andy Whitcroft8905a672007-11-28 16:21:06 -08001012 my $type = '';
1013 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001014 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001015 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001016 my $c;
1017 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001018
1019 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001020 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001021 @stack = (['', 0]) if ($#stack == -1);
1022
Andy Whitcroft773647a2008-03-28 14:15:58 -07001023 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001024 # If we are about to drop off the end, pull in more
1025 # context.
1026 if ($off >= $len) {
1027 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001028 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001029 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001030 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001031 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001032 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001033 $len = length($blk);
1034 $line++;
1035 last;
1036 }
1037 # Bail if there is no further context.
1038 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001039 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001040 last;
1041 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001042 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1043 $level++;
1044 $type = '#';
1045 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001046 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001047 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001048 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001049 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001050
Andy Whitcroft773647a2008-03-28 14:15:58 -07001051 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001052
1053 # Handle nested #if/#else.
1054 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1055 push(@stack, [ $type, $level ]);
1056 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1057 ($type, $level) = @{$stack[$#stack - 1]};
1058 } elsif ($remainder =~ /^#\s*endif\b/) {
1059 ($type, $level) = @{pop(@stack)};
1060 }
1061
Andy Whitcroft8905a672007-11-28 16:21:06 -08001062 # Statement ends at the ';' or a close '}' at the
1063 # outermost level.
1064 if ($level == 0 && $c eq ';') {
1065 last;
1066 }
1067
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001068 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001069 if ($level == 0 && $coff_set == 0 &&
1070 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1071 $remainder =~ /^(else)(?:\s|{)/ &&
1072 $remainder !~ /^else\s+if\b/) {
1073 $coff = $off + length($1) - 1;
1074 $coff_set = 1;
1075 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1076 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001077 }
1078
Andy Whitcroft8905a672007-11-28 16:21:06 -08001079 if (($type eq '' || $type eq '(') && $c eq '(') {
1080 $level++;
1081 $type = '(';
1082 }
1083 if ($type eq '(' && $c eq ')') {
1084 $level--;
1085 $type = ($level != 0)? '(' : '';
1086
1087 if ($level == 0 && $coff < $soff) {
1088 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001089 $coff_set = 1;
1090 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001091 }
1092 }
1093 if (($type eq '' || $type eq '{') && $c eq '{') {
1094 $level++;
1095 $type = '{';
1096 }
1097 if ($type eq '{' && $c eq '}') {
1098 $level--;
1099 $type = ($level != 0)? '{' : '';
1100
1101 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001102 if (substr($blk, $off + 1, 1) eq ';') {
1103 $off++;
1104 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001105 last;
1106 }
1107 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001108 # Preprocessor commands end at the newline unless escaped.
1109 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1110 $level--;
1111 $type = '';
1112 $off++;
1113 last;
1114 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001115 $off++;
1116 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001117 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001118 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001119 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001120 $line++;
1121 $remain--;
1122 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001123
1124 my $statement = substr($blk, $soff, $off - $soff + 1);
1125 my $condition = substr($blk, $soff, $coff - $soff + 1);
1126
1127 #warn "STATEMENT<$statement>\n";
1128 #warn "CONDITION<$condition>\n";
1129
Andy Whitcroft773647a2008-03-28 14:15:58 -07001130 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001131
1132 return ($statement, $condition,
1133 $line, $remain + 1, $off - $loff + 1, $level);
1134}
1135
Andy Whitcroftcf655042008-03-04 14:28:20 -08001136sub statement_lines {
1137 my ($stmt) = @_;
1138
1139 # Strip the diff line prefixes and rip blank lines at start and end.
1140 $stmt =~ s/(^|\n)./$1/g;
1141 $stmt =~ s/^\s*//;
1142 $stmt =~ s/\s*$//;
1143
1144 my @stmt_lines = ($stmt =~ /\n/g);
1145
1146 return $#stmt_lines + 2;
1147}
1148
1149sub statement_rawlines {
1150 my ($stmt) = @_;
1151
1152 my @stmt_lines = ($stmt =~ /\n/g);
1153
1154 return $#stmt_lines + 2;
1155}
1156
1157sub statement_block_size {
1158 my ($stmt) = @_;
1159
1160 $stmt =~ s/(^|\n)./$1/g;
1161 $stmt =~ s/^\s*{//;
1162 $stmt =~ s/}\s*$//;
1163 $stmt =~ s/^\s*//;
1164 $stmt =~ s/\s*$//;
1165
1166 my @stmt_lines = ($stmt =~ /\n/g);
1167 my @stmt_statements = ($stmt =~ /;/g);
1168
1169 my $stmt_lines = $#stmt_lines + 2;
1170 my $stmt_statements = $#stmt_statements + 1;
1171
1172 if ($stmt_lines > $stmt_statements) {
1173 return $stmt_lines;
1174 } else {
1175 return $stmt_statements;
1176 }
1177}
1178
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001179sub ctx_statement_full {
1180 my ($linenr, $remain, $off) = @_;
1181 my ($statement, $condition, $level);
1182
1183 my (@chunks);
1184
Andy Whitcroftcf655042008-03-04 14:28:20 -08001185 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001186 ($statement, $condition, $linenr, $remain, $off, $level) =
1187 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001188 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001189 push(@chunks, [ $condition, $statement ]);
1190 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1191 return ($level, $linenr, @chunks);
1192 }
1193
1194 # Pull in the following conditional/block pairs and see if they
1195 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001196 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001197 ($statement, $condition, $linenr, $remain, $off, $level) =
1198 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001199 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001200 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001201 #print "C: push\n";
1202 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001203 }
1204
1205 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001206}
1207
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001208sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001209 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001210 my $line;
1211 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001212 my $blk = '';
1213 my @o;
1214 my @c;
1215 my @res = ();
1216
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001217 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001218 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001219 for ($line = $start; $remain > 0; $line++) {
1220 next if ($rawlines[$line] =~ /^-/);
1221 $remain--;
1222
1223 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001224
1225 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001226 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001227 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001228 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001229 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001230 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001231 $level = pop(@stack);
1232 }
1233
Andy Whitcroft01464f32010-10-26 14:23:19 -07001234 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001235 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1236 if ($off > 0) {
1237 $off--;
1238 next;
1239 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001240
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001241 if ($c eq $close && $level > 0) {
1242 $level--;
1243 last if ($level == 0);
1244 } elsif ($c eq $open) {
1245 $level++;
1246 }
1247 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001248
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001249 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001250 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001251 }
1252
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001253 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001254 }
1255
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001256 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001257}
1258sub ctx_block_outer {
1259 my ($linenr, $remain) = @_;
1260
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001261 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1262 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001263}
1264sub ctx_block {
1265 my ($linenr, $remain) = @_;
1266
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001267 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1268 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001269}
1270sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001271 my ($linenr, $remain, $off) = @_;
1272
1273 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1274 return @r;
1275}
1276sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001277 my ($linenr, $remain) = @_;
1278
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001279 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001280}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001281sub ctx_statement_level {
1282 my ($linenr, $remain, $off) = @_;
1283
1284 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1285}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001286
1287sub ctx_locate_comment {
1288 my ($first_line, $end_line) = @_;
1289
1290 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001291 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001292 return $current_comment if (defined $current_comment);
1293
1294 # Look through the context and try and figure out if there is a
1295 # comment.
1296 my $in_comment = 0;
1297 $current_comment = '';
1298 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001299 my $line = $rawlines[$linenr - 1];
1300 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001301 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1302 $in_comment = 1;
1303 }
1304 if ($line =~ m@/\*@) {
1305 $in_comment = 1;
1306 }
1307 if (!$in_comment && $current_comment ne '') {
1308 $current_comment = '';
1309 }
1310 $current_comment .= $line . "\n" if ($in_comment);
1311 if ($line =~ m@\*/@) {
1312 $in_comment = 0;
1313 }
1314 }
1315
1316 chomp($current_comment);
1317 return($current_comment);
1318}
1319sub ctx_has_comment {
1320 my ($first_line, $end_line) = @_;
1321 my $cmt = ctx_locate_comment($first_line, $end_line);
1322
Andy Whitcroft00df3442007-06-08 13:47:06 -07001323 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001324 ##print "CMMT: $cmt\n";
1325
1326 return ($cmt ne '');
1327}
1328
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001329sub raw_line {
1330 my ($linenr, $cnt) = @_;
1331
1332 my $offset = $linenr - 1;
1333 $cnt++;
1334
1335 my $line;
1336 while ($cnt) {
1337 $line = $rawlines[$offset++];
1338 next if (defined($line) && $line =~ /^-/);
1339 $cnt--;
1340 }
1341
1342 return $line;
1343}
1344
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001345sub cat_vet {
1346 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001347 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001348
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001349 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001350 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1351 $res .= $1;
1352 if ($2 ne '') {
1353 $coded = sprintf("^%c", unpack('C', $2) + 64);
1354 $res .= $coded;
1355 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001356 }
1357 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001358
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001359 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001360}
1361
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001362my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001363my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001364my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001365my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001366
1367sub annotate_reset {
1368 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001369 $av_pending = '_';
1370 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001371 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001372}
1373
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001374sub annotate_values {
1375 my ($stream, $type) = @_;
1376
1377 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001378 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001379 my $cur = $stream;
1380
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001381 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001382
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001383 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001384 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001385 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001386 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001387 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001388 print "WS($1)\n" if ($dbg_values > 1);
1389 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001390 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001391 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001392 }
1393
Florian Micklerc023e4732011-01-12 16:59:58 -08001394 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001395 print "CAST($1)\n" if ($dbg_values > 1);
1396 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001397 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001398
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001399 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001400 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001401 $type = 'T';
1402
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001403 } elsif ($cur =~ /^($Modifier)\s*/) {
1404 print "MODIFIER($1)\n" if ($dbg_values > 1);
1405 $type = 'T';
1406
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001407 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001408 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001410 push(@av_paren_type, $type);
1411 if ($2 ne '') {
1412 $av_pending = 'N';
1413 }
1414 $type = 'E';
1415
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001416 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001417 print "UNDEF($1)\n" if ($dbg_values > 1);
1418 $av_preprocessor = 1;
1419 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001420
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001421 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001422 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001423 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001424
1425 push(@av_paren_type, $type);
1426 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001427 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001428
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001429 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001430 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1431 $av_preprocessor = 1;
1432
1433 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1434
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001435 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001437 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001438 print "PRE_END($1)\n" if ($dbg_values > 1);
1439
1440 $av_preprocessor = 1;
1441
1442 # Assume all arms of the conditional end as this
1443 # one does, and continue as if the #endif was not here.
1444 pop(@av_paren_type);
1445 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001446 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001447
1448 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001449 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001450
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001451 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1452 print "ATTR($1)\n" if ($dbg_values > 1);
1453 $av_pending = $type;
1454 $type = 'N';
1455
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001456 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001457 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001458 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001459 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001460 }
1461 $type = 'N';
1462
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001463 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001464 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001465 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001466 $type = 'N';
1467
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001468 } elsif ($cur =~/^(case)/o) {
1469 print "CASE($1)\n" if ($dbg_values > 1);
1470 $av_pend_colon = 'C';
1471 $type = 'N';
1472
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001473 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001474 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001475 $type = 'N';
1476
1477 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001478 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001479 push(@av_paren_type, $av_pending);
1480 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001481 $type = 'N';
1482
1483 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001484 my $new_type = pop(@av_paren_type);
1485 if ($new_type ne '_') {
1486 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001487 print "PAREN('$1') -> $type\n"
1488 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001489 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001490 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001491 }
1492
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001493 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001494 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001495 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001496 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001497
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001498 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1499 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001500 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001501 } elsif ($type eq 'E') {
1502 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001503 }
1504 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1505 $type = 'V';
1506
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001507 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001508 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001509 $type = 'V';
1510
1511 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001512 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001513 $type = 'N';
1514
Andy Whitcroftcf655042008-03-04 14:28:20 -08001515 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001516 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001517 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001518 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001519
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001520 } elsif ($cur =~/^(,)/) {
1521 print "COMMA($1)\n" if ($dbg_values > 1);
1522 $type = 'C';
1523
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001524 } elsif ($cur =~ /^(\?)/o) {
1525 print "QUESTION($1)\n" if ($dbg_values > 1);
1526 $type = 'N';
1527
1528 } elsif ($cur =~ /^(:)/o) {
1529 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1530
1531 substr($var, length($res), 1, $av_pend_colon);
1532 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1533 $type = 'E';
1534 } else {
1535 $type = 'N';
1536 }
1537 $av_pend_colon = 'O';
1538
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001539 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001540 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001541 $type = 'N';
1542
Andy Whitcroft0d413862008-10-15 22:02:16 -07001543 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001544 my $variant;
1545
1546 print "OPV($1)\n" if ($dbg_values > 1);
1547 if ($type eq 'V') {
1548 $variant = 'B';
1549 } else {
1550 $variant = 'U';
1551 }
1552
1553 substr($var, length($res), 1, $variant);
1554 $type = 'N';
1555
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001556 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001557 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001558 if ($1 ne '++' && $1 ne '--') {
1559 $type = 'N';
1560 }
1561
1562 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001563 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001564 }
1565 if (defined $1) {
1566 $cur = substr($cur, length($1));
1567 $res .= $type x length($1);
1568 }
1569 }
1570
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001571 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001572}
1573
Andy Whitcroft8905a672007-11-28 16:21:06 -08001574sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001575 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001576 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001577 ^(?:
1578 $Modifier|
1579 $Storage|
1580 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001581 DEFINE_\S+
1582 )$|
1583 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001584 goto|
1585 return|
1586 case|
1587 else|
1588 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001589 do|
1590 \#|
1591 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001592 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001593 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001594 )}x;
1595 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1596 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001597 # Check for modifiers.
1598 $possible =~ s/\s*$Storage\s*//g;
1599 $possible =~ s/\s*$Sparse\s*//g;
1600 if ($possible =~ /^\s*$/) {
1601
1602 } elsif ($possible =~ /\s/) {
1603 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001604 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001605 if ($modifier !~ $notPermitted) {
1606 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1607 push(@modifierList, $modifier);
1608 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001609 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001610
1611 } else {
1612 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1613 push(@typeList, $possible);
1614 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001615 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001616 } else {
1617 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001618 }
1619}
1620
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001621my $prefix = '';
1622
Joe Perches000d1cc12011-07-25 17:13:25 -07001623sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001624 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001625
Joe Perchescbec18a2014-04-03 14:49:19 -07001626 return defined $use_type{$type} if (scalar keys %use_type > 0);
1627
1628 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001629}
1630
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001631sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001632 my ($level, $type, $msg) = @_;
1633
1634 if (!show_type($type) ||
1635 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001636 return 0;
1637 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001638 my $line;
1639 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001640 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001641 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001642 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001643 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001644 $line = (split('\n', $line))[0] . "\n" if ($terse);
1645
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001646 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001647
1648 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001649}
Joe Perchescbec18a2014-04-03 14:49:19 -07001650
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001651sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001652 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001653}
Joe Perches000d1cc12011-07-25 17:13:25 -07001654
Joe Perchesd752fcc2014-08-06 16:11:05 -07001655sub fixup_current_range {
1656 my ($lineRef, $offset, $length) = @_;
1657
1658 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1659 my $o = $1;
1660 my $l = $2;
1661 my $no = $o + $offset;
1662 my $nl = $l + $length;
1663 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1664 }
1665}
1666
1667sub fix_inserted_deleted_lines {
1668 my ($linesRef, $insertedRef, $deletedRef) = @_;
1669
1670 my $range_last_linenr = 0;
1671 my $delta_offset = 0;
1672
1673 my $old_linenr = 0;
1674 my $new_linenr = 0;
1675
1676 my $next_insert = 0;
1677 my $next_delete = 0;
1678
1679 my @lines = ();
1680
1681 my $inserted = @{$insertedRef}[$next_insert++];
1682 my $deleted = @{$deletedRef}[$next_delete++];
1683
1684 foreach my $old_line (@{$linesRef}) {
1685 my $save_line = 1;
1686 my $line = $old_line; #don't modify the array
1687 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1688 $delta_offset = 0;
1689 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1690 $range_last_linenr = $new_linenr;
1691 fixup_current_range(\$line, $delta_offset, 0);
1692 }
1693
1694 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1695 $deleted = @{$deletedRef}[$next_delete++];
1696 $save_line = 0;
1697 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1698 }
1699
1700 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1701 push(@lines, ${$inserted}{'LINE'});
1702 $inserted = @{$insertedRef}[$next_insert++];
1703 $new_linenr++;
1704 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1705 }
1706
1707 if ($save_line) {
1708 push(@lines, $line);
1709 $new_linenr++;
1710 }
1711
1712 $old_linenr++;
1713 }
1714
1715 return @lines;
1716}
1717
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001718sub fix_insert_line {
1719 my ($linenr, $line) = @_;
1720
1721 my $inserted = {
1722 LINENR => $linenr,
1723 LINE => $line,
1724 };
1725 push(@fixed_inserted, $inserted);
1726}
1727
1728sub fix_delete_line {
1729 my ($linenr, $line) = @_;
1730
1731 my $deleted = {
1732 LINENR => $linenr,
1733 LINE => $line,
1734 };
1735
1736 push(@fixed_deleted, $deleted);
1737}
1738
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001739sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001740 my ($type, $msg) = @_;
1741
1742 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001743 our $clean = 0;
1744 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001745 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001746 }
Joe Perches3705ce52013-07-03 15:05:31 -07001747 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001748}
1749sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001750 my ($type, $msg) = @_;
1751
1752 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001753 our $clean = 0;
1754 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001755 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001756 }
Joe Perches3705ce52013-07-03 15:05:31 -07001757 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001758}
1759sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001760 my ($type, $msg) = @_;
1761
1762 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001763 our $clean = 0;
1764 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001765 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001766 }
Joe Perches3705ce52013-07-03 15:05:31 -07001767 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001768}
1769
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001770sub check_absolute_file {
1771 my ($absolute, $herecurr) = @_;
1772 my $file = $absolute;
1773
1774 ##print "absolute<$absolute>\n";
1775
1776 # See if any suffix of this path is a path within the tree.
1777 while ($file =~ s@^[^/]*/@@) {
1778 if (-f "$root/$file") {
1779 ##print "file<$file>\n";
1780 last;
1781 }
1782 }
1783 if (! -f _) {
1784 return 0;
1785 }
1786
1787 # It is, so see if the prefix is acceptable.
1788 my $prefix = $absolute;
1789 substr($prefix, -length($file)) = '';
1790
1791 ##print "prefix<$prefix>\n";
1792 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001793 WARN("USE_RELATIVE_PATH",
1794 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001795 }
1796}
1797
Joe Perches3705ce52013-07-03 15:05:31 -07001798sub trim {
1799 my ($string) = @_;
1800
Joe Perchesb34c6482013-09-11 14:24:01 -07001801 $string =~ s/^\s+|\s+$//g;
1802
1803 return $string;
1804}
1805
1806sub ltrim {
1807 my ($string) = @_;
1808
1809 $string =~ s/^\s+//;
1810
1811 return $string;
1812}
1813
1814sub rtrim {
1815 my ($string) = @_;
1816
1817 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001818
1819 return $string;
1820}
1821
Joe Perches52ea8502013-11-12 15:10:09 -08001822sub string_find_replace {
1823 my ($string, $find, $replace) = @_;
1824
1825 $string =~ s/$find/$replace/g;
1826
1827 return $string;
1828}
1829
Joe Perches3705ce52013-07-03 15:05:31 -07001830sub tabify {
1831 my ($leading) = @_;
1832
1833 my $source_indent = 8;
1834 my $max_spaces_before_tab = $source_indent - 1;
1835 my $spaces_to_tab = " " x $source_indent;
1836
1837 #convert leading spaces to tabs
1838 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1839 #Remove spaces before a tab
1840 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1841
1842 return "$leading";
1843}
1844
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001845sub pos_last_openparen {
1846 my ($line) = @_;
1847
1848 my $pos = 0;
1849
1850 my $opens = $line =~ tr/\(/\(/;
1851 my $closes = $line =~ tr/\)/\)/;
1852
1853 my $last_openparen = 0;
1854
1855 if (($opens == 0) || ($closes >= $opens)) {
1856 return -1;
1857 }
1858
1859 my $len = length($line);
1860
1861 for ($pos = 0; $pos < $len; $pos++) {
1862 my $string = substr($line, $pos);
1863 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1864 $pos += length($1) - 1;
1865 } elsif (substr($line, $pos, 1) eq '(') {
1866 $last_openparen = $pos;
1867 } elsif (index($string, '(') == -1) {
1868 last;
1869 }
1870 }
1871
Joe Perches91cb5192014-04-03 14:49:32 -07001872 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001873}
1874
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001875sub process {
1876 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001877
1878 my $linenr=0;
1879 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001880 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001881 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001882 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001883
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001884 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001885 my $indent;
1886 my $previndent=0;
1887 my $stashindent=0;
1888
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001889 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001890 my $signoff = 0;
1891 my $is_patch = 0;
1892
Joe Perches29ee1b02014-08-06 16:10:35 -07001893 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001894 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001895 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001896 my $non_utf8_charset = 0;
1897
Joe Perches365dd4e2014-08-06 16:10:42 -07001898 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001899 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001900
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001901 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001902 our $cnt_lines = 0;
1903 our $cnt_error = 0;
1904 our $cnt_warn = 0;
1905 our $cnt_chk = 0;
1906
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001907 # Trace the real file/line as we go.
1908 my $realfile = '';
1909 my $realline = 0;
1910 my $realcnt = 0;
1911 my $here = '';
1912 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001913 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001914 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001915 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001916
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001917 my $prev_values = 'E';
1918
1919 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001920 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001921 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001922 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001923 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001924
Joe Perches7e51f192013-09-11 14:23:57 -07001925 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001926
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001927 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001928 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001929 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001930 my @setup_docs = ();
1931 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001932
Joe Perchesd8b07712013-11-12 15:10:06 -08001933 my $camelcase_file_seeded = 0;
1934
Andy Whitcroft773647a2008-03-28 14:15:58 -07001935 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001936 my $line;
1937 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001938 $linenr++;
1939 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001940
Joe Perches3705ce52013-07-03 15:05:31 -07001941 push(@fixed, $rawline) if ($fix);
1942
Andy Whitcroft773647a2008-03-28 14:15:58 -07001943 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001944 $setup_docs = 0;
1945 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1946 $setup_docs = 1;
1947 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001948 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001949 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001950 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1951 $realline=$1-1;
1952 if (defined $2) {
1953 $realcnt=$3+1;
1954 } else {
1955 $realcnt=1+1;
1956 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001957 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001958
1959 # Guestimate if this is a continuing comment. Run
1960 # the context looking for a comment "edge". If this
1961 # edge is a close comment then we must be in a comment
1962 # at context start.
1963 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001964 my $cnt = $realcnt;
1965 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1966 next if (defined $rawlines[$ln - 1] &&
1967 $rawlines[$ln - 1] =~ /^-/);
1968 $cnt--;
1969 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001970 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001971 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1972 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1973 ($edge) = $1;
1974 last;
1975 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001976 }
1977 if (defined $edge && $edge eq '*/') {
1978 $in_comment = 1;
1979 }
1980
1981 # Guestimate if this is a continuing comment. If this
1982 # is the start of a diff block and this line starts
1983 # ' *' then it is very likely a comment.
1984 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001985 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001986 {
1987 $in_comment = 1;
1988 }
1989
1990 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1991 sanitise_line_reset($in_comment);
1992
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001993 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001994 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001995 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001996 $line = sanitise_line($rawline);
1997 }
1998 push(@lines, $line);
1999
2000 if ($realcnt > 1) {
2001 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2002 } else {
2003 $realcnt = 0;
2004 }
2005
2006 #print "==>$rawline\n";
2007 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002008
2009 if ($setup_docs && $line =~ /^\+/) {
2010 push(@setup_docs, $line);
2011 }
2012 }
2013
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002014 $prefix = '';
2015
Andy Whitcroft773647a2008-03-28 14:15:58 -07002016 $realcnt = 0;
2017 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002018 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002019 foreach my $line (@lines) {
2020 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002021 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002022 my $sline = $line; #copy of $line
2023 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002024
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002025 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002026
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002027#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002028 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002029 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002030 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002031 $realline=$1-1;
2032 if (defined $2) {
2033 $realcnt=$3+1;
2034 } else {
2035 $realcnt=1+1;
2036 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002037 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002038 $prev_values = 'E';
2039
Andy Whitcroft773647a2008-03-28 14:15:58 -07002040 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002041 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002042 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002043 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002044 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002045
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002046# track the line number as we move through the hunk, note that
2047# new versions of GNU diff omit the leading space on completely
2048# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002049 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002050 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002051 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002052
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002053 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002054 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002055
2056 # Track the previous line.
2057 ($prevline, $stashline) = ($stashline, $line);
2058 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002059 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2060
Andy Whitcroft773647a2008-03-28 14:15:58 -07002061 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002062
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002063 } elsif ($realcnt == 1) {
2064 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002065 }
2066
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002067 my $hunk_line = ($realcnt != 0);
2068
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002069#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002070 $prefix = "$filename:$realline: " if ($emacs && $file);
2071 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2072
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002073 $here = "#$linenr: " if (!$file);
2074 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002075
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002076 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002077 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002078 if ($line =~ /^diff --git.*?(\S+)$/) {
2079 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002080 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002081 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002082 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002083 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002084 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002085 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002086 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002087
2088 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002089 if (!$file && $tree && $p1_prefix ne '' &&
2090 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002091 WARN("PATCH_PREFIX",
2092 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002093 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002094
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002095 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002096 ERROR("MODIFIED_INCLUDE_ASM",
2097 "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 -07002098 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002099 $found_file = 1;
2100 }
2101
2102 if ($found_file) {
2103 if ($realfile =~ m@^(drivers/net/|net/)@) {
2104 $check = 1;
2105 } else {
2106 $check = $check_orig;
2107 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002108 next;
2109 }
2110
Randy Dunlap389834b2007-06-08 13:47:03 -07002111 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002112
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002113 my $hereline = "$here\n$rawline\n";
2114 my $herecurr = "$here\n$rawline\n";
2115 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002116
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002117 $cnt_lines++ if ($realcnt != 0);
2118
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002119# Check for incorrect file permissions
2120 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2121 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002122 if ($realfile !~ m@scripts/@ &&
2123 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002124 ERROR("EXECUTE_PERMISSIONS",
2125 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002126 }
2127 }
2128
Joe Perches20112472011-07-25 17:13:23 -07002129# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002130 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002131 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002132 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002133 }
2134
Joe Perchese0d975b2014-12-10 15:51:49 -08002135# Check if MAINTAINERS is being updated. If so, there's probably no need to
2136# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2137 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2138 $reported_maintainer_file = 1;
2139 }
2140
Joe Perches20112472011-07-25 17:13:23 -07002141# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002142 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002143 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002144 my $space_before = $1;
2145 my $sign_off = $2;
2146 my $space_after = $3;
2147 my $email = $4;
2148 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2149
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002150 if ($sign_off !~ /$signature_tags/) {
2151 WARN("BAD_SIGN_OFF",
2152 "Non-standard signature: $sign_off\n" . $herecurr);
2153 }
Joe Perches20112472011-07-25 17:13:23 -07002154 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002155 if (WARN("BAD_SIGN_OFF",
2156 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2157 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002158 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002159 "$ucfirst_sign_off $email";
2160 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002161 }
Joe Perches20112472011-07-25 17:13:23 -07002162 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002163 if (WARN("BAD_SIGN_OFF",
2164 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2165 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002166 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002167 "$ucfirst_sign_off $email";
2168 }
2169
Joe Perches20112472011-07-25 17:13:23 -07002170 }
2171 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002172 if (WARN("BAD_SIGN_OFF",
2173 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2174 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002175 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002176 "$ucfirst_sign_off $email";
2177 }
Joe Perches20112472011-07-25 17:13:23 -07002178 }
2179
2180 my ($email_name, $email_address, $comment) = parse_email($email);
2181 my $suggested_email = format_email(($email_name, $email_address));
2182 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002183 ERROR("BAD_SIGN_OFF",
2184 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002185 } else {
2186 my $dequoted = $suggested_email;
2187 $dequoted =~ s/^"//;
2188 $dequoted =~ s/" </ </;
2189 # Don't force email to have quotes
2190 # Allow just an angle bracketed address
2191 if ("$dequoted$comment" ne $email &&
2192 "<$email_address>$comment" ne $email &&
2193 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002194 WARN("BAD_SIGN_OFF",
2195 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002196 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002197 }
Joe Perches7e51f192013-09-11 14:23:57 -07002198
2199# Check for duplicate signatures
2200 my $sig_nospace = $line;
2201 $sig_nospace =~ s/\s//g;
2202 $sig_nospace = lc($sig_nospace);
2203 if (defined $signatures{$sig_nospace}) {
2204 WARN("BAD_SIGN_OFF",
2205 "Duplicate signature\n" . $herecurr);
2206 } else {
2207 $signatures{$sig_nospace} = 1;
2208 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002209 }
2210
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002211# Check email subject for common tools that don't need to be mentioned
2212 if ($in_header_lines &&
2213 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2214 WARN("EMAIL_SUBJECT",
2215 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2216 }
2217
Joe Perches9b3189e2014-06-04 16:12:10 -07002218# Check for old stable address
2219 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2220 ERROR("STABLE_ADDRESS",
2221 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2222 }
2223
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002224# Check for unwanted Gerrit info
2225 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2226 ERROR("GERRIT_CHANGE_ID",
2227 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2228 }
2229
Joe Perches0d7835f2015-02-13 14:38:35 -08002230# Check for git id commit length and improperly formed commit descriptions
2231 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002232 my $init_char = $1;
2233 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002234 my $short = 1;
2235 my $long = 0;
2236 my $case = 1;
2237 my $space = 1;
2238 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002239 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002240 my $id = '0123456789ab';
2241 my $orig_desc = "commit description";
2242 my $description = "";
2243
2244 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2245 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2246 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2247 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2248 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2249 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002250 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002251 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2252 defined $rawlines[$linenr] &&
2253 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2254 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002255 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002256 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2257 defined $rawlines[$linenr] &&
2258 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2259 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2260 $orig_desc = $1;
2261 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2262 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002263 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002264 }
2265
2266 ($id, $description) = git_commit_info($orig_commit,
2267 $id, $orig_desc);
2268
Joe Perches19c146a2015-02-13 14:39:00 -08002269 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002270 ERROR("GIT_COMMIT_ID",
2271 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2272 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002273 }
2274
Joe Perches13f19372014-08-06 16:10:59 -07002275# Check for added, moved or deleted files
2276 if (!$reported_maintainer_file && !$in_commit_log &&
2277 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2278 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2279 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2280 (defined($1) || defined($2))))) {
2281 $reported_maintainer_file = 1;
2282 WARN("FILE_PATH_CHANGES",
2283 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2284 }
2285
Andy Whitcroft00df3442007-06-08 13:47:06 -07002286# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002287 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002288 ERROR("CORRUPTED_PATCH",
2289 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002290 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002291 }
2292
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002293# Check for absolute kernel paths.
2294 if ($tree) {
2295 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2296 my $file = $1;
2297
2298 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2299 check_absolute_file($1, $herecurr)) {
2300 #
2301 } else {
2302 check_absolute_file($file, $herecurr);
2303 }
2304 }
2305 }
2306
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002307# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2308 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002309 $rawline !~ m/^$UTF8*$/) {
2310 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2311
2312 my $blank = copy_spacing($rawline);
2313 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2314 my $hereptr = "$hereline$ptr\n";
2315
Joe Perches34d99212011-07-25 17:13:26 -07002316 CHK("INVALID_UTF8",
2317 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002318 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002319
Joe Perches15662b32011-10-31 17:13:12 -07002320# Check if it's the start of a commit log
2321# (not a header line and we haven't seen the patch filename)
2322 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002323 !($rawline =~ /^\s+\S/ ||
2324 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002325 $in_header_lines = 0;
2326 $in_commit_log = 1;
2327 }
2328
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002329# Check if there is UTF-8 in a commit log when a mail header has explicitly
2330# declined it, i.e defined some charset where it is missing.
2331 if ($in_header_lines &&
2332 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2333 $1 !~ /utf-8/i) {
2334 $non_utf8_charset = 1;
2335 }
2336
2337 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002338 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002339 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002340 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2341 }
2342
Kees Cook66b47b42014-10-13 15:51:57 -07002343# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002344 if (defined($misspellings) &&
2345 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002346 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002347 my $typo = $1;
2348 my $typo_fix = $spelling_fix{lc($typo)};
2349 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2350 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2351 my $msg_type = \&WARN;
2352 $msg_type = \&CHK if ($file);
2353 if (&{$msg_type}("TYPO_SPELLING",
2354 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2355 $fix) {
2356 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2357 }
2358 }
2359 }
2360
Andy Whitcroft306708542008-10-15 22:02:28 -07002361# ignore non-hunk lines and lines being removed
2362 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002363
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002364#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002365 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002366 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002367 if (ERROR("DOS_LINE_ENDINGS",
2368 "DOS line endings\n" . $herevet) &&
2369 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002370 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002371 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002372 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2373 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002374 if (ERROR("TRAILING_WHITESPACE",
2375 "trailing whitespace\n" . $herevet) &&
2376 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002377 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002378 }
2379
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002380 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002381 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002382
Josh Triplett4783f892013-11-12 15:10:12 -08002383# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002384 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002385 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2386 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002387 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2388 my $msg_type = \&ERROR;
2389 $msg_type = \&CHK if ($file);
2390 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002391 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
Josh Triplett4783f892013-11-12 15:10:12 -08002392 }
2393
Andi Kleen33549572010-05-24 14:33:29 -07002394# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002395# Only applies when adding the entry originally, after that we do not have
2396# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002397 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002398 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002399 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002400 my $cnt = $realcnt;
2401 my $ln = $linenr + 1;
2402 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002403 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002404 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002405 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002406 $f = $lines[$ln - 1];
2407 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2408 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002409
2410 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002411 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002412
Joe Perches8d73e0e2014-08-06 16:10:46 -07002413 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002414 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002415 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002416 $length = -1;
2417 }
2418
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002419 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002420 $f =~ s/#.*//;
2421 $f =~ s/^\s+//;
2422 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002423 if ($f =~ /^\s*config\s/) {
2424 $is_end = 1;
2425 last;
2426 }
Andi Kleen33549572010-05-24 14:33:29 -07002427 $length++;
2428 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002429 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2430 WARN("CONFIG_DESCRIPTION",
2431 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2432 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002433 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002434 }
2435
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002436# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2437 if ($realfile =~ /Kconfig/ &&
2438 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2439 WARN("CONFIG_EXPERIMENTAL",
2440 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2441 }
2442
Christoph Jaeger327953e2015-02-13 14:38:29 -08002443# discourage the use of boolean for type definition attributes of Kconfig options
2444 if ($realfile =~ /Kconfig/ &&
2445 $line =~ /^\+\s*\bboolean\b/) {
2446 WARN("CONFIG_TYPE_BOOLEAN",
2447 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2448 }
2449
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002450 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2451 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2452 my $flag = $1;
2453 my $replacement = {
2454 'EXTRA_AFLAGS' => 'asflags-y',
2455 'EXTRA_CFLAGS' => 'ccflags-y',
2456 'EXTRA_CPPFLAGS' => 'cppflags-y',
2457 'EXTRA_LDFLAGS' => 'ldflags-y',
2458 };
2459
2460 WARN("DEPRECATED_VARIABLE",
2461 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2462 }
2463
Rob Herringbff5da42014-01-23 15:54:51 -08002464# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002465 if (defined $root &&
2466 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2467 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2468
Rob Herringbff5da42014-01-23 15:54:51 -08002469 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2470
Florian Vaussardcc933192014-04-03 14:49:27 -07002471 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2472 my $vp_file = $dt_path . "vendor-prefixes.txt";
2473
Rob Herringbff5da42014-01-23 15:54:51 -08002474 foreach my $compat (@compats) {
2475 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002476 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2477 my $compat3 = $compat;
2478 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2479 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002480 if ( $? >> 8 ) {
2481 WARN("UNDOCUMENTED_DT_STRING",
2482 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2483 }
2484
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002485 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2486 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002487 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002488 if ( $? >> 8 ) {
2489 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002490 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002491 }
2492 }
2493 }
2494
Andy Whitcroft5368df22008-10-15 22:02:27 -07002495# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002496 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002497
Joe Perches6cd7f382012-12-17 16:01:54 -08002498#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002499 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002500 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002501 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002502 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002503 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002504 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002505 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002506 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002507 }
2508
Andy Whitcroft8905a672007-11-28 16:21:06 -08002509# check for adding lines without a newline.
2510 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002511 WARN("MISSING_EOF_NEWLINE",
2512 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002513 }
2514
Mike Frysinger42e41c52009-09-21 17:04:40 -07002515# Blackfin: use hi/lo macros
2516 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2517 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2518 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002519 ERROR("LO_MACRO",
2520 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002521 }
2522 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2523 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002524 ERROR("HI_MACRO",
2525 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002526 }
2527 }
2528
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002529# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002530 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002531
2532# at the beginning of a line any tabs must come first and anything
2533# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002534 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2535 $rawline =~ /^\+\s* \s*/) {
2536 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002537 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002538 if (ERROR("CODE_INDENT",
2539 "code indent should use tabs where possible\n" . $herevet) &&
2540 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002541 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002542 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002543 }
2544
Alberto Panizzo08e44362010-03-05 13:43:54 -08002545# check for space before tabs.
2546 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2547 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002548 if (WARN("SPACE_BEFORE_TAB",
2549 "please, no space before tabs\n" . $herevet) &&
2550 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002551 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002552 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002553 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002554 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002555 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002556 }
2557
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002558# check for && or || at the start of a line
2559 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2560 CHK("LOGICAL_CONTINUATIONS",
2561 "Logical continuations should be on the previous line\n" . $hereprev);
2562 }
2563
2564# check multi-line statement indentation matches previous line
2565 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002566 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002567 $prevline =~ /^\+(\t*)(.*)$/;
2568 my $oldindent = $1;
2569 my $rest = $2;
2570
2571 my $pos = pos_last_openparen($rest);
2572 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002573 $line =~ /^(\+| )([ \t]*)/;
2574 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002575
2576 my $goodtabindent = $oldindent .
2577 "\t" x ($pos / 8) .
2578 " " x ($pos % 8);
2579 my $goodspaceindent = $oldindent . " " x $pos;
2580
2581 if ($newindent ne $goodtabindent &&
2582 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002583
2584 if (CHK("PARENTHESIS_ALIGNMENT",
2585 "Alignment should match open parenthesis\n" . $hereprev) &&
2586 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002587 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002588 s/^\+[ \t]*/\+$goodtabindent/;
2589 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002590 }
2591 }
2592 }
2593
Joe Perches6ab3a972015-04-16 12:44:05 -07002594# check for space after cast like "(int) foo" or "(struct foo) bar"
2595# avoid checking a few false positives:
2596# "sizeof(<type>)" or "__alignof__(<type>)"
2597# function pointer declarations like "(*foo)(int) = bar;"
2598# structure definitions like "(struct foo) { 0 };"
2599# multiline macros that define functions
2600# known attributes or the __attribute__ keyword
2601 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2602 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002603 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002604 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002605 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002606 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002607 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002608 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002609 }
2610
Joe Perches05880602012-10-04 17:13:35 -07002611 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002612 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002613 $rawline =~ /^\+[ \t]*\*/ &&
2614 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002615 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2616 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2617 }
2618
2619 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002620 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2621 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002622 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002623 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2624 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2625 "networking block comments start with * on subsequent lines\n" . $hereprev);
2626 }
2627
2628 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002629 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2630 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2631 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2632 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002633 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2634 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2635 }
2636
Joe Perches7f619192014-08-06 16:10:39 -07002637# check for missing blank lines after struct/union declarations
2638# with exceptions for various attributes and macros
2639 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2640 $line =~ /^\+/ &&
2641 !($line =~ /^\+\s*$/ ||
2642 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2643 $line =~ /^\+\s*MODULE_/i ||
2644 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2645 $line =~ /^\+[a-z_]*init/ ||
2646 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2647 $line =~ /^\+\s*DECLARE/ ||
2648 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002649 if (CHK("LINE_SPACING",
2650 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2651 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002652 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002653 }
Joe Perches7f619192014-08-06 16:10:39 -07002654 }
2655
Joe Perches365dd4e2014-08-06 16:10:42 -07002656# check for multiple consecutive blank lines
2657 if ($prevline =~ /^[\+ ]\s*$/ &&
2658 $line =~ /^\+\s*$/ &&
2659 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002660 if (CHK("LINE_SPACING",
2661 "Please don't use multiple blank lines\n" . $hereprev) &&
2662 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002663 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002664 }
2665
Joe Perches365dd4e2014-08-06 16:10:42 -07002666 $last_blank_line = $linenr;
2667 }
2668
Joe Perches3b617e32014-04-03 14:49:28 -07002669# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002670 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2671 # actual declarations
2672 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002673 # function pointer declarations
2674 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002675 # foo bar; where foo is some local typedef or #define
2676 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2677 # known declaration macros
2678 $prevline =~ /^\+\s+$declaration_macros/) &&
2679 # for "else if" which can look like "$Ident $Ident"
2680 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2681 # other possible extensions of declaration lines
2682 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2683 # not starting a section or a macro "\" extended line
2684 $prevline =~ /(?:\{\s*|\\)$/) &&
2685 # looks like a declaration
2686 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002687 # function pointer declarations
2688 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002689 # foo bar; where foo is some local typedef or #define
2690 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2691 # known declaration macros
2692 $sline =~ /^\+\s+$declaration_macros/ ||
2693 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002694 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002695 # start or end of block or continuation of declaration
2696 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2697 # bitfield continuation
2698 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2699 # other possible extensions of declaration lines
2700 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2701 # indentation of previous and current line are the same
2702 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002703 if (WARN("LINE_SPACING",
2704 "Missing a blank line after declarations\n" . $hereprev) &&
2705 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002706 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002707 }
Joe Perches3b617e32014-04-03 14:49:28 -07002708 }
2709
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002710# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002711# Exceptions:
2712# 1) within comments
2713# 2) indented preprocessor commands
2714# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002715 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002716 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002717 if (WARN("LEADING_SPACE",
2718 "please, no spaces at the start of a line\n" . $herevet) &&
2719 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002720 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002721 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002722 }
2723
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002724# check we are in a valid C source file if not then ignore this hunk
2725 next if ($realfile !~ /\.(h|c)$/);
2726
Joe Perches032a4c02014-08-06 16:10:29 -07002727# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002728# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002729# if the previous line is a break or return and is indented 1 tab more...
2730 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2731 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002732 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2733 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2734 defined $lines[$linenr] &&
2735 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002736 WARN("UNNECESSARY_ELSE",
2737 "else is not generally useful after a break or return\n" . $hereprev);
2738 }
2739 }
2740
Joe Perchesc00df192014-08-06 16:11:01 -07002741# check indentation of a line with a break;
2742# if the previous line is a goto or return and is indented the same # of tabs
2743 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2744 my $tabs = $1;
2745 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2746 WARN("UNNECESSARY_BREAK",
2747 "break is not useful after a goto or return\n" . $hereprev);
2748 }
2749 }
2750
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002751# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2752 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2753 WARN("CONFIG_EXPERIMENTAL",
2754 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2755 }
2756
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002757# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002758 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002759 WARN("CVS_KEYWORD",
2760 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002761 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002762
Mike Frysinger42e41c52009-09-21 17:04:40 -07002763# Blackfin: don't use __builtin_bfin_[cs]sync
2764 if ($line =~ /__builtin_bfin_csync/) {
2765 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002766 ERROR("CSYNC",
2767 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002768 }
2769 if ($line =~ /__builtin_bfin_ssync/) {
2770 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002771 ERROR("SSYNC",
2772 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002773 }
2774
Joe Perches56e77d72013-02-21 16:44:14 -08002775# check for old HOTPLUG __dev<foo> section markings
2776 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2777 WARN("HOTPLUG_SECTION",
2778 "Using $1 is unnecessary\n" . $herecurr);
2779 }
2780
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002781# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002782 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2783 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002784#print "LINE<$line>\n";
2785 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002786 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002787 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002788 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002789 $stat =~ s/\n./\n /g;
2790 $cond =~ s/\n./\n /g;
2791
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002792#print "linenr<$linenr> <$stat>\n";
2793 # If this statement has no statement boundaries within
2794 # it there is no point in retrying a statement scan
2795 # until we hit end of it.
2796 my $frag = $stat; $frag =~ s/;+\s*$//;
2797 if ($frag !~ /(?:{|;)/) {
2798#print "skip<$line_nr_next>\n";
2799 $suppress_statement = $line_nr_next;
2800 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002801
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002802 # Find the real next line.
2803 $realline_next = $line_nr_next;
2804 if (defined $realline_next &&
2805 (!defined $lines[$realline_next - 1] ||
2806 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2807 $realline_next++;
2808 }
2809
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002810 my $s = $stat;
2811 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002812
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002813 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002814 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002815
2816 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002817 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002818
Andy Whitcroft463f2862009-09-21 17:04:34 -07002819 } elsif ($s =~ /^.\s*else\b/s) {
2820
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002821 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002822 } 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 -07002823 my $type = $1;
2824 $type =~ s/\s+/ /g;
2825 possible($type, "A:" . $s);
2826
Andy Whitcroft8905a672007-11-28 16:21:06 -08002827 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002828 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002829 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002830 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002831
2832 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002833 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002834 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002835 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002836
2837 # Check for any sort of function declaration.
2838 # int foo(something bar, other baz);
2839 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002840 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 -08002841 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002842
Andy Whitcroftcf655042008-03-04 14:28:20 -08002843 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002844 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002845 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002846
Andy Whitcroft8905a672007-11-28 16:21:06 -08002847 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002848 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002849
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002850 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002851 }
2852 }
2853 }
2854
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002855 }
2856
Andy Whitcroft653d4872007-06-23 17:16:34 -07002857#
2858# Checks which may be anchored in the context.
2859#
2860
2861# Check for switch () and associated case and default
2862# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002863 if ($line=~/\bswitch\s*\(.*\)/) {
2864 my $err = '';
2865 my $sep = '';
2866 my @ctx = ctx_block_outer($linenr, $realcnt);
2867 shift(@ctx);
2868 for my $ctx (@ctx) {
2869 my ($clen, $cindent) = line_stats($ctx);
2870 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2871 $indent != $cindent) {
2872 $err .= "$sep$ctx\n";
2873 $sep = '';
2874 } else {
2875 $sep = "[...]\n";
2876 }
2877 }
2878 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002879 ERROR("SWITCH_CASE_INDENT_LEVEL",
2880 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002881 }
2882 }
2883
2884# if/while/etc brace do not go on next line, unless defining a do while loop,
2885# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002886 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002887 my $pre_ctx = "$1$2";
2888
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002889 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002890
2891 if ($line =~ /^\+\t{6,}/) {
2892 WARN("DEEP_INDENTATION",
2893 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2894 }
2895
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002896 my $ctx_cnt = $realcnt - $#ctx - 1;
2897 my $ctx = join("\n", @ctx);
2898
Andy Whitcroft548596d2008-07-23 21:29:01 -07002899 my $ctx_ln = $linenr;
2900 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002901
Andy Whitcroft548596d2008-07-23 21:29:01 -07002902 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2903 defined $lines[$ctx_ln - 1] &&
2904 $lines[$ctx_ln - 1] =~ /^-/)) {
2905 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2906 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002907 $ctx_ln++;
2908 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002909
Andy Whitcroft53210162008-07-23 21:29:03 -07002910 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2911 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002912
Joe Perchesd752fcc2014-08-06 16:11:05 -07002913 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002914 ERROR("OPEN_BRACE",
2915 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002916 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002917 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002918 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2919 $ctx =~ /\)\s*\;\s*$/ &&
2920 defined $lines[$ctx_ln - 1])
2921 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002922 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2923 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002924 WARN("TRAILING_SEMICOLON",
2925 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002926 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002927 }
2928 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002929 }
2930
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002931# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002932 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002933 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2934 ctx_statement_block($linenr, $realcnt, 0)
2935 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002936 my ($s, $c) = ($stat, $cond);
2937
2938 substr($s, 0, length($c), '');
2939
2940 # Make sure we remove the line prefixes as we have
2941 # none on the first line, and are going to readd them
2942 # where necessary.
2943 $s =~ s/\n./\n/gs;
2944
2945 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002946 my @newlines = ($c =~ /\n/gs);
2947 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002948
2949 # We want to check the first line inside the block
2950 # starting at the end of the conditional, so remove:
2951 # 1) any blank line termination
2952 # 2) any opening brace { on end of the line
2953 # 3) any do (...) {
2954 my $continuation = 0;
2955 my $check = 0;
2956 $s =~ s/^.*\bdo\b//;
2957 $s =~ s/^\s*{//;
2958 if ($s =~ s/^\s*\\//) {
2959 $continuation = 1;
2960 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002961 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002962 $check = 1;
2963 $cond_lines++;
2964 }
2965
2966 # Also ignore a loop construct at the end of a
2967 # preprocessor statement.
2968 if (($prevline =~ /^.\s*#\s*define\s/ ||
2969 $prevline =~ /\\\s*$/) && $continuation == 0) {
2970 $check = 0;
2971 }
2972
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002973 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002974 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002975 while ($cond_ptr != $cond_lines) {
2976 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002977
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002978 # If we see an #else/#elif then the code
2979 # is not linear.
2980 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2981 $check = 0;
2982 }
2983
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002984 # Ignore:
2985 # 1) blank lines, they should be at 0,
2986 # 2) preprocessor lines, and
2987 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002988 if ($continuation ||
2989 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002990 $s =~ /^\s*#\s*?/ ||
2991 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002992 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002993 if ($s =~ s/^.*?\n//) {
2994 $cond_lines++;
2995 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002996 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002997 }
2998
2999 my (undef, $sindent) = line_stats("+" . $s);
3000 my $stat_real = raw_line($linenr, $cond_lines);
3001
3002 # Check if either of these lines are modified, else
3003 # this is not this patch's fault.
3004 if (!defined($stat_real) ||
3005 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3006 $check = 0;
3007 }
3008 if (defined($stat_real) && $cond_lines > 1) {
3009 $stat_real = "[...]\n$stat_real";
3010 }
3011
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003012 #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 -07003013
3014 if ($check && (($sindent % 8) != 0 ||
3015 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003016 WARN("SUSPECT_CODE_INDENT",
3017 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003018 }
3019 }
3020
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003021 # Track the 'values' across context and added lines.
3022 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003023 my ($curr_values, $curr_vars) =
3024 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003025 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003026 if ($dbg_values) {
3027 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003028 print "$linenr > .$outline\n";
3029 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003030 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003031 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003032 $prev_values = substr($curr_values, -1);
3033
Andy Whitcroft00df3442007-06-08 13:47:06 -07003034#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003035 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003036
Andy Whitcroft653d4872007-06-23 17:16:34 -07003037# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003038 if ($dbg_type) {
3039 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003040 ERROR("TEST_TYPE",
3041 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003042 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003043 ERROR("TEST_NOT_TYPE",
3044 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003045 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003046 next;
3047 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003048# TEST: allow direct testing of the attribute matcher.
3049 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003050 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003051 ERROR("TEST_ATTR",
3052 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003053 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003054 ERROR("TEST_NOT_ATTR",
3055 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003056 }
3057 next;
3058 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003059
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003060# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003061 if ($line =~ /^.\s*{/ &&
3062 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003063 if (ERROR("OPEN_BRACE",
3064 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003065 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3066 fix_delete_line($fixlinenr - 1, $prevrawline);
3067 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003068 my $fixedline = $prevrawline;
3069 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003070 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003071 $fixedline = $line;
3072 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003073 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003074 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003075 }
3076
Andy Whitcroft653d4872007-06-23 17:16:34 -07003077#
3078# Checks which are anchored on the added line.
3079#
3080
3081# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003082 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003083 my $path = $1;
3084 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003085 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003086 "malformed #include filename\n" . $herecurr);
3087 }
3088 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3089 ERROR("UAPI_INCLUDE",
3090 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003091 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003092 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003093
3094# no C99 // comments
3095 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003096 if (ERROR("C99_COMMENTS",
3097 "do not use C99 // comments\n" . $herecurr) &&
3098 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003099 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003100 if ($line =~ /\/\/(.*)$/) {
3101 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003102 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003103 }
3104 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003105 }
3106 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003107 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003108 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003109
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003110# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3111# the whole statement.
3112#print "APW <$lines[$realline_next - 1]>\n";
3113 if (defined $realline_next &&
3114 exists $lines[$realline_next - 1] &&
3115 !defined $suppress_export{$realline_next} &&
3116 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3117 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003118 # Handle definitions which produce identifiers with
3119 # a prefix:
3120 # XXX(foo);
3121 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003122 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003123 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003124 $name =~ /^${Ident}_$2/) {
3125#print "FOO C name<$name>\n";
3126 $suppress_export{$realline_next} = 1;
3127
3128 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003129 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003130 ^.DEFINE_$Ident\(\Q$name\E\)|
3131 ^.DECLARE_$Ident\(\Q$name\E\)|
3132 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003133 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3134 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003135 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003136#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3137 $suppress_export{$realline_next} = 2;
3138 } else {
3139 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003140 }
3141 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003142 if (!defined $suppress_export{$linenr} &&
3143 $prevline =~ /^.\s*$/ &&
3144 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3145 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3146#print "FOO B <$lines[$linenr - 1]>\n";
3147 $suppress_export{$linenr} = 2;
3148 }
3149 if (defined $suppress_export{$linenr} &&
3150 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003151 WARN("EXPORT_SYMBOL",
3152 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003153 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003154
Joe Eloff5150bda2010-08-09 17:21:00 -07003155# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003156 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3157 if (ERROR("GLOBAL_INITIALISERS",
3158 "do not initialise globals to 0 or NULL\n" .
3159 $herecurr) &&
3160 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003161 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003162 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003163 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003164# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003165 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3166 if (ERROR("INITIALISED_STATIC",
3167 "do not initialise statics to 0 or NULL\n" .
3168 $herecurr) &&
3169 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003170 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003171 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003172 }
3173
Joe Perches18130872014-08-06 16:11:22 -07003174# check for misordered declarations of char/short/int/long with signed/unsigned
3175 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3176 my $tmp = trim($1);
3177 WARN("MISORDERED_TYPE",
3178 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3179 }
3180
Joe Perchescb710ec2010-10-26 14:23:20 -07003181# check for static const char * arrays.
3182 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003183 WARN("STATIC_CONST_CHAR_ARRAY",
3184 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003185 $herecurr);
3186 }
3187
3188# check for static char foo[] = "bar" declarations.
3189 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003190 WARN("STATIC_CONST_CHAR_ARRAY",
3191 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003192 $herecurr);
3193 }
3194
Joe Perches9b0fa602014-04-03 14:49:18 -07003195# check for non-global char *foo[] = {"bar", ...} declarations.
3196 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3197 WARN("STATIC_CONST_CHAR_ARRAY",
3198 "char * array declaration might be better as static const\n" .
3199 $herecurr);
3200 }
3201
Joe Perchesb36190c2014-01-27 17:07:18 -08003202# check for function declarations without arguments like "int foo()"
3203 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3204 if (ERROR("FUNCTION_WITHOUT_ARGS",
3205 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3206 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003207 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003208 }
3209 }
3210
Joe Perches92e112f2013-12-13 11:36:22 -07003211# check for uses of DEFINE_PCI_DEVICE_TABLE
3212 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3213 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3214 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3215 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003216 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
Joe Perches92e112f2013-12-13 11:36:22 -07003217 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003218 }
3219
Andy Whitcroft653d4872007-06-23 17:16:34 -07003220# check for new typedefs, only function parameters and sparse annotations
3221# make sense.
3222 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003223 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003224 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003225 $line !~ /\b$typeTypedefs\b/ &&
Joe Perches021158b2015-02-13 14:38:43 -08003226 $line !~ /\b$typeOtherOSTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003227 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003228 WARN("NEW_TYPEDEFS",
3229 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003230 }
3231
3232# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003233 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003234 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3235 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003236 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003237
Andy Whitcroft65863862009-01-06 14:41:21 -08003238 # Should start with a space.
3239 $to =~ s/^(\S)/ $1/;
3240 # Should not end with a space.
3241 $to =~ s/\s+$//;
3242 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003243 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003244 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003245
Joe Perches3705ce52013-07-03 15:05:31 -07003246## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003247 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003248 if (ERROR("POINTER_LOCATION",
3249 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3250 $fix) {
3251 my $sub_from = $ident;
3252 my $sub_to = $ident;
3253 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003254 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003255 s@\Q$sub_from\E@$sub_to@;
3256 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003257 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003258 }
3259 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3260 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003261 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003262
Andy Whitcroft65863862009-01-06 14:41:21 -08003263 # Should start with a space.
3264 $to =~ s/^(\S)/ $1/;
3265 # Should not end with a space.
3266 $to =~ s/\s+$//;
3267 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003268 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003269 }
3270 # Modifiers should have spaces.
3271 $to =~ s/(\b$Modifier$)/$1 /;
3272
Joe Perches3705ce52013-07-03 15:05:31 -07003273## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003274 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003275 if (ERROR("POINTER_LOCATION",
3276 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3277 $fix) {
3278
3279 my $sub_from = $match;
3280 my $sub_to = $match;
3281 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003282 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003283 s@\Q$sub_from\E@$sub_to@;
3284 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003285 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003286 }
3287
3288# # no BUG() or BUG_ON()
3289# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3290# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3291# print "$herecurr";
3292# $clean = 0;
3293# }
3294
Andy Whitcroft8905a672007-11-28 16:21:06 -08003295 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003296 WARN("LINUX_VERSION_CODE",
3297 "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 -08003298 }
3299
Joe Perches17441222011-06-15 15:08:17 -07003300# check for uses of printk_ratelimit
3301 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003302 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003303 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003304 }
3305
Andy Whitcroft00df3442007-06-08 13:47:06 -07003306# printk should use KERN_* levels. Note that follow on printk's on the
3307# same line do not need a level, so we use the current block context
3308# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003309# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003310# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003311 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003312 my $ok = 0;
3313 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3314 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003315 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003316 # with "\n" ignore it, else it is to blame
3317 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3318 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3319 $ok = 1;
3320 }
3321 last;
3322 }
3323 }
3324 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003325 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3326 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003327 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003328 }
3329
Joe Perches243f3802012-05-31 16:26:09 -07003330 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3331 my $orig = $1;
3332 my $level = lc($orig);
3333 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003334 my $level2 = $level;
3335 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003336 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003337 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07003338 }
3339
3340 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003341 if (WARN("PREFER_PR_LEVEL",
3342 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3343 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003344 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003345 s/\bpr_warning\b/pr_warn/;
3346 }
Joe Perches243f3802012-05-31 16:26:09 -07003347 }
3348
Joe Perchesdc139312013-02-21 16:44:13 -08003349 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3350 my $orig = $1;
3351 my $level = lc($orig);
3352 $level = "warn" if ($level eq "warning");
3353 $level = "dbg" if ($level eq "debug");
3354 WARN("PREFER_DEV_LEVEL",
3355 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3356 }
3357
Andy Whitcroft653d4872007-06-23 17:16:34 -07003358# function brace can't be on same line, except for #defines of do while,
3359# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003360 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003361 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003362 if (ERROR("OPEN_BRACE",
3363 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3364 $fix) {
3365 fix_delete_line($fixlinenr, $rawline);
3366 my $fixed_line = $rawline;
3367 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3368 my $line1 = $1;
3369 my $line2 = $2;
3370 fix_insert_line($fixlinenr, ltrim($line1));
3371 fix_insert_line($fixlinenr, "\+{");
3372 if ($line2 !~ /^\s*$/) {
3373 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3374 }
3375 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003376 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003377
Andy Whitcroft8905a672007-11-28 16:21:06 -08003378# open braces for enum, union and struct go on the same line.
3379 if ($line =~ /^.\s*{/ &&
3380 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003381 if (ERROR("OPEN_BRACE",
3382 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3383 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3384 fix_delete_line($fixlinenr - 1, $prevrawline);
3385 fix_delete_line($fixlinenr, $rawline);
3386 my $fixedline = rtrim($prevrawline) . " {";
3387 fix_insert_line($fixlinenr, $fixedline);
3388 $fixedline = $rawline;
3389 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3390 if ($fixedline !~ /^\+\s*$/) {
3391 fix_insert_line($fixlinenr, $fixedline);
3392 }
3393 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003394 }
3395
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003396# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003397 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3398 if (WARN("SPACING",
3399 "missing space after $1 definition\n" . $herecurr) &&
3400 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003401 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003402 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3403 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003404 }
3405
Joe Perches31070b52014-01-23 15:54:49 -08003406# Function pointer declarations
3407# check spacing between type, funcptr, and args
3408# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003409 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003410 my $declare = $1;
3411 my $pre_pointer_space = $2;
3412 my $post_pointer_space = $3;
3413 my $funcname = $4;
3414 my $post_funcname_space = $5;
3415 my $pre_args_space = $6;
3416
Joe Perches91f72e92014-04-03 14:49:12 -07003417# the $Declare variable will capture all spaces after the type
3418# so check it for a missing trailing missing space but pointer return types
3419# don't need a space so don't warn for those.
3420 my $post_declare_space = "";
3421 if ($declare =~ /(\s+)$/) {
3422 $post_declare_space = $1;
3423 $declare = rtrim($declare);
3424 }
3425 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003426 WARN("SPACING",
3427 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003428 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003429 }
3430
3431# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003432# This test is not currently implemented because these declarations are
3433# equivalent to
3434# int foo(int bar, ...)
3435# and this is form shouldn't/doesn't generate a checkpatch warning.
3436#
3437# elsif ($declare =~ /\s{2,}$/) {
3438# WARN("SPACING",
3439# "Multiple spaces after return type\n" . $herecurr);
3440# }
Joe Perches31070b52014-01-23 15:54:49 -08003441
3442# unnecessary space "type ( *funcptr)(args...)"
3443 if (defined $pre_pointer_space &&
3444 $pre_pointer_space =~ /^\s/) {
3445 WARN("SPACING",
3446 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3447 }
3448
3449# unnecessary space "type (* funcptr)(args...)"
3450 if (defined $post_pointer_space &&
3451 $post_pointer_space =~ /^\s/) {
3452 WARN("SPACING",
3453 "Unnecessary space before function pointer name\n" . $herecurr);
3454 }
3455
3456# unnecessary space "type (*funcptr )(args...)"
3457 if (defined $post_funcname_space &&
3458 $post_funcname_space =~ /^\s/) {
3459 WARN("SPACING",
3460 "Unnecessary space after function pointer name\n" . $herecurr);
3461 }
3462
3463# unnecessary space "type (*funcptr) (args...)"
3464 if (defined $pre_args_space &&
3465 $pre_args_space =~ /^\s/) {
3466 WARN("SPACING",
3467 "Unnecessary space before function pointer arguments\n" . $herecurr);
3468 }
3469
3470 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003471 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003472 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003473 }
3474 }
3475
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003476# check for spacing round square brackets; allowed:
3477# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003478# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3479# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003480 while ($line =~ /(.*?\s)\[/g) {
3481 my ($where, $prefix) = ($-[1], $1);
3482 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003483 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003484 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003485 if (ERROR("BRACKET_SPACE",
3486 "space prohibited before open square bracket '['\n" . $herecurr) &&
3487 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003488 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003489 s/^(\+.*?)\s+\[/$1\[/;
3490 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003491 }
3492 }
3493
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003494# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003495 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003496 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003497 my $ctx_before = substr($line, 0, $-[1]);
3498 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003499
3500 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003501 if ($name =~ /^(?:
3502 if|for|while|switch|return|case|
3503 volatile|__volatile__|
3504 __attribute__|format|__extension__|
3505 asm|__asm__)$/x)
3506 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003507 # cpp #define statements have non-optional spaces, ie
3508 # if there is a space between the name and the open
3509 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003510 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003511
3512 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003513 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003514
3515 # If this whole things ends with a type its most
3516 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003517 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003518
3519 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003520 if (WARN("SPACING",
3521 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3522 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003523 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003524 s/\b$name\s+\(/$name\(/;
3525 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003526 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003527 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003528
Andy Whitcroft653d4872007-06-23 17:16:34 -07003529# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003530 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003531 my $fixed_line = "";
3532 my $line_fixed = 0;
3533
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003534 my $ops = qr{
3535 <<=|>>=|<=|>=|==|!=|
3536 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3537 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003538 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003539 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003540 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003541 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003542
3543## print("element count: <" . $#elements . ">\n");
3544## foreach my $el (@elements) {
3545## print("el: <$el>\n");
3546## }
3547
3548 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003549 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003550
Joe Perches3705ce52013-07-03 15:05:31 -07003551 foreach my $el (@elements) {
3552 push(@fix_elements, substr($rawline, $off, length($el)));
3553 $off += length($el);
3554 }
3555
3556 $off = 0;
3557
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003558 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003559 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003560
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003561 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003562
3563 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3564
3565## print("n: <$n> good: <$good>\n");
3566
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003567 $off += length($elements[$n]);
3568
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003569 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003570 my $ca = substr($opline, 0, $off);
3571 my $cc = '';
3572 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3573 $cc = substr($opline, $off + length($elements[$n + 1]));
3574 }
3575 my $cb = "$ca$;$cc";
3576
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003577 my $a = '';
3578 $a = 'V' if ($elements[$n] ne '');
3579 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003580 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003581 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3582 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003583 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003584
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003585 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003586
3587 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003588 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003589 $c = 'V' if ($elements[$n + 2] ne '');
3590 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003591 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003592 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3593 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003594 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003595 } else {
3596 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003597 }
3598
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003599 my $ctx = "${a}x${c}";
3600
3601 my $at = "(ctx:$ctx)";
3602
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003603 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003604 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003605
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003606 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003607 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003608
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003609 # Get the full operator variant.
3610 my $opv = $op . substr($curr_vars, $off, 1);
3611
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003612 # Ignore operators passed as parameters.
3613 if ($op_type ne 'V' &&
3614 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3615
Andy Whitcroftcf655042008-03-04 14:28:20 -08003616# # Ignore comments
3617# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003618
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003619 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003620 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003621 if ($ctx !~ /.x[WEBC]/ &&
3622 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003623 if (ERROR("SPACING",
3624 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003625 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003626 $line_fixed = 1;
3627 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003628 }
3629
3630 # // is a comment
3631 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003632
Joe Perchesb00e4812014-04-03 14:49:33 -07003633 # : when part of a bitfield
3634 } elsif ($opv eq ':B') {
3635 # skip the bitfield test for now
3636
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003637 # No spaces for:
3638 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003639 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003640 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003641 if (ERROR("SPACING",
3642 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003643 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003644 if (defined $fix_elements[$n + 2]) {
3645 $fix_elements[$n + 2] =~ s/^\s+//;
3646 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003647 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003648 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003649 }
3650
Joe Perches23810972014-12-10 15:51:32 -08003651 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003652 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003653 my $rtrim_before = 0;
3654 my $space_after = 0;
3655 if ($ctx =~ /Wx./) {
3656 if (ERROR("SPACING",
3657 "space prohibited before that '$op' $at\n" . $hereptr)) {
3658 $line_fixed = 1;
3659 $rtrim_before = 1;
3660 }
3661 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003662 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003663 if (ERROR("SPACING",
3664 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003665 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003666 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003667 $space_after = 1;
3668 }
3669 }
3670 if ($rtrim_before || $space_after) {
3671 if ($rtrim_before) {
3672 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3673 } else {
3674 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3675 }
3676 if ($space_after) {
3677 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003678 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003679 }
3680
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003681 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003682 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003683 #warn "'*' is part of type\n";
3684
3685 # unary operators should have a space before and
3686 # none after. May be left adjacent to another
3687 # unary operator, or a cast
3688 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003689 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003690 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003691 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003692 if (ERROR("SPACING",
3693 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003694 if ($n != $last_after + 2) {
3695 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3696 $line_fixed = 1;
3697 }
Joe Perches3705ce52013-07-03 15:05:31 -07003698 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003699 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003700 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003701 # A unary '*' may be const
3702
3703 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003704 if (ERROR("SPACING",
3705 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003706 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003707 if (defined $fix_elements[$n + 2]) {
3708 $fix_elements[$n + 2] =~ s/^\s+//;
3709 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003710 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003711 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003712 }
3713
3714 # unary ++ and unary -- are allowed no space on one side.
3715 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003716 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003717 if (ERROR("SPACING",
3718 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003719 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003720 $line_fixed = 1;
3721 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003722 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003723 if ($ctx =~ /Wx[BE]/ ||
3724 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003725 if (ERROR("SPACING",
3726 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003727 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003728 $line_fixed = 1;
3729 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003730 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003731 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003732 if (ERROR("SPACING",
3733 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003734 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003735 if (defined $fix_elements[$n + 2]) {
3736 $fix_elements[$n + 2] =~ s/^\s+//;
3737 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003738 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003739 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003740 }
3741
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003742 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003743 } elsif ($op eq '<<' or $op eq '>>' or
3744 $op eq '&' or $op eq '^' or $op eq '|' or
3745 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003746 $op eq '*' or $op eq '/' or
3747 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003748 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003749 if ($check) {
3750 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3751 if (CHK("SPACING",
3752 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3753 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3754 $fix_elements[$n + 2] =~ s/^\s+//;
3755 $line_fixed = 1;
3756 }
3757 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3758 if (CHK("SPACING",
3759 "space preferred before that '$op' $at\n" . $hereptr)) {
3760 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3761 $line_fixed = 1;
3762 }
3763 }
3764 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003765 if (ERROR("SPACING",
3766 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003767 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3768 if (defined $fix_elements[$n + 2]) {
3769 $fix_elements[$n + 2] =~ s/^\s+//;
3770 }
Joe Perches3705ce52013-07-03 15:05:31 -07003771 $line_fixed = 1;
3772 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003773 }
3774
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003775 # A colon needs no spaces before when it is
3776 # terminating a case value or a label.
3777 } elsif ($opv eq ':C' || $opv eq ':L') {
3778 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003779 if (ERROR("SPACING",
3780 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003781 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003782 $line_fixed = 1;
3783 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003784 }
3785
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003786 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003787 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003788 my $ok = 0;
3789
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003790 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003791 if (($op eq '<' &&
3792 $cc =~ /^\S+\@\S+>/) ||
3793 ($op eq '>' &&
3794 $ca =~ /<\S+\@\S+$/))
3795 {
3796 $ok = 1;
3797 }
3798
Joe Perches84731622013-11-12 15:10:05 -08003799 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003800 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003801 my $msg_type = \&ERROR;
3802 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3803
3804 if (&{$msg_type}("SPACING",
3805 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003806 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3807 if (defined $fix_elements[$n + 2]) {
3808 $fix_elements[$n + 2] =~ s/^\s+//;
3809 }
Joe Perches3705ce52013-07-03 15:05:31 -07003810 $line_fixed = 1;
3811 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003812 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003813 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003814 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003815
3816## print("n: <$n> GOOD: <$good>\n");
3817
3818 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003819 }
Joe Perches3705ce52013-07-03 15:05:31 -07003820
3821 if (($#elements % 2) == 0) {
3822 $fixed_line = $fixed_line . $fix_elements[$#elements];
3823 }
3824
Joe Perches194f66f2014-08-06 16:11:03 -07003825 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3826 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003827 }
3828
3829
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003830 }
3831
Joe Perches786b6322013-07-03 15:05:32 -07003832# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003833 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003834 if (WARN("SPACING",
3835 "space prohibited before semicolon\n" . $herecurr) &&
3836 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003837 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003838 s/^(\+.*\S)\s+;/$1;/;
3839 }
3840 }
3841
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003842# check for multiple assignments
3843 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003844 CHK("MULTIPLE_ASSIGNMENTS",
3845 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003846 }
3847
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003848## # check for multiple declarations, allowing for a function declaration
3849## # continuation.
3850## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3851## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3852##
3853## # Remove any bracketed sections to ensure we do not
3854## # falsly report the parameters of functions.
3855## my $ln = $line;
3856## while ($ln =~ s/\([^\(\)]*\)//g) {
3857## }
3858## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003859## WARN("MULTIPLE_DECLARATION",
3860## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003861## }
3862## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003863
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003864#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003865 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3866 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003867 if (ERROR("SPACING",
3868 "space required before the open brace '{'\n" . $herecurr) &&
3869 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003870 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003871 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003872 }
3873
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003874## # check for blank lines before declarations
3875## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3876## $prevrawline =~ /^.\s*$/) {
3877## WARN("SPACING",
3878## "No blank lines before declarations\n" . $hereprev);
3879## }
3880##
3881
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003882# closing brace should have a space following it when it has anything
3883# on the line
3884 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003885 if (ERROR("SPACING",
3886 "space required after that close brace '}'\n" . $herecurr) &&
3887 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003888 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003889 s/}((?!(?:,|;|\)))\S)/} $1/;
3890 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003891 }
3892
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003893# check spacing on square brackets
3894 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003895 if (ERROR("SPACING",
3896 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3897 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003898 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003899 s/\[\s+/\[/;
3900 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003901 }
3902 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003903 if (ERROR("SPACING",
3904 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3905 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003906 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003907 s/\s+\]/\]/;
3908 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003909 }
3910
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003911# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003912 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3913 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003914 if (ERROR("SPACING",
3915 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3916 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003917 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003918 s/\(\s+/\(/;
3919 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003920 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003921 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003922 $line !~ /for\s*\(.*;\s+\)/ &&
3923 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003924 if (ERROR("SPACING",
3925 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3926 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003927 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003928 s/\s+\)/\)/;
3929 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003930 }
3931
Joe Perchese2826fd2014-08-06 16:10:48 -07003932# check unnecessary parentheses around addressof/dereference single $Lvals
3933# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3934
3935 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003936 my $var = $1;
3937 if (CHK("UNNECESSARY_PARENTHESES",
3938 "Unnecessary parentheses around $var\n" . $herecurr) &&
3939 $fix) {
3940 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3941 }
3942 }
3943
3944# check for unnecessary parentheses around function pointer uses
3945# ie: (foo->bar)(); should be foo->bar();
3946# but not "if (foo->bar) (" to avoid some false positives
3947 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3948 my $var = $2;
3949 if (CHK("UNNECESSARY_PARENTHESES",
3950 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3951 $fix) {
3952 my $var2 = deparenthesize($var);
3953 $var2 =~ s/\s//g;
3954 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3955 }
3956 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003957
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003958#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003959 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003960 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003961 if (WARN("INDENTED_LABEL",
3962 "labels should not be indented\n" . $herecurr) &&
3963 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003964 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003965 s/^(.)\s+/$1/;
3966 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003967 }
3968
Joe Perches5b9553a2014-04-03 14:49:21 -07003969# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003970 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003971 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003972 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003973 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3974 my $value = $1;
3975 $value = deparenthesize($value);
3976 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3977 ERROR("RETURN_PARENTHESES",
3978 "return is not a function, parentheses are not required\n" . $herecurr);
3979 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003980 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003981 ERROR("SPACING",
3982 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003983 }
3984 }
Joe Perches507e5142013-11-12 15:10:13 -08003985
Joe Perchesb43ae212014-06-23 13:22:07 -07003986# unnecessary return in a void function
3987# at end-of-function, with the previous line a single leading tab, then return;
3988# and the line before that not a goto label target like "out:"
3989 if ($sline =~ /^[ \+]}\s*$/ &&
3990 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3991 $linenr >= 3 &&
3992 $lines[$linenr - 3] =~ /^[ +]/ &&
3993 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003994 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003995 "void function return statements are not generally useful\n" . $hereprev);
3996 }
Joe Perches9819cf22014-06-04 16:12:09 -07003997
Joe Perches189248d2014-01-23 15:54:47 -08003998# if statements using unnecessary parentheses - ie: if ((foo == bar))
3999 if ($^V && $^V ge 5.10.0 &&
4000 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4001 my $openparens = $1;
4002 my $count = $openparens =~ tr@\(@\(@;
4003 my $msg = "";
4004 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4005 my $comp = $4; #Not $1 because of $LvalOrFunc
4006 $msg = " - maybe == should be = ?" if ($comp eq "==");
4007 WARN("UNNECESSARY_PARENTHESES",
4008 "Unnecessary parentheses$msg\n" . $herecurr);
4009 }
4010 }
4011
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004012# Return of what appears to be an errno should normally be -'ve
4013 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
4014 my $name = $1;
4015 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004016 WARN("USE_NEGATIVE_ERRNO",
4017 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004018 }
4019 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004020
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004021# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004022 if ($line =~ /\b(if|while|for|switch)\(/) {
4023 if (ERROR("SPACING",
4024 "space required before the open parenthesis '('\n" . $herecurr) &&
4025 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004026 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004027 s/\b(if|while|for|switch)\(/$1 \(/;
4028 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004029 }
4030
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004031# Check for illegal assignment in if conditional -- and check for trailing
4032# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004033 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004034 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4035 ctx_statement_block($linenr, $realcnt, 0)
4036 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004037 my ($stat_next) = ctx_statement_block($line_nr_next,
4038 $remain_next, $off_next);
4039 $stat_next =~ s/\n./\n /g;
4040 ##print "stat<$stat> stat_next<$stat_next>\n";
4041
4042 if ($stat_next =~ /^\s*while\b/) {
4043 # If the statement carries leading newlines,
4044 # then count those as offsets.
4045 my ($whitespace) =
4046 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4047 my $offset =
4048 statement_rawlines($whitespace) - 1;
4049
4050 $suppress_whiletrailers{$line_nr_next +
4051 $offset} = 1;
4052 }
4053 }
4054 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004055 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004056 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004057 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004058
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004059 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004060 ERROR("ASSIGN_IN_IF",
4061 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004062 }
4063
4064 # Find out what is on the end of the line after the
4065 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004066 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004067 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004068 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004069 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4070 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004071 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004072 # Find out how long the conditional actually is.
4073 my @newlines = ($c =~ /\n/gs);
4074 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004075 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004076
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004077 $stat_real = raw_line($linenr, $cond_lines)
4078 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004079 if (defined($stat_real) && $cond_lines > 1) {
4080 $stat_real = "[...]\n$stat_real";
4081 }
4082
Joe Perches000d1cc12011-07-25 17:13:25 -07004083 ERROR("TRAILING_STATEMENTS",
4084 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004085 }
4086 }
4087
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004088# Check for bitwise tests written as boolean
4089 if ($line =~ /
4090 (?:
4091 (?:\[|\(|\&\&|\|\|)
4092 \s*0[xX][0-9]+\s*
4093 (?:\&\&|\|\|)
4094 |
4095 (?:\&\&|\|\|)
4096 \s*0[xX][0-9]+\s*
4097 (?:\&\&|\|\||\)|\])
4098 )/x)
4099 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004100 WARN("HEXADECIMAL_BOOLEAN_TEST",
4101 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004102 }
4103
Andy Whitcroft8905a672007-11-28 16:21:06 -08004104# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004105 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4106 my $s = $1;
4107 $s =~ s/$;//g; # Remove any comments
4108 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004109 ERROR("TRAILING_STATEMENTS",
4110 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004111 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004112 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004113# if should not continue a brace
4114 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004115 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004116 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004117 $herecurr);
4118 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004119# case and default should not have general statements after them
4120 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4121 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004122 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004123 \s*return\s+
4124 )/xg)
4125 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004126 ERROR("TRAILING_STATEMENTS",
4127 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004128 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004129
4130 # Check for }<nl>else {, these must be at the same
4131 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004132 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4133 $previndent == $indent) {
4134 if (ERROR("ELSE_AFTER_BRACE",
4135 "else should follow close brace '}'\n" . $hereprev) &&
4136 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4137 fix_delete_line($fixlinenr - 1, $prevrawline);
4138 fix_delete_line($fixlinenr, $rawline);
4139 my $fixedline = $prevrawline;
4140 $fixedline =~ s/}\s*$//;
4141 if ($fixedline !~ /^\+\s*$/) {
4142 fix_insert_line($fixlinenr, $fixedline);
4143 }
4144 $fixedline = $rawline;
4145 $fixedline =~ s/^(.\s*)else/$1} else/;
4146 fix_insert_line($fixlinenr, $fixedline);
4147 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004148 }
4149
Joe Perches8b8856f2014-08-06 16:11:14 -07004150 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4151 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004152 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4153
4154 # Find out what is on the end of the line after the
4155 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004156 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004157 $s =~ s/\n.*//g;
4158
4159 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004160 if (ERROR("WHILE_AFTER_BRACE",
4161 "while should follow close brace '}'\n" . $hereprev) &&
4162 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4163 fix_delete_line($fixlinenr - 1, $prevrawline);
4164 fix_delete_line($fixlinenr, $rawline);
4165 my $fixedline = $prevrawline;
4166 my $trailing = $rawline;
4167 $trailing =~ s/^\+//;
4168 $trailing = trim($trailing);
4169 $fixedline =~ s/}\s*$/} $trailing/;
4170 fix_insert_line($fixlinenr, $fixedline);
4171 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004172 }
4173 }
4174
Joe Perches95e2c602013-07-03 15:05:20 -07004175#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004176 while ($line =~ m{($Constant|$Lval)}g) {
4177 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004178
4179#gcc binary extension
4180 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004181 if (WARN("GCC_BINARY_CONSTANT",
4182 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4183 $fix) {
4184 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004185 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004186 s/\b$var\b/$hexval/;
4187 }
Joe Perches95e2c602013-07-03 15:05:20 -07004188 }
4189
4190#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004191 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004192 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004193#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004194 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004195#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004196 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4197#Ignore some three character SI units explicitly, like MiB and KHz
4198 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004199 while ($var =~ m{($Ident)}g) {
4200 my $word = $1;
4201 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004202 if ($check) {
4203 seed_camelcase_includes();
4204 if (!$file && !$camelcase_file_seeded) {
4205 seed_camelcase_file($realfile);
4206 $camelcase_file_seeded = 1;
4207 }
4208 }
Joe Perches7e781f62013-09-11 14:23:55 -07004209 if (!defined $camelcase{$word}) {
4210 $camelcase{$word} = 1;
4211 CHK("CAMELCASE",
4212 "Avoid CamelCase: <$word>\n" . $herecurr);
4213 }
Joe Perches34456862013-07-03 15:05:34 -07004214 }
Joe Perches323c1262012-12-17 16:02:07 -08004215 }
4216 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004217
4218#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004219 if ($line =~ /\#\s*define.*\\\s+$/) {
4220 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4221 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4222 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004223 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004224 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004225 }
4226
Andy Whitcroft653d4872007-06-23 17:16:34 -07004227#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004228 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004229 my $file = "$1.h";
4230 my $checkfile = "include/linux/$file";
4231 if (-f "$root/$checkfile" &&
4232 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004233 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004234 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004235 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004236 CHK("ARCH_INCLUDE_LINUX",
4237 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004238 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004239 WARN("INCLUDE_LINUX",
4240 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004241 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004242 }
4243 }
4244
Andy Whitcroft653d4872007-06-23 17:16:34 -07004245# multi-statement macros should be enclosed in a do while loop, grab the
4246# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004247# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004248 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4249 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004250 my $ln = $linenr;
4251 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004252 my ($off, $dstat, $dcond, $rest);
4253 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004254 my $has_flow_statement = 0;
4255 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004256 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004257 ctx_statement_block($linenr, $realcnt, 0);
4258 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004259 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004260 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004261
Joe Perches08a28432014-10-13 15:51:55 -07004262 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4263 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4264
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004265 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004266 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004267 $dstat =~ s/\\\n.//g;
4268 $dstat =~ s/^\s*//s;
4269 $dstat =~ s/\s*$//s;
4270
4271 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004272 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4273 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004274 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004275 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004276 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004277
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004278 # Flatten any obvious string concatentation.
4279 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4280 $dstat =~ s/$Ident\s*("X*")/$1/)
4281 {
4282 }
4283
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004284 my $exceptions = qr{
4285 $Declare|
4286 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004287 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004288 DECLARE_PER_CPU|
4289 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004290 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004291 union|
4292 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004293 \.$Ident\s*=\s*|
4294 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004295 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004296 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004297 if ($dstat ne '' &&
4298 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4299 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004300 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004301 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004302 $dstat !~ /$exceptions/ &&
4303 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004304 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004305 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004306 $dstat !~ /^for\s*$Constant$/ && # for (...)
4307 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4308 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004309 $dstat !~ /^\({/ && # ({...
4310 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004311 {
4312 $ctx =~ s/\n*$//;
4313 my $herectx = $here . "\n";
4314 my $cnt = statement_rawlines($ctx);
4315
4316 for (my $n = 0; $n < $cnt; $n++) {
4317 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004318 }
4319
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004320 if ($dstat =~ /;/) {
4321 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4322 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4323 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004324 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004325 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004326 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004327 }
Joe Perches5023d342012-12-17 16:01:47 -08004328
Joe Perches08a28432014-10-13 15:51:55 -07004329# check for macros with flow control, but without ## concatenation
4330# ## concatenation is commonly a macro that defines a function so ignore those
4331 if ($has_flow_statement && !$has_arg_concat) {
4332 my $herectx = $here . "\n";
4333 my $cnt = statement_rawlines($ctx);
4334
4335 for (my $n = 0; $n < $cnt; $n++) {
4336 $herectx .= raw_line($linenr, $n) . "\n";
4337 }
4338 WARN("MACRO_WITH_FLOW_CONTROL",
4339 "Macros with flow control statements should be avoided\n" . "$herectx");
4340 }
4341
Joe Perches481eb482012-12-17 16:01:56 -08004342# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004343
4344 } else {
4345 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004346 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4347 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004348 $line =~ /^\+.*\\$/) {
4349 WARN("LINE_CONTINUATIONS",
4350 "Avoid unnecessary line continuations\n" . $herecurr);
4351 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004352 }
4353
Joe Perchesb13edf72012-07-30 14:41:24 -07004354# do {} while (0) macro tests:
4355# single-statement macros do not need to be enclosed in do while (0) loop,
4356# macro should not end with a semicolon
4357 if ($^V && $^V ge 5.10.0 &&
4358 $realfile !~ m@/vmlinux.lds.h$@ &&
4359 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4360 my $ln = $linenr;
4361 my $cnt = $realcnt;
4362 my ($off, $dstat, $dcond, $rest);
4363 my $ctx = '';
4364 ($dstat, $dcond, $ln, $cnt, $off) =
4365 ctx_statement_block($linenr, $realcnt, 0);
4366 $ctx = $dstat;
4367
4368 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004369 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004370
4371 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4372 my $stmts = $2;
4373 my $semis = $3;
4374
4375 $ctx =~ s/\n*$//;
4376 my $cnt = statement_rawlines($ctx);
4377 my $herectx = $here . "\n";
4378
4379 for (my $n = 0; $n < $cnt; $n++) {
4380 $herectx .= raw_line($linenr, $n) . "\n";
4381 }
4382
Joe Perchesac8e97f2012-08-21 16:15:53 -07004383 if (($stmts =~ tr/;/;/) == 1 &&
4384 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004385 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4386 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4387 }
4388 if (defined $semis && $semis ne "") {
4389 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4390 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4391 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004392 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4393 $ctx =~ s/\n*$//;
4394 my $cnt = statement_rawlines($ctx);
4395 my $herectx = $here . "\n";
4396
4397 for (my $n = 0; $n < $cnt; $n++) {
4398 $herectx .= raw_line($linenr, $n) . "\n";
4399 }
4400
4401 WARN("TRAILING_SEMICOLON",
4402 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004403 }
4404 }
4405
Mike Frysinger080ba922009-01-06 14:41:25 -08004406# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4407# all assignments may have only one of the following with an assignment:
4408# .
4409# ALIGN(...)
4410# VMLINUX_SYMBOL(...)
4411 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004412 WARN("MISSING_VMLINUX_SYMBOL",
4413 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004414 }
4415
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004416# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004417 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4418 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004419 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004420 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004421 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4422 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004423 my @allowed = ();
4424 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004425 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004426 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004427 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004428 for my $chunk (@chunks) {
4429 my ($cond, $block) = @{$chunk};
4430
Andy Whitcroft773647a2008-03-28 14:15:58 -07004431 # If the condition carries leading newlines, then count those as offsets.
4432 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4433 my $offset = statement_rawlines($whitespace) - 1;
4434
Joe Perchesaad4f612012-03-23 15:02:19 -07004435 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004436 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4437
4438 # We have looked at and allowed this specific line.
4439 $suppress_ifbraces{$ln + $offset} = 1;
4440
4441 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004442 $ln += statement_rawlines($block) - 1;
4443
Andy Whitcroft773647a2008-03-28 14:15:58 -07004444 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004445
4446 $seen++ if ($block =~ /^\s*{/);
4447
Joe Perchesaad4f612012-03-23 15:02:19 -07004448 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004449 if (statement_lines($cond) > 1) {
4450 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004451 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004452 }
4453 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004454 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004455 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004456 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004457 if (statement_block_size($block) > 1) {
4458 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004459 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004460 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004461 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004462 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004463 if ($seen) {
4464 my $sum_allowed = 0;
4465 foreach (@allowed) {
4466 $sum_allowed += $_;
4467 }
4468 if ($sum_allowed == 0) {
4469 WARN("BRACES",
4470 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4471 } elsif ($sum_allowed != $allow &&
4472 $seen != $allow) {
4473 CHK("BRACES",
4474 "braces {} should be used on all arms of this statement\n" . $herectx);
4475 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004476 }
4477 }
4478 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004479 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004480 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004481 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004482
Andy Whitcroftcf655042008-03-04 14:28:20 -08004483 # Check the pre-context.
4484 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4485 #print "APW: ALLOWED: pre<$1>\n";
4486 $allowed = 1;
4487 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004488
4489 my ($level, $endln, @chunks) =
4490 ctx_statement_full($linenr, $realcnt, $-[0]);
4491
Andy Whitcroftcf655042008-03-04 14:28:20 -08004492 # Check the condition.
4493 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004494 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004495 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004496 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004497 }
4498 if (statement_lines($cond) > 1) {
4499 #print "APW: ALLOWED: cond<$cond>\n";
4500 $allowed = 1;
4501 }
4502 if ($block =~/\b(?:if|for|while)\b/) {
4503 #print "APW: ALLOWED: block<$block>\n";
4504 $allowed = 1;
4505 }
4506 if (statement_block_size($block) > 1) {
4507 #print "APW: ALLOWED: lines block<$block>\n";
4508 $allowed = 1;
4509 }
4510 # Check the post-context.
4511 if (defined $chunks[1]) {
4512 my ($cond, $block) = @{$chunks[1]};
4513 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004514 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004515 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004516 if ($block =~ /^\s*\{/) {
4517 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4518 $allowed = 1;
4519 }
4520 }
4521 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004522 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004523 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004524
Andy Whitcroftf0556632008-10-15 22:02:23 -07004525 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004526 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004527 }
4528
Joe Perches000d1cc12011-07-25 17:13:25 -07004529 WARN("BRACES",
4530 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004531 }
4532 }
4533
Joe Perches0979ae62012-12-17 16:01:59 -08004534# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004535 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004536 if (CHK("BRACES",
4537 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4538 $fix && $prevrawline =~ /^\+/) {
4539 fix_delete_line($fixlinenr - 1, $prevrawline);
4540 }
Joe Perches0979ae62012-12-17 16:01:59 -08004541 }
Joe Perches77b9a532013-07-03 15:05:29 -07004542 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004543 if (CHK("BRACES",
4544 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4545 $fix) {
4546 fix_delete_line($fixlinenr, $rawline);
4547 }
Joe Perches0979ae62012-12-17 16:01:59 -08004548 }
4549
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004550# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004551 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4552 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004553 WARN("VOLATILE",
4554 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004555 }
4556
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004557# Check for user-visible strings broken across lines, which breaks the ability
4558# to grep for the string. Make exceptions when the previous string ends in a
4559# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4560# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4561 if ($line =~ /^\+\s*"[X\t]*"/ &&
4562 $prevline =~ /"\s*$/ &&
4563 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4564 if (WARN("SPLIT_STRING",
4565 "quoted string split across lines\n" . $hereprev) &&
4566 $fix &&
4567 $prevrawline =~ /^\+.*"\s*$/ &&
4568 $last_coalesced_string_linenr != $linenr - 1) {
4569 my $extracted_string = get_quoted_string($line, $rawline);
4570 my $comma_close = "";
4571 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4572 $comma_close = $1;
4573 }
4574
4575 fix_delete_line($fixlinenr - 1, $prevrawline);
4576 fix_delete_line($fixlinenr, $rawline);
4577 my $fixedline = $prevrawline;
4578 $fixedline =~ s/"\s*$//;
4579 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4580 fix_insert_line($fixlinenr - 1, $fixedline);
4581 $fixedline = $rawline;
4582 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4583 if ($fixedline !~ /\+\s*$/) {
4584 fix_insert_line($fixlinenr, $fixedline);
4585 }
4586 $last_coalesced_string_linenr = $linenr;
4587 }
4588 }
4589
4590# check for missing a space in a string concatenation
4591 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4592 WARN('MISSING_SPACE',
4593 "break quoted strings at a space character\n" . $hereprev);
4594 }
4595
4596# check for spaces before a quoted newline
4597 if ($rawline =~ /^.*\".*\s\\n/) {
4598 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4599 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4600 $fix) {
4601 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4602 }
4603
4604 }
4605
Joe Perchesf17dba42014-10-13 15:51:51 -07004606# concatenated string without spaces between elements
4607 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4608 CHK("CONCATENATED_STRING",
4609 "Concatenated strings should use spaces between elements\n" . $herecurr);
4610 }
4611
Joe Perches90ad30e2014-12-10 15:51:59 -08004612# uncoalesced string fragments
4613 if ($line =~ /"X*"\s*"/) {
4614 WARN("STRING_FRAGMENTS",
4615 "Consecutive strings are generally better as a single string\n" . $herecurr);
4616 }
4617
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004618# check for %L{u,d,i} in strings
4619 my $string;
4620 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4621 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4622 $string =~ s/%%/__/g;
4623 if ($string =~ /(?<!%)%L[udi]/) {
4624 WARN("PRINTF_L",
4625 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4626 last;
4627 }
4628 }
4629
4630# check for line continuations in quoted strings with odd counts of "
4631 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4632 WARN("LINE_CONTINUATIONS",
4633 "Avoid line continuations in quoted strings\n" . $herecurr);
4634 }
4635
Andy Whitcroft00df3442007-06-08 13:47:06 -07004636# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004637 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004638 CHK("REDUNDANT_CODE",
4639 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004640 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004641 }
4642
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004643# check for needless "if (<foo>) fn(<foo>)" uses
4644 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4645 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4646 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4647 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004648 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004649 }
4650 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004651
Joe Perchesebfdc402014-08-06 16:10:27 -07004652# check for unnecessary "Out of Memory" messages
4653 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4654 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4655 (defined $1 || defined $3) &&
4656 $linenr > 3) {
4657 my $testval = $2;
4658 my $testline = $lines[$linenr - 3];
4659
4660 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4661# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4662
4663 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4664 WARN("OOM_MESSAGE",
4665 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4666 }
4667 }
4668
Joe Perchesf78d98f2014-10-13 15:52:01 -07004669# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004670 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004671 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4672 my $level = $1;
4673 if (WARN("UNNECESSARY_KERN_LEVEL",
4674 "Possible unnecessary $level\n" . $herecurr) &&
4675 $fix) {
4676 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4677 }
4678 }
4679
Joe Perchesabb08a52014-12-10 15:51:46 -08004680# check for mask then right shift without a parentheses
4681 if ($^V && $^V ge 5.10.0 &&
4682 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4683 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4684 WARN("MASK_THEN_SHIFT",
4685 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4686 }
4687
Joe Perchesb75ac612014-12-10 15:52:02 -08004688# check for pointer comparisons to NULL
4689 if ($^V && $^V ge 5.10.0) {
4690 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4691 my $val = $1;
4692 my $equal = "!";
4693 $equal = "" if ($4 eq "!=");
4694 if (CHK("COMPARISON_TO_NULL",
4695 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4696 $fix) {
4697 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4698 }
4699 }
4700 }
4701
Joe Perches8716de32013-09-11 14:24:05 -07004702# check for bad placement of section $InitAttribute (e.g.: __initdata)
4703 if ($line =~ /(\b$InitAttribute\b)/) {
4704 my $attr = $1;
4705 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4706 my $ptr = $1;
4707 my $var = $2;
4708 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4709 ERROR("MISPLACED_INIT",
4710 "$attr should be placed after $var\n" . $herecurr)) ||
4711 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4712 WARN("MISPLACED_INIT",
4713 "$attr should be placed after $var\n" . $herecurr))) &&
4714 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004715 $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
Joe Perches8716de32013-09-11 14:24:05 -07004716 }
4717 }
4718 }
4719
Joe Perchese970b882013-11-12 15:10:10 -08004720# check for $InitAttributeData (ie: __initdata) with const
4721 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4722 my $attr = $1;
4723 $attr =~ /($InitAttributePrefix)(.*)/;
4724 my $attr_prefix = $1;
4725 my $attr_type = $2;
4726 if (ERROR("INIT_ATTRIBUTE",
4727 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4728 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004729 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004730 s/$InitAttributeData/${attr_prefix}initconst/;
4731 }
4732 }
4733
4734# check for $InitAttributeConst (ie: __initconst) without const
4735 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4736 my $attr = $1;
4737 if (ERROR("INIT_ATTRIBUTE",
4738 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4739 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004740 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004741 /(^\+\s*(?:static\s+))/;
4742 $lead = rtrim($1);
4743 $lead = "$lead " if ($lead !~ /^\+$/);
4744 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004745 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08004746 }
4747 }
4748
Joe Perchesfbdb8132014-04-03 14:49:14 -07004749# don't use __constant_<foo> functions outside of include/uapi/
4750 if ($realfile !~ m@^include/uapi/@ &&
4751 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4752 my $constant_func = $1;
4753 my $func = $constant_func;
4754 $func =~ s/^__constant_//;
4755 if (WARN("CONSTANT_CONVERSION",
4756 "$constant_func should be $func\n" . $herecurr) &&
4757 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004758 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004759 }
4760 }
4761
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004762# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004763 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004764 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004765 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004766 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004767 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004768 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4769 }
4770 if ($delay > 2000) {
4771 WARN("LONG_UDELAY",
4772 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004773 }
4774 }
4775
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004776# warn about unexpectedly long msleep's
4777 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4778 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004779 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004780 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004781 }
4782 }
4783
Joe Perches36ec1932013-07-03 15:05:25 -07004784# check for comparisons of jiffies
4785 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4786 WARN("JIFFIES_COMPARISON",
4787 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4788 }
4789
Joe Perches9d7a34a2013-07-03 15:05:26 -07004790# check for comparisons of get_jiffies_64()
4791 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4792 WARN("JIFFIES_COMPARISON",
4793 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4794 }
4795
Andy Whitcroft00df3442007-06-08 13:47:06 -07004796# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004797# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004798# print "#ifdef in C files should be avoided\n";
4799# print "$herecurr";
4800# $clean = 0;
4801# }
4802
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004803# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004804 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004805 if (ERROR("SPACING",
4806 "exactly one space required after that #$1\n" . $herecurr) &&
4807 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004808 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004809 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4810 }
4811
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004812 }
4813
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004814# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004815 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4816 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004817 my $which = $1;
4818 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004819 CHK("UNCOMMENTED_DEFINITION",
4820 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004821 }
4822 }
4823# check for memory barriers without a comment.
4824 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4825 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004826 WARN("MEMORY_BARRIER",
4827 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004828 }
4829 }
4830# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004831 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004832 CHK("ARCH_DEFINES",
4833 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004834 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004835
Tobias Klauserd4977c72010-05-24 14:33:30 -07004836# Check that the storage class is at the beginning of a declaration
4837 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004838 WARN("STORAGE_CLASS",
4839 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004840 }
4841
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004842# check the location of the inline attribute, that it is between
4843# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004844 if ($line =~ /\b$Type\s+$Inline\b/ ||
4845 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004846 ERROR("INLINE_LOCATION",
4847 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004848 }
4849
Andy Whitcroft8905a672007-11-28 16:21:06 -08004850# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004851 if ($realfile !~ m@\binclude/uapi/@ &&
4852 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004853 if (WARN("INLINE",
4854 "plain inline is preferred over $1\n" . $herecurr) &&
4855 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004856 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004857
4858 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004859 }
4860
Joe Perches3d130fd2011-01-12 17:00:00 -08004861# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004862 if ($realfile !~ m@\binclude/uapi/@ &&
4863 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004864 WARN("PREFER_PACKED",
4865 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004866 }
4867
Joe Perches39b7e282011-07-25 17:13:24 -07004868# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004869 if ($realfile !~ m@\binclude/uapi/@ &&
4870 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004871 WARN("PREFER_ALIGNED",
4872 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004873 }
4874
Joe Perches5f14d3b2012-01-10 15:09:52 -08004875# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004876 if ($realfile !~ m@\binclude/uapi/@ &&
4877 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004878 if (WARN("PREFER_PRINTF",
4879 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4880 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004881 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004882
4883 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004884 }
4885
Joe Perches6061d942012-03-23 15:02:16 -07004886# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004887 if ($realfile !~ m@\binclude/uapi/@ &&
4888 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004889 if (WARN("PREFER_SCANF",
4890 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4891 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004892 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004893 }
Joe Perches6061d942012-03-23 15:02:16 -07004894 }
4895
Joe Perches619a9082014-12-10 15:51:35 -08004896# Check for __attribute__ weak, or __weak declarations (may have link issues)
4897 if ($^V && $^V ge 5.10.0 &&
4898 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4899 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4900 $line =~ /\b__weak\b/)) {
4901 ERROR("WEAK_DECLARATION",
4902 "Using weak declarations can have unintended link defects\n" . $herecurr);
4903 }
4904
Joe Perches8f53a9b2010-03-05 13:43:48 -08004905# check for sizeof(&)
4906 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004907 WARN("SIZEOF_ADDRESS",
4908 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004909 }
4910
Joe Perches66c80b62012-07-30 14:41:22 -07004911# check for sizeof without parenthesis
4912 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004913 if (WARN("SIZEOF_PARENTHESIS",
4914 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4915 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004916 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004917 }
Joe Perches66c80b62012-07-30 14:41:22 -07004918 }
4919
Joe Perches88982fe2012-12-17 16:02:00 -08004920# check for struct spinlock declarations
4921 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4922 WARN("USE_SPINLOCK_T",
4923 "struct spinlock should be spinlock_t\n" . $herecurr);
4924 }
4925
Joe Perchesa6962d72013-04-29 16:18:13 -07004926# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004927 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004928 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08004929 $fmt =~ s/%%//g;
4930 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004931 if (WARN("PREFER_SEQ_PUTS",
4932 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4933 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004934 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004935 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004936 }
4937 }
4938
Andy Whitcroft554e1652012-01-10 15:09:57 -08004939# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004940 if ($^V && $^V ge 5.10.0 &&
4941 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004942 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004943
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004944 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004945 my $ms_val = $7;
4946 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004947
Andy Whitcroft554e1652012-01-10 15:09:57 -08004948 if ($ms_size =~ /^(0x|)0$/i) {
4949 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004950 "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 -08004951 } elsif ($ms_size =~ /^(0x|)1$/i) {
4952 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004953 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4954 }
4955 }
4956
Joe Perches98a9bba2014-01-23 15:54:52 -08004957# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4958 if ($^V && $^V ge 5.10.0 &&
4959 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4960 if (WARN("PREFER_ETHER_ADDR_COPY",
4961 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4962 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004963 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
Joe Perches98a9bba2014-01-23 15:54:52 -08004964 }
4965 }
4966
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004967# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004968 if ($^V && $^V ge 5.10.0 &&
4969 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004970 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004971 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004972 my $call = $1;
4973 my $cast1 = deparenthesize($2);
4974 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004975 my $cast2 = deparenthesize($7);
4976 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004977 my $cast;
4978
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004979 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004980 $cast = "$cast1 or $cast2";
4981 } elsif ($cast1 ne "") {
4982 $cast = $cast1;
4983 } else {
4984 $cast = $cast2;
4985 }
4986 WARN("MINMAX",
4987 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004988 }
4989 }
4990
Joe Perches4a273192012-07-30 14:41:20 -07004991# check usleep_range arguments
4992 if ($^V && $^V ge 5.10.0 &&
4993 defined $stat &&
4994 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4995 my $min = $1;
4996 my $max = $7;
4997 if ($min eq $max) {
4998 WARN("USLEEP_RANGE",
4999 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5000 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5001 $min > $max) {
5002 WARN("USLEEP_RANGE",
5003 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5004 }
5005 }
5006
Joe Perches823b7942013-11-12 15:10:15 -08005007# check for naked sscanf
5008 if ($^V && $^V ge 5.10.0 &&
5009 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005010 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005011 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5012 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5013 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5014 my $lc = $stat =~ tr@\n@@;
5015 $lc = $lc + $linenr;
5016 my $stat_real = raw_line($linenr, 0);
5017 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5018 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5019 }
5020 WARN("NAKED_SSCANF",
5021 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5022 }
5023
Joe Perchesafc819a2014-06-04 16:12:08 -07005024# check for simple sscanf that should be kstrto<foo>
5025 if ($^V && $^V ge 5.10.0 &&
5026 defined $stat &&
5027 $line =~ /\bsscanf\b/) {
5028 my $lc = $stat =~ tr@\n@@;
5029 $lc = $lc + $linenr;
5030 my $stat_real = raw_line($linenr, 0);
5031 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5032 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5033 }
5034 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5035 my $format = $6;
5036 my $count = $format =~ tr@%@%@;
5037 if ($count == 1 &&
5038 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5039 WARN("SSCANF_TO_KSTRTO",
5040 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5041 }
5042 }
5043 }
5044
Joe Perches70dc8a42013-09-11 14:23:58 -07005045# check for new externs in .h files.
5046 if ($realfile =~ /\.h$/ &&
5047 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005048 if (CHK("AVOID_EXTERNS",
5049 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005050 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005051 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005052 }
5053 }
5054
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005055# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005056 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005057 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005058 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005059 my $function_name = $1;
5060 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005061
5062 my $s = $stat;
5063 if (defined $cond) {
5064 substr($s, 0, length($cond), '');
5065 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005066 if ($s =~ /^\s*;/ &&
5067 $function_name ne 'uninitialized_var')
5068 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005069 WARN("AVOID_EXTERNS",
5070 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005071 }
5072
5073 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005074 WARN("FUNCTION_ARGUMENTS",
5075 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005076 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005077
5078 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5079 $stat =~ /^.\s*extern\s+/)
5080 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005081 WARN("AVOID_EXTERNS",
5082 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005083 }
5084
5085# checks for new __setup's
5086 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5087 my $name = $1;
5088
5089 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005090 CHK("UNDOCUMENTED_SETUP",
5091 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005092 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005093 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005094
5095# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005096 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005097 WARN("UNNECESSARY_CASTS",
5098 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005099 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005100
Joe Perchesa640d252013-07-03 15:05:21 -07005101# alloc style
5102# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5103 if ($^V && $^V ge 5.10.0 &&
5104 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5105 CHK("ALLOC_SIZEOF_STRUCT",
5106 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5107 }
5108
Joe Perches60a55362014-06-04 16:12:07 -07005109# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5110 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005111 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07005112 my $oldfunc = $3;
5113 my $a1 = $4;
5114 my $a2 = $10;
5115 my $newfunc = "kmalloc_array";
5116 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005117 my $r1 = $a1;
5118 my $r2 = $a2;
5119 if ($a1 =~ /^sizeof\s*\S/) {
5120 $r1 = $a2;
5121 $r2 = $a1;
5122 }
5123 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5124 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005125 if (WARN("ALLOC_WITH_MULTIPLY",
5126 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5127 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005128 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
Joe Perches60a55362014-06-04 16:12:07 -07005129
5130 }
5131 }
5132 }
5133
Joe Perches972fdea2013-04-29 16:18:12 -07005134# check for krealloc arg reuse
5135 if ($^V && $^V ge 5.10.0 &&
5136 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5137 WARN("KREALLOC_ARG_REUSE",
5138 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5139 }
5140
Joe Perches5ce59ae2013-02-21 16:44:18 -08005141# check for alloc argument mismatch
5142 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5143 WARN("ALLOC_ARRAY_ARGS",
5144 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5145 }
5146
Joe Perchescaf2a542011-01-12 16:59:56 -08005147# check for multiple semicolons
5148 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005149 if (WARN("ONE_SEMICOLON",
5150 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5151 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005152 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005153 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005154 }
5155
Joe Perches0ab90192014-12-10 15:51:57 -08005156# check for #defines like: 1 << <digit> that could be BIT(digit)
5157 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5158 my $ull = "";
5159 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5160 if (CHK("BIT_MACRO",
5161 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5162 $fix) {
5163 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5164 }
5165 }
5166
Joe Perchese81f2392014-08-06 16:11:25 -07005167# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005168 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5169 my $has_break = 0;
5170 my $has_statement = 0;
5171 my $count = 0;
5172 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005173 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005174 $prevline--;
5175 my $rline = $rawlines[$prevline - 1];
5176 my $fline = $lines[$prevline - 1];
5177 last if ($fline =~ /^\@\@/);
5178 next if ($fline =~ /^\-/);
5179 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5180 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5181 next if ($fline =~ /^.[\s$;]*$/);
5182 $has_statement = 1;
5183 $count++;
5184 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5185 }
5186 if (!$has_break && $has_statement) {
5187 WARN("MISSING_BREAK",
5188 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5189 }
5190 }
5191
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005192# check for switch/default statements without a break;
5193 if ($^V && $^V ge 5.10.0 &&
5194 defined $stat &&
5195 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5196 my $ctx = '';
5197 my $herectx = $here . "\n";
5198 my $cnt = statement_rawlines($stat);
5199 for (my $n = 0; $n < $cnt; $n++) {
5200 $herectx .= raw_line($linenr, $n) . "\n";
5201 }
5202 WARN("DEFAULT_NO_BREAK",
5203 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005204 }
5205
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005206# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005207 if ($line =~ /\b__FUNCTION__\b/) {
5208 if (WARN("USE_FUNC",
5209 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5210 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005211 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005212 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005213 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005214
Joe Perches62ec8182015-02-13 14:38:18 -08005215# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5216 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5217 ERROR("DATE_TIME",
5218 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5219 }
5220
Joe Perches2c924882012-03-23 15:02:20 -07005221# check for use of yield()
5222 if ($line =~ /\byield\s*\(\s*\)/) {
5223 WARN("YIELD",
5224 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5225 }
5226
Joe Perches179f8f42013-07-03 15:05:30 -07005227# check for comparisons against true and false
5228 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5229 my $lead = $1;
5230 my $arg = $2;
5231 my $test = $3;
5232 my $otype = $4;
5233 my $trail = $5;
5234 my $op = "!";
5235
5236 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5237
5238 my $type = lc($otype);
5239 if ($type =~ /^(?:true|false)$/) {
5240 if (("$test" eq "==" && "$type" eq "true") ||
5241 ("$test" eq "!=" && "$type" eq "false")) {
5242 $op = "";
5243 }
5244
5245 CHK("BOOL_COMPARISON",
5246 "Using comparison to $otype is error prone\n" . $herecurr);
5247
5248## maybe suggesting a correct construct would better
5249## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5250
5251 }
5252 }
5253
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005254# check for semaphores initialized locked
5255 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005256 WARN("CONSIDER_COMPLETION",
5257 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005258 }
Joe Perches6712d852012-03-23 15:02:20 -07005259
Joe Perches67d0a072011-10-31 17:13:10 -07005260# recommend kstrto* over simple_strto* and strict_strto*
5261 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005262 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005263 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005264 }
Joe Perches6712d852012-03-23 15:02:20 -07005265
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005266# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005267 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005268 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005269 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07005270 }
Joe Perches6712d852012-03-23 15:02:20 -07005271
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005272# check for various structs that are normally const (ops, kgdb, device_tree)
5273 my $const_structs = qr{
5274 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005275 address_space_operations|
5276 backlight_ops|
5277 block_device_operations|
5278 dentry_operations|
5279 dev_pm_ops|
5280 dma_map_ops|
5281 extent_io_ops|
5282 file_lock_operations|
5283 file_operations|
5284 hv_ops|
5285 ide_dma_ops|
5286 intel_dvo_dev_ops|
5287 item_operations|
5288 iwl_ops|
5289 kgdb_arch|
5290 kgdb_io|
5291 kset_uevent_ops|
5292 lock_manager_operations|
5293 microcode_ops|
5294 mtrr_ops|
5295 neigh_ops|
5296 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005297 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005298 pci_raw_ops|
5299 pipe_buf_operations|
5300 platform_hibernation_ops|
5301 platform_suspend_ops|
5302 proto_ops|
5303 rpc_pipe_ops|
5304 seq_operations|
5305 snd_ac97_build_ops|
5306 soc_pcmcia_socket_ops|
5307 stacktrace_ops|
5308 sysfs_ops|
5309 tty_operations|
5310 usb_mon_operations|
5311 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005312 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005313 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005314 WARN("CONST_STRUCT",
5315 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005316 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005317 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005318
5319# use of NR_CPUS is usually wrong
5320# ignore definitions of NR_CPUS and usage to define arrays as likely right
5321 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005322 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5323 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005324 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5325 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5326 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005327 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005328 WARN("NR_CPUS",
5329 "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 -07005330 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005331
Joe Perches52ea8502013-11-12 15:10:09 -08005332# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5333 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5334 ERROR("DEFINE_ARCH_HAS",
5335 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5336 }
5337
Joe Perchesacd93622015-02-13 14:38:38 -08005338# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5339 if ($^V && $^V ge 5.10.0 &&
5340 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5341 WARN("LIKELY_MISUSE",
5342 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5343 }
5344
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005345# whine mightly about in_atomic
5346 if ($line =~ /\bin_atomic\s*\(/) {
5347 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005348 ERROR("IN_ATOMIC",
5349 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005350 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005351 WARN("IN_ATOMIC",
5352 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005353 }
5354 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005355
5356# check for lockdep_set_novalidate_class
5357 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5358 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5359 if ($realfile !~ m@^kernel/lockdep@ &&
5360 $realfile !~ m@^include/linux/lockdep@ &&
5361 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005362 ERROR("LOCKDEP",
5363 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005364 }
5365 }
Dave Jones88f88312011-01-12 16:59:59 -08005366
Joe Perchesb392c642015-04-16 12:44:16 -07005367 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5368 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005369 WARN("EXPORTED_WORLD_WRITABLE",
5370 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005371 }
Joe Perches24358802014-04-03 14:49:13 -07005372
Joe Perches515a2352014-04-03 14:49:24 -07005373# Mode permission misuses where it seems decimal should be octal
5374# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5375 if ($^V && $^V ge 5.10.0 &&
5376 $line =~ /$mode_perms_search/) {
5377 foreach my $entry (@mode_permission_funcs) {
5378 my $func = $entry->[0];
5379 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005380
Joe Perches515a2352014-04-03 14:49:24 -07005381 my $skip_args = "";
5382 if ($arg_pos > 1) {
5383 $arg_pos--;
5384 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5385 }
5386 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5387 if ($line =~ /$test/) {
5388 my $val = $1;
5389 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005390
Joe Perches515a2352014-04-03 14:49:24 -07005391 if ($val !~ /^0$/ &&
5392 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5393 length($val) ne 4)) {
5394 ERROR("NON_OCTAL_PERMISSIONS",
5395 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005396 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5397 ERROR("EXPORTED_WORLD_WRITABLE",
5398 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005399 }
Joe Perches24358802014-04-03 14:49:13 -07005400 }
5401 }
5402 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005403 }
5404
5405 # If we have no input at all, then there is nothing to report on
5406 # so just keep quiet.
5407 if ($#rawlines == -1) {
5408 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005409 }
5410
Andy Whitcroft8905a672007-11-28 16:21:06 -08005411 # In mailback mode only produce a report in the negative, for
5412 # things that appear to be patches.
5413 if ($mailback && ($clean == 1 || !$is_patch)) {
5414 exit(0);
5415 }
5416
5417 # This is not a patch, and we are are in 'no-patch' mode so
5418 # just keep quiet.
5419 if (!$chk_patch && !$is_patch) {
5420 exit(0);
5421 }
5422
5423 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005424 ERROR("NOT_UNIFIED_DIFF",
5425 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005426 }
5427 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005428 ERROR("MISSING_SIGN_OFF",
5429 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005430 }
5431
Andy Whitcroft8905a672007-11-28 16:21:06 -08005432 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005433 if ($summary && !($clean == 1 && $quiet == 1)) {
5434 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005435 print "total: $cnt_error errors, $cnt_warn warnings, " .
5436 (($check)? "$cnt_chk checks, " : "") .
5437 "$cnt_lines lines checked\n";
5438 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005439 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005440
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005441 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005442
5443 if ($^V lt 5.10.0) {
5444 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5445 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5446 }
5447
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005448 # If there were whitespace errors which cleanpatch can fix
5449 # then suggest that.
5450 if ($rpt_cleaners) {
5451 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5452 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005453 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005454 }
5455 }
5456
Joe Perches91bfe482013-09-11 14:23:59 -07005457 hash_show_words(\%use_type, "Used");
5458 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005459
Joe Perchesd752fcc2014-08-06 16:11:05 -07005460 if ($clean == 0 && $fix &&
5461 ("@rawlines" ne "@fixed" ||
5462 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005463 my $newfile = $filename;
5464 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005465 my $linecount = 0;
5466 my $f;
5467
Joe Perchesd752fcc2014-08-06 16:11:05 -07005468 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5469
Joe Perches3705ce52013-07-03 15:05:31 -07005470 open($f, '>', $newfile)
5471 or die "$P: Can't open $newfile for write\n";
5472 foreach my $fixed_line (@fixed) {
5473 $linecount++;
5474 if ($file) {
5475 if ($linecount > 3) {
5476 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005477 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005478 }
5479 } else {
5480 print $f $fixed_line . "\n";
5481 }
5482 }
5483 close($f);
5484
5485 if (!$quiet) {
5486 print << "EOM";
5487Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5488
5489Do _NOT_ trust the results written to this file.
5490Do _NOT_ submit these changes without inspecting them for correctness.
5491
5492This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5493No warranties, expressed or implied...
5494
5495EOM
5496 }
5497 }
5498
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005499 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005500 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005501 }
5502 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005503 print << "EOM";
5504$vname has style problems, please review.
5505
5506If any of these errors are false positives, please report
5507them to the maintainer, see CHECKPATCH in MAINTAINERS.
5508EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005509 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005510
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005511 return $clean;
5512}