blob: 6b79beb2751dd013c6a49a68f4e2387fa5c711e9 [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
Wolfram Sang7840a942010-08-09 17:20:57 -0700446our $allowed_asm_includes = qr{(?x:
447 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700448 memory|
449 time|
450 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700451)};
452# memory.h: ARM has a custom one
453
Kees Cook66b47b42014-10-13 15:51:57 -0700454# Load common spelling mistakes and build regular expression list.
455my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700456my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700457
Joe Perches36061e32014-12-10 15:51:43 -0800458if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800459 while (<$spelling>) {
460 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700461
Joe Perches36061e32014-12-10 15:51:43 -0800462 $line =~ s/\s*\n?$//g;
463 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700464
Joe Perches36061e32014-12-10 15:51:43 -0800465 next if ($line =~ m/^\s*#/);
466 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700467
Joe Perches36061e32014-12-10 15:51:43 -0800468 my ($suspect, $fix) = split(/\|\|/, $line);
469
Joe Perches36061e32014-12-10 15:51:43 -0800470 $spelling_fix{$suspect} = $fix;
471 }
472 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800473} else {
474 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700475}
Kees Cook66b47b42014-10-13 15:51:57 -0700476
Joe Perchesebfd7d62015-04-16 12:44:14 -0700477if ($codespell) {
478 if (open(my $spelling, '<', $codespellfile)) {
479 while (<$spelling>) {
480 my $line = $_;
481
482 $line =~ s/\s*\n?$//g;
483 $line =~ s/^\s*//g;
484
485 next if ($line =~ m/^\s*#/);
486 next if ($line =~ m/^\s*$/);
487 next if ($line =~ m/, disabled/i);
488
489 $line =~ s/,.*$//;
490
491 my ($suspect, $fix) = split(/->/, $line);
492
493 $spelling_fix{$suspect} = $fix;
494 }
495 close($spelling);
496 } else {
497 warn "No codespell typos will be found - file '$codespellfile': $!\n";
498 }
499}
500
501$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
502
Andy Whitcroft8905a672007-11-28 16:21:06 -0800503sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700504 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
505 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700506 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700507 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700508 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700510 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800511 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800512 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Perches021158b2015-02-13 14:38:43 -0800513 (?:$typeOtherOSTypedefs\b)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700514 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700515 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800516 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700517 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518 }x;
Joe Perches18130872014-08-06 16:11:22 -0700519 $NonptrTypeMisordered = qr{
520 (?:$Modifier\s+|const\s+)*
521 (?:
522 (?:${Misordered}\b)
523 )
524 (?:\s+$Modifier|\s+const)*
525 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700526 $NonptrTypeWithAttr = qr{
527 (?:$Modifier\s+|const\s+)*
528 (?:
529 (?:typeof|__typeof__)\s*\([^\)]*\)|
530 (?:$typeTypedefs\b)|
Joe Perches021158b2015-02-13 14:38:43 -0800531 (?:$typeOtherOSTypedefs\b)|
Joe Perches8716de32013-09-11 14:24:05 -0700532 (?:${allWithAttr}\b)
533 )
534 (?:\s+$Modifier|\s+const)*
535 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800536 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700537 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700538 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700539 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800540 }x;
Joe Perches18130872014-08-06 16:11:22 -0700541 $TypeMisordered = qr{
542 $NonptrTypeMisordered
543 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
544 (?:\s+$Inline|\s+$Modifier)*
545 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700546 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700547 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800548}
549build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700550
Joe Perches7d2367a2011-07-25 17:13:22 -0700551our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700552
553# Using $balanced_parens, $LvalOrFunc, or $FuncArg
554# requires at least perl version v5.10.0
555# Any use must be runtime checked with $^V
556
557our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700558our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800559our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700560
Joe Perchesf8422302014-08-06 16:11:31 -0700561our $declaration_macros = qr{(?x:
562 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
563 (?:$Storage\s+)?LIST_HEAD\s*\(|
564 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
565)};
566
Joe Perches7d2367a2011-07-25 17:13:22 -0700567sub deparenthesize {
568 my ($string) = @_;
569 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700570
571 while ($string =~ /^\s*\(.*\)\s*$/) {
572 $string =~ s@^\s*\(\s*@@;
573 $string =~ s@\s*\)\s*$@@;
574 }
575
Joe Perches7d2367a2011-07-25 17:13:22 -0700576 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700577
Joe Perches7d2367a2011-07-25 17:13:22 -0700578 return $string;
579}
580
Joe Perches34456862013-07-03 15:05:34 -0700581sub seed_camelcase_file {
582 my ($file) = @_;
583
584 return if (!(-f $file));
585
586 local $/;
587
588 open(my $include_file, '<', "$file")
589 or warn "$P: Can't read '$file' $!\n";
590 my $text = <$include_file>;
591 close($include_file);
592
593 my @lines = split('\n', $text);
594
595 foreach my $line (@lines) {
596 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
597 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
598 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800599 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
600 $camelcase{$1} = 1;
601 } 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 -0700602 $camelcase{$1} = 1;
603 }
604 }
605}
606
607my $camelcase_seeded = 0;
608sub seed_camelcase_includes {
609 return if ($camelcase_seeded);
610
611 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700612 my $camelcase_cache = "";
613 my @include_files = ();
614
615 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700616
Richard Genoud3645e322014-02-10 14:25:32 -0800617 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700618 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
619 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700620 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700621 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700622 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700623 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700624 @include_files = split('\n', $files);
625 foreach my $file (@include_files) {
626 my $date = POSIX::strftime("%Y%m%d%H%M",
627 localtime((stat $file)[9]));
628 $last_mod_date = $date if ($last_mod_date < $date);
629 }
630 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700631 }
Joe Perchesc707a812013-07-08 16:00:43 -0700632
633 if ($camelcase_cache ne "" && -f $camelcase_cache) {
634 open(my $camelcase_file, '<', "$camelcase_cache")
635 or warn "$P: Can't read '$camelcase_cache' $!\n";
636 while (<$camelcase_file>) {
637 chomp;
638 $camelcase{$_} = 1;
639 }
640 close($camelcase_file);
641
642 return;
643 }
644
Richard Genoud3645e322014-02-10 14:25:32 -0800645 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700646 $files = `git ls-files "include/*.h"`;
647 @include_files = split('\n', $files);
648 }
649
Joe Perches34456862013-07-03 15:05:34 -0700650 foreach my $file (@include_files) {
651 seed_camelcase_file($file);
652 }
Joe Perches351b2a12013-07-03 15:05:36 -0700653
Joe Perchesc707a812013-07-08 16:00:43 -0700654 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700655 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700656 open(my $camelcase_file, '>', "$camelcase_cache")
657 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700658 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
659 print $camelcase_file ("$_\n");
660 }
661 close($camelcase_file);
662 }
Joe Perches34456862013-07-03 15:05:34 -0700663}
664
Joe Perchesd311cd42014-08-06 16:10:57 -0700665sub git_commit_info {
666 my ($commit, $id, $desc) = @_;
667
668 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
669
670 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
671 $output =~ s/^\s*//gm;
672 my @lines = split("\n", $output);
673
Joe Perches0d7835f2015-02-13 14:38:35 -0800674 return ($id, $desc) if ($#lines < 0);
675
Joe Perchesd311cd42014-08-06 16:10:57 -0700676 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
677# Maybe one day convert this block of bash into something that returns
678# all matching commit ids, but it's very slow...
679#
680# echo "checking commits $1..."
681# git rev-list --remotes | grep -i "^$1" |
682# while read line ; do
683# git log --format='%H %s' -1 $line |
684# echo "commit $(cut -c 1-12,41-)"
685# done
686 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
687 } else {
688 $id = substr($lines[0], 0, 12);
689 $desc = substr($lines[0], 41);
690 }
691
692 return ($id, $desc);
693}
694
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700695$chk_signoff = 0 if ($file);
696
Andy Whitcroft00df3442007-06-08 13:47:06 -0700697my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800698my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700699my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700700my @fixed_inserted = ();
701my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700702my $fixlinenr = -1;
703
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800704my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700705for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800706 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700707 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800708 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700709 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800710 } elsif ($filename eq '-') {
711 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700712 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800713 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700714 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700715 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800716 if ($filename eq '-') {
717 $vname = 'Your patch';
718 } else {
719 $vname = $filename;
720 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800721 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700722 chomp;
723 push(@rawlines, $_);
724 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800725 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800726 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700727 $exit = 1;
728 }
729 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800730 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700731 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700732 @fixed_inserted = ();
733 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700734 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700735}
736
737exit($exit);
738
739sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700740 my ($root) = @_;
741
742 my @tree_check = (
743 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
744 "README", "Documentation", "arch", "include", "drivers",
745 "fs", "init", "ipc", "kernel", "lib", "scripts",
746 );
747
748 foreach my $check (@tree_check) {
749 if (! -e $root . '/' . $check) {
750 return 0;
751 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700752 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700753 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700754}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700755
Joe Perches20112472011-07-25 17:13:23 -0700756sub parse_email {
757 my ($formatted_email) = @_;
758
759 my $name = "";
760 my $address = "";
761 my $comment = "";
762
763 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
764 $name = $1;
765 $address = $2;
766 $comment = $3 if defined $3;
767 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
768 $address = $1;
769 $comment = $2 if defined $2;
770 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
771 $address = $1;
772 $comment = $2 if defined $2;
773 $formatted_email =~ s/$address.*$//;
774 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700775 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700776 $name =~ s/^\"|\"$//g;
777 # If there's a name left after stripping spaces and
778 # leading quotes, and the address doesn't have both
779 # leading and trailing angle brackets, the address
780 # is invalid. ie:
781 # "joe smith joe@smith.com" bad
782 # "joe smith <joe@smith.com" bad
783 if ($name ne "" && $address !~ /^<[^>]+>$/) {
784 $name = "";
785 $address = "";
786 $comment = "";
787 }
788 }
789
Joe Perches3705ce52013-07-03 15:05:31 -0700790 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700791 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700792 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700793 $address =~ s/^\<|\>$//g;
794
795 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
796 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
797 $name = "\"$name\"";
798 }
799
800 return ($name, $address, $comment);
801}
802
803sub format_email {
804 my ($name, $address) = @_;
805
806 my $formatted_email;
807
Joe Perches3705ce52013-07-03 15:05:31 -0700808 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700809 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700810 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700811
812 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
813 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
814 $name = "\"$name\"";
815 }
816
817 if ("$name" eq "") {
818 $formatted_email = "$address";
819 } else {
820 $formatted_email = "$name <$address>";
821 }
822
823 return $formatted_email;
824}
825
Joe Perchesd311cd42014-08-06 16:10:57 -0700826sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700827 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700828
Joe Perchesbd474ca2014-08-06 16:11:10 -0700829 foreach my $path (split(/:/, $ENV{PATH})) {
830 if (-e "$path/$bin") {
831 return "$path/$bin";
832 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700833 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700834
Joe Perchesbd474ca2014-08-06 16:11:10 -0700835 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700836}
837
Joe Perches000d1cc12011-07-25 17:13:25 -0700838sub which_conf {
839 my ($conf) = @_;
840
841 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
842 if (-e "$path/$conf") {
843 return "$path/$conf";
844 }
845 }
846
847 return "";
848}
849
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700850sub expand_tabs {
851 my ($str) = @_;
852
853 my $res = '';
854 my $n = 0;
855 for my $c (split(//, $str)) {
856 if ($c eq "\t") {
857 $res .= ' ';
858 $n++;
859 for (; ($n % 8) != 0; $n++) {
860 $res .= ' ';
861 }
862 next;
863 }
864 $res .= $c;
865 $n++;
866 }
867
868 return $res;
869}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700870sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700871 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700872 return $res;
873}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700874
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700875sub line_stats {
876 my ($line) = @_;
877
878 # Drop the diff line leader and expand tabs
879 $line =~ s/^.//;
880 $line = expand_tabs($line);
881
882 # Pick the indent from the front of the line.
883 my ($white) = ($line =~ /^(\s*)/);
884
885 return (length($line), length($white));
886}
887
Andy Whitcroft773647a2008-03-28 14:15:58 -0700888my $sanitise_quote = '';
889
890sub sanitise_line_reset {
891 my ($in_comment) = @_;
892
893 if ($in_comment) {
894 $sanitise_quote = '*/';
895 } else {
896 $sanitise_quote = '';
897 }
898}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700899sub sanitise_line {
900 my ($line) = @_;
901
902 my $res = '';
903 my $l = '';
904
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800905 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700906 my $off = 0;
907 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700908
Andy Whitcroft773647a2008-03-28 14:15:58 -0700909 # Always copy over the diff marker.
910 $res = substr($line, 0, 1);
911
912 for ($off = 1; $off < length($line); $off++) {
913 $c = substr($line, $off, 1);
914
915 # Comments we are wacking completly including the begin
916 # and end, all to $;.
917 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
918 $sanitise_quote = '*/';
919
920 substr($res, $off, 2, "$;$;");
921 $off++;
922 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800923 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700924 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700925 $sanitise_quote = '';
926 substr($res, $off, 2, "$;$;");
927 $off++;
928 next;
929 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700930 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
931 $sanitise_quote = '//';
932
933 substr($res, $off, 2, $sanitise_quote);
934 $off++;
935 next;
936 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700937
938 # A \ in a string means ignore the next character.
939 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
940 $c eq "\\") {
941 substr($res, $off, 2, 'XX');
942 $off++;
943 next;
944 }
945 # Regular quotes.
946 if ($c eq "'" || $c eq '"') {
947 if ($sanitise_quote eq '') {
948 $sanitise_quote = $c;
949
950 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700951 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700952 } elsif ($sanitise_quote eq $c) {
953 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700954 }
955 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700956
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800957 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700958 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
959 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700960 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
961 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700962 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
963 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700964 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700965 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700966 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800967 }
968
Daniel Walker113f04a2009-09-21 17:04:35 -0700969 if ($sanitise_quote eq '//') {
970 $sanitise_quote = '';
971 }
972
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800973 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700974 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800975 my $clean = 'X' x length($1);
976 $res =~ s@\<.*\>@<$clean>@;
977
978 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700979 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800980 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700981 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800982 }
983
Andy Whitcroft00df3442007-06-08 13:47:06 -0700984 return $res;
985}
986
Joe Perchesa6962d72013-04-29 16:18:13 -0700987sub get_quoted_string {
988 my ($line, $rawline) = @_;
989
Joe Perches5e4f6ba2014-12-10 15:52:05 -0800990 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -0700991 return substr($rawline, $-[0], $+[0] - $-[0]);
992}
993
Andy Whitcroft8905a672007-11-28 16:21:06 -0800994sub ctx_statement_block {
995 my ($linenr, $remain, $off) = @_;
996 my $line = $linenr - 1;
997 my $blk = '';
998 my $soff = $off;
999 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001000 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001001
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001002 my $loff = 0;
1003
Andy Whitcroft8905a672007-11-28 16:21:06 -08001004 my $type = '';
1005 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001006 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001007 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001008 my $c;
1009 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001010
1011 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001012 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001013 @stack = (['', 0]) if ($#stack == -1);
1014
Andy Whitcroft773647a2008-03-28 14:15:58 -07001015 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001016 # If we are about to drop off the end, pull in more
1017 # context.
1018 if ($off >= $len) {
1019 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001020 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001021 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001022 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001023 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001024 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001025 $len = length($blk);
1026 $line++;
1027 last;
1028 }
1029 # Bail if there is no further context.
1030 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001031 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001032 last;
1033 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001034 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1035 $level++;
1036 $type = '#';
1037 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001038 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001039 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001040 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001041 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001042
Andy Whitcroft773647a2008-03-28 14:15:58 -07001043 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001044
1045 # Handle nested #if/#else.
1046 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1047 push(@stack, [ $type, $level ]);
1048 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1049 ($type, $level) = @{$stack[$#stack - 1]};
1050 } elsif ($remainder =~ /^#\s*endif\b/) {
1051 ($type, $level) = @{pop(@stack)};
1052 }
1053
Andy Whitcroft8905a672007-11-28 16:21:06 -08001054 # Statement ends at the ';' or a close '}' at the
1055 # outermost level.
1056 if ($level == 0 && $c eq ';') {
1057 last;
1058 }
1059
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001060 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001061 if ($level == 0 && $coff_set == 0 &&
1062 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1063 $remainder =~ /^(else)(?:\s|{)/ &&
1064 $remainder !~ /^else\s+if\b/) {
1065 $coff = $off + length($1) - 1;
1066 $coff_set = 1;
1067 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1068 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001069 }
1070
Andy Whitcroft8905a672007-11-28 16:21:06 -08001071 if (($type eq '' || $type eq '(') && $c eq '(') {
1072 $level++;
1073 $type = '(';
1074 }
1075 if ($type eq '(' && $c eq ')') {
1076 $level--;
1077 $type = ($level != 0)? '(' : '';
1078
1079 if ($level == 0 && $coff < $soff) {
1080 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001081 $coff_set = 1;
1082 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001083 }
1084 }
1085 if (($type eq '' || $type eq '{') && $c eq '{') {
1086 $level++;
1087 $type = '{';
1088 }
1089 if ($type eq '{' && $c eq '}') {
1090 $level--;
1091 $type = ($level != 0)? '{' : '';
1092
1093 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001094 if (substr($blk, $off + 1, 1) eq ';') {
1095 $off++;
1096 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001097 last;
1098 }
1099 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001100 # Preprocessor commands end at the newline unless escaped.
1101 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1102 $level--;
1103 $type = '';
1104 $off++;
1105 last;
1106 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001107 $off++;
1108 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001109 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001110 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001111 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001112 $line++;
1113 $remain--;
1114 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001115
1116 my $statement = substr($blk, $soff, $off - $soff + 1);
1117 my $condition = substr($blk, $soff, $coff - $soff + 1);
1118
1119 #warn "STATEMENT<$statement>\n";
1120 #warn "CONDITION<$condition>\n";
1121
Andy Whitcroft773647a2008-03-28 14:15:58 -07001122 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001123
1124 return ($statement, $condition,
1125 $line, $remain + 1, $off - $loff + 1, $level);
1126}
1127
Andy Whitcroftcf655042008-03-04 14:28:20 -08001128sub statement_lines {
1129 my ($stmt) = @_;
1130
1131 # Strip the diff line prefixes and rip blank lines at start and end.
1132 $stmt =~ s/(^|\n)./$1/g;
1133 $stmt =~ s/^\s*//;
1134 $stmt =~ s/\s*$//;
1135
1136 my @stmt_lines = ($stmt =~ /\n/g);
1137
1138 return $#stmt_lines + 2;
1139}
1140
1141sub statement_rawlines {
1142 my ($stmt) = @_;
1143
1144 my @stmt_lines = ($stmt =~ /\n/g);
1145
1146 return $#stmt_lines + 2;
1147}
1148
1149sub statement_block_size {
1150 my ($stmt) = @_;
1151
1152 $stmt =~ s/(^|\n)./$1/g;
1153 $stmt =~ s/^\s*{//;
1154 $stmt =~ s/}\s*$//;
1155 $stmt =~ s/^\s*//;
1156 $stmt =~ s/\s*$//;
1157
1158 my @stmt_lines = ($stmt =~ /\n/g);
1159 my @stmt_statements = ($stmt =~ /;/g);
1160
1161 my $stmt_lines = $#stmt_lines + 2;
1162 my $stmt_statements = $#stmt_statements + 1;
1163
1164 if ($stmt_lines > $stmt_statements) {
1165 return $stmt_lines;
1166 } else {
1167 return $stmt_statements;
1168 }
1169}
1170
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001171sub ctx_statement_full {
1172 my ($linenr, $remain, $off) = @_;
1173 my ($statement, $condition, $level);
1174
1175 my (@chunks);
1176
Andy Whitcroftcf655042008-03-04 14:28:20 -08001177 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001178 ($statement, $condition, $linenr, $remain, $off, $level) =
1179 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001180 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001181 push(@chunks, [ $condition, $statement ]);
1182 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1183 return ($level, $linenr, @chunks);
1184 }
1185
1186 # Pull in the following conditional/block pairs and see if they
1187 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001188 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001189 ($statement, $condition, $linenr, $remain, $off, $level) =
1190 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001191 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001192 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001193 #print "C: push\n";
1194 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001195 }
1196
1197 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001198}
1199
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001200sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001201 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001202 my $line;
1203 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001204 my $blk = '';
1205 my @o;
1206 my @c;
1207 my @res = ();
1208
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001209 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001210 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001211 for ($line = $start; $remain > 0; $line++) {
1212 next if ($rawlines[$line] =~ /^-/);
1213 $remain--;
1214
1215 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001216
1217 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001218 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001219 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001220 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001221 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001222 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001223 $level = pop(@stack);
1224 }
1225
Andy Whitcroft01464f32010-10-26 14:23:19 -07001226 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001227 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1228 if ($off > 0) {
1229 $off--;
1230 next;
1231 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001232
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001233 if ($c eq $close && $level > 0) {
1234 $level--;
1235 last if ($level == 0);
1236 } elsif ($c eq $open) {
1237 $level++;
1238 }
1239 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001240
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001241 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001242 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001243 }
1244
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001245 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001246 }
1247
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001248 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001249}
1250sub ctx_block_outer {
1251 my ($linenr, $remain) = @_;
1252
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001253 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1254 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001255}
1256sub ctx_block {
1257 my ($linenr, $remain) = @_;
1258
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001259 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1260 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001261}
1262sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001263 my ($linenr, $remain, $off) = @_;
1264
1265 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1266 return @r;
1267}
1268sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001269 my ($linenr, $remain) = @_;
1270
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001271 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001272}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001273sub ctx_statement_level {
1274 my ($linenr, $remain, $off) = @_;
1275
1276 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1277}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001278
1279sub ctx_locate_comment {
1280 my ($first_line, $end_line) = @_;
1281
1282 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001283 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001284 return $current_comment if (defined $current_comment);
1285
1286 # Look through the context and try and figure out if there is a
1287 # comment.
1288 my $in_comment = 0;
1289 $current_comment = '';
1290 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001291 my $line = $rawlines[$linenr - 1];
1292 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001293 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1294 $in_comment = 1;
1295 }
1296 if ($line =~ m@/\*@) {
1297 $in_comment = 1;
1298 }
1299 if (!$in_comment && $current_comment ne '') {
1300 $current_comment = '';
1301 }
1302 $current_comment .= $line . "\n" if ($in_comment);
1303 if ($line =~ m@\*/@) {
1304 $in_comment = 0;
1305 }
1306 }
1307
1308 chomp($current_comment);
1309 return($current_comment);
1310}
1311sub ctx_has_comment {
1312 my ($first_line, $end_line) = @_;
1313 my $cmt = ctx_locate_comment($first_line, $end_line);
1314
Andy Whitcroft00df3442007-06-08 13:47:06 -07001315 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001316 ##print "CMMT: $cmt\n";
1317
1318 return ($cmt ne '');
1319}
1320
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001321sub raw_line {
1322 my ($linenr, $cnt) = @_;
1323
1324 my $offset = $linenr - 1;
1325 $cnt++;
1326
1327 my $line;
1328 while ($cnt) {
1329 $line = $rawlines[$offset++];
1330 next if (defined($line) && $line =~ /^-/);
1331 $cnt--;
1332 }
1333
1334 return $line;
1335}
1336
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001337sub cat_vet {
1338 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001339 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001340
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001341 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001342 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1343 $res .= $1;
1344 if ($2 ne '') {
1345 $coded = sprintf("^%c", unpack('C', $2) + 64);
1346 $res .= $coded;
1347 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001348 }
1349 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001350
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001351 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001352}
1353
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001354my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001355my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001356my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001357my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001358
1359sub annotate_reset {
1360 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001361 $av_pending = '_';
1362 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001363 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001364}
1365
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001366sub annotate_values {
1367 my ($stream, $type) = @_;
1368
1369 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001370 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001371 my $cur = $stream;
1372
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001373 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001374
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001375 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001376 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001377 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001378 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001379 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001380 print "WS($1)\n" if ($dbg_values > 1);
1381 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001382 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001383 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001384 }
1385
Florian Micklerc023e4732011-01-12 16:59:58 -08001386 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001387 print "CAST($1)\n" if ($dbg_values > 1);
1388 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001389 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001390
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001391 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001392 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001393 $type = 'T';
1394
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001395 } elsif ($cur =~ /^($Modifier)\s*/) {
1396 print "MODIFIER($1)\n" if ($dbg_values > 1);
1397 $type = 'T';
1398
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001399 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001400 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001401 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001402 push(@av_paren_type, $type);
1403 if ($2 ne '') {
1404 $av_pending = 'N';
1405 }
1406 $type = 'E';
1407
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001408 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001409 print "UNDEF($1)\n" if ($dbg_values > 1);
1410 $av_preprocessor = 1;
1411 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001412
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001413 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001414 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001415 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001416
1417 push(@av_paren_type, $type);
1418 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001419 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001420
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001421 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001422 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1423 $av_preprocessor = 1;
1424
1425 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1426
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*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001430 print "PRE_END($1)\n" if ($dbg_values > 1);
1431
1432 $av_preprocessor = 1;
1433
1434 # Assume all arms of the conditional end as this
1435 # one does, and continue as if the #endif was not here.
1436 pop(@av_paren_type);
1437 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001438 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001439
1440 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001441 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001442
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001443 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1444 print "ATTR($1)\n" if ($dbg_values > 1);
1445 $av_pending = $type;
1446 $type = 'N';
1447
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001448 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001449 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001450 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001451 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001452 }
1453 $type = 'N';
1454
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001455 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001456 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001457 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001458 $type = 'N';
1459
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001460 } elsif ($cur =~/^(case)/o) {
1461 print "CASE($1)\n" if ($dbg_values > 1);
1462 $av_pend_colon = 'C';
1463 $type = 'N';
1464
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001465 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001466 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001467 $type = 'N';
1468
1469 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001470 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001471 push(@av_paren_type, $av_pending);
1472 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001473 $type = 'N';
1474
1475 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001476 my $new_type = pop(@av_paren_type);
1477 if ($new_type ne '_') {
1478 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001479 print "PAREN('$1') -> $type\n"
1480 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001481 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001482 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001483 }
1484
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001485 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001486 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001487 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001488 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001489
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001490 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1491 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001492 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001493 } elsif ($type eq 'E') {
1494 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001495 }
1496 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1497 $type = 'V';
1498
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001499 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001500 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001501 $type = 'V';
1502
1503 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001504 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001505 $type = 'N';
1506
Andy Whitcroftcf655042008-03-04 14:28:20 -08001507 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001508 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001509 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001510 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001511
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001512 } elsif ($cur =~/^(,)/) {
1513 print "COMMA($1)\n" if ($dbg_values > 1);
1514 $type = 'C';
1515
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001516 } elsif ($cur =~ /^(\?)/o) {
1517 print "QUESTION($1)\n" if ($dbg_values > 1);
1518 $type = 'N';
1519
1520 } elsif ($cur =~ /^(:)/o) {
1521 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1522
1523 substr($var, length($res), 1, $av_pend_colon);
1524 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1525 $type = 'E';
1526 } else {
1527 $type = 'N';
1528 }
1529 $av_pend_colon = 'O';
1530
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001531 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001532 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001533 $type = 'N';
1534
Andy Whitcroft0d413862008-10-15 22:02:16 -07001535 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001536 my $variant;
1537
1538 print "OPV($1)\n" if ($dbg_values > 1);
1539 if ($type eq 'V') {
1540 $variant = 'B';
1541 } else {
1542 $variant = 'U';
1543 }
1544
1545 substr($var, length($res), 1, $variant);
1546 $type = 'N';
1547
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001548 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001549 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001550 if ($1 ne '++' && $1 ne '--') {
1551 $type = 'N';
1552 }
1553
1554 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001555 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001556 }
1557 if (defined $1) {
1558 $cur = substr($cur, length($1));
1559 $res .= $type x length($1);
1560 }
1561 }
1562
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001563 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001564}
1565
Andy Whitcroft8905a672007-11-28 16:21:06 -08001566sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001567 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001568 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001569 ^(?:
1570 $Modifier|
1571 $Storage|
1572 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001573 DEFINE_\S+
1574 )$|
1575 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001576 goto|
1577 return|
1578 case|
1579 else|
1580 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001581 do|
1582 \#|
1583 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001584 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001585 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001586 )}x;
1587 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1588 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001589 # Check for modifiers.
1590 $possible =~ s/\s*$Storage\s*//g;
1591 $possible =~ s/\s*$Sparse\s*//g;
1592 if ($possible =~ /^\s*$/) {
1593
1594 } elsif ($possible =~ /\s/) {
1595 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001596 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001597 if ($modifier !~ $notPermitted) {
1598 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1599 push(@modifierList, $modifier);
1600 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001601 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001602
1603 } else {
1604 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1605 push(@typeList, $possible);
1606 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001607 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001608 } else {
1609 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001610 }
1611}
1612
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001613my $prefix = '';
1614
Joe Perches000d1cc12011-07-25 17:13:25 -07001615sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001616 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001617
Joe Perchescbec18a2014-04-03 14:49:19 -07001618 return defined $use_type{$type} if (scalar keys %use_type > 0);
1619
1620 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001621}
1622
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001623sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001624 my ($level, $type, $msg) = @_;
1625
1626 if (!show_type($type) ||
1627 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001628 return 0;
1629 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001630 my $line;
1631 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001632 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001633 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001634 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001635 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001636 $line = (split('\n', $line))[0] . "\n" if ($terse);
1637
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001638 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001639
1640 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001641}
Joe Perchescbec18a2014-04-03 14:49:19 -07001642
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001643sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001644 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001645}
Joe Perches000d1cc12011-07-25 17:13:25 -07001646
Joe Perchesd752fcc2014-08-06 16:11:05 -07001647sub fixup_current_range {
1648 my ($lineRef, $offset, $length) = @_;
1649
1650 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1651 my $o = $1;
1652 my $l = $2;
1653 my $no = $o + $offset;
1654 my $nl = $l + $length;
1655 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1656 }
1657}
1658
1659sub fix_inserted_deleted_lines {
1660 my ($linesRef, $insertedRef, $deletedRef) = @_;
1661
1662 my $range_last_linenr = 0;
1663 my $delta_offset = 0;
1664
1665 my $old_linenr = 0;
1666 my $new_linenr = 0;
1667
1668 my $next_insert = 0;
1669 my $next_delete = 0;
1670
1671 my @lines = ();
1672
1673 my $inserted = @{$insertedRef}[$next_insert++];
1674 my $deleted = @{$deletedRef}[$next_delete++];
1675
1676 foreach my $old_line (@{$linesRef}) {
1677 my $save_line = 1;
1678 my $line = $old_line; #don't modify the array
1679 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1680 $delta_offset = 0;
1681 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1682 $range_last_linenr = $new_linenr;
1683 fixup_current_range(\$line, $delta_offset, 0);
1684 }
1685
1686 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1687 $deleted = @{$deletedRef}[$next_delete++];
1688 $save_line = 0;
1689 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1690 }
1691
1692 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1693 push(@lines, ${$inserted}{'LINE'});
1694 $inserted = @{$insertedRef}[$next_insert++];
1695 $new_linenr++;
1696 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1697 }
1698
1699 if ($save_line) {
1700 push(@lines, $line);
1701 $new_linenr++;
1702 }
1703
1704 $old_linenr++;
1705 }
1706
1707 return @lines;
1708}
1709
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001710sub fix_insert_line {
1711 my ($linenr, $line) = @_;
1712
1713 my $inserted = {
1714 LINENR => $linenr,
1715 LINE => $line,
1716 };
1717 push(@fixed_inserted, $inserted);
1718}
1719
1720sub fix_delete_line {
1721 my ($linenr, $line) = @_;
1722
1723 my $deleted = {
1724 LINENR => $linenr,
1725 LINE => $line,
1726 };
1727
1728 push(@fixed_deleted, $deleted);
1729}
1730
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001731sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001732 my ($type, $msg) = @_;
1733
1734 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001735 our $clean = 0;
1736 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001737 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001738 }
Joe Perches3705ce52013-07-03 15:05:31 -07001739 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001740}
1741sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001742 my ($type, $msg) = @_;
1743
1744 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001745 our $clean = 0;
1746 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001747 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001748 }
Joe Perches3705ce52013-07-03 15:05:31 -07001749 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001750}
1751sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001752 my ($type, $msg) = @_;
1753
1754 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001755 our $clean = 0;
1756 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001757 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001758 }
Joe Perches3705ce52013-07-03 15:05:31 -07001759 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001760}
1761
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001762sub check_absolute_file {
1763 my ($absolute, $herecurr) = @_;
1764 my $file = $absolute;
1765
1766 ##print "absolute<$absolute>\n";
1767
1768 # See if any suffix of this path is a path within the tree.
1769 while ($file =~ s@^[^/]*/@@) {
1770 if (-f "$root/$file") {
1771 ##print "file<$file>\n";
1772 last;
1773 }
1774 }
1775 if (! -f _) {
1776 return 0;
1777 }
1778
1779 # It is, so see if the prefix is acceptable.
1780 my $prefix = $absolute;
1781 substr($prefix, -length($file)) = '';
1782
1783 ##print "prefix<$prefix>\n";
1784 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001785 WARN("USE_RELATIVE_PATH",
1786 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001787 }
1788}
1789
Joe Perches3705ce52013-07-03 15:05:31 -07001790sub trim {
1791 my ($string) = @_;
1792
Joe Perchesb34c6482013-09-11 14:24:01 -07001793 $string =~ s/^\s+|\s+$//g;
1794
1795 return $string;
1796}
1797
1798sub ltrim {
1799 my ($string) = @_;
1800
1801 $string =~ s/^\s+//;
1802
1803 return $string;
1804}
1805
1806sub rtrim {
1807 my ($string) = @_;
1808
1809 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001810
1811 return $string;
1812}
1813
Joe Perches52ea8502013-11-12 15:10:09 -08001814sub string_find_replace {
1815 my ($string, $find, $replace) = @_;
1816
1817 $string =~ s/$find/$replace/g;
1818
1819 return $string;
1820}
1821
Joe Perches3705ce52013-07-03 15:05:31 -07001822sub tabify {
1823 my ($leading) = @_;
1824
1825 my $source_indent = 8;
1826 my $max_spaces_before_tab = $source_indent - 1;
1827 my $spaces_to_tab = " " x $source_indent;
1828
1829 #convert leading spaces to tabs
1830 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1831 #Remove spaces before a tab
1832 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1833
1834 return "$leading";
1835}
1836
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001837sub pos_last_openparen {
1838 my ($line) = @_;
1839
1840 my $pos = 0;
1841
1842 my $opens = $line =~ tr/\(/\(/;
1843 my $closes = $line =~ tr/\)/\)/;
1844
1845 my $last_openparen = 0;
1846
1847 if (($opens == 0) || ($closes >= $opens)) {
1848 return -1;
1849 }
1850
1851 my $len = length($line);
1852
1853 for ($pos = 0; $pos < $len; $pos++) {
1854 my $string = substr($line, $pos);
1855 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1856 $pos += length($1) - 1;
1857 } elsif (substr($line, $pos, 1) eq '(') {
1858 $last_openparen = $pos;
1859 } elsif (index($string, '(') == -1) {
1860 last;
1861 }
1862 }
1863
Joe Perches91cb5192014-04-03 14:49:32 -07001864 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001865}
1866
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001867sub process {
1868 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001869
1870 my $linenr=0;
1871 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001872 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001873 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001874 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001875
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001876 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001877 my $indent;
1878 my $previndent=0;
1879 my $stashindent=0;
1880
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001881 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001882 my $signoff = 0;
1883 my $is_patch = 0;
1884
Joe Perches29ee1b02014-08-06 16:10:35 -07001885 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001886 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001887 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001888 my $non_utf8_charset = 0;
1889
Joe Perches365dd4e2014-08-06 16:10:42 -07001890 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001891 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001892
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001893 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001894 our $cnt_lines = 0;
1895 our $cnt_error = 0;
1896 our $cnt_warn = 0;
1897 our $cnt_chk = 0;
1898
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001899 # Trace the real file/line as we go.
1900 my $realfile = '';
1901 my $realline = 0;
1902 my $realcnt = 0;
1903 my $here = '';
1904 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001905 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001906 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001907 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001908
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001909 my $prev_values = 'E';
1910
1911 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001912 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001913 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001914 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001915 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001916
Joe Perches7e51f192013-09-11 14:23:57 -07001917 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001918
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001919 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001920 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001921 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001922 my @setup_docs = ();
1923 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001924
Joe Perchesd8b07712013-11-12 15:10:06 -08001925 my $camelcase_file_seeded = 0;
1926
Andy Whitcroft773647a2008-03-28 14:15:58 -07001927 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001928 my $line;
1929 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001930 $linenr++;
1931 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001932
Joe Perches3705ce52013-07-03 15:05:31 -07001933 push(@fixed, $rawline) if ($fix);
1934
Andy Whitcroft773647a2008-03-28 14:15:58 -07001935 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001936 $setup_docs = 0;
1937 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1938 $setup_docs = 1;
1939 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001940 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001941 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001942 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1943 $realline=$1-1;
1944 if (defined $2) {
1945 $realcnt=$3+1;
1946 } else {
1947 $realcnt=1+1;
1948 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001949 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001950
1951 # Guestimate if this is a continuing comment. Run
1952 # the context looking for a comment "edge". If this
1953 # edge is a close comment then we must be in a comment
1954 # at context start.
1955 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001956 my $cnt = $realcnt;
1957 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1958 next if (defined $rawlines[$ln - 1] &&
1959 $rawlines[$ln - 1] =~ /^-/);
1960 $cnt--;
1961 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001962 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001963 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1964 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1965 ($edge) = $1;
1966 last;
1967 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001968 }
1969 if (defined $edge && $edge eq '*/') {
1970 $in_comment = 1;
1971 }
1972
1973 # Guestimate if this is a continuing comment. If this
1974 # is the start of a diff block and this line starts
1975 # ' *' then it is very likely a comment.
1976 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001977 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001978 {
1979 $in_comment = 1;
1980 }
1981
1982 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1983 sanitise_line_reset($in_comment);
1984
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001985 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001986 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001987 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001988 $line = sanitise_line($rawline);
1989 }
1990 push(@lines, $line);
1991
1992 if ($realcnt > 1) {
1993 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1994 } else {
1995 $realcnt = 0;
1996 }
1997
1998 #print "==>$rawline\n";
1999 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002000
2001 if ($setup_docs && $line =~ /^\+/) {
2002 push(@setup_docs, $line);
2003 }
2004 }
2005
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002006 $prefix = '';
2007
Andy Whitcroft773647a2008-03-28 14:15:58 -07002008 $realcnt = 0;
2009 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002010 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002011 foreach my $line (@lines) {
2012 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002013 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002014 my $sline = $line; #copy of $line
2015 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002016
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002017 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002018
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002019#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002020 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002021 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002022 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002023 $realline=$1-1;
2024 if (defined $2) {
2025 $realcnt=$3+1;
2026 } else {
2027 $realcnt=1+1;
2028 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002029 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002030 $prev_values = 'E';
2031
Andy Whitcroft773647a2008-03-28 14:15:58 -07002032 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002033 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002034 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002035 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002036 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002037
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002038# track the line number as we move through the hunk, note that
2039# new versions of GNU diff omit the leading space on completely
2040# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002041 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002042 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002043 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002044
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002045 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002046 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002047
2048 # Track the previous line.
2049 ($prevline, $stashline) = ($stashline, $line);
2050 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002051 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2052
Andy Whitcroft773647a2008-03-28 14:15:58 -07002053 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002054
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002055 } elsif ($realcnt == 1) {
2056 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002057 }
2058
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002059 my $hunk_line = ($realcnt != 0);
2060
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002061#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002062 $prefix = "$filename:$realline: " if ($emacs && $file);
2063 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2064
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002065 $here = "#$linenr: " if (!$file);
2066 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002067
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002068 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002069 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002070 if ($line =~ /^diff --git.*?(\S+)$/) {
2071 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002072 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002073 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002074 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002075 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002076 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002077 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002078 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002079
2080 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002081 if (!$file && $tree && $p1_prefix ne '' &&
2082 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002083 WARN("PATCH_PREFIX",
2084 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002085 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002086
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002087 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002088 ERROR("MODIFIED_INCLUDE_ASM",
2089 "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 -07002090 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002091 $found_file = 1;
2092 }
2093
2094 if ($found_file) {
2095 if ($realfile =~ m@^(drivers/net/|net/)@) {
2096 $check = 1;
2097 } else {
2098 $check = $check_orig;
2099 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002100 next;
2101 }
2102
Randy Dunlap389834b2007-06-08 13:47:03 -07002103 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002104
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002105 my $hereline = "$here\n$rawline\n";
2106 my $herecurr = "$here\n$rawline\n";
2107 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002108
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002109 $cnt_lines++ if ($realcnt != 0);
2110
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002111# Check for incorrect file permissions
2112 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2113 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002114 if ($realfile !~ m@scripts/@ &&
2115 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002116 ERROR("EXECUTE_PERMISSIONS",
2117 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002118 }
2119 }
2120
Joe Perches20112472011-07-25 17:13:23 -07002121# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002122 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002123 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002124 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002125 }
2126
Joe Perchese0d975b2014-12-10 15:51:49 -08002127# Check if MAINTAINERS is being updated. If so, there's probably no need to
2128# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2129 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2130 $reported_maintainer_file = 1;
2131 }
2132
Joe Perches20112472011-07-25 17:13:23 -07002133# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002134 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002135 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002136 my $space_before = $1;
2137 my $sign_off = $2;
2138 my $space_after = $3;
2139 my $email = $4;
2140 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2141
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002142 if ($sign_off !~ /$signature_tags/) {
2143 WARN("BAD_SIGN_OFF",
2144 "Non-standard signature: $sign_off\n" . $herecurr);
2145 }
Joe Perches20112472011-07-25 17:13:23 -07002146 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002147 if (WARN("BAD_SIGN_OFF",
2148 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2149 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002150 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002151 "$ucfirst_sign_off $email";
2152 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002153 }
Joe Perches20112472011-07-25 17:13:23 -07002154 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002155 if (WARN("BAD_SIGN_OFF",
2156 "'$ucfirst_sign_off' is the preferred signature form\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 }
2161
Joe Perches20112472011-07-25 17:13:23 -07002162 }
2163 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002164 if (WARN("BAD_SIGN_OFF",
2165 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2166 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002167 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002168 "$ucfirst_sign_off $email";
2169 }
Joe Perches20112472011-07-25 17:13:23 -07002170 }
2171
2172 my ($email_name, $email_address, $comment) = parse_email($email);
2173 my $suggested_email = format_email(($email_name, $email_address));
2174 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002175 ERROR("BAD_SIGN_OFF",
2176 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002177 } else {
2178 my $dequoted = $suggested_email;
2179 $dequoted =~ s/^"//;
2180 $dequoted =~ s/" </ </;
2181 # Don't force email to have quotes
2182 # Allow just an angle bracketed address
2183 if ("$dequoted$comment" ne $email &&
2184 "<$email_address>$comment" ne $email &&
2185 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002186 WARN("BAD_SIGN_OFF",
2187 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002188 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002189 }
Joe Perches7e51f192013-09-11 14:23:57 -07002190
2191# Check for duplicate signatures
2192 my $sig_nospace = $line;
2193 $sig_nospace =~ s/\s//g;
2194 $sig_nospace = lc($sig_nospace);
2195 if (defined $signatures{$sig_nospace}) {
2196 WARN("BAD_SIGN_OFF",
2197 "Duplicate signature\n" . $herecurr);
2198 } else {
2199 $signatures{$sig_nospace} = 1;
2200 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002201 }
2202
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002203# Check email subject for common tools that don't need to be mentioned
2204 if ($in_header_lines &&
2205 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2206 WARN("EMAIL_SUBJECT",
2207 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2208 }
2209
Joe Perches9b3189e2014-06-04 16:12:10 -07002210# Check for old stable address
2211 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2212 ERROR("STABLE_ADDRESS",
2213 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2214 }
2215
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002216# Check for unwanted Gerrit info
2217 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2218 ERROR("GERRIT_CHANGE_ID",
2219 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2220 }
2221
Joe Perches0d7835f2015-02-13 14:38:35 -08002222# Check for git id commit length and improperly formed commit descriptions
2223 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002224 my $init_char = $1;
2225 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002226 my $short = 1;
2227 my $long = 0;
2228 my $case = 1;
2229 my $space = 1;
2230 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002231 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002232 my $id = '0123456789ab';
2233 my $orig_desc = "commit description";
2234 my $description = "";
2235
2236 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2237 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2238 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2239 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2240 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2241 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002242 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002243 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2244 defined $rawlines[$linenr] &&
2245 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2246 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002247 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002248 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2249 defined $rawlines[$linenr] &&
2250 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2251 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2252 $orig_desc = $1;
2253 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2254 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002255 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002256 }
2257
2258 ($id, $description) = git_commit_info($orig_commit,
2259 $id, $orig_desc);
2260
Joe Perches19c146a2015-02-13 14:39:00 -08002261 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002262 ERROR("GIT_COMMIT_ID",
2263 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2264 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002265 }
2266
Joe Perches13f19372014-08-06 16:10:59 -07002267# Check for added, moved or deleted files
2268 if (!$reported_maintainer_file && !$in_commit_log &&
2269 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2270 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2271 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2272 (defined($1) || defined($2))))) {
2273 $reported_maintainer_file = 1;
2274 WARN("FILE_PATH_CHANGES",
2275 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2276 }
2277
Andy Whitcroft00df3442007-06-08 13:47:06 -07002278# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002279 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002280 ERROR("CORRUPTED_PATCH",
2281 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002282 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002283 }
2284
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002285# Check for absolute kernel paths.
2286 if ($tree) {
2287 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2288 my $file = $1;
2289
2290 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2291 check_absolute_file($1, $herecurr)) {
2292 #
2293 } else {
2294 check_absolute_file($file, $herecurr);
2295 }
2296 }
2297 }
2298
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002299# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2300 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002301 $rawline !~ m/^$UTF8*$/) {
2302 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2303
2304 my $blank = copy_spacing($rawline);
2305 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2306 my $hereptr = "$hereline$ptr\n";
2307
Joe Perches34d99212011-07-25 17:13:26 -07002308 CHK("INVALID_UTF8",
2309 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002310 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002311
Joe Perches15662b32011-10-31 17:13:12 -07002312# Check if it's the start of a commit log
2313# (not a header line and we haven't seen the patch filename)
2314 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002315 !($rawline =~ /^\s+\S/ ||
2316 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002317 $in_header_lines = 0;
2318 $in_commit_log = 1;
2319 }
2320
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002321# Check if there is UTF-8 in a commit log when a mail header has explicitly
2322# declined it, i.e defined some charset where it is missing.
2323 if ($in_header_lines &&
2324 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2325 $1 !~ /utf-8/i) {
2326 $non_utf8_charset = 1;
2327 }
2328
2329 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002330 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002331 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002332 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2333 }
2334
Kees Cook66b47b42014-10-13 15:51:57 -07002335# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002336 if (defined($misspellings) &&
2337 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002338 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002339 my $typo = $1;
2340 my $typo_fix = $spelling_fix{lc($typo)};
2341 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2342 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2343 my $msg_type = \&WARN;
2344 $msg_type = \&CHK if ($file);
2345 if (&{$msg_type}("TYPO_SPELLING",
2346 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2347 $fix) {
2348 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2349 }
2350 }
2351 }
2352
Andy Whitcroft306708542008-10-15 22:02:28 -07002353# ignore non-hunk lines and lines being removed
2354 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002355
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002356#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002357 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002358 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002359 if (ERROR("DOS_LINE_ENDINGS",
2360 "DOS line endings\n" . $herevet) &&
2361 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002362 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002363 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002364 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2365 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002366 if (ERROR("TRAILING_WHITESPACE",
2367 "trailing whitespace\n" . $herevet) &&
2368 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002369 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002370 }
2371
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002372 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002373 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002374
Josh Triplett4783f892013-11-12 15:10:12 -08002375# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002376 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002377 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2378 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002379 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2380 my $msg_type = \&ERROR;
2381 $msg_type = \&CHK if ($file);
2382 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002383 "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 -08002384 }
2385
Andi Kleen33549572010-05-24 14:33:29 -07002386# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002387# Only applies when adding the entry originally, after that we do not have
2388# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002389 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002390 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002391 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002392 my $cnt = $realcnt;
2393 my $ln = $linenr + 1;
2394 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002395 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002396 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002397 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002398 $f = $lines[$ln - 1];
2399 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2400 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002401
2402 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002403 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002404
Joe Perches8d73e0e2014-08-06 16:10:46 -07002405 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002406 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002407 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002408 $length = -1;
2409 }
2410
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002411 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002412 $f =~ s/#.*//;
2413 $f =~ s/^\s+//;
2414 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002415 if ($f =~ /^\s*config\s/) {
2416 $is_end = 1;
2417 last;
2418 }
Andi Kleen33549572010-05-24 14:33:29 -07002419 $length++;
2420 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002421 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2422 WARN("CONFIG_DESCRIPTION",
2423 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2424 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002425 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002426 }
2427
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002428# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2429 if ($realfile =~ /Kconfig/ &&
2430 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2431 WARN("CONFIG_EXPERIMENTAL",
2432 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2433 }
2434
Christoph Jaeger327953e2015-02-13 14:38:29 -08002435# discourage the use of boolean for type definition attributes of Kconfig options
2436 if ($realfile =~ /Kconfig/ &&
2437 $line =~ /^\+\s*\bboolean\b/) {
2438 WARN("CONFIG_TYPE_BOOLEAN",
2439 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2440 }
2441
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002442 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2443 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2444 my $flag = $1;
2445 my $replacement = {
2446 'EXTRA_AFLAGS' => 'asflags-y',
2447 'EXTRA_CFLAGS' => 'ccflags-y',
2448 'EXTRA_CPPFLAGS' => 'cppflags-y',
2449 'EXTRA_LDFLAGS' => 'ldflags-y',
2450 };
2451
2452 WARN("DEPRECATED_VARIABLE",
2453 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2454 }
2455
Rob Herringbff5da42014-01-23 15:54:51 -08002456# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002457 if (defined $root &&
2458 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2459 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2460
Rob Herringbff5da42014-01-23 15:54:51 -08002461 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2462
Florian Vaussardcc933192014-04-03 14:49:27 -07002463 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2464 my $vp_file = $dt_path . "vendor-prefixes.txt";
2465
Rob Herringbff5da42014-01-23 15:54:51 -08002466 foreach my $compat (@compats) {
2467 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002468 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2469 my $compat3 = $compat;
2470 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2471 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002472 if ( $? >> 8 ) {
2473 WARN("UNDOCUMENTED_DT_STRING",
2474 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2475 }
2476
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002477 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2478 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002479 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002480 if ( $? >> 8 ) {
2481 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002482 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002483 }
2484 }
2485 }
2486
Andy Whitcroft5368df22008-10-15 22:02:27 -07002487# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002488 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002489
Joe Perches6cd7f382012-12-17 16:01:54 -08002490#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002491 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002492 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002493 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002494 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002495 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002496 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002497 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002498 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002499 }
2500
Andy Whitcroft8905a672007-11-28 16:21:06 -08002501# check for adding lines without a newline.
2502 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002503 WARN("MISSING_EOF_NEWLINE",
2504 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002505 }
2506
Mike Frysinger42e41c52009-09-21 17:04:40 -07002507# Blackfin: use hi/lo macros
2508 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2509 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2510 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002511 ERROR("LO_MACRO",
2512 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002513 }
2514 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2515 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002516 ERROR("HI_MACRO",
2517 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002518 }
2519 }
2520
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002521# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002522 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002523
2524# at the beginning of a line any tabs must come first and anything
2525# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002526 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2527 $rawline =~ /^\+\s* \s*/) {
2528 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002529 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002530 if (ERROR("CODE_INDENT",
2531 "code indent should use tabs where possible\n" . $herevet) &&
2532 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002533 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002534 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002535 }
2536
Alberto Panizzo08e44362010-03-05 13:43:54 -08002537# check for space before tabs.
2538 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2539 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002540 if (WARN("SPACE_BEFORE_TAB",
2541 "please, no space before tabs\n" . $herevet) &&
2542 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002543 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002544 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002545 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002546 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002547 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002548 }
2549
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002550# check for && or || at the start of a line
2551 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2552 CHK("LOGICAL_CONTINUATIONS",
2553 "Logical continuations should be on the previous line\n" . $hereprev);
2554 }
2555
2556# check multi-line statement indentation matches previous line
2557 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002558 $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 -07002559 $prevline =~ /^\+(\t*)(.*)$/;
2560 my $oldindent = $1;
2561 my $rest = $2;
2562
2563 my $pos = pos_last_openparen($rest);
2564 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002565 $line =~ /^(\+| )([ \t]*)/;
2566 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002567
2568 my $goodtabindent = $oldindent .
2569 "\t" x ($pos / 8) .
2570 " " x ($pos % 8);
2571 my $goodspaceindent = $oldindent . " " x $pos;
2572
2573 if ($newindent ne $goodtabindent &&
2574 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002575
2576 if (CHK("PARENTHESIS_ALIGNMENT",
2577 "Alignment should match open parenthesis\n" . $hereprev) &&
2578 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002579 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002580 s/^\+[ \t]*/\+$goodtabindent/;
2581 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002582 }
2583 }
2584 }
2585
Joe Perches6ab3a972015-04-16 12:44:05 -07002586# check for space after cast like "(int) foo" or "(struct foo) bar"
2587# avoid checking a few false positives:
2588# "sizeof(<type>)" or "__alignof__(<type>)"
2589# function pointer declarations like "(*foo)(int) = bar;"
2590# structure definitions like "(struct foo) { 0 };"
2591# multiline macros that define functions
2592# known attributes or the __attribute__ keyword
2593 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2594 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002595 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002596 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002597 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002598 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002599 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002600 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002601 }
2602
Joe Perches05880602012-10-04 17:13:35 -07002603 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002604 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002605 $rawline =~ /^\+[ \t]*\*/ &&
2606 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002607 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2608 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2609 }
2610
2611 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002612 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2613 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002614 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002615 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2616 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2617 "networking block comments start with * on subsequent lines\n" . $hereprev);
2618 }
2619
2620 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002621 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2622 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2623 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2624 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002625 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2626 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2627 }
2628
Joe Perches7f619192014-08-06 16:10:39 -07002629# check for missing blank lines after struct/union declarations
2630# with exceptions for various attributes and macros
2631 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2632 $line =~ /^\+/ &&
2633 !($line =~ /^\+\s*$/ ||
2634 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2635 $line =~ /^\+\s*MODULE_/i ||
2636 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2637 $line =~ /^\+[a-z_]*init/ ||
2638 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2639 $line =~ /^\+\s*DECLARE/ ||
2640 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002641 if (CHK("LINE_SPACING",
2642 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2643 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002644 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002645 }
Joe Perches7f619192014-08-06 16:10:39 -07002646 }
2647
Joe Perches365dd4e2014-08-06 16:10:42 -07002648# check for multiple consecutive blank lines
2649 if ($prevline =~ /^[\+ ]\s*$/ &&
2650 $line =~ /^\+\s*$/ &&
2651 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002652 if (CHK("LINE_SPACING",
2653 "Please don't use multiple blank lines\n" . $hereprev) &&
2654 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002655 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002656 }
2657
Joe Perches365dd4e2014-08-06 16:10:42 -07002658 $last_blank_line = $linenr;
2659 }
2660
Joe Perches3b617e32014-04-03 14:49:28 -07002661# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002662 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2663 # actual declarations
2664 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002665 # function pointer declarations
2666 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002667 # foo bar; where foo is some local typedef or #define
2668 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2669 # known declaration macros
2670 $prevline =~ /^\+\s+$declaration_macros/) &&
2671 # for "else if" which can look like "$Ident $Ident"
2672 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2673 # other possible extensions of declaration lines
2674 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2675 # not starting a section or a macro "\" extended line
2676 $prevline =~ /(?:\{\s*|\\)$/) &&
2677 # looks like a declaration
2678 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002679 # function pointer declarations
2680 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002681 # foo bar; where foo is some local typedef or #define
2682 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2683 # known declaration macros
2684 $sline =~ /^\+\s+$declaration_macros/ ||
2685 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002686 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002687 # start or end of block or continuation of declaration
2688 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2689 # bitfield continuation
2690 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2691 # other possible extensions of declaration lines
2692 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2693 # indentation of previous and current line are the same
2694 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002695 if (WARN("LINE_SPACING",
2696 "Missing a blank line after declarations\n" . $hereprev) &&
2697 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002698 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002699 }
Joe Perches3b617e32014-04-03 14:49:28 -07002700 }
2701
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002702# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002703# Exceptions:
2704# 1) within comments
2705# 2) indented preprocessor commands
2706# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002707 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002708 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002709 if (WARN("LEADING_SPACE",
2710 "please, no spaces at the start of a line\n" . $herevet) &&
2711 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002712 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002713 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002714 }
2715
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002716# check we are in a valid C source file if not then ignore this hunk
2717 next if ($realfile !~ /\.(h|c)$/);
2718
Joe Perches032a4c02014-08-06 16:10:29 -07002719# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002720# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002721# if the previous line is a break or return and is indented 1 tab more...
2722 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2723 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002724 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2725 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2726 defined $lines[$linenr] &&
2727 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002728 WARN("UNNECESSARY_ELSE",
2729 "else is not generally useful after a break or return\n" . $hereprev);
2730 }
2731 }
2732
Joe Perchesc00df192014-08-06 16:11:01 -07002733# check indentation of a line with a break;
2734# if the previous line is a goto or return and is indented the same # of tabs
2735 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2736 my $tabs = $1;
2737 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2738 WARN("UNNECESSARY_BREAK",
2739 "break is not useful after a goto or return\n" . $hereprev);
2740 }
2741 }
2742
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002743# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2744 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2745 WARN("CONFIG_EXPERIMENTAL",
2746 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2747 }
2748
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002749# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002750 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002751 WARN("CVS_KEYWORD",
2752 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002753 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002754
Mike Frysinger42e41c52009-09-21 17:04:40 -07002755# Blackfin: don't use __builtin_bfin_[cs]sync
2756 if ($line =~ /__builtin_bfin_csync/) {
2757 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002758 ERROR("CSYNC",
2759 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002760 }
2761 if ($line =~ /__builtin_bfin_ssync/) {
2762 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002763 ERROR("SSYNC",
2764 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002765 }
2766
Joe Perches56e77d72013-02-21 16:44:14 -08002767# check for old HOTPLUG __dev<foo> section markings
2768 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2769 WARN("HOTPLUG_SECTION",
2770 "Using $1 is unnecessary\n" . $herecurr);
2771 }
2772
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002773# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002774 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2775 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002776#print "LINE<$line>\n";
2777 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002778 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002779 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002780 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002781 $stat =~ s/\n./\n /g;
2782 $cond =~ s/\n./\n /g;
2783
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002784#print "linenr<$linenr> <$stat>\n";
2785 # If this statement has no statement boundaries within
2786 # it there is no point in retrying a statement scan
2787 # until we hit end of it.
2788 my $frag = $stat; $frag =~ s/;+\s*$//;
2789 if ($frag !~ /(?:{|;)/) {
2790#print "skip<$line_nr_next>\n";
2791 $suppress_statement = $line_nr_next;
2792 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002793
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002794 # Find the real next line.
2795 $realline_next = $line_nr_next;
2796 if (defined $realline_next &&
2797 (!defined $lines[$realline_next - 1] ||
2798 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2799 $realline_next++;
2800 }
2801
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002802 my $s = $stat;
2803 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002804
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002805 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002806 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002807
2808 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002809 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002810
Andy Whitcroft463f2862009-09-21 17:04:34 -07002811 } elsif ($s =~ /^.\s*else\b/s) {
2812
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002813 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002814 } 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 -07002815 my $type = $1;
2816 $type =~ s/\s+/ /g;
2817 possible($type, "A:" . $s);
2818
Andy Whitcroft8905a672007-11-28 16:21:06 -08002819 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002820 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002821 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002822 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002823
2824 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002825 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002826 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002827 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002828
2829 # Check for any sort of function declaration.
2830 # int foo(something bar, other baz);
2831 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002832 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 -08002833 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002834
Andy Whitcroftcf655042008-03-04 14:28:20 -08002835 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002836 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002837 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002838
Andy Whitcroft8905a672007-11-28 16:21:06 -08002839 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002840 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002841
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002842 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002843 }
2844 }
2845 }
2846
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002847 }
2848
Andy Whitcroft653d4872007-06-23 17:16:34 -07002849#
2850# Checks which may be anchored in the context.
2851#
2852
2853# Check for switch () and associated case and default
2854# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002855 if ($line=~/\bswitch\s*\(.*\)/) {
2856 my $err = '';
2857 my $sep = '';
2858 my @ctx = ctx_block_outer($linenr, $realcnt);
2859 shift(@ctx);
2860 for my $ctx (@ctx) {
2861 my ($clen, $cindent) = line_stats($ctx);
2862 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2863 $indent != $cindent) {
2864 $err .= "$sep$ctx\n";
2865 $sep = '';
2866 } else {
2867 $sep = "[...]\n";
2868 }
2869 }
2870 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002871 ERROR("SWITCH_CASE_INDENT_LEVEL",
2872 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002873 }
2874 }
2875
2876# if/while/etc brace do not go on next line, unless defining a do while loop,
2877# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002878 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 -07002879 my $pre_ctx = "$1$2";
2880
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002881 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002882
2883 if ($line =~ /^\+\t{6,}/) {
2884 WARN("DEEP_INDENTATION",
2885 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2886 }
2887
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002888 my $ctx_cnt = $realcnt - $#ctx - 1;
2889 my $ctx = join("\n", @ctx);
2890
Andy Whitcroft548596d2008-07-23 21:29:01 -07002891 my $ctx_ln = $linenr;
2892 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002893
Andy Whitcroft548596d2008-07-23 21:29:01 -07002894 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2895 defined $lines[$ctx_ln - 1] &&
2896 $lines[$ctx_ln - 1] =~ /^-/)) {
2897 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2898 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002899 $ctx_ln++;
2900 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002901
Andy Whitcroft53210162008-07-23 21:29:03 -07002902 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2903 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002904
Joe Perchesd752fcc2014-08-06 16:11:05 -07002905 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002906 ERROR("OPEN_BRACE",
2907 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002908 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002909 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002910 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2911 $ctx =~ /\)\s*\;\s*$/ &&
2912 defined $lines[$ctx_ln - 1])
2913 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002914 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2915 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002916 WARN("TRAILING_SEMICOLON",
2917 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002918 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002919 }
2920 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002921 }
2922
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002923# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002924 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 -08002925 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2926 ctx_statement_block($linenr, $realcnt, 0)
2927 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002928 my ($s, $c) = ($stat, $cond);
2929
2930 substr($s, 0, length($c), '');
2931
2932 # Make sure we remove the line prefixes as we have
2933 # none on the first line, and are going to readd them
2934 # where necessary.
2935 $s =~ s/\n./\n/gs;
2936
2937 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002938 my @newlines = ($c =~ /\n/gs);
2939 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002940
2941 # We want to check the first line inside the block
2942 # starting at the end of the conditional, so remove:
2943 # 1) any blank line termination
2944 # 2) any opening brace { on end of the line
2945 # 3) any do (...) {
2946 my $continuation = 0;
2947 my $check = 0;
2948 $s =~ s/^.*\bdo\b//;
2949 $s =~ s/^\s*{//;
2950 if ($s =~ s/^\s*\\//) {
2951 $continuation = 1;
2952 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002953 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002954 $check = 1;
2955 $cond_lines++;
2956 }
2957
2958 # Also ignore a loop construct at the end of a
2959 # preprocessor statement.
2960 if (($prevline =~ /^.\s*#\s*define\s/ ||
2961 $prevline =~ /\\\s*$/) && $continuation == 0) {
2962 $check = 0;
2963 }
2964
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002965 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002966 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002967 while ($cond_ptr != $cond_lines) {
2968 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002969
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002970 # If we see an #else/#elif then the code
2971 # is not linear.
2972 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2973 $check = 0;
2974 }
2975
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002976 # Ignore:
2977 # 1) blank lines, they should be at 0,
2978 # 2) preprocessor lines, and
2979 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002980 if ($continuation ||
2981 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002982 $s =~ /^\s*#\s*?/ ||
2983 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002984 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002985 if ($s =~ s/^.*?\n//) {
2986 $cond_lines++;
2987 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002988 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002989 }
2990
2991 my (undef, $sindent) = line_stats("+" . $s);
2992 my $stat_real = raw_line($linenr, $cond_lines);
2993
2994 # Check if either of these lines are modified, else
2995 # this is not this patch's fault.
2996 if (!defined($stat_real) ||
2997 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2998 $check = 0;
2999 }
3000 if (defined($stat_real) && $cond_lines > 1) {
3001 $stat_real = "[...]\n$stat_real";
3002 }
3003
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003004 #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 -07003005
3006 if ($check && (($sindent % 8) != 0 ||
3007 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003008 WARN("SUSPECT_CODE_INDENT",
3009 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003010 }
3011 }
3012
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003013 # Track the 'values' across context and added lines.
3014 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003015 my ($curr_values, $curr_vars) =
3016 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003017 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003018 if ($dbg_values) {
3019 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003020 print "$linenr > .$outline\n";
3021 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003022 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003023 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003024 $prev_values = substr($curr_values, -1);
3025
Andy Whitcroft00df3442007-06-08 13:47:06 -07003026#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003027 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003028
Andy Whitcroft653d4872007-06-23 17:16:34 -07003029# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003030 if ($dbg_type) {
3031 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003032 ERROR("TEST_TYPE",
3033 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003034 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003035 ERROR("TEST_NOT_TYPE",
3036 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003037 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003038 next;
3039 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003040# TEST: allow direct testing of the attribute matcher.
3041 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003042 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003043 ERROR("TEST_ATTR",
3044 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003045 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003046 ERROR("TEST_NOT_ATTR",
3047 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003048 }
3049 next;
3050 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003051
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003052# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003053 if ($line =~ /^.\s*{/ &&
3054 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003055 if (ERROR("OPEN_BRACE",
3056 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003057 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3058 fix_delete_line($fixlinenr - 1, $prevrawline);
3059 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003060 my $fixedline = $prevrawline;
3061 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003062 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003063 $fixedline = $line;
3064 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003065 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003066 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003067 }
3068
Andy Whitcroft653d4872007-06-23 17:16:34 -07003069#
3070# Checks which are anchored on the added line.
3071#
3072
3073# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003074 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003075 my $path = $1;
3076 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003077 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003078 "malformed #include filename\n" . $herecurr);
3079 }
3080 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3081 ERROR("UAPI_INCLUDE",
3082 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003083 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003084 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003085
3086# no C99 // comments
3087 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003088 if (ERROR("C99_COMMENTS",
3089 "do not use C99 // comments\n" . $herecurr) &&
3090 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003091 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003092 if ($line =~ /\/\/(.*)$/) {
3093 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003094 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003095 }
3096 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003097 }
3098 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003099 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003100 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003101
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003102# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3103# the whole statement.
3104#print "APW <$lines[$realline_next - 1]>\n";
3105 if (defined $realline_next &&
3106 exists $lines[$realline_next - 1] &&
3107 !defined $suppress_export{$realline_next} &&
3108 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3109 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003110 # Handle definitions which produce identifiers with
3111 # a prefix:
3112 # XXX(foo);
3113 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003114 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003115 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003116 $name =~ /^${Ident}_$2/) {
3117#print "FOO C name<$name>\n";
3118 $suppress_export{$realline_next} = 1;
3119
3120 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003121 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003122 ^.DEFINE_$Ident\(\Q$name\E\)|
3123 ^.DECLARE_$Ident\(\Q$name\E\)|
3124 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003125 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3126 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003127 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003128#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3129 $suppress_export{$realline_next} = 2;
3130 } else {
3131 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003132 }
3133 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003134 if (!defined $suppress_export{$linenr} &&
3135 $prevline =~ /^.\s*$/ &&
3136 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3137 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3138#print "FOO B <$lines[$linenr - 1]>\n";
3139 $suppress_export{$linenr} = 2;
3140 }
3141 if (defined $suppress_export{$linenr} &&
3142 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003143 WARN("EXPORT_SYMBOL",
3144 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003145 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003146
Joe Eloff5150bda2010-08-09 17:21:00 -07003147# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003148 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3149 if (ERROR("GLOBAL_INITIALISERS",
3150 "do not initialise globals to 0 or NULL\n" .
3151 $herecurr) &&
3152 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003153 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003154 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003155 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003156# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003157 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3158 if (ERROR("INITIALISED_STATIC",
3159 "do not initialise statics to 0 or NULL\n" .
3160 $herecurr) &&
3161 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003162 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003163 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003164 }
3165
Joe Perches18130872014-08-06 16:11:22 -07003166# check for misordered declarations of char/short/int/long with signed/unsigned
3167 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3168 my $tmp = trim($1);
3169 WARN("MISORDERED_TYPE",
3170 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3171 }
3172
Joe Perchescb710ec2010-10-26 14:23:20 -07003173# check for static const char * arrays.
3174 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003175 WARN("STATIC_CONST_CHAR_ARRAY",
3176 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003177 $herecurr);
3178 }
3179
3180# check for static char foo[] = "bar" declarations.
3181 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003182 WARN("STATIC_CONST_CHAR_ARRAY",
3183 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003184 $herecurr);
3185 }
3186
Joe Perches9b0fa602014-04-03 14:49:18 -07003187# check for non-global char *foo[] = {"bar", ...} declarations.
3188 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3189 WARN("STATIC_CONST_CHAR_ARRAY",
3190 "char * array declaration might be better as static const\n" .
3191 $herecurr);
3192 }
3193
Joe Perchesb36190c2014-01-27 17:07:18 -08003194# check for function declarations without arguments like "int foo()"
3195 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3196 if (ERROR("FUNCTION_WITHOUT_ARGS",
3197 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3198 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003199 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003200 }
3201 }
3202
Joe Perches92e112f2013-12-13 11:36:22 -07003203# check for uses of DEFINE_PCI_DEVICE_TABLE
3204 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3205 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3206 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3207 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003208 $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 -07003209 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003210 }
3211
Andy Whitcroft653d4872007-06-23 17:16:34 -07003212# check for new typedefs, only function parameters and sparse annotations
3213# make sense.
3214 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003215 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003216 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003217 $line !~ /\b$typeTypedefs\b/ &&
Joe Perches021158b2015-02-13 14:38:43 -08003218 $line !~ /\b$typeOtherOSTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003219 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003220 WARN("NEW_TYPEDEFS",
3221 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003222 }
3223
3224# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003225 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003226 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3227 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003228 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003229
Andy Whitcroft65863862009-01-06 14:41:21 -08003230 # Should start with a space.
3231 $to =~ s/^(\S)/ $1/;
3232 # Should not end with a space.
3233 $to =~ s/\s+$//;
3234 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003235 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003236 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003237
Joe Perches3705ce52013-07-03 15:05:31 -07003238## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003239 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003240 if (ERROR("POINTER_LOCATION",
3241 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3242 $fix) {
3243 my $sub_from = $ident;
3244 my $sub_to = $ident;
3245 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003246 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003247 s@\Q$sub_from\E@$sub_to@;
3248 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003249 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003250 }
3251 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3252 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003253 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003254
Andy Whitcroft65863862009-01-06 14:41:21 -08003255 # Should start with a space.
3256 $to =~ s/^(\S)/ $1/;
3257 # Should not end with a space.
3258 $to =~ s/\s+$//;
3259 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003260 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003261 }
3262 # Modifiers should have spaces.
3263 $to =~ s/(\b$Modifier$)/$1 /;
3264
Joe Perches3705ce52013-07-03 15:05:31 -07003265## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003266 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003267 if (ERROR("POINTER_LOCATION",
3268 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3269 $fix) {
3270
3271 my $sub_from = $match;
3272 my $sub_to = $match;
3273 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003274 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003275 s@\Q$sub_from\E@$sub_to@;
3276 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003277 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003278 }
3279
3280# # no BUG() or BUG_ON()
3281# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3282# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3283# print "$herecurr";
3284# $clean = 0;
3285# }
3286
Andy Whitcroft8905a672007-11-28 16:21:06 -08003287 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003288 WARN("LINUX_VERSION_CODE",
3289 "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 -08003290 }
3291
Joe Perches17441222011-06-15 15:08:17 -07003292# check for uses of printk_ratelimit
3293 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003294 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003295 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003296 }
3297
Andy Whitcroft00df3442007-06-08 13:47:06 -07003298# printk should use KERN_* levels. Note that follow on printk's on the
3299# same line do not need a level, so we use the current block context
3300# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003301# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003302# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003303 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003304 my $ok = 0;
3305 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3306 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003307 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003308 # with "\n" ignore it, else it is to blame
3309 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3310 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3311 $ok = 1;
3312 }
3313 last;
3314 }
3315 }
3316 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003317 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3318 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003319 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003320 }
3321
Joe Perches243f3802012-05-31 16:26:09 -07003322 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3323 my $orig = $1;
3324 my $level = lc($orig);
3325 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003326 my $level2 = $level;
3327 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003328 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003329 "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 -07003330 }
3331
3332 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003333 if (WARN("PREFER_PR_LEVEL",
3334 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3335 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003336 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003337 s/\bpr_warning\b/pr_warn/;
3338 }
Joe Perches243f3802012-05-31 16:26:09 -07003339 }
3340
Joe Perchesdc139312013-02-21 16:44:13 -08003341 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3342 my $orig = $1;
3343 my $level = lc($orig);
3344 $level = "warn" if ($level eq "warning");
3345 $level = "dbg" if ($level eq "debug");
3346 WARN("PREFER_DEV_LEVEL",
3347 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3348 }
3349
Andy Whitcroft653d4872007-06-23 17:16:34 -07003350# function brace can't be on same line, except for #defines of do while,
3351# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003352 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003353 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003354 if (ERROR("OPEN_BRACE",
3355 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3356 $fix) {
3357 fix_delete_line($fixlinenr, $rawline);
3358 my $fixed_line = $rawline;
3359 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3360 my $line1 = $1;
3361 my $line2 = $2;
3362 fix_insert_line($fixlinenr, ltrim($line1));
3363 fix_insert_line($fixlinenr, "\+{");
3364 if ($line2 !~ /^\s*$/) {
3365 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3366 }
3367 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003368 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003369
Andy Whitcroft8905a672007-11-28 16:21:06 -08003370# open braces for enum, union and struct go on the same line.
3371 if ($line =~ /^.\s*{/ &&
3372 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003373 if (ERROR("OPEN_BRACE",
3374 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3375 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3376 fix_delete_line($fixlinenr - 1, $prevrawline);
3377 fix_delete_line($fixlinenr, $rawline);
3378 my $fixedline = rtrim($prevrawline) . " {";
3379 fix_insert_line($fixlinenr, $fixedline);
3380 $fixedline = $rawline;
3381 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3382 if ($fixedline !~ /^\+\s*$/) {
3383 fix_insert_line($fixlinenr, $fixedline);
3384 }
3385 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003386 }
3387
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003388# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003389 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3390 if (WARN("SPACING",
3391 "missing space after $1 definition\n" . $herecurr) &&
3392 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003393 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003394 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3395 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003396 }
3397
Joe Perches31070b52014-01-23 15:54:49 -08003398# Function pointer declarations
3399# check spacing between type, funcptr, and args
3400# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003401 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003402 my $declare = $1;
3403 my $pre_pointer_space = $2;
3404 my $post_pointer_space = $3;
3405 my $funcname = $4;
3406 my $post_funcname_space = $5;
3407 my $pre_args_space = $6;
3408
Joe Perches91f72e92014-04-03 14:49:12 -07003409# the $Declare variable will capture all spaces after the type
3410# so check it for a missing trailing missing space but pointer return types
3411# don't need a space so don't warn for those.
3412 my $post_declare_space = "";
3413 if ($declare =~ /(\s+)$/) {
3414 $post_declare_space = $1;
3415 $declare = rtrim($declare);
3416 }
3417 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003418 WARN("SPACING",
3419 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003420 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003421 }
3422
3423# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003424# This test is not currently implemented because these declarations are
3425# equivalent to
3426# int foo(int bar, ...)
3427# and this is form shouldn't/doesn't generate a checkpatch warning.
3428#
3429# elsif ($declare =~ /\s{2,}$/) {
3430# WARN("SPACING",
3431# "Multiple spaces after return type\n" . $herecurr);
3432# }
Joe Perches31070b52014-01-23 15:54:49 -08003433
3434# unnecessary space "type ( *funcptr)(args...)"
3435 if (defined $pre_pointer_space &&
3436 $pre_pointer_space =~ /^\s/) {
3437 WARN("SPACING",
3438 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3439 }
3440
3441# unnecessary space "type (* funcptr)(args...)"
3442 if (defined $post_pointer_space &&
3443 $post_pointer_space =~ /^\s/) {
3444 WARN("SPACING",
3445 "Unnecessary space before function pointer name\n" . $herecurr);
3446 }
3447
3448# unnecessary space "type (*funcptr )(args...)"
3449 if (defined $post_funcname_space &&
3450 $post_funcname_space =~ /^\s/) {
3451 WARN("SPACING",
3452 "Unnecessary space after function pointer name\n" . $herecurr);
3453 }
3454
3455# unnecessary space "type (*funcptr) (args...)"
3456 if (defined $pre_args_space &&
3457 $pre_args_space =~ /^\s/) {
3458 WARN("SPACING",
3459 "Unnecessary space before function pointer arguments\n" . $herecurr);
3460 }
3461
3462 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003463 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003464 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003465 }
3466 }
3467
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003468# check for spacing round square brackets; allowed:
3469# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003470# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3471# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003472 while ($line =~ /(.*?\s)\[/g) {
3473 my ($where, $prefix) = ($-[1], $1);
3474 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003475 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003476 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003477 if (ERROR("BRACKET_SPACE",
3478 "space prohibited before open square bracket '['\n" . $herecurr) &&
3479 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003480 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003481 s/^(\+.*?)\s+\[/$1\[/;
3482 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003483 }
3484 }
3485
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003486# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003487 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003488 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003489 my $ctx_before = substr($line, 0, $-[1]);
3490 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003491
3492 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003493 if ($name =~ /^(?:
3494 if|for|while|switch|return|case|
3495 volatile|__volatile__|
3496 __attribute__|format|__extension__|
3497 asm|__asm__)$/x)
3498 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003499 # cpp #define statements have non-optional spaces, ie
3500 # if there is a space between the name and the open
3501 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003502 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003503
3504 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003505 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003506
3507 # If this whole things ends with a type its most
3508 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003509 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003510
3511 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003512 if (WARN("SPACING",
3513 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3514 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003515 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003516 s/\b$name\s+\(/$name\(/;
3517 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003518 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003519 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003520
Andy Whitcroft653d4872007-06-23 17:16:34 -07003521# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003522 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003523 my $fixed_line = "";
3524 my $line_fixed = 0;
3525
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003526 my $ops = qr{
3527 <<=|>>=|<=|>=|==|!=|
3528 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3529 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003530 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003531 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003532 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003533 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003534
3535## print("element count: <" . $#elements . ">\n");
3536## foreach my $el (@elements) {
3537## print("el: <$el>\n");
3538## }
3539
3540 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003541 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003542
Joe Perches3705ce52013-07-03 15:05:31 -07003543 foreach my $el (@elements) {
3544 push(@fix_elements, substr($rawline, $off, length($el)));
3545 $off += length($el);
3546 }
3547
3548 $off = 0;
3549
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003550 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003551 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003552
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003553 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003554
3555 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3556
3557## print("n: <$n> good: <$good>\n");
3558
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003559 $off += length($elements[$n]);
3560
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003561 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003562 my $ca = substr($opline, 0, $off);
3563 my $cc = '';
3564 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3565 $cc = substr($opline, $off + length($elements[$n + 1]));
3566 }
3567 my $cb = "$ca$;$cc";
3568
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003569 my $a = '';
3570 $a = 'V' if ($elements[$n] ne '');
3571 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003572 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003573 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3574 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003575 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003576
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003577 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003578
3579 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003580 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003581 $c = 'V' if ($elements[$n + 2] ne '');
3582 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003583 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003584 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3585 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003586 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003587 } else {
3588 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003589 }
3590
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003591 my $ctx = "${a}x${c}";
3592
3593 my $at = "(ctx:$ctx)";
3594
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003595 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003596 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003597
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003598 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003599 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003600
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003601 # Get the full operator variant.
3602 my $opv = $op . substr($curr_vars, $off, 1);
3603
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003604 # Ignore operators passed as parameters.
3605 if ($op_type ne 'V' &&
3606 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3607
Andy Whitcroftcf655042008-03-04 14:28:20 -08003608# # Ignore comments
3609# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003610
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003611 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003612 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003613 if ($ctx !~ /.x[WEBC]/ &&
3614 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003615 if (ERROR("SPACING",
3616 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003617 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003618 $line_fixed = 1;
3619 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003620 }
3621
3622 # // is a comment
3623 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003624
Joe Perchesb00e4812014-04-03 14:49:33 -07003625 # : when part of a bitfield
3626 } elsif ($opv eq ':B') {
3627 # skip the bitfield test for now
3628
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003629 # No spaces for:
3630 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003631 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003632 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003633 if (ERROR("SPACING",
3634 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003635 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003636 if (defined $fix_elements[$n + 2]) {
3637 $fix_elements[$n + 2] =~ s/^\s+//;
3638 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003639 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003640 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003641 }
3642
Joe Perches23810972014-12-10 15:51:32 -08003643 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003644 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003645 my $rtrim_before = 0;
3646 my $space_after = 0;
3647 if ($ctx =~ /Wx./) {
3648 if (ERROR("SPACING",
3649 "space prohibited before that '$op' $at\n" . $hereptr)) {
3650 $line_fixed = 1;
3651 $rtrim_before = 1;
3652 }
3653 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003654 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003655 if (ERROR("SPACING",
3656 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003657 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003658 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003659 $space_after = 1;
3660 }
3661 }
3662 if ($rtrim_before || $space_after) {
3663 if ($rtrim_before) {
3664 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3665 } else {
3666 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3667 }
3668 if ($space_after) {
3669 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003670 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003671 }
3672
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003673 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003674 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003675 #warn "'*' is part of type\n";
3676
3677 # unary operators should have a space before and
3678 # none after. May be left adjacent to another
3679 # unary operator, or a cast
3680 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003681 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003682 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003683 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003684 if (ERROR("SPACING",
3685 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003686 if ($n != $last_after + 2) {
3687 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3688 $line_fixed = 1;
3689 }
Joe Perches3705ce52013-07-03 15:05:31 -07003690 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003691 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003692 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003693 # A unary '*' may be const
3694
3695 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003696 if (ERROR("SPACING",
3697 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003698 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003699 if (defined $fix_elements[$n + 2]) {
3700 $fix_elements[$n + 2] =~ s/^\s+//;
3701 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003702 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003703 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003704 }
3705
3706 # unary ++ and unary -- are allowed no space on one side.
3707 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003708 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003709 if (ERROR("SPACING",
3710 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003711 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003712 $line_fixed = 1;
3713 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003714 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003715 if ($ctx =~ /Wx[BE]/ ||
3716 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003717 if (ERROR("SPACING",
3718 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003719 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003720 $line_fixed = 1;
3721 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003722 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003723 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003724 if (ERROR("SPACING",
3725 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003726 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003727 if (defined $fix_elements[$n + 2]) {
3728 $fix_elements[$n + 2] =~ s/^\s+//;
3729 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003730 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003731 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003732 }
3733
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003734 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003735 } elsif ($op eq '<<' or $op eq '>>' or
3736 $op eq '&' or $op eq '^' or $op eq '|' or
3737 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003738 $op eq '*' or $op eq '/' or
3739 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003740 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003741 if ($check) {
3742 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3743 if (CHK("SPACING",
3744 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3745 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3746 $fix_elements[$n + 2] =~ s/^\s+//;
3747 $line_fixed = 1;
3748 }
3749 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3750 if (CHK("SPACING",
3751 "space preferred before that '$op' $at\n" . $hereptr)) {
3752 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3753 $line_fixed = 1;
3754 }
3755 }
3756 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003757 if (ERROR("SPACING",
3758 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003759 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3760 if (defined $fix_elements[$n + 2]) {
3761 $fix_elements[$n + 2] =~ s/^\s+//;
3762 }
Joe Perches3705ce52013-07-03 15:05:31 -07003763 $line_fixed = 1;
3764 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003765 }
3766
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003767 # A colon needs no spaces before when it is
3768 # terminating a case value or a label.
3769 } elsif ($opv eq ':C' || $opv eq ':L') {
3770 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003771 if (ERROR("SPACING",
3772 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003773 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003774 $line_fixed = 1;
3775 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003776 }
3777
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003778 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003779 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003780 my $ok = 0;
3781
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003782 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003783 if (($op eq '<' &&
3784 $cc =~ /^\S+\@\S+>/) ||
3785 ($op eq '>' &&
3786 $ca =~ /<\S+\@\S+$/))
3787 {
3788 $ok = 1;
3789 }
3790
Joe Perches84731622013-11-12 15:10:05 -08003791 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003792 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003793 my $msg_type = \&ERROR;
3794 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3795
3796 if (&{$msg_type}("SPACING",
3797 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003798 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3799 if (defined $fix_elements[$n + 2]) {
3800 $fix_elements[$n + 2] =~ s/^\s+//;
3801 }
Joe Perches3705ce52013-07-03 15:05:31 -07003802 $line_fixed = 1;
3803 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003804 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003805 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003806 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003807
3808## print("n: <$n> GOOD: <$good>\n");
3809
3810 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003811 }
Joe Perches3705ce52013-07-03 15:05:31 -07003812
3813 if (($#elements % 2) == 0) {
3814 $fixed_line = $fixed_line . $fix_elements[$#elements];
3815 }
3816
Joe Perches194f66f2014-08-06 16:11:03 -07003817 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3818 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003819 }
3820
3821
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003822 }
3823
Joe Perches786b6322013-07-03 15:05:32 -07003824# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003825 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003826 if (WARN("SPACING",
3827 "space prohibited before semicolon\n" . $herecurr) &&
3828 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003829 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003830 s/^(\+.*\S)\s+;/$1;/;
3831 }
3832 }
3833
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003834# check for multiple assignments
3835 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003836 CHK("MULTIPLE_ASSIGNMENTS",
3837 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003838 }
3839
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003840## # check for multiple declarations, allowing for a function declaration
3841## # continuation.
3842## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3843## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3844##
3845## # Remove any bracketed sections to ensure we do not
3846## # falsly report the parameters of functions.
3847## my $ln = $line;
3848## while ($ln =~ s/\([^\(\)]*\)//g) {
3849## }
3850## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003851## WARN("MULTIPLE_DECLARATION",
3852## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003853## }
3854## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003855
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003856#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003857 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3858 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003859 if (ERROR("SPACING",
3860 "space required before the open brace '{'\n" . $herecurr) &&
3861 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003862 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003863 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003864 }
3865
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003866## # check for blank lines before declarations
3867## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3868## $prevrawline =~ /^.\s*$/) {
3869## WARN("SPACING",
3870## "No blank lines before declarations\n" . $hereprev);
3871## }
3872##
3873
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003874# closing brace should have a space following it when it has anything
3875# on the line
3876 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003877 if (ERROR("SPACING",
3878 "space required after that close brace '}'\n" . $herecurr) &&
3879 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003880 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003881 s/}((?!(?:,|;|\)))\S)/} $1/;
3882 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003883 }
3884
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003885# check spacing on square brackets
3886 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003887 if (ERROR("SPACING",
3888 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3889 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003890 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003891 s/\[\s+/\[/;
3892 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003893 }
3894 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003895 if (ERROR("SPACING",
3896 "space prohibited before that close 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
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003903# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003904 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3905 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003906 if (ERROR("SPACING",
3907 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3908 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003909 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003910 s/\(\s+/\(/;
3911 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003912 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003913 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003914 $line !~ /for\s*\(.*;\s+\)/ &&
3915 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003916 if (ERROR("SPACING",
3917 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3918 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003919 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003920 s/\s+\)/\)/;
3921 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003922 }
3923
Joe Perchese2826fd2014-08-06 16:10:48 -07003924# check unnecessary parentheses around addressof/dereference single $Lvals
3925# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3926
3927 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003928 my $var = $1;
3929 if (CHK("UNNECESSARY_PARENTHESES",
3930 "Unnecessary parentheses around $var\n" . $herecurr) &&
3931 $fix) {
3932 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3933 }
3934 }
3935
3936# check for unnecessary parentheses around function pointer uses
3937# ie: (foo->bar)(); should be foo->bar();
3938# but not "if (foo->bar) (" to avoid some false positives
3939 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3940 my $var = $2;
3941 if (CHK("UNNECESSARY_PARENTHESES",
3942 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3943 $fix) {
3944 my $var2 = deparenthesize($var);
3945 $var2 =~ s/\s//g;
3946 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3947 }
3948 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003949
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003950#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003951 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003952 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003953 if (WARN("INDENTED_LABEL",
3954 "labels should not be indented\n" . $herecurr) &&
3955 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003956 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003957 s/^(.)\s+/$1/;
3958 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003959 }
3960
Joe Perches5b9553a2014-04-03 14:49:21 -07003961# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003962 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003963 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003964 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003965 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3966 my $value = $1;
3967 $value = deparenthesize($value);
3968 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3969 ERROR("RETURN_PARENTHESES",
3970 "return is not a function, parentheses are not required\n" . $herecurr);
3971 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003972 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003973 ERROR("SPACING",
3974 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003975 }
3976 }
Joe Perches507e5142013-11-12 15:10:13 -08003977
Joe Perchesb43ae212014-06-23 13:22:07 -07003978# unnecessary return in a void function
3979# at end-of-function, with the previous line a single leading tab, then return;
3980# and the line before that not a goto label target like "out:"
3981 if ($sline =~ /^[ \+]}\s*$/ &&
3982 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3983 $linenr >= 3 &&
3984 $lines[$linenr - 3] =~ /^[ +]/ &&
3985 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003986 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003987 "void function return statements are not generally useful\n" . $hereprev);
3988 }
Joe Perches9819cf22014-06-04 16:12:09 -07003989
Joe Perches189248d2014-01-23 15:54:47 -08003990# if statements using unnecessary parentheses - ie: if ((foo == bar))
3991 if ($^V && $^V ge 5.10.0 &&
3992 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3993 my $openparens = $1;
3994 my $count = $openparens =~ tr@\(@\(@;
3995 my $msg = "";
3996 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3997 my $comp = $4; #Not $1 because of $LvalOrFunc
3998 $msg = " - maybe == should be = ?" if ($comp eq "==");
3999 WARN("UNNECESSARY_PARENTHESES",
4000 "Unnecessary parentheses$msg\n" . $herecurr);
4001 }
4002 }
4003
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004004# Return of what appears to be an errno should normally be -'ve
4005 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
4006 my $name = $1;
4007 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004008 WARN("USE_NEGATIVE_ERRNO",
4009 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004010 }
4011 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004012
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004013# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004014 if ($line =~ /\b(if|while|for|switch)\(/) {
4015 if (ERROR("SPACING",
4016 "space required before the open parenthesis '('\n" . $herecurr) &&
4017 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004018 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004019 s/\b(if|while|for|switch)\(/$1 \(/;
4020 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004021 }
4022
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004023# Check for illegal assignment in if conditional -- and check for trailing
4024# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004025 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004026 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4027 ctx_statement_block($linenr, $realcnt, 0)
4028 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004029 my ($stat_next) = ctx_statement_block($line_nr_next,
4030 $remain_next, $off_next);
4031 $stat_next =~ s/\n./\n /g;
4032 ##print "stat<$stat> stat_next<$stat_next>\n";
4033
4034 if ($stat_next =~ /^\s*while\b/) {
4035 # If the statement carries leading newlines,
4036 # then count those as offsets.
4037 my ($whitespace) =
4038 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4039 my $offset =
4040 statement_rawlines($whitespace) - 1;
4041
4042 $suppress_whiletrailers{$line_nr_next +
4043 $offset} = 1;
4044 }
4045 }
4046 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004047 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004048 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004049 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004050
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004051 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004052 ERROR("ASSIGN_IN_IF",
4053 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004054 }
4055
4056 # Find out what is on the end of the line after the
4057 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004058 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004059 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004060 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004061 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4062 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004063 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004064 # Find out how long the conditional actually is.
4065 my @newlines = ($c =~ /\n/gs);
4066 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004067 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004068
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004069 $stat_real = raw_line($linenr, $cond_lines)
4070 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004071 if (defined($stat_real) && $cond_lines > 1) {
4072 $stat_real = "[...]\n$stat_real";
4073 }
4074
Joe Perches000d1cc12011-07-25 17:13:25 -07004075 ERROR("TRAILING_STATEMENTS",
4076 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004077 }
4078 }
4079
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004080# Check for bitwise tests written as boolean
4081 if ($line =~ /
4082 (?:
4083 (?:\[|\(|\&\&|\|\|)
4084 \s*0[xX][0-9]+\s*
4085 (?:\&\&|\|\|)
4086 |
4087 (?:\&\&|\|\|)
4088 \s*0[xX][0-9]+\s*
4089 (?:\&\&|\|\||\)|\])
4090 )/x)
4091 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004092 WARN("HEXADECIMAL_BOOLEAN_TEST",
4093 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004094 }
4095
Andy Whitcroft8905a672007-11-28 16:21:06 -08004096# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004097 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4098 my $s = $1;
4099 $s =~ s/$;//g; # Remove any comments
4100 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004101 ERROR("TRAILING_STATEMENTS",
4102 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004103 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004104 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004105# if should not continue a brace
4106 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004107 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004108 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004109 $herecurr);
4110 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004111# case and default should not have general statements after them
4112 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4113 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004114 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004115 \s*return\s+
4116 )/xg)
4117 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004118 ERROR("TRAILING_STATEMENTS",
4119 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004120 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004121
4122 # Check for }<nl>else {, these must be at the same
4123 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004124 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4125 $previndent == $indent) {
4126 if (ERROR("ELSE_AFTER_BRACE",
4127 "else should follow close brace '}'\n" . $hereprev) &&
4128 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4129 fix_delete_line($fixlinenr - 1, $prevrawline);
4130 fix_delete_line($fixlinenr, $rawline);
4131 my $fixedline = $prevrawline;
4132 $fixedline =~ s/}\s*$//;
4133 if ($fixedline !~ /^\+\s*$/) {
4134 fix_insert_line($fixlinenr, $fixedline);
4135 }
4136 $fixedline = $rawline;
4137 $fixedline =~ s/^(.\s*)else/$1} else/;
4138 fix_insert_line($fixlinenr, $fixedline);
4139 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004140 }
4141
Joe Perches8b8856f2014-08-06 16:11:14 -07004142 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4143 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004144 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4145
4146 # Find out what is on the end of the line after the
4147 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004148 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004149 $s =~ s/\n.*//g;
4150
4151 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004152 if (ERROR("WHILE_AFTER_BRACE",
4153 "while should follow close brace '}'\n" . $hereprev) &&
4154 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4155 fix_delete_line($fixlinenr - 1, $prevrawline);
4156 fix_delete_line($fixlinenr, $rawline);
4157 my $fixedline = $prevrawline;
4158 my $trailing = $rawline;
4159 $trailing =~ s/^\+//;
4160 $trailing = trim($trailing);
4161 $fixedline =~ s/}\s*$/} $trailing/;
4162 fix_insert_line($fixlinenr, $fixedline);
4163 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004164 }
4165 }
4166
Joe Perches95e2c602013-07-03 15:05:20 -07004167#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004168 while ($line =~ m{($Constant|$Lval)}g) {
4169 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004170
4171#gcc binary extension
4172 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004173 if (WARN("GCC_BINARY_CONSTANT",
4174 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4175 $fix) {
4176 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004177 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004178 s/\b$var\b/$hexval/;
4179 }
Joe Perches95e2c602013-07-03 15:05:20 -07004180 }
4181
4182#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004183 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004184 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004185#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004186 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004187#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004188 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4189#Ignore some three character SI units explicitly, like MiB and KHz
4190 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004191 while ($var =~ m{($Ident)}g) {
4192 my $word = $1;
4193 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004194 if ($check) {
4195 seed_camelcase_includes();
4196 if (!$file && !$camelcase_file_seeded) {
4197 seed_camelcase_file($realfile);
4198 $camelcase_file_seeded = 1;
4199 }
4200 }
Joe Perches7e781f62013-09-11 14:23:55 -07004201 if (!defined $camelcase{$word}) {
4202 $camelcase{$word} = 1;
4203 CHK("CAMELCASE",
4204 "Avoid CamelCase: <$word>\n" . $herecurr);
4205 }
Joe Perches34456862013-07-03 15:05:34 -07004206 }
Joe Perches323c1262012-12-17 16:02:07 -08004207 }
4208 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004209
4210#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004211 if ($line =~ /\#\s*define.*\\\s+$/) {
4212 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4213 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4214 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004215 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004216 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004217 }
4218
Andy Whitcroft653d4872007-06-23 17:16:34 -07004219#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004220 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004221 my $file = "$1.h";
4222 my $checkfile = "include/linux/$file";
4223 if (-f "$root/$checkfile" &&
4224 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004225 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004226 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004227 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004228 CHK("ARCH_INCLUDE_LINUX",
4229 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004230 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004231 WARN("INCLUDE_LINUX",
4232 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004233 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004234 }
4235 }
4236
Andy Whitcroft653d4872007-06-23 17:16:34 -07004237# multi-statement macros should be enclosed in a do while loop, grab the
4238# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004239# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004240 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4241 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004242 my $ln = $linenr;
4243 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004244 my ($off, $dstat, $dcond, $rest);
4245 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004246 my $has_flow_statement = 0;
4247 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004248 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004249 ctx_statement_block($linenr, $realcnt, 0);
4250 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004251 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004252 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004253
Joe Perches08a28432014-10-13 15:51:55 -07004254 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4255 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4256
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004257 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004258 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004259 $dstat =~ s/\\\n.//g;
4260 $dstat =~ s/^\s*//s;
4261 $dstat =~ s/\s*$//s;
4262
4263 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004264 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4265 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004266 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004267 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004268 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004269
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004270 # Flatten any obvious string concatentation.
4271 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4272 $dstat =~ s/$Ident\s*("X*")/$1/)
4273 {
4274 }
4275
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004276 my $exceptions = qr{
4277 $Declare|
4278 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004279 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004280 DECLARE_PER_CPU|
4281 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004282 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004283 union|
4284 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004285 \.$Ident\s*=\s*|
4286 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004287 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004288 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004289 if ($dstat ne '' &&
4290 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4291 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004292 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004293 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004294 $dstat !~ /$exceptions/ &&
4295 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004296 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004297 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004298 $dstat !~ /^for\s*$Constant$/ && # for (...)
4299 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4300 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004301 $dstat !~ /^\({/ && # ({...
4302 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004303 {
4304 $ctx =~ s/\n*$//;
4305 my $herectx = $here . "\n";
4306 my $cnt = statement_rawlines($ctx);
4307
4308 for (my $n = 0; $n < $cnt; $n++) {
4309 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004310 }
4311
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004312 if ($dstat =~ /;/) {
4313 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4314 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4315 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004316 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004317 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004318 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004319 }
Joe Perches5023d342012-12-17 16:01:47 -08004320
Joe Perches08a28432014-10-13 15:51:55 -07004321# check for macros with flow control, but without ## concatenation
4322# ## concatenation is commonly a macro that defines a function so ignore those
4323 if ($has_flow_statement && !$has_arg_concat) {
4324 my $herectx = $here . "\n";
4325 my $cnt = statement_rawlines($ctx);
4326
4327 for (my $n = 0; $n < $cnt; $n++) {
4328 $herectx .= raw_line($linenr, $n) . "\n";
4329 }
4330 WARN("MACRO_WITH_FLOW_CONTROL",
4331 "Macros with flow control statements should be avoided\n" . "$herectx");
4332 }
4333
Joe Perches481eb482012-12-17 16:01:56 -08004334# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004335
4336 } else {
4337 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004338 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4339 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004340 $line =~ /^\+.*\\$/) {
4341 WARN("LINE_CONTINUATIONS",
4342 "Avoid unnecessary line continuations\n" . $herecurr);
4343 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004344 }
4345
Joe Perchesb13edf72012-07-30 14:41:24 -07004346# do {} while (0) macro tests:
4347# single-statement macros do not need to be enclosed in do while (0) loop,
4348# macro should not end with a semicolon
4349 if ($^V && $^V ge 5.10.0 &&
4350 $realfile !~ m@/vmlinux.lds.h$@ &&
4351 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4352 my $ln = $linenr;
4353 my $cnt = $realcnt;
4354 my ($off, $dstat, $dcond, $rest);
4355 my $ctx = '';
4356 ($dstat, $dcond, $ln, $cnt, $off) =
4357 ctx_statement_block($linenr, $realcnt, 0);
4358 $ctx = $dstat;
4359
4360 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004361 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004362
4363 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4364 my $stmts = $2;
4365 my $semis = $3;
4366
4367 $ctx =~ s/\n*$//;
4368 my $cnt = statement_rawlines($ctx);
4369 my $herectx = $here . "\n";
4370
4371 for (my $n = 0; $n < $cnt; $n++) {
4372 $herectx .= raw_line($linenr, $n) . "\n";
4373 }
4374
Joe Perchesac8e97f2012-08-21 16:15:53 -07004375 if (($stmts =~ tr/;/;/) == 1 &&
4376 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004377 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4378 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4379 }
4380 if (defined $semis && $semis ne "") {
4381 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4382 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4383 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004384 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4385 $ctx =~ s/\n*$//;
4386 my $cnt = statement_rawlines($ctx);
4387 my $herectx = $here . "\n";
4388
4389 for (my $n = 0; $n < $cnt; $n++) {
4390 $herectx .= raw_line($linenr, $n) . "\n";
4391 }
4392
4393 WARN("TRAILING_SEMICOLON",
4394 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004395 }
4396 }
4397
Mike Frysinger080ba922009-01-06 14:41:25 -08004398# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4399# all assignments may have only one of the following with an assignment:
4400# .
4401# ALIGN(...)
4402# VMLINUX_SYMBOL(...)
4403 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004404 WARN("MISSING_VMLINUX_SYMBOL",
4405 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004406 }
4407
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004408# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004409 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4410 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004411 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004412 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004413 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4414 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004415 my @allowed = ();
4416 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004417 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004418 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004419 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004420 for my $chunk (@chunks) {
4421 my ($cond, $block) = @{$chunk};
4422
Andy Whitcroft773647a2008-03-28 14:15:58 -07004423 # If the condition carries leading newlines, then count those as offsets.
4424 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4425 my $offset = statement_rawlines($whitespace) - 1;
4426
Joe Perchesaad4f612012-03-23 15:02:19 -07004427 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004428 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4429
4430 # We have looked at and allowed this specific line.
4431 $suppress_ifbraces{$ln + $offset} = 1;
4432
4433 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004434 $ln += statement_rawlines($block) - 1;
4435
Andy Whitcroft773647a2008-03-28 14:15:58 -07004436 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004437
4438 $seen++ if ($block =~ /^\s*{/);
4439
Joe Perchesaad4f612012-03-23 15:02:19 -07004440 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004441 if (statement_lines($cond) > 1) {
4442 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004443 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004444 }
4445 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004446 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004447 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004448 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004449 if (statement_block_size($block) > 1) {
4450 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004451 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004452 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004453 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004454 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004455 if ($seen) {
4456 my $sum_allowed = 0;
4457 foreach (@allowed) {
4458 $sum_allowed += $_;
4459 }
4460 if ($sum_allowed == 0) {
4461 WARN("BRACES",
4462 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4463 } elsif ($sum_allowed != $allow &&
4464 $seen != $allow) {
4465 CHK("BRACES",
4466 "braces {} should be used on all arms of this statement\n" . $herectx);
4467 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004468 }
4469 }
4470 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004471 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004472 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004473 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004474
Andy Whitcroftcf655042008-03-04 14:28:20 -08004475 # Check the pre-context.
4476 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4477 #print "APW: ALLOWED: pre<$1>\n";
4478 $allowed = 1;
4479 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004480
4481 my ($level, $endln, @chunks) =
4482 ctx_statement_full($linenr, $realcnt, $-[0]);
4483
Andy Whitcroftcf655042008-03-04 14:28:20 -08004484 # Check the condition.
4485 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004486 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004487 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004488 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004489 }
4490 if (statement_lines($cond) > 1) {
4491 #print "APW: ALLOWED: cond<$cond>\n";
4492 $allowed = 1;
4493 }
4494 if ($block =~/\b(?:if|for|while)\b/) {
4495 #print "APW: ALLOWED: block<$block>\n";
4496 $allowed = 1;
4497 }
4498 if (statement_block_size($block) > 1) {
4499 #print "APW: ALLOWED: lines block<$block>\n";
4500 $allowed = 1;
4501 }
4502 # Check the post-context.
4503 if (defined $chunks[1]) {
4504 my ($cond, $block) = @{$chunks[1]};
4505 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004506 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004507 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004508 if ($block =~ /^\s*\{/) {
4509 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4510 $allowed = 1;
4511 }
4512 }
4513 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004514 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004515 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004516
Andy Whitcroftf0556632008-10-15 22:02:23 -07004517 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004518 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004519 }
4520
Joe Perches000d1cc12011-07-25 17:13:25 -07004521 WARN("BRACES",
4522 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004523 }
4524 }
4525
Joe Perches0979ae62012-12-17 16:01:59 -08004526# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004527 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004528 if (CHK("BRACES",
4529 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4530 $fix && $prevrawline =~ /^\+/) {
4531 fix_delete_line($fixlinenr - 1, $prevrawline);
4532 }
Joe Perches0979ae62012-12-17 16:01:59 -08004533 }
Joe Perches77b9a532013-07-03 15:05:29 -07004534 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004535 if (CHK("BRACES",
4536 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4537 $fix) {
4538 fix_delete_line($fixlinenr, $rawline);
4539 }
Joe Perches0979ae62012-12-17 16:01:59 -08004540 }
4541
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004542# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004543 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4544 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004545 WARN("VOLATILE",
4546 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004547 }
4548
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004549# Check for user-visible strings broken across lines, which breaks the ability
4550# to grep for the string. Make exceptions when the previous string ends in a
4551# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4552# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4553 if ($line =~ /^\+\s*"[X\t]*"/ &&
4554 $prevline =~ /"\s*$/ &&
4555 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4556 if (WARN("SPLIT_STRING",
4557 "quoted string split across lines\n" . $hereprev) &&
4558 $fix &&
4559 $prevrawline =~ /^\+.*"\s*$/ &&
4560 $last_coalesced_string_linenr != $linenr - 1) {
4561 my $extracted_string = get_quoted_string($line, $rawline);
4562 my $comma_close = "";
4563 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4564 $comma_close = $1;
4565 }
4566
4567 fix_delete_line($fixlinenr - 1, $prevrawline);
4568 fix_delete_line($fixlinenr, $rawline);
4569 my $fixedline = $prevrawline;
4570 $fixedline =~ s/"\s*$//;
4571 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4572 fix_insert_line($fixlinenr - 1, $fixedline);
4573 $fixedline = $rawline;
4574 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4575 if ($fixedline !~ /\+\s*$/) {
4576 fix_insert_line($fixlinenr, $fixedline);
4577 }
4578 $last_coalesced_string_linenr = $linenr;
4579 }
4580 }
4581
4582# check for missing a space in a string concatenation
4583 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4584 WARN('MISSING_SPACE',
4585 "break quoted strings at a space character\n" . $hereprev);
4586 }
4587
4588# check for spaces before a quoted newline
4589 if ($rawline =~ /^.*\".*\s\\n/) {
4590 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4591 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4592 $fix) {
4593 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4594 }
4595
4596 }
4597
Joe Perchesf17dba42014-10-13 15:51:51 -07004598# concatenated string without spaces between elements
4599 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4600 CHK("CONCATENATED_STRING",
4601 "Concatenated strings should use spaces between elements\n" . $herecurr);
4602 }
4603
Joe Perches90ad30e2014-12-10 15:51:59 -08004604# uncoalesced string fragments
4605 if ($line =~ /"X*"\s*"/) {
4606 WARN("STRING_FRAGMENTS",
4607 "Consecutive strings are generally better as a single string\n" . $herecurr);
4608 }
4609
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004610# check for %L{u,d,i} in strings
4611 my $string;
4612 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4613 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4614 $string =~ s/%%/__/g;
4615 if ($string =~ /(?<!%)%L[udi]/) {
4616 WARN("PRINTF_L",
4617 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4618 last;
4619 }
4620 }
4621
4622# check for line continuations in quoted strings with odd counts of "
4623 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4624 WARN("LINE_CONTINUATIONS",
4625 "Avoid line continuations in quoted strings\n" . $herecurr);
4626 }
4627
Andy Whitcroft00df3442007-06-08 13:47:06 -07004628# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004629 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004630 CHK("REDUNDANT_CODE",
4631 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004632 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004633 }
4634
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004635# check for needless "if (<foo>) fn(<foo>)" uses
4636 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4637 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4638 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4639 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004640 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004641 }
4642 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004643
Joe Perchesebfdc402014-08-06 16:10:27 -07004644# check for unnecessary "Out of Memory" messages
4645 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4646 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4647 (defined $1 || defined $3) &&
4648 $linenr > 3) {
4649 my $testval = $2;
4650 my $testline = $lines[$linenr - 3];
4651
4652 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4653# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4654
4655 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4656 WARN("OOM_MESSAGE",
4657 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4658 }
4659 }
4660
Joe Perchesf78d98f2014-10-13 15:52:01 -07004661# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004662 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004663 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4664 my $level = $1;
4665 if (WARN("UNNECESSARY_KERN_LEVEL",
4666 "Possible unnecessary $level\n" . $herecurr) &&
4667 $fix) {
4668 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4669 }
4670 }
4671
Joe Perchesabb08a52014-12-10 15:51:46 -08004672# check for mask then right shift without a parentheses
4673 if ($^V && $^V ge 5.10.0 &&
4674 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4675 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4676 WARN("MASK_THEN_SHIFT",
4677 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4678 }
4679
Joe Perchesb75ac612014-12-10 15:52:02 -08004680# check for pointer comparisons to NULL
4681 if ($^V && $^V ge 5.10.0) {
4682 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4683 my $val = $1;
4684 my $equal = "!";
4685 $equal = "" if ($4 eq "!=");
4686 if (CHK("COMPARISON_TO_NULL",
4687 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4688 $fix) {
4689 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4690 }
4691 }
4692 }
4693
Joe Perches8716de32013-09-11 14:24:05 -07004694# check for bad placement of section $InitAttribute (e.g.: __initdata)
4695 if ($line =~ /(\b$InitAttribute\b)/) {
4696 my $attr = $1;
4697 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4698 my $ptr = $1;
4699 my $var = $2;
4700 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4701 ERROR("MISPLACED_INIT",
4702 "$attr should be placed after $var\n" . $herecurr)) ||
4703 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4704 WARN("MISPLACED_INIT",
4705 "$attr should be placed after $var\n" . $herecurr))) &&
4706 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004707 $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 -07004708 }
4709 }
4710 }
4711
Joe Perchese970b882013-11-12 15:10:10 -08004712# check for $InitAttributeData (ie: __initdata) with const
4713 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4714 my $attr = $1;
4715 $attr =~ /($InitAttributePrefix)(.*)/;
4716 my $attr_prefix = $1;
4717 my $attr_type = $2;
4718 if (ERROR("INIT_ATTRIBUTE",
4719 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4720 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004721 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004722 s/$InitAttributeData/${attr_prefix}initconst/;
4723 }
4724 }
4725
4726# check for $InitAttributeConst (ie: __initconst) without const
4727 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4728 my $attr = $1;
4729 if (ERROR("INIT_ATTRIBUTE",
4730 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4731 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004732 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004733 /(^\+\s*(?:static\s+))/;
4734 $lead = rtrim($1);
4735 $lead = "$lead " if ($lead !~ /^\+$/);
4736 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004737 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08004738 }
4739 }
4740
Joe Perchesfbdb8132014-04-03 14:49:14 -07004741# don't use __constant_<foo> functions outside of include/uapi/
4742 if ($realfile !~ m@^include/uapi/@ &&
4743 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4744 my $constant_func = $1;
4745 my $func = $constant_func;
4746 $func =~ s/^__constant_//;
4747 if (WARN("CONSTANT_CONVERSION",
4748 "$constant_func should be $func\n" . $herecurr) &&
4749 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004750 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004751 }
4752 }
4753
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004754# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004755 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004756 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004757 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004758 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004759 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004760 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4761 }
4762 if ($delay > 2000) {
4763 WARN("LONG_UDELAY",
4764 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004765 }
4766 }
4767
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004768# warn about unexpectedly long msleep's
4769 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4770 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004771 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004772 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004773 }
4774 }
4775
Joe Perches36ec1932013-07-03 15:05:25 -07004776# check for comparisons of jiffies
4777 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4778 WARN("JIFFIES_COMPARISON",
4779 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4780 }
4781
Joe Perches9d7a34a2013-07-03 15:05:26 -07004782# check for comparisons of get_jiffies_64()
4783 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4784 WARN("JIFFIES_COMPARISON",
4785 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4786 }
4787
Andy Whitcroft00df3442007-06-08 13:47:06 -07004788# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004789# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004790# print "#ifdef in C files should be avoided\n";
4791# print "$herecurr";
4792# $clean = 0;
4793# }
4794
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004795# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004796 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004797 if (ERROR("SPACING",
4798 "exactly one space required after that #$1\n" . $herecurr) &&
4799 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004800 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004801 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4802 }
4803
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004804 }
4805
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004806# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004807 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4808 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004809 my $which = $1;
4810 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004811 CHK("UNCOMMENTED_DEFINITION",
4812 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004813 }
4814 }
4815# check for memory barriers without a comment.
4816 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4817 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004818 WARN("MEMORY_BARRIER",
4819 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004820 }
4821 }
4822# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004823 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004824 CHK("ARCH_DEFINES",
4825 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004826 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004827
Tobias Klauserd4977c72010-05-24 14:33:30 -07004828# Check that the storage class is at the beginning of a declaration
4829 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004830 WARN("STORAGE_CLASS",
4831 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004832 }
4833
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004834# check the location of the inline attribute, that it is between
4835# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004836 if ($line =~ /\b$Type\s+$Inline\b/ ||
4837 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004838 ERROR("INLINE_LOCATION",
4839 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004840 }
4841
Andy Whitcroft8905a672007-11-28 16:21:06 -08004842# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004843 if ($realfile !~ m@\binclude/uapi/@ &&
4844 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004845 if (WARN("INLINE",
4846 "plain inline is preferred over $1\n" . $herecurr) &&
4847 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004848 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004849
4850 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004851 }
4852
Joe Perches3d130fd2011-01-12 17:00:00 -08004853# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004854 if ($realfile !~ m@\binclude/uapi/@ &&
4855 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004856 WARN("PREFER_PACKED",
4857 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004858 }
4859
Joe Perches39b7e282011-07-25 17:13:24 -07004860# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004861 if ($realfile !~ m@\binclude/uapi/@ &&
4862 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004863 WARN("PREFER_ALIGNED",
4864 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004865 }
4866
Joe Perches5f14d3b2012-01-10 15:09:52 -08004867# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004868 if ($realfile !~ m@\binclude/uapi/@ &&
4869 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004870 if (WARN("PREFER_PRINTF",
4871 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4872 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004873 $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 -07004874
4875 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004876 }
4877
Joe Perches6061d942012-03-23 15:02:16 -07004878# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004879 if ($realfile !~ m@\binclude/uapi/@ &&
4880 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004881 if (WARN("PREFER_SCANF",
4882 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4883 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004884 $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 -07004885 }
Joe Perches6061d942012-03-23 15:02:16 -07004886 }
4887
Joe Perches619a9082014-12-10 15:51:35 -08004888# Check for __attribute__ weak, or __weak declarations (may have link issues)
4889 if ($^V && $^V ge 5.10.0 &&
4890 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4891 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4892 $line =~ /\b__weak\b/)) {
4893 ERROR("WEAK_DECLARATION",
4894 "Using weak declarations can have unintended link defects\n" . $herecurr);
4895 }
4896
Joe Perches8f53a9b2010-03-05 13:43:48 -08004897# check for sizeof(&)
4898 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004899 WARN("SIZEOF_ADDRESS",
4900 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004901 }
4902
Joe Perches66c80b62012-07-30 14:41:22 -07004903# check for sizeof without parenthesis
4904 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004905 if (WARN("SIZEOF_PARENTHESIS",
4906 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4907 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004908 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004909 }
Joe Perches66c80b62012-07-30 14:41:22 -07004910 }
4911
Joe Perches88982fe2012-12-17 16:02:00 -08004912# check for struct spinlock declarations
4913 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4914 WARN("USE_SPINLOCK_T",
4915 "struct spinlock should be spinlock_t\n" . $herecurr);
4916 }
4917
Joe Perchesa6962d72013-04-29 16:18:13 -07004918# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004919 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004920 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08004921 $fmt =~ s/%%//g;
4922 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004923 if (WARN("PREFER_SEQ_PUTS",
4924 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4925 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004926 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004927 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004928 }
4929 }
4930
Andy Whitcroft554e1652012-01-10 15:09:57 -08004931# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004932 if ($^V && $^V ge 5.10.0 &&
4933 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004934 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004935
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004936 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004937 my $ms_val = $7;
4938 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004939
Andy Whitcroft554e1652012-01-10 15:09:57 -08004940 if ($ms_size =~ /^(0x|)0$/i) {
4941 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004942 "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 -08004943 } elsif ($ms_size =~ /^(0x|)1$/i) {
4944 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004945 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4946 }
4947 }
4948
Joe Perches98a9bba2014-01-23 15:54:52 -08004949# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4950 if ($^V && $^V ge 5.10.0 &&
4951 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4952 if (WARN("PREFER_ETHER_ADDR_COPY",
4953 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4954 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004955 $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 -08004956 }
4957 }
4958
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004959# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004960 if ($^V && $^V ge 5.10.0 &&
4961 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004962 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004963 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004964 my $call = $1;
4965 my $cast1 = deparenthesize($2);
4966 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004967 my $cast2 = deparenthesize($7);
4968 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004969 my $cast;
4970
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004971 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004972 $cast = "$cast1 or $cast2";
4973 } elsif ($cast1 ne "") {
4974 $cast = $cast1;
4975 } else {
4976 $cast = $cast2;
4977 }
4978 WARN("MINMAX",
4979 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004980 }
4981 }
4982
Joe Perches4a273192012-07-30 14:41:20 -07004983# check usleep_range arguments
4984 if ($^V && $^V ge 5.10.0 &&
4985 defined $stat &&
4986 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4987 my $min = $1;
4988 my $max = $7;
4989 if ($min eq $max) {
4990 WARN("USLEEP_RANGE",
4991 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4992 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4993 $min > $max) {
4994 WARN("USLEEP_RANGE",
4995 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4996 }
4997 }
4998
Joe Perches823b7942013-11-12 15:10:15 -08004999# check for naked sscanf
5000 if ($^V && $^V ge 5.10.0 &&
5001 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005002 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005003 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5004 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5005 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5006 my $lc = $stat =~ tr@\n@@;
5007 $lc = $lc + $linenr;
5008 my $stat_real = raw_line($linenr, 0);
5009 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5010 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5011 }
5012 WARN("NAKED_SSCANF",
5013 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5014 }
5015
Joe Perchesafc819a2014-06-04 16:12:08 -07005016# check for simple sscanf that should be kstrto<foo>
5017 if ($^V && $^V ge 5.10.0 &&
5018 defined $stat &&
5019 $line =~ /\bsscanf\b/) {
5020 my $lc = $stat =~ tr@\n@@;
5021 $lc = $lc + $linenr;
5022 my $stat_real = raw_line($linenr, 0);
5023 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5024 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5025 }
5026 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5027 my $format = $6;
5028 my $count = $format =~ tr@%@%@;
5029 if ($count == 1 &&
5030 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5031 WARN("SSCANF_TO_KSTRTO",
5032 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5033 }
5034 }
5035 }
5036
Joe Perches70dc8a42013-09-11 14:23:58 -07005037# check for new externs in .h files.
5038 if ($realfile =~ /\.h$/ &&
5039 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005040 if (CHK("AVOID_EXTERNS",
5041 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005042 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005043 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005044 }
5045 }
5046
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005047# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005048 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005049 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005050 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005051 my $function_name = $1;
5052 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005053
5054 my $s = $stat;
5055 if (defined $cond) {
5056 substr($s, 0, length($cond), '');
5057 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005058 if ($s =~ /^\s*;/ &&
5059 $function_name ne 'uninitialized_var')
5060 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005061 WARN("AVOID_EXTERNS",
5062 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005063 }
5064
5065 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005066 WARN("FUNCTION_ARGUMENTS",
5067 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005068 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005069
5070 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5071 $stat =~ /^.\s*extern\s+/)
5072 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005073 WARN("AVOID_EXTERNS",
5074 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005075 }
5076
5077# checks for new __setup's
5078 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5079 my $name = $1;
5080
5081 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005082 CHK("UNDOCUMENTED_SETUP",
5083 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005084 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005085 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005086
5087# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005088 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005089 WARN("UNNECESSARY_CASTS",
5090 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005091 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005092
Joe Perchesa640d252013-07-03 15:05:21 -07005093# alloc style
5094# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5095 if ($^V && $^V ge 5.10.0 &&
5096 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5097 CHK("ALLOC_SIZEOF_STRUCT",
5098 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5099 }
5100
Joe Perches60a55362014-06-04 16:12:07 -07005101# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5102 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005103 $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 -07005104 my $oldfunc = $3;
5105 my $a1 = $4;
5106 my $a2 = $10;
5107 my $newfunc = "kmalloc_array";
5108 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005109 my $r1 = $a1;
5110 my $r2 = $a2;
5111 if ($a1 =~ /^sizeof\s*\S/) {
5112 $r1 = $a2;
5113 $r2 = $a1;
5114 }
5115 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5116 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005117 if (WARN("ALLOC_WITH_MULTIPLY",
5118 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5119 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005120 $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 -07005121
5122 }
5123 }
5124 }
5125
Joe Perches972fdea2013-04-29 16:18:12 -07005126# check for krealloc arg reuse
5127 if ($^V && $^V ge 5.10.0 &&
5128 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5129 WARN("KREALLOC_ARG_REUSE",
5130 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5131 }
5132
Joe Perches5ce59ae2013-02-21 16:44:18 -08005133# check for alloc argument mismatch
5134 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5135 WARN("ALLOC_ARRAY_ARGS",
5136 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5137 }
5138
Joe Perchescaf2a542011-01-12 16:59:56 -08005139# check for multiple semicolons
5140 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005141 if (WARN("ONE_SEMICOLON",
5142 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5143 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005144 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005145 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005146 }
5147
Joe Perches0ab90192014-12-10 15:51:57 -08005148# check for #defines like: 1 << <digit> that could be BIT(digit)
5149 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5150 my $ull = "";
5151 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5152 if (CHK("BIT_MACRO",
5153 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5154 $fix) {
5155 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5156 }
5157 }
5158
Joe Perchese81f2392014-08-06 16:11:25 -07005159# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005160 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5161 my $has_break = 0;
5162 my $has_statement = 0;
5163 my $count = 0;
5164 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005165 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005166 $prevline--;
5167 my $rline = $rawlines[$prevline - 1];
5168 my $fline = $lines[$prevline - 1];
5169 last if ($fline =~ /^\@\@/);
5170 next if ($fline =~ /^\-/);
5171 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5172 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5173 next if ($fline =~ /^.[\s$;]*$/);
5174 $has_statement = 1;
5175 $count++;
5176 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5177 }
5178 if (!$has_break && $has_statement) {
5179 WARN("MISSING_BREAK",
5180 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5181 }
5182 }
5183
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005184# check for switch/default statements without a break;
5185 if ($^V && $^V ge 5.10.0 &&
5186 defined $stat &&
5187 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5188 my $ctx = '';
5189 my $herectx = $here . "\n";
5190 my $cnt = statement_rawlines($stat);
5191 for (my $n = 0; $n < $cnt; $n++) {
5192 $herectx .= raw_line($linenr, $n) . "\n";
5193 }
5194 WARN("DEFAULT_NO_BREAK",
5195 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005196 }
5197
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005198# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005199 if ($line =~ /\b__FUNCTION__\b/) {
5200 if (WARN("USE_FUNC",
5201 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5202 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005203 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005204 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005205 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005206
Joe Perches62ec8182015-02-13 14:38:18 -08005207# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5208 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5209 ERROR("DATE_TIME",
5210 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5211 }
5212
Joe Perches2c924882012-03-23 15:02:20 -07005213# check for use of yield()
5214 if ($line =~ /\byield\s*\(\s*\)/) {
5215 WARN("YIELD",
5216 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5217 }
5218
Joe Perches179f8f42013-07-03 15:05:30 -07005219# check for comparisons against true and false
5220 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5221 my $lead = $1;
5222 my $arg = $2;
5223 my $test = $3;
5224 my $otype = $4;
5225 my $trail = $5;
5226 my $op = "!";
5227
5228 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5229
5230 my $type = lc($otype);
5231 if ($type =~ /^(?:true|false)$/) {
5232 if (("$test" eq "==" && "$type" eq "true") ||
5233 ("$test" eq "!=" && "$type" eq "false")) {
5234 $op = "";
5235 }
5236
5237 CHK("BOOL_COMPARISON",
5238 "Using comparison to $otype is error prone\n" . $herecurr);
5239
5240## maybe suggesting a correct construct would better
5241## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5242
5243 }
5244 }
5245
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005246# check for semaphores initialized locked
5247 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005248 WARN("CONSIDER_COMPLETION",
5249 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005250 }
Joe Perches6712d852012-03-23 15:02:20 -07005251
Joe Perches67d0a072011-10-31 17:13:10 -07005252# recommend kstrto* over simple_strto* and strict_strto*
5253 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005254 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005255 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005256 }
Joe Perches6712d852012-03-23 15:02:20 -07005257
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005258# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005259 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005260 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005261 "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 -07005262 }
Joe Perches6712d852012-03-23 15:02:20 -07005263
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005264# check for various structs that are normally const (ops, kgdb, device_tree)
5265 my $const_structs = qr{
5266 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005267 address_space_operations|
5268 backlight_ops|
5269 block_device_operations|
5270 dentry_operations|
5271 dev_pm_ops|
5272 dma_map_ops|
5273 extent_io_ops|
5274 file_lock_operations|
5275 file_operations|
5276 hv_ops|
5277 ide_dma_ops|
5278 intel_dvo_dev_ops|
5279 item_operations|
5280 iwl_ops|
5281 kgdb_arch|
5282 kgdb_io|
5283 kset_uevent_ops|
5284 lock_manager_operations|
5285 microcode_ops|
5286 mtrr_ops|
5287 neigh_ops|
5288 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005289 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005290 pci_raw_ops|
5291 pipe_buf_operations|
5292 platform_hibernation_ops|
5293 platform_suspend_ops|
5294 proto_ops|
5295 rpc_pipe_ops|
5296 seq_operations|
5297 snd_ac97_build_ops|
5298 soc_pcmcia_socket_ops|
5299 stacktrace_ops|
5300 sysfs_ops|
5301 tty_operations|
5302 usb_mon_operations|
5303 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005304 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005305 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005306 WARN("CONST_STRUCT",
5307 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005308 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005309 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005310
5311# use of NR_CPUS is usually wrong
5312# ignore definitions of NR_CPUS and usage to define arrays as likely right
5313 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005314 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5315 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005316 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5317 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5318 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005319 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005320 WARN("NR_CPUS",
5321 "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 -07005322 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005323
Joe Perches52ea8502013-11-12 15:10:09 -08005324# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5325 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5326 ERROR("DEFINE_ARCH_HAS",
5327 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5328 }
5329
Joe Perchesacd93622015-02-13 14:38:38 -08005330# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5331 if ($^V && $^V ge 5.10.0 &&
5332 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5333 WARN("LIKELY_MISUSE",
5334 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5335 }
5336
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005337# whine mightly about in_atomic
5338 if ($line =~ /\bin_atomic\s*\(/) {
5339 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005340 ERROR("IN_ATOMIC",
5341 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005342 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005343 WARN("IN_ATOMIC",
5344 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005345 }
5346 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005347
5348# check for lockdep_set_novalidate_class
5349 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5350 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5351 if ($realfile !~ m@^kernel/lockdep@ &&
5352 $realfile !~ m@^include/linux/lockdep@ &&
5353 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005354 ERROR("LOCKDEP",
5355 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005356 }
5357 }
Dave Jones88f88312011-01-12 16:59:59 -08005358
5359 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5360 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005361 WARN("EXPORTED_WORLD_WRITABLE",
5362 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005363 }
Joe Perches24358802014-04-03 14:49:13 -07005364
Joe Perches515a2352014-04-03 14:49:24 -07005365# Mode permission misuses where it seems decimal should be octal
5366# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5367 if ($^V && $^V ge 5.10.0 &&
5368 $line =~ /$mode_perms_search/) {
5369 foreach my $entry (@mode_permission_funcs) {
5370 my $func = $entry->[0];
5371 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005372
Joe Perches515a2352014-04-03 14:49:24 -07005373 my $skip_args = "";
5374 if ($arg_pos > 1) {
5375 $arg_pos--;
5376 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5377 }
5378 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5379 if ($line =~ /$test/) {
5380 my $val = $1;
5381 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005382
Joe Perches515a2352014-04-03 14:49:24 -07005383 if ($val !~ /^0$/ &&
5384 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5385 length($val) ne 4)) {
5386 ERROR("NON_OCTAL_PERMISSIONS",
5387 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005388 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5389 ERROR("EXPORTED_WORLD_WRITABLE",
5390 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005391 }
Joe Perches24358802014-04-03 14:49:13 -07005392 }
5393 }
5394 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005395 }
5396
5397 # If we have no input at all, then there is nothing to report on
5398 # so just keep quiet.
5399 if ($#rawlines == -1) {
5400 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005401 }
5402
Andy Whitcroft8905a672007-11-28 16:21:06 -08005403 # In mailback mode only produce a report in the negative, for
5404 # things that appear to be patches.
5405 if ($mailback && ($clean == 1 || !$is_patch)) {
5406 exit(0);
5407 }
5408
5409 # This is not a patch, and we are are in 'no-patch' mode so
5410 # just keep quiet.
5411 if (!$chk_patch && !$is_patch) {
5412 exit(0);
5413 }
5414
5415 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005416 ERROR("NOT_UNIFIED_DIFF",
5417 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005418 }
5419 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005420 ERROR("MISSING_SIGN_OFF",
5421 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005422 }
5423
Andy Whitcroft8905a672007-11-28 16:21:06 -08005424 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005425 if ($summary && !($clean == 1 && $quiet == 1)) {
5426 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005427 print "total: $cnt_error errors, $cnt_warn warnings, " .
5428 (($check)? "$cnt_chk checks, " : "") .
5429 "$cnt_lines lines checked\n";
5430 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005431 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005432
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005433 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005434
5435 if ($^V lt 5.10.0) {
5436 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5437 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5438 }
5439
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005440 # If there were whitespace errors which cleanpatch can fix
5441 # then suggest that.
5442 if ($rpt_cleaners) {
5443 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5444 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005445 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005446 }
5447 }
5448
Joe Perches91bfe482013-09-11 14:23:59 -07005449 hash_show_words(\%use_type, "Used");
5450 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005451
Joe Perchesd752fcc2014-08-06 16:11:05 -07005452 if ($clean == 0 && $fix &&
5453 ("@rawlines" ne "@fixed" ||
5454 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005455 my $newfile = $filename;
5456 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005457 my $linecount = 0;
5458 my $f;
5459
Joe Perchesd752fcc2014-08-06 16:11:05 -07005460 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5461
Joe Perches3705ce52013-07-03 15:05:31 -07005462 open($f, '>', $newfile)
5463 or die "$P: Can't open $newfile for write\n";
5464 foreach my $fixed_line (@fixed) {
5465 $linecount++;
5466 if ($file) {
5467 if ($linecount > 3) {
5468 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005469 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005470 }
5471 } else {
5472 print $f $fixed_line . "\n";
5473 }
5474 }
5475 close($f);
5476
5477 if (!$quiet) {
5478 print << "EOM";
5479Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5480
5481Do _NOT_ trust the results written to this file.
5482Do _NOT_ submit these changes without inspecting them for correctness.
5483
5484This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5485No warranties, expressed or implied...
5486
5487EOM
5488 }
5489 }
5490
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005491 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005492 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005493 }
5494 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005495 print << "EOM";
5496$vname has style problems, please review.
5497
5498If any of these errors are false positives, please report
5499them to the maintainer, see CHECKPATCH in MAINTAINERS.
5500EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005501 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005502
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005503 return $clean;
5504}