blob: 78a951f80706dc911a991597bb6ec8ea205d2211 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
13my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080014my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070015
Joe Perches000d1cc12011-07-25 17:13:25 -070016my $V = '0.32';
Andy Whitcroft0a920b5b2007-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 Perches2ac73b42014-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 Whitcroft0a920b5b2007-06-01 00:46:48 -0700130GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -0700161
Joe Perches9624b8d2014-01-23 15:54:44 -0800162$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700163$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800164
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -0700174if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700175 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Perchese970b8842013-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
Joe Perchesab7e23f2015-04-16 12:44:22 -0700326our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800327our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700328our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700329our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700331our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800332our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700333our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800334
Joe Perches15662b32011-10-31 17:13:12 -0700335our $NON_ASCII_UTF8 = qr{
336 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700337 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
338 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
339 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
340 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
341 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
342 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
343}x;
344
Joe Perches15662b32011-10-31 17:13:12 -0700345our $UTF8 = qr{
346 [\x09\x0A\x0D\x20-\x7E] # ASCII
347 | $NON_ASCII_UTF8
348}x;
349
Joe Perches021158b2015-02-13 14:38:43 -0800350our $typeOtherOSTypedefs = qr{(?x:
351 u_(?:char|short|int|long) | # bsd
352 u(?:nchar|short|int|long) # sysv
353)};
354
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700355our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700356 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700357 atomic_t
358)};
359
Joe Perches691e6692010-03-05 13:43:51 -0800360our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700361 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700362 (?:[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 -0700363 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700364 panic|
Joe Perches06668722013-11-12 15:10:07 -0800365 MODULE_[A-Z_]+|
366 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800367)};
368
Joe Perches20112472011-07-25 17:13:23 -0700369our $signature_tags = qr{(?xi:
370 Signed-off-by:|
371 Acked-by:|
372 Tested-by:|
373 Reviewed-by:|
374 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700375 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700376 To:|
377 Cc:
378)};
379
Joe Perches18130872014-08-06 16:11:22 -0700380our @typeListMisordered = (
381 qr{char\s+(?:un)?signed},
382 qr{int\s+(?:(?:un)?signed\s+)?short\s},
383 qr{int\s+short(?:\s+(?:un)?signed)},
384 qr{short\s+int(?:\s+(?:un)?signed)},
385 qr{(?:un)?signed\s+int\s+short},
386 qr{short\s+(?:un)?signed},
387 qr{long\s+int\s+(?:un)?signed},
388 qr{int\s+long\s+(?:un)?signed},
389 qr{long\s+(?:un)?signed\s+int},
390 qr{int\s+(?:un)?signed\s+long},
391 qr{int\s+(?:un)?signed},
392 qr{int\s+long\s+long\s+(?:un)?signed},
393 qr{long\s+long\s+int\s+(?:un)?signed},
394 qr{long\s+long\s+(?:un)?signed\s+int},
395 qr{long\s+long\s+(?:un)?signed},
396 qr{long\s+(?:un)?signed},
397);
398
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399our @typeList = (
400 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700401 qr{(?:(?:un)?signed\s+)?char},
402 qr{(?:(?:un)?signed\s+)?short\s+int},
403 qr{(?:(?:un)?signed\s+)?short},
404 qr{(?:(?:un)?signed\s+)?int},
405 qr{(?:(?:un)?signed\s+)?long\s+int},
406 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
407 qr{(?:(?:un)?signed\s+)?long\s+long},
408 qr{(?:(?:un)?signed\s+)?long},
409 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800410 qr{float},
411 qr{double},
412 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800413 qr{struct\s+$Ident},
414 qr{union\s+$Ident},
415 qr{enum\s+$Ident},
416 qr{${Ident}_t},
417 qr{${Ident}_handler},
418 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700419 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800420);
Joe Perches8716de32013-09-11 14:24:05 -0700421our @typeListWithAttr = (
422 @typeList,
423 qr{struct\s+$InitAttribute\s+$Ident},
424 qr{union\s+$InitAttribute\s+$Ident},
425);
426
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700427our @modifierList = (
428 qr{fastcall},
429);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800430
Joe Perches24358802014-04-03 14:49:13 -0700431our @mode_permission_funcs = (
432 ["module_param", 3],
433 ["module_param_(?:array|named|string)", 4],
434 ["module_param_array_named", 5],
435 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
436 ["proc_create(?:_data|)", 2],
437 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
438);
439
Joe Perches515a2352014-04-03 14:49:24 -0700440#Create a search pattern for all these functions to speed up a loop below
441our $mode_perms_search = "";
442foreach my $entry (@mode_permission_funcs) {
443 $mode_perms_search .= '|' if ($mode_perms_search ne "");
444 $mode_perms_search .= $entry->[0];
445}
446
Joe Perchesb392c642015-04-16 12:44:16 -0700447our $mode_perms_world_writable = qr{
448 S_IWUGO |
449 S_IWOTH |
450 S_IRWXUGO |
451 S_IALLUGO |
452 0[0-7][0-7][2367]
453}x;
454
Wolfram Sang7840a942010-08-09 17:20:57 -0700455our $allowed_asm_includes = qr{(?x:
456 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700457 memory|
458 time|
459 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700460)};
461# memory.h: ARM has a custom one
462
Kees Cook66b47b42014-10-13 15:51:57 -0700463# Load common spelling mistakes and build regular expression list.
464my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700465my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700466
Joe Perches36061e32014-12-10 15:51:43 -0800467if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800468 while (<$spelling>) {
469 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700470
Joe Perches36061e32014-12-10 15:51:43 -0800471 $line =~ s/\s*\n?$//g;
472 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700473
Joe Perches36061e32014-12-10 15:51:43 -0800474 next if ($line =~ m/^\s*#/);
475 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700476
Joe Perches36061e32014-12-10 15:51:43 -0800477 my ($suspect, $fix) = split(/\|\|/, $line);
478
Joe Perches36061e32014-12-10 15:51:43 -0800479 $spelling_fix{$suspect} = $fix;
480 }
481 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800482} else {
483 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700484}
Kees Cook66b47b42014-10-13 15:51:57 -0700485
Joe Perchesebfd7d62015-04-16 12:44:14 -0700486if ($codespell) {
487 if (open(my $spelling, '<', $codespellfile)) {
488 while (<$spelling>) {
489 my $line = $_;
490
491 $line =~ s/\s*\n?$//g;
492 $line =~ s/^\s*//g;
493
494 next if ($line =~ m/^\s*#/);
495 next if ($line =~ m/^\s*$/);
496 next if ($line =~ m/, disabled/i);
497
498 $line =~ s/,.*$//;
499
500 my ($suspect, $fix) = split(/->/, $line);
501
502 $spelling_fix{$suspect} = $fix;
503 }
504 close($spelling);
505 } else {
506 warn "No codespell typos will be found - file '$codespellfile': $!\n";
507 }
508}
509
510$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
511
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700513 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
514 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700515 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700516 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700517 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700518 $BasicType = qr{
519 (?:$typeOtherOSTypedefs\b)|
520 (?:$typeTypedefs\b)|
521 (?:${all}\b)
522 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700524 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800525 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800526 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Perches021158b2015-02-13 14:38:43 -0800527 (?:$typeOtherOSTypedefs\b)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700528 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700529 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800530 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700531 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800532 }x;
Joe Perches18130872014-08-06 16:11:22 -0700533 $NonptrTypeMisordered = qr{
534 (?:$Modifier\s+|const\s+)*
535 (?:
536 (?:${Misordered}\b)
537 )
538 (?:\s+$Modifier|\s+const)*
539 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700540 $NonptrTypeWithAttr = qr{
541 (?:$Modifier\s+|const\s+)*
542 (?:
543 (?:typeof|__typeof__)\s*\([^\)]*\)|
544 (?:$typeTypedefs\b)|
Joe Perches021158b2015-02-13 14:38:43 -0800545 (?:$typeOtherOSTypedefs\b)|
Joe Perches8716de32013-09-11 14:24:05 -0700546 (?:${allWithAttr}\b)
547 )
548 (?:\s+$Modifier|\s+const)*
549 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800550 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700551 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700552 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700553 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800554 }x;
Joe Perches18130872014-08-06 16:11:22 -0700555 $TypeMisordered = qr{
556 $NonptrTypeMisordered
557 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
558 (?:\s+$Inline|\s+$Modifier)*
559 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700560 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700561 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800562}
563build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700564
Joe Perches7d2367a2011-07-25 17:13:22 -0700565our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700566
567# Using $balanced_parens, $LvalOrFunc, or $FuncArg
568# requires at least perl version v5.10.0
569# Any use must be runtime checked with $^V
570
571our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700572our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800573our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700574
Joe Perchesf8422302014-08-06 16:11:31 -0700575our $declaration_macros = qr{(?x:
576 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
577 (?:$Storage\s+)?LIST_HEAD\s*\(|
578 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
579)};
580
Joe Perches7d2367a2011-07-25 17:13:22 -0700581sub deparenthesize {
582 my ($string) = @_;
583 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700584
585 while ($string =~ /^\s*\(.*\)\s*$/) {
586 $string =~ s@^\s*\(\s*@@;
587 $string =~ s@\s*\)\s*$@@;
588 }
589
Joe Perches7d2367a2011-07-25 17:13:22 -0700590 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700591
Joe Perches7d2367a2011-07-25 17:13:22 -0700592 return $string;
593}
594
Joe Perches34456862013-07-03 15:05:34 -0700595sub seed_camelcase_file {
596 my ($file) = @_;
597
598 return if (!(-f $file));
599
600 local $/;
601
602 open(my $include_file, '<', "$file")
603 or warn "$P: Can't read '$file' $!\n";
604 my $text = <$include_file>;
605 close($include_file);
606
607 my @lines = split('\n', $text);
608
609 foreach my $line (@lines) {
610 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
611 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
612 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800613 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
614 $camelcase{$1} = 1;
615 } 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 -0700616 $camelcase{$1} = 1;
617 }
618 }
619}
620
621my $camelcase_seeded = 0;
622sub seed_camelcase_includes {
623 return if ($camelcase_seeded);
624
625 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700626 my $camelcase_cache = "";
627 my @include_files = ();
628
629 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700630
Richard Genoud3645e322014-02-10 14:25:32 -0800631 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700632 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
633 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700634 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700635 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700636 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700637 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700638 @include_files = split('\n', $files);
639 foreach my $file (@include_files) {
640 my $date = POSIX::strftime("%Y%m%d%H%M",
641 localtime((stat $file)[9]));
642 $last_mod_date = $date if ($last_mod_date < $date);
643 }
644 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700645 }
Joe Perchesc707a812013-07-08 16:00:43 -0700646
647 if ($camelcase_cache ne "" && -f $camelcase_cache) {
648 open(my $camelcase_file, '<', "$camelcase_cache")
649 or warn "$P: Can't read '$camelcase_cache' $!\n";
650 while (<$camelcase_file>) {
651 chomp;
652 $camelcase{$_} = 1;
653 }
654 close($camelcase_file);
655
656 return;
657 }
658
Richard Genoud3645e322014-02-10 14:25:32 -0800659 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700660 $files = `git ls-files "include/*.h"`;
661 @include_files = split('\n', $files);
662 }
663
Joe Perches34456862013-07-03 15:05:34 -0700664 foreach my $file (@include_files) {
665 seed_camelcase_file($file);
666 }
Joe Perches351b2a12013-07-03 15:05:36 -0700667
Joe Perchesc707a812013-07-08 16:00:43 -0700668 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700669 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700670 open(my $camelcase_file, '>', "$camelcase_cache")
671 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700672 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
673 print $camelcase_file ("$_\n");
674 }
675 close($camelcase_file);
676 }
Joe Perches34456862013-07-03 15:05:34 -0700677}
678
Joe Perchesd311cd42014-08-06 16:10:57 -0700679sub git_commit_info {
680 my ($commit, $id, $desc) = @_;
681
682 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
683
684 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
685 $output =~ s/^\s*//gm;
686 my @lines = split("\n", $output);
687
Joe Perches0d7835f2015-02-13 14:38:35 -0800688 return ($id, $desc) if ($#lines < 0);
689
Joe Perchesd311cd42014-08-06 16:10:57 -0700690 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
691# Maybe one day convert this block of bash into something that returns
692# all matching commit ids, but it's very slow...
693#
694# echo "checking commits $1..."
695# git rev-list --remotes | grep -i "^$1" |
696# while read line ; do
697# git log --format='%H %s' -1 $line |
698# echo "commit $(cut -c 1-12,41-)"
699# done
700 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
701 } else {
702 $id = substr($lines[0], 0, 12);
703 $desc = substr($lines[0], 41);
704 }
705
706 return ($id, $desc);
707}
708
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700709$chk_signoff = 0 if ($file);
710
Andy Whitcroft00df3442007-06-08 13:47:06 -0700711my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800712my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700713my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700714my @fixed_inserted = ();
715my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700716my $fixlinenr = -1;
717
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800718my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700719for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800720 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700721 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800722 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700723 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800724 } elsif ($filename eq '-') {
725 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700726 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800727 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700728 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700729 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800730 if ($filename eq '-') {
731 $vname = 'Your patch';
732 } else {
733 $vname = $filename;
734 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800735 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700736 chomp;
737 push(@rawlines, $_);
738 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800739 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800740 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700741 $exit = 1;
742 }
743 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800744 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700745 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700746 @fixed_inserted = ();
747 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700748 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700749}
750
751exit($exit);
752
753sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700754 my ($root) = @_;
755
756 my @tree_check = (
757 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
758 "README", "Documentation", "arch", "include", "drivers",
759 "fs", "init", "ipc", "kernel", "lib", "scripts",
760 );
761
762 foreach my $check (@tree_check) {
763 if (! -e $root . '/' . $check) {
764 return 0;
765 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700766 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700767 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700768}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700769
Joe Perches20112472011-07-25 17:13:23 -0700770sub parse_email {
771 my ($formatted_email) = @_;
772
773 my $name = "";
774 my $address = "";
775 my $comment = "";
776
777 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
778 $name = $1;
779 $address = $2;
780 $comment = $3 if defined $3;
781 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
782 $address = $1;
783 $comment = $2 if defined $2;
784 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
785 $address = $1;
786 $comment = $2 if defined $2;
787 $formatted_email =~ s/$address.*$//;
788 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700789 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700790 $name =~ s/^\"|\"$//g;
791 # If there's a name left after stripping spaces and
792 # leading quotes, and the address doesn't have both
793 # leading and trailing angle brackets, the address
794 # is invalid. ie:
795 # "joe smith joe@smith.com" bad
796 # "joe smith <joe@smith.com" bad
797 if ($name ne "" && $address !~ /^<[^>]+>$/) {
798 $name = "";
799 $address = "";
800 $comment = "";
801 }
802 }
803
Joe Perches3705ce52013-07-03 15:05:31 -0700804 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700805 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700806 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700807 $address =~ s/^\<|\>$//g;
808
809 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
810 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
811 $name = "\"$name\"";
812 }
813
814 return ($name, $address, $comment);
815}
816
817sub format_email {
818 my ($name, $address) = @_;
819
820 my $formatted_email;
821
Joe Perches3705ce52013-07-03 15:05:31 -0700822 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700823 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700824 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700825
826 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
827 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
828 $name = "\"$name\"";
829 }
830
831 if ("$name" eq "") {
832 $formatted_email = "$address";
833 } else {
834 $formatted_email = "$name <$address>";
835 }
836
837 return $formatted_email;
838}
839
Joe Perchesd311cd42014-08-06 16:10:57 -0700840sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700841 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700842
Joe Perchesbd474ca2014-08-06 16:11:10 -0700843 foreach my $path (split(/:/, $ENV{PATH})) {
844 if (-e "$path/$bin") {
845 return "$path/$bin";
846 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700847 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700848
Joe Perchesbd474ca2014-08-06 16:11:10 -0700849 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700850}
851
Joe Perches000d1cc12011-07-25 17:13:25 -0700852sub which_conf {
853 my ($conf) = @_;
854
855 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
856 if (-e "$path/$conf") {
857 return "$path/$conf";
858 }
859 }
860
861 return "";
862}
863
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700864sub expand_tabs {
865 my ($str) = @_;
866
867 my $res = '';
868 my $n = 0;
869 for my $c (split(//, $str)) {
870 if ($c eq "\t") {
871 $res .= ' ';
872 $n++;
873 for (; ($n % 8) != 0; $n++) {
874 $res .= ' ';
875 }
876 next;
877 }
878 $res .= $c;
879 $n++;
880 }
881
882 return $res;
883}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700884sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700885 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700886 return $res;
887}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700888
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700889sub line_stats {
890 my ($line) = @_;
891
892 # Drop the diff line leader and expand tabs
893 $line =~ s/^.//;
894 $line = expand_tabs($line);
895
896 # Pick the indent from the front of the line.
897 my ($white) = ($line =~ /^(\s*)/);
898
899 return (length($line), length($white));
900}
901
Andy Whitcroft773647a2008-03-28 14:15:58 -0700902my $sanitise_quote = '';
903
904sub sanitise_line_reset {
905 my ($in_comment) = @_;
906
907 if ($in_comment) {
908 $sanitise_quote = '*/';
909 } else {
910 $sanitise_quote = '';
911 }
912}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700913sub sanitise_line {
914 my ($line) = @_;
915
916 my $res = '';
917 my $l = '';
918
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800919 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700920 my $off = 0;
921 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700922
Andy Whitcroft773647a2008-03-28 14:15:58 -0700923 # Always copy over the diff marker.
924 $res = substr($line, 0, 1);
925
926 for ($off = 1; $off < length($line); $off++) {
927 $c = substr($line, $off, 1);
928
929 # Comments we are wacking completly including the begin
930 # and end, all to $;.
931 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
932 $sanitise_quote = '*/';
933
934 substr($res, $off, 2, "$;$;");
935 $off++;
936 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800937 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700938 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700939 $sanitise_quote = '';
940 substr($res, $off, 2, "$;$;");
941 $off++;
942 next;
943 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700944 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
945 $sanitise_quote = '//';
946
947 substr($res, $off, 2, $sanitise_quote);
948 $off++;
949 next;
950 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700951
952 # A \ in a string means ignore the next character.
953 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
954 $c eq "\\") {
955 substr($res, $off, 2, 'XX');
956 $off++;
957 next;
958 }
959 # Regular quotes.
960 if ($c eq "'" || $c eq '"') {
961 if ($sanitise_quote eq '') {
962 $sanitise_quote = $c;
963
964 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700965 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700966 } elsif ($sanitise_quote eq $c) {
967 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700968 }
969 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700970
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800971 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700972 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
973 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700974 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
975 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700976 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
977 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700978 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700979 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700980 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800981 }
982
Daniel Walker113f04a2009-09-21 17:04:35 -0700983 if ($sanitise_quote eq '//') {
984 $sanitise_quote = '';
985 }
986
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800987 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700988 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800989 my $clean = 'X' x length($1);
990 $res =~ s@\<.*\>@<$clean>@;
991
992 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700993 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700995 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800996 }
997
Andy Whitcroft00df3442007-06-08 13:47:06 -0700998 return $res;
999}
1000
Joe Perchesa6962d72013-04-29 16:18:13 -07001001sub get_quoted_string {
1002 my ($line, $rawline) = @_;
1003
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001004 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001005 return substr($rawline, $-[0], $+[0] - $-[0]);
1006}
1007
Andy Whitcroft8905a672007-11-28 16:21:06 -08001008sub ctx_statement_block {
1009 my ($linenr, $remain, $off) = @_;
1010 my $line = $linenr - 1;
1011 my $blk = '';
1012 my $soff = $off;
1013 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001014 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001015
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001016 my $loff = 0;
1017
Andy Whitcroft8905a672007-11-28 16:21:06 -08001018 my $type = '';
1019 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001020 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001021 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001022 my $c;
1023 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001024
1025 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001026 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001027 @stack = (['', 0]) if ($#stack == -1);
1028
Andy Whitcroft773647a2008-03-28 14:15:58 -07001029 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001030 # If we are about to drop off the end, pull in more
1031 # context.
1032 if ($off >= $len) {
1033 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001034 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001035 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001036 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001037 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001039 $len = length($blk);
1040 $line++;
1041 last;
1042 }
1043 # Bail if there is no further context.
1044 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001045 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001046 last;
1047 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001048 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1049 $level++;
1050 $type = '#';
1051 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001052 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001053 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001054 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001055 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001056
Andy Whitcroft773647a2008-03-28 14:15:58 -07001057 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001058
1059 # Handle nested #if/#else.
1060 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1061 push(@stack, [ $type, $level ]);
1062 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1063 ($type, $level) = @{$stack[$#stack - 1]};
1064 } elsif ($remainder =~ /^#\s*endif\b/) {
1065 ($type, $level) = @{pop(@stack)};
1066 }
1067
Andy Whitcroft8905a672007-11-28 16:21:06 -08001068 # Statement ends at the ';' or a close '}' at the
1069 # outermost level.
1070 if ($level == 0 && $c eq ';') {
1071 last;
1072 }
1073
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001074 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001075 if ($level == 0 && $coff_set == 0 &&
1076 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1077 $remainder =~ /^(else)(?:\s|{)/ &&
1078 $remainder !~ /^else\s+if\b/) {
1079 $coff = $off + length($1) - 1;
1080 $coff_set = 1;
1081 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1082 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001083 }
1084
Andy Whitcroft8905a672007-11-28 16:21:06 -08001085 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 && $coff < $soff) {
1094 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001095 $coff_set = 1;
1096 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001097 }
1098 }
1099 if (($type eq '' || $type eq '{') && $c eq '{') {
1100 $level++;
1101 $type = '{';
1102 }
1103 if ($type eq '{' && $c eq '}') {
1104 $level--;
1105 $type = ($level != 0)? '{' : '';
1106
1107 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001108 if (substr($blk, $off + 1, 1) eq ';') {
1109 $off++;
1110 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001111 last;
1112 }
1113 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001114 # Preprocessor commands end at the newline unless escaped.
1115 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1116 $level--;
1117 $type = '';
1118 $off++;
1119 last;
1120 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001121 $off++;
1122 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001123 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001124 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001125 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001126 $line++;
1127 $remain--;
1128 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001129
1130 my $statement = substr($blk, $soff, $off - $soff + 1);
1131 my $condition = substr($blk, $soff, $coff - $soff + 1);
1132
1133 #warn "STATEMENT<$statement>\n";
1134 #warn "CONDITION<$condition>\n";
1135
Andy Whitcroft773647a2008-03-28 14:15:58 -07001136 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001137
1138 return ($statement, $condition,
1139 $line, $remain + 1, $off - $loff + 1, $level);
1140}
1141
Andy Whitcroftcf655042008-03-04 14:28:20 -08001142sub statement_lines {
1143 my ($stmt) = @_;
1144
1145 # Strip the diff line prefixes and rip blank lines at start and end.
1146 $stmt =~ s/(^|\n)./$1/g;
1147 $stmt =~ s/^\s*//;
1148 $stmt =~ s/\s*$//;
1149
1150 my @stmt_lines = ($stmt =~ /\n/g);
1151
1152 return $#stmt_lines + 2;
1153}
1154
1155sub statement_rawlines {
1156 my ($stmt) = @_;
1157
1158 my @stmt_lines = ($stmt =~ /\n/g);
1159
1160 return $#stmt_lines + 2;
1161}
1162
1163sub statement_block_size {
1164 my ($stmt) = @_;
1165
1166 $stmt =~ s/(^|\n)./$1/g;
1167 $stmt =~ s/^\s*{//;
1168 $stmt =~ s/}\s*$//;
1169 $stmt =~ s/^\s*//;
1170 $stmt =~ s/\s*$//;
1171
1172 my @stmt_lines = ($stmt =~ /\n/g);
1173 my @stmt_statements = ($stmt =~ /;/g);
1174
1175 my $stmt_lines = $#stmt_lines + 2;
1176 my $stmt_statements = $#stmt_statements + 1;
1177
1178 if ($stmt_lines > $stmt_statements) {
1179 return $stmt_lines;
1180 } else {
1181 return $stmt_statements;
1182 }
1183}
1184
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001185sub ctx_statement_full {
1186 my ($linenr, $remain, $off) = @_;
1187 my ($statement, $condition, $level);
1188
1189 my (@chunks);
1190
Andy Whitcroftcf655042008-03-04 14:28:20 -08001191 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001192 ($statement, $condition, $linenr, $remain, $off, $level) =
1193 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001194 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001195 push(@chunks, [ $condition, $statement ]);
1196 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1197 return ($level, $linenr, @chunks);
1198 }
1199
1200 # Pull in the following conditional/block pairs and see if they
1201 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001202 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001203 ($statement, $condition, $linenr, $remain, $off, $level) =
1204 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001205 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001206 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001207 #print "C: push\n";
1208 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001209 }
1210
1211 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001212}
1213
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001214sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001215 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001216 my $line;
1217 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001218 my $blk = '';
1219 my @o;
1220 my @c;
1221 my @res = ();
1222
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001223 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001224 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001225 for ($line = $start; $remain > 0; $line++) {
1226 next if ($rawlines[$line] =~ /^-/);
1227 $remain--;
1228
1229 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001230
1231 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001232 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001233 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001234 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001235 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001236 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001237 $level = pop(@stack);
1238 }
1239
Andy Whitcroft01464f32010-10-26 14:23:19 -07001240 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001241 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1242 if ($off > 0) {
1243 $off--;
1244 next;
1245 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001246
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001247 if ($c eq $close && $level > 0) {
1248 $level--;
1249 last if ($level == 0);
1250 } elsif ($c eq $open) {
1251 $level++;
1252 }
1253 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001254
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001255 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001256 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001257 }
1258
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001259 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001260 }
1261
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001262 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001263}
1264sub ctx_block_outer {
1265 my ($linenr, $remain) = @_;
1266
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001267 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1268 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001269}
1270sub ctx_block {
1271 my ($linenr, $remain) = @_;
1272
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001273 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1274 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001275}
1276sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001277 my ($linenr, $remain, $off) = @_;
1278
1279 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1280 return @r;
1281}
1282sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001283 my ($linenr, $remain) = @_;
1284
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001285 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001286}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001287sub ctx_statement_level {
1288 my ($linenr, $remain, $off) = @_;
1289
1290 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1291}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001292
1293sub ctx_locate_comment {
1294 my ($first_line, $end_line) = @_;
1295
1296 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001297 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001298 return $current_comment if (defined $current_comment);
1299
1300 # Look through the context and try and figure out if there is a
1301 # comment.
1302 my $in_comment = 0;
1303 $current_comment = '';
1304 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001305 my $line = $rawlines[$linenr - 1];
1306 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001307 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1308 $in_comment = 1;
1309 }
1310 if ($line =~ m@/\*@) {
1311 $in_comment = 1;
1312 }
1313 if (!$in_comment && $current_comment ne '') {
1314 $current_comment = '';
1315 }
1316 $current_comment .= $line . "\n" if ($in_comment);
1317 if ($line =~ m@\*/@) {
1318 $in_comment = 0;
1319 }
1320 }
1321
1322 chomp($current_comment);
1323 return($current_comment);
1324}
1325sub ctx_has_comment {
1326 my ($first_line, $end_line) = @_;
1327 my $cmt = ctx_locate_comment($first_line, $end_line);
1328
Andy Whitcroft00df3442007-06-08 13:47:06 -07001329 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001330 ##print "CMMT: $cmt\n";
1331
1332 return ($cmt ne '');
1333}
1334
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001335sub raw_line {
1336 my ($linenr, $cnt) = @_;
1337
1338 my $offset = $linenr - 1;
1339 $cnt++;
1340
1341 my $line;
1342 while ($cnt) {
1343 $line = $rawlines[$offset++];
1344 next if (defined($line) && $line =~ /^-/);
1345 $cnt--;
1346 }
1347
1348 return $line;
1349}
1350
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001351sub cat_vet {
1352 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001353 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001354
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001355 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001356 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1357 $res .= $1;
1358 if ($2 ne '') {
1359 $coded = sprintf("^%c", unpack('C', $2) + 64);
1360 $res .= $coded;
1361 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001362 }
1363 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001364
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001365 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001366}
1367
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001368my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001369my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001370my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001371my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001372
1373sub annotate_reset {
1374 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001375 $av_pending = '_';
1376 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001377 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001378}
1379
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001380sub annotate_values {
1381 my ($stream, $type) = @_;
1382
1383 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001384 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001385 my $cur = $stream;
1386
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001387 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001388
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001389 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001390 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001391 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001392 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001393 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001394 print "WS($1)\n" if ($dbg_values > 1);
1395 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001396 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001397 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001398 }
1399
Florian Micklerc023e4732011-01-12 16:59:58 -08001400 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001401 print "CAST($1)\n" if ($dbg_values > 1);
1402 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001403 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001404
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001405 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001406 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001407 $type = 'T';
1408
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001409 } elsif ($cur =~ /^($Modifier)\s*/) {
1410 print "MODIFIER($1)\n" if ($dbg_values > 1);
1411 $type = 'T';
1412
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001413 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001414 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001415 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001416 push(@av_paren_type, $type);
1417 if ($2 ne '') {
1418 $av_pending = 'N';
1419 }
1420 $type = 'E';
1421
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001422 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001423 print "UNDEF($1)\n" if ($dbg_values > 1);
1424 $av_preprocessor = 1;
1425 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001426
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001427 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001428 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001429 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001430
1431 push(@av_paren_type, $type);
1432 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001433 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001434
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001435 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1437 $av_preprocessor = 1;
1438
1439 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1440
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001441 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001442
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001443 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001444 print "PRE_END($1)\n" if ($dbg_values > 1);
1445
1446 $av_preprocessor = 1;
1447
1448 # Assume all arms of the conditional end as this
1449 # one does, and continue as if the #endif was not here.
1450 pop(@av_paren_type);
1451 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001452 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001453
1454 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001455 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001456
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001457 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1458 print "ATTR($1)\n" if ($dbg_values > 1);
1459 $av_pending = $type;
1460 $type = 'N';
1461
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001462 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001463 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001464 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001465 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001466 }
1467 $type = 'N';
1468
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001469 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001470 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001471 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001472 $type = 'N';
1473
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001474 } elsif ($cur =~/^(case)/o) {
1475 print "CASE($1)\n" if ($dbg_values > 1);
1476 $av_pend_colon = 'C';
1477 $type = 'N';
1478
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001479 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001480 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001481 $type = 'N';
1482
1483 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001484 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001485 push(@av_paren_type, $av_pending);
1486 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001487 $type = 'N';
1488
1489 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001490 my $new_type = pop(@av_paren_type);
1491 if ($new_type ne '_') {
1492 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001493 print "PAREN('$1') -> $type\n"
1494 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001495 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001496 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001497 }
1498
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001499 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001500 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001501 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001502 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001503
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001504 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1505 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001506 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001507 } elsif ($type eq 'E') {
1508 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001509 }
1510 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1511 $type = 'V';
1512
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001513 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001514 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001515 $type = 'V';
1516
1517 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001518 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001519 $type = 'N';
1520
Andy Whitcroftcf655042008-03-04 14:28:20 -08001521 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001522 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001523 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001524 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001525
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001526 } elsif ($cur =~/^(,)/) {
1527 print "COMMA($1)\n" if ($dbg_values > 1);
1528 $type = 'C';
1529
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001530 } elsif ($cur =~ /^(\?)/o) {
1531 print "QUESTION($1)\n" if ($dbg_values > 1);
1532 $type = 'N';
1533
1534 } elsif ($cur =~ /^(:)/o) {
1535 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1536
1537 substr($var, length($res), 1, $av_pend_colon);
1538 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1539 $type = 'E';
1540 } else {
1541 $type = 'N';
1542 }
1543 $av_pend_colon = 'O';
1544
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001545 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001546 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001547 $type = 'N';
1548
Andy Whitcroft0d413862008-10-15 22:02:16 -07001549 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001550 my $variant;
1551
1552 print "OPV($1)\n" if ($dbg_values > 1);
1553 if ($type eq 'V') {
1554 $variant = 'B';
1555 } else {
1556 $variant = 'U';
1557 }
1558
1559 substr($var, length($res), 1, $variant);
1560 $type = 'N';
1561
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001562 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001563 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001564 if ($1 ne '++' && $1 ne '--') {
1565 $type = 'N';
1566 }
1567
1568 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001569 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001570 }
1571 if (defined $1) {
1572 $cur = substr($cur, length($1));
1573 $res .= $type x length($1);
1574 }
1575 }
1576
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001577 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001578}
1579
Andy Whitcroft8905a672007-11-28 16:21:06 -08001580sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001581 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001582 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001583 ^(?:
1584 $Modifier|
1585 $Storage|
1586 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001587 DEFINE_\S+
1588 )$|
1589 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001590 goto|
1591 return|
1592 case|
1593 else|
1594 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001595 do|
1596 \#|
1597 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001598 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001599 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001600 )}x;
1601 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1602 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001603 # Check for modifiers.
1604 $possible =~ s/\s*$Storage\s*//g;
1605 $possible =~ s/\s*$Sparse\s*//g;
1606 if ($possible =~ /^\s*$/) {
1607
1608 } elsif ($possible =~ /\s/) {
1609 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001610 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001611 if ($modifier !~ $notPermitted) {
1612 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1613 push(@modifierList, $modifier);
1614 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001615 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001616
1617 } else {
1618 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1619 push(@typeList, $possible);
1620 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001621 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001622 } else {
1623 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001624 }
1625}
1626
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001627my $prefix = '';
1628
Joe Perches000d1cc12011-07-25 17:13:25 -07001629sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001630 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001631
Joe Perchescbec18a2014-04-03 14:49:19 -07001632 return defined $use_type{$type} if (scalar keys %use_type > 0);
1633
1634 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001635}
1636
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001637sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001638 my ($level, $type, $msg) = @_;
1639
1640 if (!show_type($type) ||
1641 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001642 return 0;
1643 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001644 my $line;
1645 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001646 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001647 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001648 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001649 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001650 $line = (split('\n', $line))[0] . "\n" if ($terse);
1651
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001652 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001653
1654 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001655}
Joe Perchescbec18a2014-04-03 14:49:19 -07001656
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001657sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001658 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001659}
Joe Perches000d1cc12011-07-25 17:13:25 -07001660
Joe Perchesd752fcc2014-08-06 16:11:05 -07001661sub fixup_current_range {
1662 my ($lineRef, $offset, $length) = @_;
1663
1664 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1665 my $o = $1;
1666 my $l = $2;
1667 my $no = $o + $offset;
1668 my $nl = $l + $length;
1669 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1670 }
1671}
1672
1673sub fix_inserted_deleted_lines {
1674 my ($linesRef, $insertedRef, $deletedRef) = @_;
1675
1676 my $range_last_linenr = 0;
1677 my $delta_offset = 0;
1678
1679 my $old_linenr = 0;
1680 my $new_linenr = 0;
1681
1682 my $next_insert = 0;
1683 my $next_delete = 0;
1684
1685 my @lines = ();
1686
1687 my $inserted = @{$insertedRef}[$next_insert++];
1688 my $deleted = @{$deletedRef}[$next_delete++];
1689
1690 foreach my $old_line (@{$linesRef}) {
1691 my $save_line = 1;
1692 my $line = $old_line; #don't modify the array
1693 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1694 $delta_offset = 0;
1695 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1696 $range_last_linenr = $new_linenr;
1697 fixup_current_range(\$line, $delta_offset, 0);
1698 }
1699
1700 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1701 $deleted = @{$deletedRef}[$next_delete++];
1702 $save_line = 0;
1703 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1704 }
1705
1706 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1707 push(@lines, ${$inserted}{'LINE'});
1708 $inserted = @{$insertedRef}[$next_insert++];
1709 $new_linenr++;
1710 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1711 }
1712
1713 if ($save_line) {
1714 push(@lines, $line);
1715 $new_linenr++;
1716 }
1717
1718 $old_linenr++;
1719 }
1720
1721 return @lines;
1722}
1723
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001724sub fix_insert_line {
1725 my ($linenr, $line) = @_;
1726
1727 my $inserted = {
1728 LINENR => $linenr,
1729 LINE => $line,
1730 };
1731 push(@fixed_inserted, $inserted);
1732}
1733
1734sub fix_delete_line {
1735 my ($linenr, $line) = @_;
1736
1737 my $deleted = {
1738 LINENR => $linenr,
1739 LINE => $line,
1740 };
1741
1742 push(@fixed_deleted, $deleted);
1743}
1744
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001745sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001746 my ($type, $msg) = @_;
1747
1748 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001749 our $clean = 0;
1750 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001751 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001752 }
Joe Perches3705ce52013-07-03 15:05:31 -07001753 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001754}
1755sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001756 my ($type, $msg) = @_;
1757
1758 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001759 our $clean = 0;
1760 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001761 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001762 }
Joe Perches3705ce52013-07-03 15:05:31 -07001763 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001764}
1765sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001766 my ($type, $msg) = @_;
1767
1768 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001769 our $clean = 0;
1770 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001771 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001772 }
Joe Perches3705ce52013-07-03 15:05:31 -07001773 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001774}
1775
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001776sub check_absolute_file {
1777 my ($absolute, $herecurr) = @_;
1778 my $file = $absolute;
1779
1780 ##print "absolute<$absolute>\n";
1781
1782 # See if any suffix of this path is a path within the tree.
1783 while ($file =~ s@^[^/]*/@@) {
1784 if (-f "$root/$file") {
1785 ##print "file<$file>\n";
1786 last;
1787 }
1788 }
1789 if (! -f _) {
1790 return 0;
1791 }
1792
1793 # It is, so see if the prefix is acceptable.
1794 my $prefix = $absolute;
1795 substr($prefix, -length($file)) = '';
1796
1797 ##print "prefix<$prefix>\n";
1798 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001799 WARN("USE_RELATIVE_PATH",
1800 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001801 }
1802}
1803
Joe Perches3705ce52013-07-03 15:05:31 -07001804sub trim {
1805 my ($string) = @_;
1806
Joe Perchesb34c6482013-09-11 14:24:01 -07001807 $string =~ s/^\s+|\s+$//g;
1808
1809 return $string;
1810}
1811
1812sub ltrim {
1813 my ($string) = @_;
1814
1815 $string =~ s/^\s+//;
1816
1817 return $string;
1818}
1819
1820sub rtrim {
1821 my ($string) = @_;
1822
1823 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001824
1825 return $string;
1826}
1827
Joe Perches52ea8502013-11-12 15:10:09 -08001828sub string_find_replace {
1829 my ($string, $find, $replace) = @_;
1830
1831 $string =~ s/$find/$replace/g;
1832
1833 return $string;
1834}
1835
Joe Perches3705ce52013-07-03 15:05:31 -07001836sub tabify {
1837 my ($leading) = @_;
1838
1839 my $source_indent = 8;
1840 my $max_spaces_before_tab = $source_indent - 1;
1841 my $spaces_to_tab = " " x $source_indent;
1842
1843 #convert leading spaces to tabs
1844 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1845 #Remove spaces before a tab
1846 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1847
1848 return "$leading";
1849}
1850
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001851sub pos_last_openparen {
1852 my ($line) = @_;
1853
1854 my $pos = 0;
1855
1856 my $opens = $line =~ tr/\(/\(/;
1857 my $closes = $line =~ tr/\)/\)/;
1858
1859 my $last_openparen = 0;
1860
1861 if (($opens == 0) || ($closes >= $opens)) {
1862 return -1;
1863 }
1864
1865 my $len = length($line);
1866
1867 for ($pos = 0; $pos < $len; $pos++) {
1868 my $string = substr($line, $pos);
1869 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1870 $pos += length($1) - 1;
1871 } elsif (substr($line, $pos, 1) eq '(') {
1872 $last_openparen = $pos;
1873 } elsif (index($string, '(') == -1) {
1874 last;
1875 }
1876 }
1877
Joe Perches91cb5192014-04-03 14:49:32 -07001878 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001879}
1880
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001881sub process {
1882 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001883
1884 my $linenr=0;
1885 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001886 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001887 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001888 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001889
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001890 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001891 my $indent;
1892 my $previndent=0;
1893 my $stashindent=0;
1894
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001895 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001896 my $signoff = 0;
1897 my $is_patch = 0;
1898
Joe Perches29ee1b02014-08-06 16:10:35 -07001899 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001900 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches2a076f42015-04-16 12:44:28 -07001901 my $commit_log_long_line = 0;
Joe Perches13f19372014-08-06 16:10:59 -07001902 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001903 my $non_utf8_charset = 0;
1904
Joe Perches365dd4e2014-08-06 16:10:42 -07001905 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001906 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001907
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001908 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001909 our $cnt_lines = 0;
1910 our $cnt_error = 0;
1911 our $cnt_warn = 0;
1912 our $cnt_chk = 0;
1913
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001914 # Trace the real file/line as we go.
1915 my $realfile = '';
1916 my $realline = 0;
1917 my $realcnt = 0;
1918 my $here = '';
1919 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001920 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001921 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001922 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001923
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001924 my $prev_values = 'E';
1925
1926 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001927 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001928 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001929 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001930 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001931
Joe Perches7e51f192013-09-11 14:23:57 -07001932 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001933
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001934 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001935 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001936 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001937 my @setup_docs = ();
1938 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001939
Joe Perchesd8b07712013-11-12 15:10:06 -08001940 my $camelcase_file_seeded = 0;
1941
Andy Whitcroft773647a2008-03-28 14:15:58 -07001942 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001943 my $line;
1944 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001945 $linenr++;
1946 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001947
Joe Perches3705ce52013-07-03 15:05:31 -07001948 push(@fixed, $rawline) if ($fix);
1949
Andy Whitcroft773647a2008-03-28 14:15:58 -07001950 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001951 $setup_docs = 0;
1952 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1953 $setup_docs = 1;
1954 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001955 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001956 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001957 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1958 $realline=$1-1;
1959 if (defined $2) {
1960 $realcnt=$3+1;
1961 } else {
1962 $realcnt=1+1;
1963 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001964 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001965
1966 # Guestimate if this is a continuing comment. Run
1967 # the context looking for a comment "edge". If this
1968 # edge is a close comment then we must be in a comment
1969 # at context start.
1970 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001971 my $cnt = $realcnt;
1972 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1973 next if (defined $rawlines[$ln - 1] &&
1974 $rawlines[$ln - 1] =~ /^-/);
1975 $cnt--;
1976 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001977 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001978 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1979 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1980 ($edge) = $1;
1981 last;
1982 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001983 }
1984 if (defined $edge && $edge eq '*/') {
1985 $in_comment = 1;
1986 }
1987
1988 # Guestimate if this is a continuing comment. If this
1989 # is the start of a diff block and this line starts
1990 # ' *' then it is very likely a comment.
1991 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001992 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001993 {
1994 $in_comment = 1;
1995 }
1996
1997 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1998 sanitise_line_reset($in_comment);
1999
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002000 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002001 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002002 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002003 $line = sanitise_line($rawline);
2004 }
2005 push(@lines, $line);
2006
2007 if ($realcnt > 1) {
2008 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2009 } else {
2010 $realcnt = 0;
2011 }
2012
2013 #print "==>$rawline\n";
2014 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002015
2016 if ($setup_docs && $line =~ /^\+/) {
2017 push(@setup_docs, $line);
2018 }
2019 }
2020
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002021 $prefix = '';
2022
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023 $realcnt = 0;
2024 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002025 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002026 foreach my $line (@lines) {
2027 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002028 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002029 my $sline = $line; #copy of $line
2030 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002031
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002032 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002033
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002034#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002035 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002036 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002037 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002038 $realline=$1-1;
2039 if (defined $2) {
2040 $realcnt=$3+1;
2041 } else {
2042 $realcnt=1+1;
2043 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002044 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002045 $prev_values = 'E';
2046
Andy Whitcroft773647a2008-03-28 14:15:58 -07002047 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002048 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002049 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002050 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002051 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002052
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002053# track the line number as we move through the hunk, note that
2054# new versions of GNU diff omit the leading space on completely
2055# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002056 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002057 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002058 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002059
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002060 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002061 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002062
2063 # Track the previous line.
2064 ($prevline, $stashline) = ($stashline, $line);
2065 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002066 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2067
Andy Whitcroft773647a2008-03-28 14:15:58 -07002068 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002069
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002070 } elsif ($realcnt == 1) {
2071 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002072 }
2073
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002074 my $hunk_line = ($realcnt != 0);
2075
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002076#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002077 $prefix = "$filename:$realline: " if ($emacs && $file);
2078 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2079
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002080 $here = "#$linenr: " if (!$file);
2081 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002082
Joe Perches2ac73b42014-06-04 16:12:05 -07002083 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002084 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002085 if ($line =~ /^diff --git.*?(\S+)$/) {
2086 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002087 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002088 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002089 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002090 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002091 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002092 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002093 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002094
2095 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002096 if (!$file && $tree && $p1_prefix ne '' &&
2097 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002098 WARN("PATCH_PREFIX",
2099 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002100 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002101
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002102 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002103 ERROR("MODIFIED_INCLUDE_ASM",
2104 "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 -07002105 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002106 $found_file = 1;
2107 }
2108
2109 if ($found_file) {
2110 if ($realfile =~ m@^(drivers/net/|net/)@) {
2111 $check = 1;
2112 } else {
2113 $check = $check_orig;
2114 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002115 next;
2116 }
2117
Randy Dunlap389834b2007-06-08 13:47:03 -07002118 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002119
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002120 my $hereline = "$here\n$rawline\n";
2121 my $herecurr = "$here\n$rawline\n";
2122 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002123
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002124 $cnt_lines++ if ($realcnt != 0);
2125
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002126# Check for incorrect file permissions
2127 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2128 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002129 if ($realfile !~ m@scripts/@ &&
2130 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002131 ERROR("EXECUTE_PERMISSIONS",
2132 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002133 }
2134 }
2135
Joe Perches20112472011-07-25 17:13:23 -07002136# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002137 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002138 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002139 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002140 }
2141
Joe Perchese0d975b2014-12-10 15:51:49 -08002142# Check if MAINTAINERS is being updated. If so, there's probably no need to
2143# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2144 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2145 $reported_maintainer_file = 1;
2146 }
2147
Joe Perches20112472011-07-25 17:13:23 -07002148# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002149 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002150 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002151 my $space_before = $1;
2152 my $sign_off = $2;
2153 my $space_after = $3;
2154 my $email = $4;
2155 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2156
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002157 if ($sign_off !~ /$signature_tags/) {
2158 WARN("BAD_SIGN_OFF",
2159 "Non-standard signature: $sign_off\n" . $herecurr);
2160 }
Joe Perches20112472011-07-25 17:13:23 -07002161 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002162 if (WARN("BAD_SIGN_OFF",
2163 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2164 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002165 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002166 "$ucfirst_sign_off $email";
2167 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002168 }
Joe Perches20112472011-07-25 17:13:23 -07002169 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002170 if (WARN("BAD_SIGN_OFF",
2171 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2172 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002173 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002174 "$ucfirst_sign_off $email";
2175 }
2176
Joe Perches20112472011-07-25 17:13:23 -07002177 }
2178 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002179 if (WARN("BAD_SIGN_OFF",
2180 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2181 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002182 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002183 "$ucfirst_sign_off $email";
2184 }
Joe Perches20112472011-07-25 17:13:23 -07002185 }
2186
2187 my ($email_name, $email_address, $comment) = parse_email($email);
2188 my $suggested_email = format_email(($email_name, $email_address));
2189 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002190 ERROR("BAD_SIGN_OFF",
2191 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002192 } else {
2193 my $dequoted = $suggested_email;
2194 $dequoted =~ s/^"//;
2195 $dequoted =~ s/" </ </;
2196 # Don't force email to have quotes
2197 # Allow just an angle bracketed address
2198 if ("$dequoted$comment" ne $email &&
2199 "<$email_address>$comment" ne $email &&
2200 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002201 WARN("BAD_SIGN_OFF",
2202 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002203 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002204 }
Joe Perches7e51f192013-09-11 14:23:57 -07002205
2206# Check for duplicate signatures
2207 my $sig_nospace = $line;
2208 $sig_nospace =~ s/\s//g;
2209 $sig_nospace = lc($sig_nospace);
2210 if (defined $signatures{$sig_nospace}) {
2211 WARN("BAD_SIGN_OFF",
2212 "Duplicate signature\n" . $herecurr);
2213 } else {
2214 $signatures{$sig_nospace} = 1;
2215 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002216 }
2217
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002218# Check email subject for common tools that don't need to be mentioned
2219 if ($in_header_lines &&
2220 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2221 WARN("EMAIL_SUBJECT",
2222 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2223 }
2224
Joe Perches9b3189e2014-06-04 16:12:10 -07002225# Check for old stable address
2226 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2227 ERROR("STABLE_ADDRESS",
2228 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2229 }
2230
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002231# Check for unwanted Gerrit info
2232 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2233 ERROR("GERRIT_CHANGE_ID",
2234 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2235 }
2236
Joe Perches2a076f42015-04-16 12:44:28 -07002237# Check for line lengths > 75 in commit log, warn once
2238 if ($in_commit_log && !$commit_log_long_line &&
2239 length($line) > 75) {
2240 WARN("COMMIT_LOG_LONG_LINE",
2241 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2242 $commit_log_long_line = 1;
2243 }
2244
Joe Perches0d7835f2015-02-13 14:38:35 -08002245# Check for git id commit length and improperly formed commit descriptions
2246 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002247 my $init_char = $1;
2248 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002249 my $short = 1;
2250 my $long = 0;
2251 my $case = 1;
2252 my $space = 1;
2253 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002254 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002255 my $id = '0123456789ab';
2256 my $orig_desc = "commit description";
2257 my $description = "";
2258
2259 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2260 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2261 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2262 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2263 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2264 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002265 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002266 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2267 defined $rawlines[$linenr] &&
2268 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2269 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002270 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002271 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2272 defined $rawlines[$linenr] &&
2273 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2274 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2275 $orig_desc = $1;
2276 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2277 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002278 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002279 }
2280
2281 ($id, $description) = git_commit_info($orig_commit,
2282 $id, $orig_desc);
2283
Joe Perches19c146a2015-02-13 14:39:00 -08002284 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002285 ERROR("GIT_COMMIT_ID",
2286 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2287 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002288 }
2289
Joe Perches13f19372014-08-06 16:10:59 -07002290# Check for added, moved or deleted files
2291 if (!$reported_maintainer_file && !$in_commit_log &&
2292 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2293 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2294 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2295 (defined($1) || defined($2))))) {
2296 $reported_maintainer_file = 1;
2297 WARN("FILE_PATH_CHANGES",
2298 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2299 }
2300
Andy Whitcroft00df3442007-06-08 13:47:06 -07002301# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002302 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002303 ERROR("CORRUPTED_PATCH",
2304 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002305 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002306 }
2307
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002308# Check for absolute kernel paths.
2309 if ($tree) {
2310 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2311 my $file = $1;
2312
2313 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2314 check_absolute_file($1, $herecurr)) {
2315 #
2316 } else {
2317 check_absolute_file($file, $herecurr);
2318 }
2319 }
2320 }
2321
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002322# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2323 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002324 $rawline !~ m/^$UTF8*$/) {
2325 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2326
2327 my $blank = copy_spacing($rawline);
2328 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2329 my $hereptr = "$hereline$ptr\n";
2330
Joe Perches34d99212011-07-25 17:13:26 -07002331 CHK("INVALID_UTF8",
2332 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002333 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002334
Joe Perches15662b32011-10-31 17:13:12 -07002335# Check if it's the start of a commit log
2336# (not a header line and we haven't seen the patch filename)
2337 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002338 !($rawline =~ /^\s+\S/ ||
2339 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002340 $in_header_lines = 0;
2341 $in_commit_log = 1;
2342 }
2343
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002344# Check if there is UTF-8 in a commit log when a mail header has explicitly
2345# declined it, i.e defined some charset where it is missing.
2346 if ($in_header_lines &&
2347 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2348 $1 !~ /utf-8/i) {
2349 $non_utf8_charset = 1;
2350 }
2351
2352 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002353 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002354 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002355 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2356 }
2357
Kees Cook66b47b42014-10-13 15:51:57 -07002358# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002359 if (defined($misspellings) &&
2360 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002361 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002362 my $typo = $1;
2363 my $typo_fix = $spelling_fix{lc($typo)};
2364 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2365 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2366 my $msg_type = \&WARN;
2367 $msg_type = \&CHK if ($file);
2368 if (&{$msg_type}("TYPO_SPELLING",
2369 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2370 $fix) {
2371 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2372 }
2373 }
2374 }
2375
Andy Whitcroft306708542008-10-15 22:02:28 -07002376# ignore non-hunk lines and lines being removed
2377 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002378
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002379#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002380 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002381 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002382 if (ERROR("DOS_LINE_ENDINGS",
2383 "DOS line endings\n" . $herevet) &&
2384 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002385 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002386 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002387 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2388 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002389 if (ERROR("TRAILING_WHITESPACE",
2390 "trailing whitespace\n" . $herevet) &&
2391 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002392 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002393 }
2394
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002395 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002396 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002397
Josh Triplett4783f892013-11-12 15:10:12 -08002398# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002399 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002400 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2401 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002402 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2403 my $msg_type = \&ERROR;
2404 $msg_type = \&CHK if ($file);
2405 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002406 "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 -08002407 }
2408
Andi Kleen33549572010-05-24 14:33:29 -07002409# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002410# Only applies when adding the entry originally, after that we do not have
2411# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002412 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002413 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002414 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002415 my $cnt = $realcnt;
2416 my $ln = $linenr + 1;
2417 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002418 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002419 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002420 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002421 $f = $lines[$ln - 1];
2422 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2423 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002424
2425 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002426 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002427
Joe Perches8d73e0e2014-08-06 16:10:46 -07002428 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002429 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002430 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002431 $length = -1;
2432 }
2433
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002434 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002435 $f =~ s/#.*//;
2436 $f =~ s/^\s+//;
2437 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002438 if ($f =~ /^\s*config\s/) {
2439 $is_end = 1;
2440 last;
2441 }
Andi Kleen33549572010-05-24 14:33:29 -07002442 $length++;
2443 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002444 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2445 WARN("CONFIG_DESCRIPTION",
2446 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2447 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002448 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002449 }
2450
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002451# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2452 if ($realfile =~ /Kconfig/ &&
2453 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2454 WARN("CONFIG_EXPERIMENTAL",
2455 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2456 }
2457
Christoph Jaeger327953e2015-02-13 14:38:29 -08002458# discourage the use of boolean for type definition attributes of Kconfig options
2459 if ($realfile =~ /Kconfig/ &&
2460 $line =~ /^\+\s*\bboolean\b/) {
2461 WARN("CONFIG_TYPE_BOOLEAN",
2462 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2463 }
2464
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002465 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2466 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2467 my $flag = $1;
2468 my $replacement = {
2469 'EXTRA_AFLAGS' => 'asflags-y',
2470 'EXTRA_CFLAGS' => 'ccflags-y',
2471 'EXTRA_CPPFLAGS' => 'cppflags-y',
2472 'EXTRA_LDFLAGS' => 'ldflags-y',
2473 };
2474
2475 WARN("DEPRECATED_VARIABLE",
2476 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2477 }
2478
Rob Herringbff5da42014-01-23 15:54:51 -08002479# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002480 if (defined $root &&
2481 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2482 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2483
Rob Herringbff5da42014-01-23 15:54:51 -08002484 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2485
Florian Vaussardcc933192014-04-03 14:49:27 -07002486 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2487 my $vp_file = $dt_path . "vendor-prefixes.txt";
2488
Rob Herringbff5da42014-01-23 15:54:51 -08002489 foreach my $compat (@compats) {
2490 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002491 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2492 my $compat3 = $compat;
2493 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2494 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002495 if ( $? >> 8 ) {
2496 WARN("UNDOCUMENTED_DT_STRING",
2497 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2498 }
2499
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002500 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2501 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002502 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002503 if ( $? >> 8 ) {
2504 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002505 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002506 }
2507 }
2508 }
2509
Andy Whitcroft5368df202008-10-15 22:02:27 -07002510# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002511 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002512
Joe Perches6cd7f382012-12-17 16:01:54 -08002513#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002514 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002515 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches29a3c462015-04-16 12:44:30 -07002516 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?$String\s*(?:|,|\)\s*;)\s*$/ ||
2517 $line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2518 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002519 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002520 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002521 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002522 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002523 }
2524
Andy Whitcroft8905a672007-11-28 16:21:06 -08002525# check for adding lines without a newline.
2526 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002527 WARN("MISSING_EOF_NEWLINE",
2528 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002529 }
2530
Mike Frysinger42e41c52009-09-21 17:04:40 -07002531# Blackfin: use hi/lo macros
2532 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2533 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2534 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002535 ERROR("LO_MACRO",
2536 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002537 }
2538 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2539 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002540 ERROR("HI_MACRO",
2541 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002542 }
2543 }
2544
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002545# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002546 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002547
2548# at the beginning of a line any tabs must come first and anything
2549# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002550 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2551 $rawline =~ /^\+\s* \s*/) {
2552 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002553 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002554 if (ERROR("CODE_INDENT",
2555 "code indent should use tabs where possible\n" . $herevet) &&
2556 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002557 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002558 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002559 }
2560
Alberto Panizzo08e44362010-03-05 13:43:54 -08002561# check for space before tabs.
2562 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2563 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002564 if (WARN("SPACE_BEFORE_TAB",
2565 "please, no space before tabs\n" . $herevet) &&
2566 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002567 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002568 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002569 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002570 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002571 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002572 }
2573
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002574# check for && or || at the start of a line
2575 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2576 CHK("LOGICAL_CONTINUATIONS",
2577 "Logical continuations should be on the previous line\n" . $hereprev);
2578 }
2579
2580# check multi-line statement indentation matches previous line
2581 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002582 $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 -07002583 $prevline =~ /^\+(\t*)(.*)$/;
2584 my $oldindent = $1;
2585 my $rest = $2;
2586
2587 my $pos = pos_last_openparen($rest);
2588 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002589 $line =~ /^(\+| )([ \t]*)/;
2590 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002591
2592 my $goodtabindent = $oldindent .
2593 "\t" x ($pos / 8) .
2594 " " x ($pos % 8);
2595 my $goodspaceindent = $oldindent . " " x $pos;
2596
2597 if ($newindent ne $goodtabindent &&
2598 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002599
2600 if (CHK("PARENTHESIS_ALIGNMENT",
2601 "Alignment should match open parenthesis\n" . $hereprev) &&
2602 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002603 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002604 s/^\+[ \t]*/\+$goodtabindent/;
2605 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002606 }
2607 }
2608 }
2609
Joe Perches6ab3a972015-04-16 12:44:05 -07002610# check for space after cast like "(int) foo" or "(struct foo) bar"
2611# avoid checking a few false positives:
2612# "sizeof(<type>)" or "__alignof__(<type>)"
2613# function pointer declarations like "(*foo)(int) = bar;"
2614# structure definitions like "(struct foo) { 0 };"
2615# multiline macros that define functions
2616# known attributes or the __attribute__ keyword
2617 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2618 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002619 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002620 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002621 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002622 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002623 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002624 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002625 }
2626
Joe Perches05880602012-10-04 17:13:35 -07002627 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002628 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002629 $rawline =~ /^\+[ \t]*\*/ &&
2630 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002631 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2632 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2633 }
2634
2635 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002636 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2637 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002638 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002639 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2640 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2641 "networking block comments start with * on subsequent lines\n" . $hereprev);
2642 }
2643
2644 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002645 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2646 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2647 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2648 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002649 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2650 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2651 }
2652
Joe Perches7f619192014-08-06 16:10:39 -07002653# check for missing blank lines after struct/union declarations
2654# with exceptions for various attributes and macros
2655 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2656 $line =~ /^\+/ &&
2657 !($line =~ /^\+\s*$/ ||
2658 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2659 $line =~ /^\+\s*MODULE_/i ||
2660 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2661 $line =~ /^\+[a-z_]*init/ ||
2662 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2663 $line =~ /^\+\s*DECLARE/ ||
2664 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002665 if (CHK("LINE_SPACING",
2666 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2667 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002668 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002669 }
Joe Perches7f619192014-08-06 16:10:39 -07002670 }
2671
Joe Perches365dd4e2014-08-06 16:10:42 -07002672# check for multiple consecutive blank lines
2673 if ($prevline =~ /^[\+ ]\s*$/ &&
2674 $line =~ /^\+\s*$/ &&
2675 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002676 if (CHK("LINE_SPACING",
2677 "Please don't use multiple blank lines\n" . $hereprev) &&
2678 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002679 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002680 }
2681
Joe Perches365dd4e2014-08-06 16:10:42 -07002682 $last_blank_line = $linenr;
2683 }
2684
Joe Perches3b617e32014-04-03 14:49:28 -07002685# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002686 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2687 # actual declarations
2688 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002689 # function pointer declarations
2690 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002691 # foo bar; where foo is some local typedef or #define
2692 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2693 # known declaration macros
2694 $prevline =~ /^\+\s+$declaration_macros/) &&
2695 # for "else if" which can look like "$Ident $Ident"
2696 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2697 # other possible extensions of declaration lines
2698 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2699 # not starting a section or a macro "\" extended line
2700 $prevline =~ /(?:\{\s*|\\)$/) &&
2701 # looks like a declaration
2702 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002703 # function pointer declarations
2704 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002705 # foo bar; where foo is some local typedef or #define
2706 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2707 # known declaration macros
2708 $sline =~ /^\+\s+$declaration_macros/ ||
2709 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002710 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002711 # start or end of block or continuation of declaration
2712 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2713 # bitfield continuation
2714 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2715 # other possible extensions of declaration lines
2716 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2717 # indentation of previous and current line are the same
2718 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002719 if (WARN("LINE_SPACING",
2720 "Missing a blank line after declarations\n" . $hereprev) &&
2721 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002722 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002723 }
Joe Perches3b617e32014-04-03 14:49:28 -07002724 }
2725
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002726# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002727# Exceptions:
2728# 1) within comments
2729# 2) indented preprocessor commands
2730# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002731 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002732 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002733 if (WARN("LEADING_SPACE",
2734 "please, no spaces at the start of a line\n" . $herevet) &&
2735 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002736 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002737 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002738 }
2739
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002740# check we are in a valid C source file if not then ignore this hunk
2741 next if ($realfile !~ /\.(h|c)$/);
2742
Joe Perches032a4c02014-08-06 16:10:29 -07002743# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002744# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002745# if the previous line is a break or return and is indented 1 tab more...
2746 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2747 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002748 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2749 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2750 defined $lines[$linenr] &&
2751 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002752 WARN("UNNECESSARY_ELSE",
2753 "else is not generally useful after a break or return\n" . $hereprev);
2754 }
2755 }
2756
Joe Perchesc00df192014-08-06 16:11:01 -07002757# check indentation of a line with a break;
2758# if the previous line is a goto or return and is indented the same # of tabs
2759 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2760 my $tabs = $1;
2761 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2762 WARN("UNNECESSARY_BREAK",
2763 "break is not useful after a goto or return\n" . $hereprev);
2764 }
2765 }
2766
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002767# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2768 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2769 WARN("CONFIG_EXPERIMENTAL",
2770 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2771 }
2772
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002773# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002774 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002775 WARN("CVS_KEYWORD",
2776 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002777 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002778
Mike Frysinger42e41c52009-09-21 17:04:40 -07002779# Blackfin: don't use __builtin_bfin_[cs]sync
2780 if ($line =~ /__builtin_bfin_csync/) {
2781 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002782 ERROR("CSYNC",
2783 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002784 }
2785 if ($line =~ /__builtin_bfin_ssync/) {
2786 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002787 ERROR("SSYNC",
2788 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002789 }
2790
Joe Perches56e77d72013-02-21 16:44:14 -08002791# check for old HOTPLUG __dev<foo> section markings
2792 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2793 WARN("HOTPLUG_SECTION",
2794 "Using $1 is unnecessary\n" . $herecurr);
2795 }
2796
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002797# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002798 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2799 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002800#print "LINE<$line>\n";
2801 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002802 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002803 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002804 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002805 $stat =~ s/\n./\n /g;
2806 $cond =~ s/\n./\n /g;
2807
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002808#print "linenr<$linenr> <$stat>\n";
2809 # If this statement has no statement boundaries within
2810 # it there is no point in retrying a statement scan
2811 # until we hit end of it.
2812 my $frag = $stat; $frag =~ s/;+\s*$//;
2813 if ($frag !~ /(?:{|;)/) {
2814#print "skip<$line_nr_next>\n";
2815 $suppress_statement = $line_nr_next;
2816 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002817
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002818 # Find the real next line.
2819 $realline_next = $line_nr_next;
2820 if (defined $realline_next &&
2821 (!defined $lines[$realline_next - 1] ||
2822 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2823 $realline_next++;
2824 }
2825
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002826 my $s = $stat;
2827 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002828
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002829 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002830 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002831
2832 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002833 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002834
Andy Whitcroft463f2862009-09-21 17:04:34 -07002835 } elsif ($s =~ /^.\s*else\b/s) {
2836
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002837 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002838 } 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 -07002839 my $type = $1;
2840 $type =~ s/\s+/ /g;
2841 possible($type, "A:" . $s);
2842
Andy Whitcroft8905a672007-11-28 16:21:06 -08002843 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002844 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002845 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002846 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002847
2848 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002849 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002850 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002851 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002852
2853 # Check for any sort of function declaration.
2854 # int foo(something bar, other baz);
2855 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002856 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 -08002857 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002858
Andy Whitcroftcf655042008-03-04 14:28:20 -08002859 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002860 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002861 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002862
Andy Whitcroft8905a672007-11-28 16:21:06 -08002863 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002864 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002865
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002866 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002867 }
2868 }
2869 }
2870
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002871 }
2872
Andy Whitcroft653d4872007-06-23 17:16:34 -07002873#
2874# Checks which may be anchored in the context.
2875#
2876
2877# Check for switch () and associated case and default
2878# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002879 if ($line=~/\bswitch\s*\(.*\)/) {
2880 my $err = '';
2881 my $sep = '';
2882 my @ctx = ctx_block_outer($linenr, $realcnt);
2883 shift(@ctx);
2884 for my $ctx (@ctx) {
2885 my ($clen, $cindent) = line_stats($ctx);
2886 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2887 $indent != $cindent) {
2888 $err .= "$sep$ctx\n";
2889 $sep = '';
2890 } else {
2891 $sep = "[...]\n";
2892 }
2893 }
2894 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002895 ERROR("SWITCH_CASE_INDENT_LEVEL",
2896 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002897 }
2898 }
2899
2900# if/while/etc brace do not go on next line, unless defining a do while loop,
2901# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002902 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 -07002903 my $pre_ctx = "$1$2";
2904
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002905 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002906
2907 if ($line =~ /^\+\t{6,}/) {
2908 WARN("DEEP_INDENTATION",
2909 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2910 }
2911
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002912 my $ctx_cnt = $realcnt - $#ctx - 1;
2913 my $ctx = join("\n", @ctx);
2914
Andy Whitcroft548596d2008-07-23 21:29:01 -07002915 my $ctx_ln = $linenr;
2916 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002917
Andy Whitcroft548596d2008-07-23 21:29:01 -07002918 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2919 defined $lines[$ctx_ln - 1] &&
2920 $lines[$ctx_ln - 1] =~ /^-/)) {
2921 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2922 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002923 $ctx_ln++;
2924 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002925
Andy Whitcroft53210162008-07-23 21:29:03 -07002926 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2927 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002928
Joe Perchesd752fcc2014-08-06 16:11:05 -07002929 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002930 ERROR("OPEN_BRACE",
2931 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002932 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002933 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002934 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2935 $ctx =~ /\)\s*\;\s*$/ &&
2936 defined $lines[$ctx_ln - 1])
2937 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002938 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2939 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002940 WARN("TRAILING_SEMICOLON",
2941 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002942 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002943 }
2944 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002945 }
2946
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002947# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002948 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 -08002949 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2950 ctx_statement_block($linenr, $realcnt, 0)
2951 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002952 my ($s, $c) = ($stat, $cond);
2953
2954 substr($s, 0, length($c), '');
2955
2956 # Make sure we remove the line prefixes as we have
2957 # none on the first line, and are going to readd them
2958 # where necessary.
2959 $s =~ s/\n./\n/gs;
2960
2961 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002962 my @newlines = ($c =~ /\n/gs);
2963 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002964
2965 # We want to check the first line inside the block
2966 # starting at the end of the conditional, so remove:
2967 # 1) any blank line termination
2968 # 2) any opening brace { on end of the line
2969 # 3) any do (...) {
2970 my $continuation = 0;
2971 my $check = 0;
2972 $s =~ s/^.*\bdo\b//;
2973 $s =~ s/^\s*{//;
2974 if ($s =~ s/^\s*\\//) {
2975 $continuation = 1;
2976 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002977 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002978 $check = 1;
2979 $cond_lines++;
2980 }
2981
2982 # Also ignore a loop construct at the end of a
2983 # preprocessor statement.
2984 if (($prevline =~ /^.\s*#\s*define\s/ ||
2985 $prevline =~ /\\\s*$/) && $continuation == 0) {
2986 $check = 0;
2987 }
2988
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002989 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002990 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002991 while ($cond_ptr != $cond_lines) {
2992 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002993
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002994 # If we see an #else/#elif then the code
2995 # is not linear.
2996 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2997 $check = 0;
2998 }
2999
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003000 # Ignore:
3001 # 1) blank lines, they should be at 0,
3002 # 2) preprocessor lines, and
3003 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003004 if ($continuation ||
3005 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003006 $s =~ /^\s*#\s*?/ ||
3007 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003008 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003009 if ($s =~ s/^.*?\n//) {
3010 $cond_lines++;
3011 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003012 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003013 }
3014
3015 my (undef, $sindent) = line_stats("+" . $s);
3016 my $stat_real = raw_line($linenr, $cond_lines);
3017
3018 # Check if either of these lines are modified, else
3019 # this is not this patch's fault.
3020 if (!defined($stat_real) ||
3021 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3022 $check = 0;
3023 }
3024 if (defined($stat_real) && $cond_lines > 1) {
3025 $stat_real = "[...]\n$stat_real";
3026 }
3027
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003028 #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 -07003029
3030 if ($check && (($sindent % 8) != 0 ||
3031 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003032 WARN("SUSPECT_CODE_INDENT",
3033 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003034 }
3035 }
3036
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003037 # Track the 'values' across context and added lines.
3038 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003039 my ($curr_values, $curr_vars) =
3040 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003041 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003042 if ($dbg_values) {
3043 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003044 print "$linenr > .$outline\n";
3045 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003046 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003047 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003048 $prev_values = substr($curr_values, -1);
3049
Andy Whitcroft00df3442007-06-08 13:47:06 -07003050#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003051 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003052
Andy Whitcroft653d4872007-06-23 17:16:34 -07003053# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003054 if ($dbg_type) {
3055 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003056 ERROR("TEST_TYPE",
3057 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003058 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003059 ERROR("TEST_NOT_TYPE",
3060 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003061 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003062 next;
3063 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003064# TEST: allow direct testing of the attribute matcher.
3065 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003066 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003067 ERROR("TEST_ATTR",
3068 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003069 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003070 ERROR("TEST_NOT_ATTR",
3071 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003072 }
3073 next;
3074 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003075
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003076# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003077 if ($line =~ /^.\s*{/ &&
3078 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003079 if (ERROR("OPEN_BRACE",
3080 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003081 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3082 fix_delete_line($fixlinenr - 1, $prevrawline);
3083 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003084 my $fixedline = $prevrawline;
3085 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003086 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003087 $fixedline = $line;
3088 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003089 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003090 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003091 }
3092
Andy Whitcroft653d4872007-06-23 17:16:34 -07003093#
3094# Checks which are anchored on the added line.
3095#
3096
3097# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003098 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003099 my $path = $1;
3100 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003101 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003102 "malformed #include filename\n" . $herecurr);
3103 }
3104 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3105 ERROR("UAPI_INCLUDE",
3106 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003107 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003108 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003109
3110# no C99 // comments
3111 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003112 if (ERROR("C99_COMMENTS",
3113 "do not use C99 // comments\n" . $herecurr) &&
3114 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003115 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003116 if ($line =~ /\/\/(.*)$/) {
3117 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003118 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003119 }
3120 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003121 }
3122 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003123 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003124 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003125
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003126# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3127# the whole statement.
3128#print "APW <$lines[$realline_next - 1]>\n";
3129 if (defined $realline_next &&
3130 exists $lines[$realline_next - 1] &&
3131 !defined $suppress_export{$realline_next} &&
3132 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3133 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003134 # Handle definitions which produce identifiers with
3135 # a prefix:
3136 # XXX(foo);
3137 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003138 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003139 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003140 $name =~ /^${Ident}_$2/) {
3141#print "FOO C name<$name>\n";
3142 $suppress_export{$realline_next} = 1;
3143
3144 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003145 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003146 ^.DEFINE_$Ident\(\Q$name\E\)|
3147 ^.DECLARE_$Ident\(\Q$name\E\)|
3148 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003149 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3150 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003151 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003152#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3153 $suppress_export{$realline_next} = 2;
3154 } else {
3155 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003156 }
3157 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003158 if (!defined $suppress_export{$linenr} &&
3159 $prevline =~ /^.\s*$/ &&
3160 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3161 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3162#print "FOO B <$lines[$linenr - 1]>\n";
3163 $suppress_export{$linenr} = 2;
3164 }
3165 if (defined $suppress_export{$linenr} &&
3166 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003167 WARN("EXPORT_SYMBOL",
3168 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003169 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003170
Joe Eloff5150bda2010-08-09 17:21:00 -07003171# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003172 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3173 if (ERROR("GLOBAL_INITIALISERS",
3174 "do not initialise globals to 0 or NULL\n" .
3175 $herecurr) &&
3176 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003177 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003178 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003179 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003180# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003181 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3182 if (ERROR("INITIALISED_STATIC",
3183 "do not initialise statics to 0 or NULL\n" .
3184 $herecurr) &&
3185 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003186 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003187 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003188 }
3189
Joe Perches18130872014-08-06 16:11:22 -07003190# check for misordered declarations of char/short/int/long with signed/unsigned
3191 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3192 my $tmp = trim($1);
3193 WARN("MISORDERED_TYPE",
3194 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3195 }
3196
Joe Perchescb710ec2010-10-26 14:23:20 -07003197# check for static const char * arrays.
3198 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003199 WARN("STATIC_CONST_CHAR_ARRAY",
3200 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003201 $herecurr);
3202 }
3203
3204# check for static char foo[] = "bar" declarations.
3205 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003206 WARN("STATIC_CONST_CHAR_ARRAY",
3207 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003208 $herecurr);
3209 }
3210
Joe Perchesab7e23f2015-04-16 12:44:22 -07003211# check for const <foo> const where <foo> is not a pointer or array type
3212 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3213 my $found = $1;
3214 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3215 WARN("CONST_CONST",
3216 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3217 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3218 WARN("CONST_CONST",
3219 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3220 }
3221 }
3222
Joe Perches9b0fa602014-04-03 14:49:18 -07003223# check for non-global char *foo[] = {"bar", ...} declarations.
3224 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3225 WARN("STATIC_CONST_CHAR_ARRAY",
3226 "char * array declaration might be better as static const\n" .
3227 $herecurr);
3228 }
3229
Joe Perchesb598b672015-04-16 12:44:36 -07003230# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3231 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3232 my $array = $1;
3233 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3234 my $array_div = $1;
3235 if (WARN("ARRAY_SIZE",
3236 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3237 $fix) {
3238 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3239 }
3240 }
3241 }
3242
Joe Perchesb36190c2014-01-27 17:07:18 -08003243# check for function declarations without arguments like "int foo()"
3244 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3245 if (ERROR("FUNCTION_WITHOUT_ARGS",
3246 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3247 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003248 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003249 }
3250 }
3251
Joe Perches92e112f2013-12-13 11:36:22 -07003252# check for uses of DEFINE_PCI_DEVICE_TABLE
3253 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3254 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3255 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3256 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003257 $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 -07003258 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003259 }
3260
Andy Whitcroft653d4872007-06-23 17:16:34 -07003261# check for new typedefs, only function parameters and sparse annotations
3262# make sense.
3263 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003264 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003265 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003266 $line !~ /\b$typeTypedefs\b/ &&
Joe Perches021158b2015-02-13 14:38:43 -08003267 $line !~ /\b$typeOtherOSTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003268 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003269 WARN("NEW_TYPEDEFS",
3270 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003271 }
3272
3273# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003274 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003275 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3276 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003277 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003278
Andy Whitcroft65863862009-01-06 14:41:21 -08003279 # Should start with a space.
3280 $to =~ s/^(\S)/ $1/;
3281 # Should not end with a space.
3282 $to =~ s/\s+$//;
3283 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003284 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003285 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003286
Joe Perches3705ce52013-07-03 15:05:31 -07003287## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003288 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003289 if (ERROR("POINTER_LOCATION",
3290 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3291 $fix) {
3292 my $sub_from = $ident;
3293 my $sub_to = $ident;
3294 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003295 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003296 s@\Q$sub_from\E@$sub_to@;
3297 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003298 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003299 }
3300 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3301 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003302 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003303
Andy Whitcroft65863862009-01-06 14:41:21 -08003304 # Should start with a space.
3305 $to =~ s/^(\S)/ $1/;
3306 # Should not end with a space.
3307 $to =~ s/\s+$//;
3308 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003309 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003310 }
3311 # Modifiers should have spaces.
3312 $to =~ s/(\b$Modifier$)/$1 /;
3313
Joe Perches3705ce52013-07-03 15:05:31 -07003314## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003315 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003316 if (ERROR("POINTER_LOCATION",
3317 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3318 $fix) {
3319
3320 my $sub_from = $match;
3321 my $sub_to = $match;
3322 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003323 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003324 s@\Q$sub_from\E@$sub_to@;
3325 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003326 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003327 }
3328
3329# # no BUG() or BUG_ON()
3330# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3331# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3332# print "$herecurr";
3333# $clean = 0;
3334# }
3335
Andy Whitcroft8905a672007-11-28 16:21:06 -08003336 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003337 WARN("LINUX_VERSION_CODE",
3338 "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 -08003339 }
3340
Joe Perches17441222011-06-15 15:08:17 -07003341# check for uses of printk_ratelimit
3342 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003343 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003344 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003345 }
3346
Andy Whitcroft00df3442007-06-08 13:47:06 -07003347# printk should use KERN_* levels. Note that follow on printk's on the
3348# same line do not need a level, so we use the current block context
3349# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003350# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003351# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003352 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003353 my $ok = 0;
3354 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3355 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003356 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003357 # with "\n" ignore it, else it is to blame
3358 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3359 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3360 $ok = 1;
3361 }
3362 last;
3363 }
3364 }
3365 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003366 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3367 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003368 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003369 }
3370
Joe Perches243f3802012-05-31 16:26:09 -07003371 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3372 my $orig = $1;
3373 my $level = lc($orig);
3374 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003375 my $level2 = $level;
3376 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003377 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003378 "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 -07003379 }
3380
3381 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003382 if (WARN("PREFER_PR_LEVEL",
3383 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3384 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003385 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003386 s/\bpr_warning\b/pr_warn/;
3387 }
Joe Perches243f3802012-05-31 16:26:09 -07003388 }
3389
Joe Perchesdc139312013-02-21 16:44:13 -08003390 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3391 my $orig = $1;
3392 my $level = lc($orig);
3393 $level = "warn" if ($level eq "warning");
3394 $level = "dbg" if ($level eq "debug");
3395 WARN("PREFER_DEV_LEVEL",
3396 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3397 }
3398
Andy Whitcroft653d4872007-06-23 17:16:34 -07003399# function brace can't be on same line, except for #defines of do while,
3400# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003401 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003402 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003403 if (ERROR("OPEN_BRACE",
3404 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3405 $fix) {
3406 fix_delete_line($fixlinenr, $rawline);
3407 my $fixed_line = $rawline;
3408 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3409 my $line1 = $1;
3410 my $line2 = $2;
3411 fix_insert_line($fixlinenr, ltrim($line1));
3412 fix_insert_line($fixlinenr, "\+{");
3413 if ($line2 !~ /^\s*$/) {
3414 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3415 }
3416 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003417 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003418
Andy Whitcroft8905a672007-11-28 16:21:06 -08003419# open braces for enum, union and struct go on the same line.
3420 if ($line =~ /^.\s*{/ &&
3421 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003422 if (ERROR("OPEN_BRACE",
3423 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3424 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3425 fix_delete_line($fixlinenr - 1, $prevrawline);
3426 fix_delete_line($fixlinenr, $rawline);
3427 my $fixedline = rtrim($prevrawline) . " {";
3428 fix_insert_line($fixlinenr, $fixedline);
3429 $fixedline = $rawline;
3430 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3431 if ($fixedline !~ /^\+\s*$/) {
3432 fix_insert_line($fixlinenr, $fixedline);
3433 }
3434 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003435 }
3436
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003437# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003438 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3439 if (WARN("SPACING",
3440 "missing space after $1 definition\n" . $herecurr) &&
3441 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003442 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003443 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3444 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003445 }
3446
Joe Perches31070b52014-01-23 15:54:49 -08003447# Function pointer declarations
3448# check spacing between type, funcptr, and args
3449# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003450 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003451 my $declare = $1;
3452 my $pre_pointer_space = $2;
3453 my $post_pointer_space = $3;
3454 my $funcname = $4;
3455 my $post_funcname_space = $5;
3456 my $pre_args_space = $6;
3457
Joe Perches91f72e92014-04-03 14:49:12 -07003458# the $Declare variable will capture all spaces after the type
3459# so check it for a missing trailing missing space but pointer return types
3460# don't need a space so don't warn for those.
3461 my $post_declare_space = "";
3462 if ($declare =~ /(\s+)$/) {
3463 $post_declare_space = $1;
3464 $declare = rtrim($declare);
3465 }
3466 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003467 WARN("SPACING",
3468 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003469 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003470 }
3471
3472# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003473# This test is not currently implemented because these declarations are
3474# equivalent to
3475# int foo(int bar, ...)
3476# and this is form shouldn't/doesn't generate a checkpatch warning.
3477#
3478# elsif ($declare =~ /\s{2,}$/) {
3479# WARN("SPACING",
3480# "Multiple spaces after return type\n" . $herecurr);
3481# }
Joe Perches31070b52014-01-23 15:54:49 -08003482
3483# unnecessary space "type ( *funcptr)(args...)"
3484 if (defined $pre_pointer_space &&
3485 $pre_pointer_space =~ /^\s/) {
3486 WARN("SPACING",
3487 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3488 }
3489
3490# unnecessary space "type (* funcptr)(args...)"
3491 if (defined $post_pointer_space &&
3492 $post_pointer_space =~ /^\s/) {
3493 WARN("SPACING",
3494 "Unnecessary space before function pointer name\n" . $herecurr);
3495 }
3496
3497# unnecessary space "type (*funcptr )(args...)"
3498 if (defined $post_funcname_space &&
3499 $post_funcname_space =~ /^\s/) {
3500 WARN("SPACING",
3501 "Unnecessary space after function pointer name\n" . $herecurr);
3502 }
3503
3504# unnecessary space "type (*funcptr) (args...)"
3505 if (defined $pre_args_space &&
3506 $pre_args_space =~ /^\s/) {
3507 WARN("SPACING",
3508 "Unnecessary space before function pointer arguments\n" . $herecurr);
3509 }
3510
3511 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003512 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003513 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003514 }
3515 }
3516
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003517# check for spacing round square brackets; allowed:
3518# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003519# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3520# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003521 while ($line =~ /(.*?\s)\[/g) {
3522 my ($where, $prefix) = ($-[1], $1);
3523 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003524 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003525 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003526 if (ERROR("BRACKET_SPACE",
3527 "space prohibited before open square bracket '['\n" . $herecurr) &&
3528 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003529 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003530 s/^(\+.*?)\s+\[/$1\[/;
3531 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003532 }
3533 }
3534
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003535# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003536 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003537 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003538 my $ctx_before = substr($line, 0, $-[1]);
3539 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003540
3541 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003542 if ($name =~ /^(?:
3543 if|for|while|switch|return|case|
3544 volatile|__volatile__|
3545 __attribute__|format|__extension__|
3546 asm|__asm__)$/x)
3547 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003548 # cpp #define statements have non-optional spaces, ie
3549 # if there is a space between the name and the open
3550 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003551 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003552
3553 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003554 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003555
3556 # If this whole things ends with a type its most
3557 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003558 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003559
3560 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003561 if (WARN("SPACING",
3562 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3563 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003564 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003565 s/\b$name\s+\(/$name\(/;
3566 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003567 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003568 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003569
Andy Whitcroft653d4872007-06-23 17:16:34 -07003570# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003571 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003572 my $fixed_line = "";
3573 my $line_fixed = 0;
3574
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003575 my $ops = qr{
3576 <<=|>>=|<=|>=|==|!=|
3577 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3578 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003579 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003580 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003581 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003582 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003583
3584## print("element count: <" . $#elements . ">\n");
3585## foreach my $el (@elements) {
3586## print("el: <$el>\n");
3587## }
3588
3589 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003590 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003591
Joe Perches3705ce52013-07-03 15:05:31 -07003592 foreach my $el (@elements) {
3593 push(@fix_elements, substr($rawline, $off, length($el)));
3594 $off += length($el);
3595 }
3596
3597 $off = 0;
3598
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003599 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003600 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003601
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003602 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003603
3604 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3605
3606## print("n: <$n> good: <$good>\n");
3607
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003608 $off += length($elements[$n]);
3609
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003610 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003611 my $ca = substr($opline, 0, $off);
3612 my $cc = '';
3613 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3614 $cc = substr($opline, $off + length($elements[$n + 1]));
3615 }
3616 my $cb = "$ca$;$cc";
3617
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003618 my $a = '';
3619 $a = 'V' if ($elements[$n] ne '');
3620 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003621 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003622 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3623 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003624 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003625
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003626 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003627
3628 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003629 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003630 $c = 'V' if ($elements[$n + 2] ne '');
3631 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003632 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003633 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3634 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003635 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003636 } else {
3637 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003638 }
3639
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003640 my $ctx = "${a}x${c}";
3641
3642 my $at = "(ctx:$ctx)";
3643
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003644 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003645 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003646
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003647 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003648 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003649
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003650 # Get the full operator variant.
3651 my $opv = $op . substr($curr_vars, $off, 1);
3652
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003653 # Ignore operators passed as parameters.
3654 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07003655 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003656
Andy Whitcroftcf655042008-03-04 14:28:20 -08003657# # Ignore comments
3658# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003659
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003660 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003661 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003662 if ($ctx !~ /.x[WEBC]/ &&
3663 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003664 if (ERROR("SPACING",
3665 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003666 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003667 $line_fixed = 1;
3668 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003669 }
3670
3671 # // is a comment
3672 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003673
Joe Perchesb00e4812014-04-03 14:49:33 -07003674 # : when part of a bitfield
3675 } elsif ($opv eq ':B') {
3676 # skip the bitfield test for now
3677
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003678 # No spaces for:
3679 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003680 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003681 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003682 if (ERROR("SPACING",
3683 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003684 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003685 if (defined $fix_elements[$n + 2]) {
3686 $fix_elements[$n + 2] =~ s/^\s+//;
3687 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003688 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003689 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003690 }
3691
Joe Perches23810972014-12-10 15:51:32 -08003692 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003693 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003694 my $rtrim_before = 0;
3695 my $space_after = 0;
3696 if ($ctx =~ /Wx./) {
3697 if (ERROR("SPACING",
3698 "space prohibited before that '$op' $at\n" . $hereptr)) {
3699 $line_fixed = 1;
3700 $rtrim_before = 1;
3701 }
3702 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003703 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003704 if (ERROR("SPACING",
3705 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003706 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003707 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003708 $space_after = 1;
3709 }
3710 }
3711 if ($rtrim_before || $space_after) {
3712 if ($rtrim_before) {
3713 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3714 } else {
3715 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3716 }
3717 if ($space_after) {
3718 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003719 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003720 }
3721
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003722 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003723 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003724 #warn "'*' is part of type\n";
3725
3726 # unary operators should have a space before and
3727 # none after. May be left adjacent to another
3728 # unary operator, or a cast
3729 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003730 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003731 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003732 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003733 if (ERROR("SPACING",
3734 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003735 if ($n != $last_after + 2) {
3736 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3737 $line_fixed = 1;
3738 }
Joe Perches3705ce52013-07-03 15:05:31 -07003739 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003740 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003741 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003742 # A unary '*' may be const
3743
3744 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003745 if (ERROR("SPACING",
3746 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003747 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003748 if (defined $fix_elements[$n + 2]) {
3749 $fix_elements[$n + 2] =~ s/^\s+//;
3750 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003751 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003752 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003753 }
3754
3755 # unary ++ and unary -- are allowed no space on one side.
3756 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003757 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003758 if (ERROR("SPACING",
3759 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003760 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003761 $line_fixed = 1;
3762 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003763 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003764 if ($ctx =~ /Wx[BE]/ ||
3765 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003766 if (ERROR("SPACING",
3767 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003768 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003769 $line_fixed = 1;
3770 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003771 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003772 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003773 if (ERROR("SPACING",
3774 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003775 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003776 if (defined $fix_elements[$n + 2]) {
3777 $fix_elements[$n + 2] =~ s/^\s+//;
3778 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003779 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003780 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003781 }
3782
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003783 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003784 } elsif ($op eq '<<' or $op eq '>>' or
3785 $op eq '&' or $op eq '^' or $op eq '|' or
3786 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003787 $op eq '*' or $op eq '/' or
3788 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003789 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003790 if ($check) {
3791 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3792 if (CHK("SPACING",
3793 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3794 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3795 $fix_elements[$n + 2] =~ s/^\s+//;
3796 $line_fixed = 1;
3797 }
3798 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3799 if (CHK("SPACING",
3800 "space preferred before that '$op' $at\n" . $hereptr)) {
3801 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3802 $line_fixed = 1;
3803 }
3804 }
3805 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003806 if (ERROR("SPACING",
3807 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003808 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3809 if (defined $fix_elements[$n + 2]) {
3810 $fix_elements[$n + 2] =~ s/^\s+//;
3811 }
Joe Perches3705ce52013-07-03 15:05:31 -07003812 $line_fixed = 1;
3813 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003814 }
3815
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003816 # A colon needs no spaces before when it is
3817 # terminating a case value or a label.
3818 } elsif ($opv eq ':C' || $opv eq ':L') {
3819 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003820 if (ERROR("SPACING",
3821 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003822 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003823 $line_fixed = 1;
3824 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003825 }
3826
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003827 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003828 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003829 my $ok = 0;
3830
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003831 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003832 if (($op eq '<' &&
3833 $cc =~ /^\S+\@\S+>/) ||
3834 ($op eq '>' &&
3835 $ca =~ /<\S+\@\S+$/))
3836 {
3837 $ok = 1;
3838 }
3839
Joe Perches84731622013-11-12 15:10:05 -08003840 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003841 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003842 my $msg_type = \&ERROR;
3843 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3844
3845 if (&{$msg_type}("SPACING",
3846 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003847 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3848 if (defined $fix_elements[$n + 2]) {
3849 $fix_elements[$n + 2] =~ s/^\s+//;
3850 }
Joe Perches3705ce52013-07-03 15:05:31 -07003851 $line_fixed = 1;
3852 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003853 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003854 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003855 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003856
3857## print("n: <$n> GOOD: <$good>\n");
3858
3859 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003860 }
Joe Perches3705ce52013-07-03 15:05:31 -07003861
3862 if (($#elements % 2) == 0) {
3863 $fixed_line = $fixed_line . $fix_elements[$#elements];
3864 }
3865
Joe Perches194f66f2014-08-06 16:11:03 -07003866 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3867 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003868 }
3869
3870
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003871 }
3872
Joe Perches786b6322013-07-03 15:05:32 -07003873# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003874 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003875 if (WARN("SPACING",
3876 "space prohibited before semicolon\n" . $herecurr) &&
3877 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003878 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003879 s/^(\+.*\S)\s+;/$1;/;
3880 }
3881 }
3882
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003883# check for multiple assignments
3884 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003885 CHK("MULTIPLE_ASSIGNMENTS",
3886 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003887 }
3888
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003889## # check for multiple declarations, allowing for a function declaration
3890## # continuation.
3891## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3892## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3893##
3894## # Remove any bracketed sections to ensure we do not
3895## # falsly report the parameters of functions.
3896## my $ln = $line;
3897## while ($ln =~ s/\([^\(\)]*\)//g) {
3898## }
3899## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003900## WARN("MULTIPLE_DECLARATION",
3901## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003902## }
3903## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003904
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003905#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003906 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3907 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003908 if (ERROR("SPACING",
3909 "space required before the open brace '{'\n" . $herecurr) &&
3910 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003911 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003912 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003913 }
3914
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003915## # check for blank lines before declarations
3916## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3917## $prevrawline =~ /^.\s*$/) {
3918## WARN("SPACING",
3919## "No blank lines before declarations\n" . $hereprev);
3920## }
3921##
3922
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003923# closing brace should have a space following it when it has anything
3924# on the line
3925 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003926 if (ERROR("SPACING",
3927 "space required after that close brace '}'\n" . $herecurr) &&
3928 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003929 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003930 s/}((?!(?:,|;|\)))\S)/} $1/;
3931 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003932 }
3933
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003934# check spacing on square brackets
3935 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003936 if (ERROR("SPACING",
3937 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3938 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003939 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003940 s/\[\s+/\[/;
3941 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003942 }
3943 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003944 if (ERROR("SPACING",
3945 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3946 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003947 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003948 s/\s+\]/\]/;
3949 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003950 }
3951
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003952# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003953 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3954 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003955 if (ERROR("SPACING",
3956 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3957 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003958 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003959 s/\(\s+/\(/;
3960 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003961 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003962 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003963 $line !~ /for\s*\(.*;\s+\)/ &&
3964 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003965 if (ERROR("SPACING",
3966 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3967 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003968 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003969 s/\s+\)/\)/;
3970 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003971 }
3972
Joe Perchese2826fd2014-08-06 16:10:48 -07003973# check unnecessary parentheses around addressof/dereference single $Lvals
3974# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3975
3976 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003977 my $var = $1;
3978 if (CHK("UNNECESSARY_PARENTHESES",
3979 "Unnecessary parentheses around $var\n" . $herecurr) &&
3980 $fix) {
3981 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3982 }
3983 }
3984
3985# check for unnecessary parentheses around function pointer uses
3986# ie: (foo->bar)(); should be foo->bar();
3987# but not "if (foo->bar) (" to avoid some false positives
3988 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3989 my $var = $2;
3990 if (CHK("UNNECESSARY_PARENTHESES",
3991 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3992 $fix) {
3993 my $var2 = deparenthesize($var);
3994 $var2 =~ s/\s//g;
3995 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3996 }
3997 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003998
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003999#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004000 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004001 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004002 if (WARN("INDENTED_LABEL",
4003 "labels should not be indented\n" . $herecurr) &&
4004 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004005 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004006 s/^(.)\s+/$1/;
4007 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004008 }
4009
Joe Perches5b9553a2014-04-03 14:49:21 -07004010# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004011 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004012 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004013 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004014 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4015 my $value = $1;
4016 $value = deparenthesize($value);
4017 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4018 ERROR("RETURN_PARENTHESES",
4019 "return is not a function, parentheses are not required\n" . $herecurr);
4020 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004021 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004022 ERROR("SPACING",
4023 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004024 }
4025 }
Joe Perches507e5142013-11-12 15:10:13 -08004026
Joe Perchesb43ae212014-06-23 13:22:07 -07004027# unnecessary return in a void function
4028# at end-of-function, with the previous line a single leading tab, then return;
4029# and the line before that not a goto label target like "out:"
4030 if ($sline =~ /^[ \+]}\s*$/ &&
4031 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4032 $linenr >= 3 &&
4033 $lines[$linenr - 3] =~ /^[ +]/ &&
4034 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004035 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004036 "void function return statements are not generally useful\n" . $hereprev);
4037 }
Joe Perches9819cf22014-06-04 16:12:09 -07004038
Joe Perches189248d2014-01-23 15:54:47 -08004039# if statements using unnecessary parentheses - ie: if ((foo == bar))
4040 if ($^V && $^V ge 5.10.0 &&
4041 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4042 my $openparens = $1;
4043 my $count = $openparens =~ tr@\(@\(@;
4044 my $msg = "";
4045 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4046 my $comp = $4; #Not $1 because of $LvalOrFunc
4047 $msg = " - maybe == should be = ?" if ($comp eq "==");
4048 WARN("UNNECESSARY_PARENTHESES",
4049 "Unnecessary parentheses$msg\n" . $herecurr);
4050 }
4051 }
4052
Joe Perchesf34e4a42015-04-16 12:44:19 -07004053# Return of what appears to be an errno should normally be negative
4054 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004055 my $name = $1;
4056 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004057 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004058 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004059 }
4060 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004061
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004062# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004063 if ($line =~ /\b(if|while|for|switch)\(/) {
4064 if (ERROR("SPACING",
4065 "space required before the open parenthesis '('\n" . $herecurr) &&
4066 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004067 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004068 s/\b(if|while|for|switch)\(/$1 \(/;
4069 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004070 }
4071
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004072# Check for illegal assignment in if conditional -- and check for trailing
4073# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004074 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004075 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4076 ctx_statement_block($linenr, $realcnt, 0)
4077 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004078 my ($stat_next) = ctx_statement_block($line_nr_next,
4079 $remain_next, $off_next);
4080 $stat_next =~ s/\n./\n /g;
4081 ##print "stat<$stat> stat_next<$stat_next>\n";
4082
4083 if ($stat_next =~ /^\s*while\b/) {
4084 # If the statement carries leading newlines,
4085 # then count those as offsets.
4086 my ($whitespace) =
4087 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4088 my $offset =
4089 statement_rawlines($whitespace) - 1;
4090
4091 $suppress_whiletrailers{$line_nr_next +
4092 $offset} = 1;
4093 }
4094 }
4095 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004096 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004097 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004098 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004099
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004100 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004101 ERROR("ASSIGN_IN_IF",
4102 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004103 }
4104
4105 # Find out what is on the end of the line after the
4106 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004107 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004108 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004109 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004110 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4111 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004112 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004113 # Find out how long the conditional actually is.
4114 my @newlines = ($c =~ /\n/gs);
4115 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004116 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004117
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004118 $stat_real = raw_line($linenr, $cond_lines)
4119 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004120 if (defined($stat_real) && $cond_lines > 1) {
4121 $stat_real = "[...]\n$stat_real";
4122 }
4123
Joe Perches000d1cc12011-07-25 17:13:25 -07004124 ERROR("TRAILING_STATEMENTS",
4125 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004126 }
4127 }
4128
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004129# Check for bitwise tests written as boolean
4130 if ($line =~ /
4131 (?:
4132 (?:\[|\(|\&\&|\|\|)
4133 \s*0[xX][0-9]+\s*
4134 (?:\&\&|\|\|)
4135 |
4136 (?:\&\&|\|\|)
4137 \s*0[xX][0-9]+\s*
4138 (?:\&\&|\|\||\)|\])
4139 )/x)
4140 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004141 WARN("HEXADECIMAL_BOOLEAN_TEST",
4142 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004143 }
4144
Andy Whitcroft8905a672007-11-28 16:21:06 -08004145# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004146 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4147 my $s = $1;
4148 $s =~ s/$;//g; # Remove any comments
4149 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004150 ERROR("TRAILING_STATEMENTS",
4151 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004152 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004153 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004154# if should not continue a brace
4155 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004156 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004157 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004158 $herecurr);
4159 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004160# case and default should not have general statements after them
4161 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4162 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004163 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004164 \s*return\s+
4165 )/xg)
4166 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004167 ERROR("TRAILING_STATEMENTS",
4168 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004169 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004170
4171 # Check for }<nl>else {, these must be at the same
4172 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004173 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4174 $previndent == $indent) {
4175 if (ERROR("ELSE_AFTER_BRACE",
4176 "else should follow close brace '}'\n" . $hereprev) &&
4177 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4178 fix_delete_line($fixlinenr - 1, $prevrawline);
4179 fix_delete_line($fixlinenr, $rawline);
4180 my $fixedline = $prevrawline;
4181 $fixedline =~ s/}\s*$//;
4182 if ($fixedline !~ /^\+\s*$/) {
4183 fix_insert_line($fixlinenr, $fixedline);
4184 }
4185 $fixedline = $rawline;
4186 $fixedline =~ s/^(.\s*)else/$1} else/;
4187 fix_insert_line($fixlinenr, $fixedline);
4188 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004189 }
4190
Joe Perches8b8856f2014-08-06 16:11:14 -07004191 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4192 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004193 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4194
4195 # Find out what is on the end of the line after the
4196 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004197 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004198 $s =~ s/\n.*//g;
4199
4200 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004201 if (ERROR("WHILE_AFTER_BRACE",
4202 "while should follow close brace '}'\n" . $hereprev) &&
4203 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4204 fix_delete_line($fixlinenr - 1, $prevrawline);
4205 fix_delete_line($fixlinenr, $rawline);
4206 my $fixedline = $prevrawline;
4207 my $trailing = $rawline;
4208 $trailing =~ s/^\+//;
4209 $trailing = trim($trailing);
4210 $fixedline =~ s/}\s*$/} $trailing/;
4211 fix_insert_line($fixlinenr, $fixedline);
4212 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004213 }
4214 }
4215
Joe Perches95e2c602013-07-03 15:05:20 -07004216#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004217 while ($line =~ m{($Constant|$Lval)}g) {
4218 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004219
4220#gcc binary extension
4221 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004222 if (WARN("GCC_BINARY_CONSTANT",
4223 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4224 $fix) {
4225 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004226 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004227 s/\b$var\b/$hexval/;
4228 }
Joe Perches95e2c602013-07-03 15:05:20 -07004229 }
4230
4231#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004232 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004233 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004234#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004235 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004236#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004237 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4238#Ignore some three character SI units explicitly, like MiB and KHz
4239 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004240 while ($var =~ m{($Ident)}g) {
4241 my $word = $1;
4242 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004243 if ($check) {
4244 seed_camelcase_includes();
4245 if (!$file && !$camelcase_file_seeded) {
4246 seed_camelcase_file($realfile);
4247 $camelcase_file_seeded = 1;
4248 }
4249 }
Joe Perches7e781f62013-09-11 14:23:55 -07004250 if (!defined $camelcase{$word}) {
4251 $camelcase{$word} = 1;
4252 CHK("CAMELCASE",
4253 "Avoid CamelCase: <$word>\n" . $herecurr);
4254 }
Joe Perches34456862013-07-03 15:05:34 -07004255 }
Joe Perches323c1262012-12-17 16:02:07 -08004256 }
4257 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004258
4259#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004260 if ($line =~ /\#\s*define.*\\\s+$/) {
4261 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4262 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4263 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004264 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004265 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004266 }
4267
Fabian Frederick0e212e02015-04-16 12:44:25 -07004268# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4269# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004270 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004271 my $file = "$1.h";
4272 my $checkfile = "include/linux/$file";
4273 if (-f "$root/$checkfile" &&
4274 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004275 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004276 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004277 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4278 if ($asminclude > 0) {
4279 if ($realfile =~ m{^arch/}) {
4280 CHK("ARCH_INCLUDE_LINUX",
4281 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4282 } else {
4283 WARN("INCLUDE_LINUX",
4284 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4285 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004286 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004287 }
4288 }
4289
Andy Whitcroft653d4872007-06-23 17:16:34 -07004290# multi-statement macros should be enclosed in a do while loop, grab the
4291# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004292# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004293 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4294 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004295 my $ln = $linenr;
4296 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004297 my ($off, $dstat, $dcond, $rest);
4298 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004299 my $has_flow_statement = 0;
4300 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004301 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004302 ctx_statement_block($linenr, $realcnt, 0);
4303 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004304 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004305 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004306
Joe Perches08a28432014-10-13 15:51:55 -07004307 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4308 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4309
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004310 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004311 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004312 $dstat =~ s/\\\n.//g;
4313 $dstat =~ s/^\s*//s;
4314 $dstat =~ s/\s*$//s;
4315
4316 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004317 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4318 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004319 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004320 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004321 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004322
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004323 # Flatten any obvious string concatentation.
4324 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4325 $dstat =~ s/$Ident\s*("X*")/$1/)
4326 {
4327 }
4328
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004329 my $exceptions = qr{
4330 $Declare|
4331 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004332 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004333 DECLARE_PER_CPU|
4334 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004335 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004336 union|
4337 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004338 \.$Ident\s*=\s*|
4339 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004340 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004341 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004342 if ($dstat ne '' &&
4343 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4344 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004345 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004346 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004347 $dstat !~ /$exceptions/ &&
4348 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004349 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004350 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004351 $dstat !~ /^for\s*$Constant$/ && # for (...)
4352 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4353 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004354 $dstat !~ /^\({/ && # ({...
4355 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004356 {
4357 $ctx =~ s/\n*$//;
4358 my $herectx = $here . "\n";
4359 my $cnt = statement_rawlines($ctx);
4360
4361 for (my $n = 0; $n < $cnt; $n++) {
4362 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004363 }
4364
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004365 if ($dstat =~ /;/) {
4366 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4367 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4368 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004369 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004370 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004371 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004372 }
Joe Perches5023d342012-12-17 16:01:47 -08004373
Joe Perches08a28432014-10-13 15:51:55 -07004374# check for macros with flow control, but without ## concatenation
4375# ## concatenation is commonly a macro that defines a function so ignore those
4376 if ($has_flow_statement && !$has_arg_concat) {
4377 my $herectx = $here . "\n";
4378 my $cnt = statement_rawlines($ctx);
4379
4380 for (my $n = 0; $n < $cnt; $n++) {
4381 $herectx .= raw_line($linenr, $n) . "\n";
4382 }
4383 WARN("MACRO_WITH_FLOW_CONTROL",
4384 "Macros with flow control statements should be avoided\n" . "$herectx");
4385 }
4386
Joe Perches481eb482012-12-17 16:01:56 -08004387# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004388
4389 } else {
4390 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004391 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4392 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004393 $line =~ /^\+.*\\$/) {
4394 WARN("LINE_CONTINUATIONS",
4395 "Avoid unnecessary line continuations\n" . $herecurr);
4396 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004397 }
4398
Joe Perchesb13edf72012-07-30 14:41:24 -07004399# do {} while (0) macro tests:
4400# single-statement macros do not need to be enclosed in do while (0) loop,
4401# macro should not end with a semicolon
4402 if ($^V && $^V ge 5.10.0 &&
4403 $realfile !~ m@/vmlinux.lds.h$@ &&
4404 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4405 my $ln = $linenr;
4406 my $cnt = $realcnt;
4407 my ($off, $dstat, $dcond, $rest);
4408 my $ctx = '';
4409 ($dstat, $dcond, $ln, $cnt, $off) =
4410 ctx_statement_block($linenr, $realcnt, 0);
4411 $ctx = $dstat;
4412
4413 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004414 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004415
4416 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4417 my $stmts = $2;
4418 my $semis = $3;
4419
4420 $ctx =~ s/\n*$//;
4421 my $cnt = statement_rawlines($ctx);
4422 my $herectx = $here . "\n";
4423
4424 for (my $n = 0; $n < $cnt; $n++) {
4425 $herectx .= raw_line($linenr, $n) . "\n";
4426 }
4427
Joe Perchesac8e97f2012-08-21 16:15:53 -07004428 if (($stmts =~ tr/;/;/) == 1 &&
4429 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004430 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4431 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4432 }
4433 if (defined $semis && $semis ne "") {
4434 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4435 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4436 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004437 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4438 $ctx =~ s/\n*$//;
4439 my $cnt = statement_rawlines($ctx);
4440 my $herectx = $here . "\n";
4441
4442 for (my $n = 0; $n < $cnt; $n++) {
4443 $herectx .= raw_line($linenr, $n) . "\n";
4444 }
4445
4446 WARN("TRAILING_SEMICOLON",
4447 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004448 }
4449 }
4450
Mike Frysinger080ba922009-01-06 14:41:25 -08004451# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4452# all assignments may have only one of the following with an assignment:
4453# .
4454# ALIGN(...)
4455# VMLINUX_SYMBOL(...)
4456 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004457 WARN("MISSING_VMLINUX_SYMBOL",
4458 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004459 }
4460
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004461# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004462 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4463 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004464 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004465 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004466 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4467 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004468 my @allowed = ();
4469 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004470 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004471 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004472 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004473 for my $chunk (@chunks) {
4474 my ($cond, $block) = @{$chunk};
4475
Andy Whitcroft773647a2008-03-28 14:15:58 -07004476 # If the condition carries leading newlines, then count those as offsets.
4477 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4478 my $offset = statement_rawlines($whitespace) - 1;
4479
Joe Perchesaad4f612012-03-23 15:02:19 -07004480 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004481 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4482
4483 # We have looked at and allowed this specific line.
4484 $suppress_ifbraces{$ln + $offset} = 1;
4485
4486 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004487 $ln += statement_rawlines($block) - 1;
4488
Andy Whitcroft773647a2008-03-28 14:15:58 -07004489 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004490
4491 $seen++ if ($block =~ /^\s*{/);
4492
Joe Perchesaad4f612012-03-23 15:02:19 -07004493 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004494 if (statement_lines($cond) > 1) {
4495 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004496 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004497 }
4498 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004499 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004500 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004501 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004502 if (statement_block_size($block) > 1) {
4503 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004504 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004505 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004506 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004507 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004508 if ($seen) {
4509 my $sum_allowed = 0;
4510 foreach (@allowed) {
4511 $sum_allowed += $_;
4512 }
4513 if ($sum_allowed == 0) {
4514 WARN("BRACES",
4515 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4516 } elsif ($sum_allowed != $allow &&
4517 $seen != $allow) {
4518 CHK("BRACES",
4519 "braces {} should be used on all arms of this statement\n" . $herectx);
4520 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004521 }
4522 }
4523 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004524 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004525 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004526 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004527
Andy Whitcroftcf655042008-03-04 14:28:20 -08004528 # Check the pre-context.
4529 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4530 #print "APW: ALLOWED: pre<$1>\n";
4531 $allowed = 1;
4532 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004533
4534 my ($level, $endln, @chunks) =
4535 ctx_statement_full($linenr, $realcnt, $-[0]);
4536
Andy Whitcroftcf655042008-03-04 14:28:20 -08004537 # Check the condition.
4538 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004539 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004540 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004541 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004542 }
4543 if (statement_lines($cond) > 1) {
4544 #print "APW: ALLOWED: cond<$cond>\n";
4545 $allowed = 1;
4546 }
4547 if ($block =~/\b(?:if|for|while)\b/) {
4548 #print "APW: ALLOWED: block<$block>\n";
4549 $allowed = 1;
4550 }
4551 if (statement_block_size($block) > 1) {
4552 #print "APW: ALLOWED: lines block<$block>\n";
4553 $allowed = 1;
4554 }
4555 # Check the post-context.
4556 if (defined $chunks[1]) {
4557 my ($cond, $block) = @{$chunks[1]};
4558 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004559 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004560 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004561 if ($block =~ /^\s*\{/) {
4562 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4563 $allowed = 1;
4564 }
4565 }
4566 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004567 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004568 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004569
Andy Whitcroftf0556632008-10-15 22:02:23 -07004570 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004571 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004572 }
4573
Joe Perches000d1cc12011-07-25 17:13:25 -07004574 WARN("BRACES",
4575 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004576 }
4577 }
4578
Joe Perches0979ae62012-12-17 16:01:59 -08004579# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004580 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004581 if (CHK("BRACES",
4582 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4583 $fix && $prevrawline =~ /^\+/) {
4584 fix_delete_line($fixlinenr - 1, $prevrawline);
4585 }
Joe Perches0979ae62012-12-17 16:01:59 -08004586 }
Joe Perches77b9a532013-07-03 15:05:29 -07004587 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004588 if (CHK("BRACES",
4589 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4590 $fix) {
4591 fix_delete_line($fixlinenr, $rawline);
4592 }
Joe Perches0979ae62012-12-17 16:01:59 -08004593 }
4594
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004595# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004596 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4597 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004598 WARN("VOLATILE",
4599 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004600 }
4601
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004602# Check for user-visible strings broken across lines, which breaks the ability
4603# to grep for the string. Make exceptions when the previous string ends in a
4604# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4605# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4606 if ($line =~ /^\+\s*"[X\t]*"/ &&
4607 $prevline =~ /"\s*$/ &&
4608 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4609 if (WARN("SPLIT_STRING",
4610 "quoted string split across lines\n" . $hereprev) &&
4611 $fix &&
4612 $prevrawline =~ /^\+.*"\s*$/ &&
4613 $last_coalesced_string_linenr != $linenr - 1) {
4614 my $extracted_string = get_quoted_string($line, $rawline);
4615 my $comma_close = "";
4616 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4617 $comma_close = $1;
4618 }
4619
4620 fix_delete_line($fixlinenr - 1, $prevrawline);
4621 fix_delete_line($fixlinenr, $rawline);
4622 my $fixedline = $prevrawline;
4623 $fixedline =~ s/"\s*$//;
4624 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4625 fix_insert_line($fixlinenr - 1, $fixedline);
4626 $fixedline = $rawline;
4627 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4628 if ($fixedline !~ /\+\s*$/) {
4629 fix_insert_line($fixlinenr, $fixedline);
4630 }
4631 $last_coalesced_string_linenr = $linenr;
4632 }
4633 }
4634
4635# check for missing a space in a string concatenation
4636 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4637 WARN('MISSING_SPACE',
4638 "break quoted strings at a space character\n" . $hereprev);
4639 }
4640
4641# check for spaces before a quoted newline
4642 if ($rawline =~ /^.*\".*\s\\n/) {
4643 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4644 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4645 $fix) {
4646 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4647 }
4648
4649 }
4650
Joe Perchesf17dba42014-10-13 15:51:51 -07004651# concatenated string without spaces between elements
4652 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4653 CHK("CONCATENATED_STRING",
4654 "Concatenated strings should use spaces between elements\n" . $herecurr);
4655 }
4656
Joe Perches90ad30e2014-12-10 15:51:59 -08004657# uncoalesced string fragments
4658 if ($line =~ /"X*"\s*"/) {
4659 WARN("STRING_FRAGMENTS",
4660 "Consecutive strings are generally better as a single string\n" . $herecurr);
4661 }
4662
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004663# check for %L{u,d,i} in strings
4664 my $string;
4665 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4666 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4667 $string =~ s/%%/__/g;
4668 if ($string =~ /(?<!%)%L[udi]/) {
4669 WARN("PRINTF_L",
4670 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4671 last;
4672 }
4673 }
4674
4675# check for line continuations in quoted strings with odd counts of "
4676 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4677 WARN("LINE_CONTINUATIONS",
4678 "Avoid line continuations in quoted strings\n" . $herecurr);
4679 }
4680
Andy Whitcroft00df3442007-06-08 13:47:06 -07004681# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004682 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004683 CHK("REDUNDANT_CODE",
4684 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004685 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004686 }
4687
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004688# check for needless "if (<foo>) fn(<foo>)" uses
4689 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4690 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4691 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4692 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004693 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004694 }
4695 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004696
Joe Perchesebfdc402014-08-06 16:10:27 -07004697# check for unnecessary "Out of Memory" messages
4698 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4699 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4700 (defined $1 || defined $3) &&
4701 $linenr > 3) {
4702 my $testval = $2;
4703 my $testline = $lines[$linenr - 3];
4704
4705 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4706# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4707
4708 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4709 WARN("OOM_MESSAGE",
4710 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4711 }
4712 }
4713
Joe Perchesf78d98f2014-10-13 15:52:01 -07004714# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004715 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004716 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4717 my $level = $1;
4718 if (WARN("UNNECESSARY_KERN_LEVEL",
4719 "Possible unnecessary $level\n" . $herecurr) &&
4720 $fix) {
4721 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4722 }
4723 }
4724
Joe Perchesabb08a52014-12-10 15:51:46 -08004725# check for mask then right shift without a parentheses
4726 if ($^V && $^V ge 5.10.0 &&
4727 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4728 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4729 WARN("MASK_THEN_SHIFT",
4730 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4731 }
4732
Joe Perchesb75ac612014-12-10 15:52:02 -08004733# check for pointer comparisons to NULL
4734 if ($^V && $^V ge 5.10.0) {
4735 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4736 my $val = $1;
4737 my $equal = "!";
4738 $equal = "" if ($4 eq "!=");
4739 if (CHK("COMPARISON_TO_NULL",
4740 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4741 $fix) {
4742 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4743 }
4744 }
4745 }
4746
Joe Perches8716de32013-09-11 14:24:05 -07004747# check for bad placement of section $InitAttribute (e.g.: __initdata)
4748 if ($line =~ /(\b$InitAttribute\b)/) {
4749 my $attr = $1;
4750 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4751 my $ptr = $1;
4752 my $var = $2;
4753 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4754 ERROR("MISPLACED_INIT",
4755 "$attr should be placed after $var\n" . $herecurr)) ||
4756 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4757 WARN("MISPLACED_INIT",
4758 "$attr should be placed after $var\n" . $herecurr))) &&
4759 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004760 $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 -07004761 }
4762 }
4763 }
4764
Joe Perchese970b8842013-11-12 15:10:10 -08004765# check for $InitAttributeData (ie: __initdata) with const
4766 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4767 my $attr = $1;
4768 $attr =~ /($InitAttributePrefix)(.*)/;
4769 my $attr_prefix = $1;
4770 my $attr_type = $2;
4771 if (ERROR("INIT_ATTRIBUTE",
4772 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4773 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004774 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004775 s/$InitAttributeData/${attr_prefix}initconst/;
4776 }
4777 }
4778
4779# check for $InitAttributeConst (ie: __initconst) without const
4780 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4781 my $attr = $1;
4782 if (ERROR("INIT_ATTRIBUTE",
4783 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4784 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004785 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004786 /(^\+\s*(?:static\s+))/;
4787 $lead = rtrim($1);
4788 $lead = "$lead " if ($lead !~ /^\+$/);
4789 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004790 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004791 }
4792 }
4793
Joe Perchesfbdb8132014-04-03 14:49:14 -07004794# don't use __constant_<foo> functions outside of include/uapi/
4795 if ($realfile !~ m@^include/uapi/@ &&
4796 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4797 my $constant_func = $1;
4798 my $func = $constant_func;
4799 $func =~ s/^__constant_//;
4800 if (WARN("CONSTANT_CONVERSION",
4801 "$constant_func should be $func\n" . $herecurr) &&
4802 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004803 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004804 }
4805 }
4806
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004807# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004808 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004809 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004810 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004811 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004812 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004813 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4814 }
4815 if ($delay > 2000) {
4816 WARN("LONG_UDELAY",
4817 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004818 }
4819 }
4820
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004821# warn about unexpectedly long msleep's
4822 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4823 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004824 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004825 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004826 }
4827 }
4828
Joe Perches36ec1932013-07-03 15:05:25 -07004829# check for comparisons of jiffies
4830 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4831 WARN("JIFFIES_COMPARISON",
4832 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4833 }
4834
Joe Perches9d7a34a2013-07-03 15:05:26 -07004835# check for comparisons of get_jiffies_64()
4836 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4837 WARN("JIFFIES_COMPARISON",
4838 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4839 }
4840
Andy Whitcroft00df3442007-06-08 13:47:06 -07004841# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004842# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004843# print "#ifdef in C files should be avoided\n";
4844# print "$herecurr";
4845# $clean = 0;
4846# }
4847
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004848# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004849 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004850 if (ERROR("SPACING",
4851 "exactly one space required after that #$1\n" . $herecurr) &&
4852 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004853 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004854 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4855 }
4856
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004857 }
4858
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004859# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004860 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4861 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004862 my $which = $1;
4863 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004864 CHK("UNCOMMENTED_DEFINITION",
4865 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004866 }
4867 }
4868# check for memory barriers without a comment.
4869 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4870 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004871 WARN("MEMORY_BARRIER",
4872 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004873 }
4874 }
4875# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004876 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004877 CHK("ARCH_DEFINES",
4878 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004879 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004880
Tobias Klauserd4977c72010-05-24 14:33:30 -07004881# Check that the storage class is at the beginning of a declaration
4882 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004883 WARN("STORAGE_CLASS",
4884 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004885 }
4886
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004887# check the location of the inline attribute, that it is between
4888# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004889 if ($line =~ /\b$Type\s+$Inline\b/ ||
4890 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004891 ERROR("INLINE_LOCATION",
4892 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004893 }
4894
Andy Whitcroft8905a672007-11-28 16:21:06 -08004895# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004896 if ($realfile !~ m@\binclude/uapi/@ &&
4897 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004898 if (WARN("INLINE",
4899 "plain inline is preferred over $1\n" . $herecurr) &&
4900 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004901 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004902
4903 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004904 }
4905
Joe Perches3d130fd2011-01-12 17:00:00 -08004906# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004907 if ($realfile !~ m@\binclude/uapi/@ &&
4908 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004909 WARN("PREFER_PACKED",
4910 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004911 }
4912
Joe Perches39b7e282011-07-25 17:13:24 -07004913# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004914 if ($realfile !~ m@\binclude/uapi/@ &&
4915 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004916 WARN("PREFER_ALIGNED",
4917 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004918 }
4919
Joe Perches5f14d3b2012-01-10 15:09:52 -08004920# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004921 if ($realfile !~ m@\binclude/uapi/@ &&
4922 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004923 if (WARN("PREFER_PRINTF",
4924 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4925 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004926 $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 -07004927
4928 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004929 }
4930
Joe Perches6061d942012-03-23 15:02:16 -07004931# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004932 if ($realfile !~ m@\binclude/uapi/@ &&
4933 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004934 if (WARN("PREFER_SCANF",
4935 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4936 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004937 $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 -07004938 }
Joe Perches6061d942012-03-23 15:02:16 -07004939 }
4940
Joe Perches619a9082014-12-10 15:51:35 -08004941# Check for __attribute__ weak, or __weak declarations (may have link issues)
4942 if ($^V && $^V ge 5.10.0 &&
4943 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4944 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4945 $line =~ /\b__weak\b/)) {
4946 ERROR("WEAK_DECLARATION",
4947 "Using weak declarations can have unintended link defects\n" . $herecurr);
4948 }
4949
Joe Perches8f53a9b2010-03-05 13:43:48 -08004950# check for sizeof(&)
4951 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004952 WARN("SIZEOF_ADDRESS",
4953 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004954 }
4955
Joe Perches66c80b62012-07-30 14:41:22 -07004956# check for sizeof without parenthesis
4957 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004958 if (WARN("SIZEOF_PARENTHESIS",
4959 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4960 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004961 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004962 }
Joe Perches66c80b62012-07-30 14:41:22 -07004963 }
4964
Joe Perches88982fe2012-12-17 16:02:00 -08004965# check for struct spinlock declarations
4966 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4967 WARN("USE_SPINLOCK_T",
4968 "struct spinlock should be spinlock_t\n" . $herecurr);
4969 }
4970
Joe Perchesa6962d72013-04-29 16:18:13 -07004971# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004972 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004973 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08004974 $fmt =~ s/%%//g;
4975 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004976 if (WARN("PREFER_SEQ_PUTS",
4977 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4978 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004979 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004980 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004981 }
4982 }
4983
Andy Whitcroft554e1652012-01-10 15:09:57 -08004984# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004985 if ($^V && $^V ge 5.10.0 &&
4986 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004987 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004988
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004989 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004990 my $ms_val = $7;
4991 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004992
Andy Whitcroft554e1652012-01-10 15:09:57 -08004993 if ($ms_size =~ /^(0x|)0$/i) {
4994 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004995 "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 -08004996 } elsif ($ms_size =~ /^(0x|)1$/i) {
4997 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004998 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4999 }
5000 }
5001
Joe Perches98a9bba2014-01-23 15:54:52 -08005002# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5003 if ($^V && $^V ge 5.10.0 &&
5004 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
5005 if (WARN("PREFER_ETHER_ADDR_COPY",
5006 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
5007 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005008 $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 -08005009 }
5010 }
5011
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005012# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005013 if ($^V && $^V ge 5.10.0 &&
5014 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005015 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005016 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005017 my $call = $1;
5018 my $cast1 = deparenthesize($2);
5019 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005020 my $cast2 = deparenthesize($7);
5021 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005022 my $cast;
5023
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005024 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005025 $cast = "$cast1 or $cast2";
5026 } elsif ($cast1 ne "") {
5027 $cast = $cast1;
5028 } else {
5029 $cast = $cast2;
5030 }
5031 WARN("MINMAX",
5032 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005033 }
5034 }
5035
Joe Perches4a273192012-07-30 14:41:20 -07005036# check usleep_range arguments
5037 if ($^V && $^V ge 5.10.0 &&
5038 defined $stat &&
5039 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5040 my $min = $1;
5041 my $max = $7;
5042 if ($min eq $max) {
5043 WARN("USLEEP_RANGE",
5044 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5045 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5046 $min > $max) {
5047 WARN("USLEEP_RANGE",
5048 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5049 }
5050 }
5051
Joe Perches823b7942013-11-12 15:10:15 -08005052# check for naked sscanf
5053 if ($^V && $^V ge 5.10.0 &&
5054 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005055 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005056 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5057 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5058 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5059 my $lc = $stat =~ tr@\n@@;
5060 $lc = $lc + $linenr;
5061 my $stat_real = raw_line($linenr, 0);
5062 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5063 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5064 }
5065 WARN("NAKED_SSCANF",
5066 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5067 }
5068
Joe Perchesafc819a2014-06-04 16:12:08 -07005069# check for simple sscanf that should be kstrto<foo>
5070 if ($^V && $^V ge 5.10.0 &&
5071 defined $stat &&
5072 $line =~ /\bsscanf\b/) {
5073 my $lc = $stat =~ tr@\n@@;
5074 $lc = $lc + $linenr;
5075 my $stat_real = raw_line($linenr, 0);
5076 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5077 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5078 }
5079 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5080 my $format = $6;
5081 my $count = $format =~ tr@%@%@;
5082 if ($count == 1 &&
5083 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5084 WARN("SSCANF_TO_KSTRTO",
5085 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5086 }
5087 }
5088 }
5089
Joe Perches70dc8a42013-09-11 14:23:58 -07005090# check for new externs in .h files.
5091 if ($realfile =~ /\.h$/ &&
5092 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005093 if (CHK("AVOID_EXTERNS",
5094 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005095 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005096 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005097 }
5098 }
5099
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005100# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005101 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005102 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005103 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005104 my $function_name = $1;
5105 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005106
5107 my $s = $stat;
5108 if (defined $cond) {
5109 substr($s, 0, length($cond), '');
5110 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005111 if ($s =~ /^\s*;/ &&
5112 $function_name ne 'uninitialized_var')
5113 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005114 WARN("AVOID_EXTERNS",
5115 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005116 }
5117
5118 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005119 WARN("FUNCTION_ARGUMENTS",
5120 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005121 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005122
5123 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5124 $stat =~ /^.\s*extern\s+/)
5125 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005126 WARN("AVOID_EXTERNS",
5127 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005128 }
5129
5130# checks for new __setup's
5131 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5132 my $name = $1;
5133
5134 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005135 CHK("UNDOCUMENTED_SETUP",
5136 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005137 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005138 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005139
5140# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005141 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005142 WARN("UNNECESSARY_CASTS",
5143 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005144 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005145
Joe Perchesa640d252013-07-03 15:05:21 -07005146# alloc style
5147# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5148 if ($^V && $^V ge 5.10.0 &&
5149 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5150 CHK("ALLOC_SIZEOF_STRUCT",
5151 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5152 }
5153
Joe Perches60a55362014-06-04 16:12:07 -07005154# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5155 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005156 $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 -07005157 my $oldfunc = $3;
5158 my $a1 = $4;
5159 my $a2 = $10;
5160 my $newfunc = "kmalloc_array";
5161 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005162 my $r1 = $a1;
5163 my $r2 = $a2;
5164 if ($a1 =~ /^sizeof\s*\S/) {
5165 $r1 = $a2;
5166 $r2 = $a1;
5167 }
5168 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5169 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005170 if (WARN("ALLOC_WITH_MULTIPLY",
5171 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5172 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005173 $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 -07005174
5175 }
5176 }
5177 }
5178
Joe Perches972fdea2013-04-29 16:18:12 -07005179# check for krealloc arg reuse
5180 if ($^V && $^V ge 5.10.0 &&
5181 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5182 WARN("KREALLOC_ARG_REUSE",
5183 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5184 }
5185
Joe Perches5ce59ae2013-02-21 16:44:18 -08005186# check for alloc argument mismatch
5187 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5188 WARN("ALLOC_ARRAY_ARGS",
5189 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5190 }
5191
Joe Perchescaf2a542011-01-12 16:59:56 -08005192# check for multiple semicolons
5193 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005194 if (WARN("ONE_SEMICOLON",
5195 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5196 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005197 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005198 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005199 }
5200
Joe Perches0ab90192014-12-10 15:51:57 -08005201# check for #defines like: 1 << <digit> that could be BIT(digit)
5202 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5203 my $ull = "";
5204 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5205 if (CHK("BIT_MACRO",
5206 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5207 $fix) {
5208 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5209 }
5210 }
5211
Joe Perchese81f2392014-08-06 16:11:25 -07005212# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005213 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5214 my $has_break = 0;
5215 my $has_statement = 0;
5216 my $count = 0;
5217 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005218 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005219 $prevline--;
5220 my $rline = $rawlines[$prevline - 1];
5221 my $fline = $lines[$prevline - 1];
5222 last if ($fline =~ /^\@\@/);
5223 next if ($fline =~ /^\-/);
5224 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5225 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5226 next if ($fline =~ /^.[\s$;]*$/);
5227 $has_statement = 1;
5228 $count++;
5229 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5230 }
5231 if (!$has_break && $has_statement) {
5232 WARN("MISSING_BREAK",
5233 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5234 }
5235 }
5236
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005237# check for switch/default statements without a break;
5238 if ($^V && $^V ge 5.10.0 &&
5239 defined $stat &&
5240 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5241 my $ctx = '';
5242 my $herectx = $here . "\n";
5243 my $cnt = statement_rawlines($stat);
5244 for (my $n = 0; $n < $cnt; $n++) {
5245 $herectx .= raw_line($linenr, $n) . "\n";
5246 }
5247 WARN("DEFAULT_NO_BREAK",
5248 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005249 }
5250
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005251# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005252 if ($line =~ /\b__FUNCTION__\b/) {
5253 if (WARN("USE_FUNC",
5254 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5255 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005256 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005257 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005258 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005259
Joe Perches62ec8182015-02-13 14:38:18 -08005260# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5261 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5262 ERROR("DATE_TIME",
5263 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5264 }
5265
Joe Perches2c924882012-03-23 15:02:20 -07005266# check for use of yield()
5267 if ($line =~ /\byield\s*\(\s*\)/) {
5268 WARN("YIELD",
5269 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5270 }
5271
Joe Perches179f8f42013-07-03 15:05:30 -07005272# check for comparisons against true and false
5273 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5274 my $lead = $1;
5275 my $arg = $2;
5276 my $test = $3;
5277 my $otype = $4;
5278 my $trail = $5;
5279 my $op = "!";
5280
5281 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5282
5283 my $type = lc($otype);
5284 if ($type =~ /^(?:true|false)$/) {
5285 if (("$test" eq "==" && "$type" eq "true") ||
5286 ("$test" eq "!=" && "$type" eq "false")) {
5287 $op = "";
5288 }
5289
5290 CHK("BOOL_COMPARISON",
5291 "Using comparison to $otype is error prone\n" . $herecurr);
5292
5293## maybe suggesting a correct construct would better
5294## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5295
5296 }
5297 }
5298
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005299# check for semaphores initialized locked
5300 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005301 WARN("CONSIDER_COMPLETION",
5302 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005303 }
Joe Perches6712d852012-03-23 15:02:20 -07005304
Joe Perches67d0a072011-10-31 17:13:10 -07005305# recommend kstrto* over simple_strto* and strict_strto*
5306 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005307 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005308 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005309 }
Joe Perches6712d852012-03-23 15:02:20 -07005310
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005311# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005312 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005313 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005314 "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 -07005315 }
Joe Perches6712d852012-03-23 15:02:20 -07005316
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005317# check for various structs that are normally const (ops, kgdb, device_tree)
5318 my $const_structs = qr{
5319 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005320 address_space_operations|
5321 backlight_ops|
5322 block_device_operations|
5323 dentry_operations|
5324 dev_pm_ops|
5325 dma_map_ops|
5326 extent_io_ops|
5327 file_lock_operations|
5328 file_operations|
5329 hv_ops|
5330 ide_dma_ops|
5331 intel_dvo_dev_ops|
5332 item_operations|
5333 iwl_ops|
5334 kgdb_arch|
5335 kgdb_io|
5336 kset_uevent_ops|
5337 lock_manager_operations|
5338 microcode_ops|
5339 mtrr_ops|
5340 neigh_ops|
5341 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005342 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005343 pci_raw_ops|
5344 pipe_buf_operations|
5345 platform_hibernation_ops|
5346 platform_suspend_ops|
5347 proto_ops|
5348 rpc_pipe_ops|
5349 seq_operations|
5350 snd_ac97_build_ops|
5351 soc_pcmcia_socket_ops|
5352 stacktrace_ops|
5353 sysfs_ops|
5354 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07005355 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005356 usb_mon_operations|
5357 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005358 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005359 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005360 WARN("CONST_STRUCT",
5361 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005362 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005363 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005364
5365# use of NR_CPUS is usually wrong
5366# ignore definitions of NR_CPUS and usage to define arrays as likely right
5367 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005368 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5369 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005370 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5371 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5372 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005373 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005374 WARN("NR_CPUS",
5375 "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 -07005376 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005377
Joe Perches52ea8502013-11-12 15:10:09 -08005378# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5379 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5380 ERROR("DEFINE_ARCH_HAS",
5381 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5382 }
5383
Joe Perchesacd93622015-02-13 14:38:38 -08005384# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5385 if ($^V && $^V ge 5.10.0 &&
5386 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5387 WARN("LIKELY_MISUSE",
5388 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5389 }
5390
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005391# whine mightly about in_atomic
5392 if ($line =~ /\bin_atomic\s*\(/) {
5393 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005394 ERROR("IN_ATOMIC",
5395 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005396 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005397 WARN("IN_ATOMIC",
5398 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005399 }
5400 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005401
5402# check for lockdep_set_novalidate_class
5403 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5404 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5405 if ($realfile !~ m@^kernel/lockdep@ &&
5406 $realfile !~ m@^include/linux/lockdep@ &&
5407 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005408 ERROR("LOCKDEP",
5409 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005410 }
5411 }
Dave Jones88f88312011-01-12 16:59:59 -08005412
Joe Perchesb392c642015-04-16 12:44:16 -07005413 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5414 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005415 WARN("EXPORTED_WORLD_WRITABLE",
5416 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005417 }
Joe Perches24358802014-04-03 14:49:13 -07005418
Joe Perches515a2352014-04-03 14:49:24 -07005419# Mode permission misuses where it seems decimal should be octal
5420# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5421 if ($^V && $^V ge 5.10.0 &&
5422 $line =~ /$mode_perms_search/) {
5423 foreach my $entry (@mode_permission_funcs) {
5424 my $func = $entry->[0];
5425 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005426
Joe Perches515a2352014-04-03 14:49:24 -07005427 my $skip_args = "";
5428 if ($arg_pos > 1) {
5429 $arg_pos--;
5430 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5431 }
5432 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5433 if ($line =~ /$test/) {
5434 my $val = $1;
5435 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005436
Joe Perches515a2352014-04-03 14:49:24 -07005437 if ($val !~ /^0$/ &&
5438 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5439 length($val) ne 4)) {
5440 ERROR("NON_OCTAL_PERMISSIONS",
5441 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005442 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5443 ERROR("EXPORTED_WORLD_WRITABLE",
5444 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005445 }
Joe Perches24358802014-04-03 14:49:13 -07005446 }
5447 }
5448 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005449 }
5450
5451 # If we have no input at all, then there is nothing to report on
5452 # so just keep quiet.
5453 if ($#rawlines == -1) {
5454 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005455 }
5456
Andy Whitcroft8905a672007-11-28 16:21:06 -08005457 # In mailback mode only produce a report in the negative, for
5458 # things that appear to be patches.
5459 if ($mailback && ($clean == 1 || !$is_patch)) {
5460 exit(0);
5461 }
5462
5463 # This is not a patch, and we are are in 'no-patch' mode so
5464 # just keep quiet.
5465 if (!$chk_patch && !$is_patch) {
5466 exit(0);
5467 }
5468
5469 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005470 ERROR("NOT_UNIFIED_DIFF",
5471 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005472 }
5473 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005474 ERROR("MISSING_SIGN_OFF",
5475 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005476 }
5477
Andy Whitcroft8905a672007-11-28 16:21:06 -08005478 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005479 if ($summary && !($clean == 1 && $quiet == 1)) {
5480 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005481 print "total: $cnt_error errors, $cnt_warn warnings, " .
5482 (($check)? "$cnt_chk checks, " : "") .
5483 "$cnt_lines lines checked\n";
5484 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005485 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005486
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005487 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005488
5489 if ($^V lt 5.10.0) {
5490 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5491 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5492 }
5493
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005494 # If there were whitespace errors which cleanpatch can fix
5495 # then suggest that.
5496 if ($rpt_cleaners) {
5497 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5498 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005499 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005500 }
5501 }
5502
Joe Perches91bfe482013-09-11 14:23:59 -07005503 hash_show_words(\%use_type, "Used");
5504 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005505
Joe Perchesd752fcc2014-08-06 16:11:05 -07005506 if ($clean == 0 && $fix &&
5507 ("@rawlines" ne "@fixed" ||
5508 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005509 my $newfile = $filename;
5510 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005511 my $linecount = 0;
5512 my $f;
5513
Joe Perchesd752fcc2014-08-06 16:11:05 -07005514 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5515
Joe Perches3705ce52013-07-03 15:05:31 -07005516 open($f, '>', $newfile)
5517 or die "$P: Can't open $newfile for write\n";
5518 foreach my $fixed_line (@fixed) {
5519 $linecount++;
5520 if ($file) {
5521 if ($linecount > 3) {
5522 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005523 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005524 }
5525 } else {
5526 print $f $fixed_line . "\n";
5527 }
5528 }
5529 close($f);
5530
5531 if (!$quiet) {
5532 print << "EOM";
5533Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5534
5535Do _NOT_ trust the results written to this file.
5536Do _NOT_ submit these changes without inspecting them for correctness.
5537
5538This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5539No warranties, expressed or implied...
5540
5541EOM
5542 }
5543 }
5544
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005545 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005546 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005547 }
5548 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005549 print << "EOM";
5550$vname has style problems, please review.
5551
5552If any of these errors are false positives, please report
5553them to the maintainer, see CHECKPATCH in MAINTAINERS.
5554EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005555 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005556
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005557 return $clean;
5558}