blob: c72e7ee1000bbfe752dc4708731d851da6845390 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
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 Perches2ac73b4f2014-06-04 16:12:05 -070029my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $summary = 1;
31my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080032my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070033my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070034my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080035my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Joe Perches34456862013-07-03 15:05:34 -070038my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070039my %use_type = ();
40my @use = ();
41my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070042my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070043my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080045my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070046my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070048my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070049my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070050my $codespell = 0;
51my $codespellfile = "/usr/local/share/codespell/dictionary.txt";
Hannes Eder77f5b102009-09-21 17:04:37 -070052
53sub help {
54 my ($exitcode) = @_;
55
56 print << "EOM";
57Usage: $P [OPTION]... [FILE]...
58Version: $V
59
60Options:
61 -q, --quiet quiet
62 --no-tree run without a kernel tree
63 --no-signoff do not check for 'Signed-off-by' line
64 --patch treat FILE as patchfile (default)
65 --emacs emacs compile window format
66 --terse one line per report
67 -f, --file treat FILE as regular source file
68 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070069 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070070 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080071 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070072 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070073 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070074 --root=PATH PATH to the kernel tree root
75 --no-summary suppress the per-file summary
76 --mailback only produce a report in case of warnings/errors
77 --summary-file include the filename in summary
78 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
79 'values', 'possible', 'type', and 'attr' (default
80 is all off)
81 --test-only=WORD report only warnings/errors containing WORD
82 literally
Joe Perches3705ce52013-07-03 15:05:31 -070083 --fix EXPERIMENTAL - may create horrible results
84 If correctable single-line errors exist, create
85 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
86 with potential errors corrected to the preferred
87 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080088 --fix-inplace EXPERIMENTAL - may create horrible results
89 Is the same as --fix, but overwrites the input
90 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070091 --ignore-perl-version override checking of perl version. expect
92 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -070093 --codespell Use the codespell dictionary for spelling/typos
94 (default:/usr/local/share/codespell/dictionary.txt)
95 --codespellfile Use this codespell dictionary
Hannes Eder77f5b102009-09-21 17:04:37 -070096 -h, --help, --version display this help and exit
97
98When FILE is - read standard input.
99EOM
100
101 exit($exitcode);
102}
103
Joe Perches000d1cc12011-07-25 17:13:25 -0700104my $conf = which_conf($configuration_file);
105if (-f $conf) {
106 my @conf_args;
107 open(my $conffile, '<', "$conf")
108 or warn "$P: Can't find a readable $configuration_file file $!\n";
109
110 while (<$conffile>) {
111 my $line = $_;
112
113 $line =~ s/\s*\n?$//g;
114 $line =~ s/^\s*//g;
115 $line =~ s/\s+/ /g;
116
117 next if ($line =~ m/^\s*#/);
118 next if ($line =~ m/^\s*$/);
119
120 my @words = split(" ", $line);
121 foreach my $word (@words) {
122 last if ($word =~ m/^#/);
123 push (@conf_args, $word);
124 }
125 }
126 close($conffile);
127 unshift(@ARGV, @conf_args) if @conf_args;
128}
129
Andy 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 Perches2ac73b4f2014-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 Whitcroft00df344f2007-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 Whitcroft00df344f2007-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 Whitcroft00df344f2007-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 Whitcroft00df344f2007-06-08 13:47:06 -0700965 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700966 } elsif ($sanitise_quote eq $c) {
967 $sanitise_quote = '';
Andy Whitcroft00df344f2007-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 Whitcroft00df344f2007-06-08 13:47:06 -0700978 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700979 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-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 Whitcroft00df344f2007-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 Whitcroft00df344f2007-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 Whitcroft00df344f2007-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 Whitcroft00df344f2007-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 Whitcroft00df344f2007-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 Perches13f19372014-08-06 16:10:59 -07001901 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001902 my $non_utf8_charset = 0;
1903
Joe Perches365dd4e2014-08-06 16:10:42 -07001904 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001905 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001906
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001907 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001908 our $cnt_lines = 0;
1909 our $cnt_error = 0;
1910 our $cnt_warn = 0;
1911 our $cnt_chk = 0;
1912
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001913 # Trace the real file/line as we go.
1914 my $realfile = '';
1915 my $realline = 0;
1916 my $realcnt = 0;
1917 my $here = '';
1918 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001919 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001920 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001921 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001922
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001923 my $prev_values = 'E';
1924
1925 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001926 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001927 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001928 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001929 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001930
Joe Perches7e51f192013-09-11 14:23:57 -07001931 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001932
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001933 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001934 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001935 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001936 my @setup_docs = ();
1937 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001938
Joe Perchesd8b07712013-11-12 15:10:06 -08001939 my $camelcase_file_seeded = 0;
1940
Andy Whitcroft773647a2008-03-28 14:15:58 -07001941 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001942 my $line;
1943 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001944 $linenr++;
1945 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001946
Joe Perches3705ce52013-07-03 15:05:31 -07001947 push(@fixed, $rawline) if ($fix);
1948
Andy Whitcroft773647a2008-03-28 14:15:58 -07001949 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001950 $setup_docs = 0;
1951 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1952 $setup_docs = 1;
1953 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001954 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001955 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001956 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1957 $realline=$1-1;
1958 if (defined $2) {
1959 $realcnt=$3+1;
1960 } else {
1961 $realcnt=1+1;
1962 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001963 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001964
1965 # Guestimate if this is a continuing comment. Run
1966 # the context looking for a comment "edge". If this
1967 # edge is a close comment then we must be in a comment
1968 # at context start.
1969 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001970 my $cnt = $realcnt;
1971 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1972 next if (defined $rawlines[$ln - 1] &&
1973 $rawlines[$ln - 1] =~ /^-/);
1974 $cnt--;
1975 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001976 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001977 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1978 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1979 ($edge) = $1;
1980 last;
1981 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001982 }
1983 if (defined $edge && $edge eq '*/') {
1984 $in_comment = 1;
1985 }
1986
1987 # Guestimate if this is a continuing comment. If this
1988 # is the start of a diff block and this line starts
1989 # ' *' then it is very likely a comment.
1990 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001991 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001992 {
1993 $in_comment = 1;
1994 }
1995
1996 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1997 sanitise_line_reset($in_comment);
1998
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001999 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002000 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002001 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002002 $line = sanitise_line($rawline);
2003 }
2004 push(@lines, $line);
2005
2006 if ($realcnt > 1) {
2007 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2008 } else {
2009 $realcnt = 0;
2010 }
2011
2012 #print "==>$rawline\n";
2013 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002014
2015 if ($setup_docs && $line =~ /^\+/) {
2016 push(@setup_docs, $line);
2017 }
2018 }
2019
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002020 $prefix = '';
2021
Andy Whitcroft773647a2008-03-28 14:15:58 -07002022 $realcnt = 0;
2023 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002024 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002025 foreach my $line (@lines) {
2026 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002027 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002028 my $sline = $line; #copy of $line
2029 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002030
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002031 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002032
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002033#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002034 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002035 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002036 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002037 $realline=$1-1;
2038 if (defined $2) {
2039 $realcnt=$3+1;
2040 } else {
2041 $realcnt=1+1;
2042 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002043 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002044 $prev_values = 'E';
2045
Andy Whitcroft773647a2008-03-28 14:15:58 -07002046 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002047 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002048 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002049 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002050 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002051
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002052# track the line number as we move through the hunk, note that
2053# new versions of GNU diff omit the leading space on completely
2054# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002055 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002056 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002057 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002058
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002059 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002060 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002061
2062 # Track the previous line.
2063 ($prevline, $stashline) = ($stashline, $line);
2064 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002065 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2066
Andy Whitcroft773647a2008-03-28 14:15:58 -07002067 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002068
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002069 } elsif ($realcnt == 1) {
2070 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002071 }
2072
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002073 my $hunk_line = ($realcnt != 0);
2074
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002075#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002076 $prefix = "$filename:$realline: " if ($emacs && $file);
2077 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2078
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002079 $here = "#$linenr: " if (!$file);
2080 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002081
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002082 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002083 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002084 if ($line =~ /^diff --git.*?(\S+)$/) {
2085 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002086 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002087 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002088 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002089 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002090 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002091 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002092 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002093
2094 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002095 if (!$file && $tree && $p1_prefix ne '' &&
2096 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002097 WARN("PATCH_PREFIX",
2098 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002099 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002100
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002101 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002102 ERROR("MODIFIED_INCLUDE_ASM",
2103 "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 -07002104 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002105 $found_file = 1;
2106 }
2107
2108 if ($found_file) {
2109 if ($realfile =~ m@^(drivers/net/|net/)@) {
2110 $check = 1;
2111 } else {
2112 $check = $check_orig;
2113 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002114 next;
2115 }
2116
Randy Dunlap389834b2007-06-08 13:47:03 -07002117 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002118
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002119 my $hereline = "$here\n$rawline\n";
2120 my $herecurr = "$here\n$rawline\n";
2121 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002122
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002123 $cnt_lines++ if ($realcnt != 0);
2124
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002125# Check for incorrect file permissions
2126 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2127 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002128 if ($realfile !~ m@scripts/@ &&
2129 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002130 ERROR("EXECUTE_PERMISSIONS",
2131 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002132 }
2133 }
2134
Joe Perches20112472011-07-25 17:13:23 -07002135# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002136 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002137 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002138 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002139 }
2140
Joe Perchese0d975b2014-12-10 15:51:49 -08002141# Check if MAINTAINERS is being updated. If so, there's probably no need to
2142# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2143 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2144 $reported_maintainer_file = 1;
2145 }
2146
Joe Perches20112472011-07-25 17:13:23 -07002147# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002148 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002149 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002150 my $space_before = $1;
2151 my $sign_off = $2;
2152 my $space_after = $3;
2153 my $email = $4;
2154 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2155
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002156 if ($sign_off !~ /$signature_tags/) {
2157 WARN("BAD_SIGN_OFF",
2158 "Non-standard signature: $sign_off\n" . $herecurr);
2159 }
Joe Perches20112472011-07-25 17:13:23 -07002160 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002161 if (WARN("BAD_SIGN_OFF",
2162 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2163 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002164 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002165 "$ucfirst_sign_off $email";
2166 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002167 }
Joe Perches20112472011-07-25 17:13:23 -07002168 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002169 if (WARN("BAD_SIGN_OFF",
2170 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2171 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002172 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002173 "$ucfirst_sign_off $email";
2174 }
2175
Joe Perches20112472011-07-25 17:13:23 -07002176 }
2177 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002178 if (WARN("BAD_SIGN_OFF",
2179 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2180 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002181 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002182 "$ucfirst_sign_off $email";
2183 }
Joe Perches20112472011-07-25 17:13:23 -07002184 }
2185
2186 my ($email_name, $email_address, $comment) = parse_email($email);
2187 my $suggested_email = format_email(($email_name, $email_address));
2188 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002189 ERROR("BAD_SIGN_OFF",
2190 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002191 } else {
2192 my $dequoted = $suggested_email;
2193 $dequoted =~ s/^"//;
2194 $dequoted =~ s/" </ </;
2195 # Don't force email to have quotes
2196 # Allow just an angle bracketed address
2197 if ("$dequoted$comment" ne $email &&
2198 "<$email_address>$comment" ne $email &&
2199 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002200 WARN("BAD_SIGN_OFF",
2201 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002202 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002203 }
Joe Perches7e51f192013-09-11 14:23:57 -07002204
2205# Check for duplicate signatures
2206 my $sig_nospace = $line;
2207 $sig_nospace =~ s/\s//g;
2208 $sig_nospace = lc($sig_nospace);
2209 if (defined $signatures{$sig_nospace}) {
2210 WARN("BAD_SIGN_OFF",
2211 "Duplicate signature\n" . $herecurr);
2212 } else {
2213 $signatures{$sig_nospace} = 1;
2214 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002215 }
2216
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002217# Check email subject for common tools that don't need to be mentioned
2218 if ($in_header_lines &&
2219 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2220 WARN("EMAIL_SUBJECT",
2221 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2222 }
2223
Joe Perches9b3189eb2014-06-04 16:12:10 -07002224# Check for old stable address
2225 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2226 ERROR("STABLE_ADDRESS",
2227 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2228 }
2229
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002230# Check for unwanted Gerrit info
2231 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2232 ERROR("GERRIT_CHANGE_ID",
2233 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2234 }
2235
Joe Perches0d7835f2015-02-13 14:38:35 -08002236# Check for git id commit length and improperly formed commit descriptions
2237 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002238 my $init_char = $1;
2239 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002240 my $short = 1;
2241 my $long = 0;
2242 my $case = 1;
2243 my $space = 1;
2244 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002245 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002246 my $id = '0123456789ab';
2247 my $orig_desc = "commit description";
2248 my $description = "";
2249
2250 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2251 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2252 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2253 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2254 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2255 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002256 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002257 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2258 defined $rawlines[$linenr] &&
2259 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2260 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002261 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002262 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2263 defined $rawlines[$linenr] &&
2264 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2265 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2266 $orig_desc = $1;
2267 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2268 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002269 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002270 }
2271
2272 ($id, $description) = git_commit_info($orig_commit,
2273 $id, $orig_desc);
2274
Joe Perches19c146a2015-02-13 14:39:00 -08002275 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002276 ERROR("GIT_COMMIT_ID",
2277 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2278 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002279 }
2280
Joe Perches13f19372014-08-06 16:10:59 -07002281# Check for added, moved or deleted files
2282 if (!$reported_maintainer_file && !$in_commit_log &&
2283 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2284 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2285 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2286 (defined($1) || defined($2))))) {
2287 $reported_maintainer_file = 1;
2288 WARN("FILE_PATH_CHANGES",
2289 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2290 }
2291
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002292# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002293 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002294 ERROR("CORRUPTED_PATCH",
2295 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002296 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002297 }
2298
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002299# Check for absolute kernel paths.
2300 if ($tree) {
2301 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2302 my $file = $1;
2303
2304 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2305 check_absolute_file($1, $herecurr)) {
2306 #
2307 } else {
2308 check_absolute_file($file, $herecurr);
2309 }
2310 }
2311 }
2312
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002313# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2314 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002315 $rawline !~ m/^$UTF8*$/) {
2316 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2317
2318 my $blank = copy_spacing($rawline);
2319 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2320 my $hereptr = "$hereline$ptr\n";
2321
Joe Perches34d99212011-07-25 17:13:26 -07002322 CHK("INVALID_UTF8",
2323 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002324 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002325
Joe Perches15662b32011-10-31 17:13:12 -07002326# Check if it's the start of a commit log
2327# (not a header line and we haven't seen the patch filename)
2328 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002329 !($rawline =~ /^\s+\S/ ||
2330 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002331 $in_header_lines = 0;
2332 $in_commit_log = 1;
2333 }
2334
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002335# Check if there is UTF-8 in a commit log when a mail header has explicitly
2336# declined it, i.e defined some charset where it is missing.
2337 if ($in_header_lines &&
2338 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2339 $1 !~ /utf-8/i) {
2340 $non_utf8_charset = 1;
2341 }
2342
2343 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002344 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002345 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002346 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2347 }
2348
Kees Cook66b47b42014-10-13 15:51:57 -07002349# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002350 if (defined($misspellings) &&
2351 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002352 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002353 my $typo = $1;
2354 my $typo_fix = $spelling_fix{lc($typo)};
2355 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2356 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2357 my $msg_type = \&WARN;
2358 $msg_type = \&CHK if ($file);
2359 if (&{$msg_type}("TYPO_SPELLING",
2360 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2361 $fix) {
2362 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2363 }
2364 }
2365 }
2366
Andy Whitcroft306708542008-10-15 22:02:28 -07002367# ignore non-hunk lines and lines being removed
2368 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002369
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002370#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002371 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002372 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002373 if (ERROR("DOS_LINE_ENDINGS",
2374 "DOS line endings\n" . $herevet) &&
2375 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002376 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002377 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002378 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2379 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002380 if (ERROR("TRAILING_WHITESPACE",
2381 "trailing whitespace\n" . $herevet) &&
2382 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002383 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002384 }
2385
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002386 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002387 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002388
Josh Triplett4783f892013-11-12 15:10:12 -08002389# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002390 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002391 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2392 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002393 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2394 my $msg_type = \&ERROR;
2395 $msg_type = \&CHK if ($file);
2396 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002397 "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 -08002398 }
2399
Andi Kleen33549572010-05-24 14:33:29 -07002400# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002401# Only applies when adding the entry originally, after that we do not have
2402# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002403 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002404 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002405 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002406 my $cnt = $realcnt;
2407 my $ln = $linenr + 1;
2408 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002409 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002410 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002411 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002412 $f = $lines[$ln - 1];
2413 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2414 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002415
2416 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002417 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002418
Joe Perches8d73e0e2014-08-06 16:10:46 -07002419 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002420 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002421 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002422 $length = -1;
2423 }
2424
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002425 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002426 $f =~ s/#.*//;
2427 $f =~ s/^\s+//;
2428 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002429 if ($f =~ /^\s*config\s/) {
2430 $is_end = 1;
2431 last;
2432 }
Andi Kleen33549572010-05-24 14:33:29 -07002433 $length++;
2434 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002435 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2436 WARN("CONFIG_DESCRIPTION",
2437 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2438 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002439 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002440 }
2441
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002442# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2443 if ($realfile =~ /Kconfig/ &&
2444 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2445 WARN("CONFIG_EXPERIMENTAL",
2446 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2447 }
2448
Christoph Jaeger327953e2015-02-13 14:38:29 -08002449# discourage the use of boolean for type definition attributes of Kconfig options
2450 if ($realfile =~ /Kconfig/ &&
2451 $line =~ /^\+\s*\bboolean\b/) {
2452 WARN("CONFIG_TYPE_BOOLEAN",
2453 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2454 }
2455
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002456 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2457 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2458 my $flag = $1;
2459 my $replacement = {
2460 'EXTRA_AFLAGS' => 'asflags-y',
2461 'EXTRA_CFLAGS' => 'ccflags-y',
2462 'EXTRA_CPPFLAGS' => 'cppflags-y',
2463 'EXTRA_LDFLAGS' => 'ldflags-y',
2464 };
2465
2466 WARN("DEPRECATED_VARIABLE",
2467 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2468 }
2469
Rob Herringbff5da42014-01-23 15:54:51 -08002470# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002471 if (defined $root &&
2472 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2473 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2474
Rob Herringbff5da42014-01-23 15:54:51 -08002475 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2476
Florian Vaussardcc933192014-04-03 14:49:27 -07002477 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2478 my $vp_file = $dt_path . "vendor-prefixes.txt";
2479
Rob Herringbff5da42014-01-23 15:54:51 -08002480 foreach my $compat (@compats) {
2481 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002482 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2483 my $compat3 = $compat;
2484 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2485 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002486 if ( $? >> 8 ) {
2487 WARN("UNDOCUMENTED_DT_STRING",
2488 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2489 }
2490
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002491 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2492 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002493 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002494 if ( $? >> 8 ) {
2495 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002496 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002497 }
2498 }
2499 }
2500
Andy Whitcroft5368df202008-10-15 22:02:27 -07002501# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002502 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002503
Joe Perches6cd7f382012-12-17 16:01:54 -08002504#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002505 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002506 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002507 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002508 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002509 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002510 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002511 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002512 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002513 }
2514
Andy Whitcroft8905a672007-11-28 16:21:06 -08002515# check for adding lines without a newline.
2516 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002517 WARN("MISSING_EOF_NEWLINE",
2518 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002519 }
2520
Mike Frysinger42e41c52009-09-21 17:04:40 -07002521# Blackfin: use hi/lo macros
2522 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2523 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2524 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002525 ERROR("LO_MACRO",
2526 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002527 }
2528 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2529 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002530 ERROR("HI_MACRO",
2531 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002532 }
2533 }
2534
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002535# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002536 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002537
2538# at the beginning of a line any tabs must come first and anything
2539# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002540 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2541 $rawline =~ /^\+\s* \s*/) {
2542 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002543 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002544 if (ERROR("CODE_INDENT",
2545 "code indent should use tabs where possible\n" . $herevet) &&
2546 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002547 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002548 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002549 }
2550
Alberto Panizzo08e44362010-03-05 13:43:54 -08002551# check for space before tabs.
2552 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2553 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002554 if (WARN("SPACE_BEFORE_TAB",
2555 "please, no space before tabs\n" . $herevet) &&
2556 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002557 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002558 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002559 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002560 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002561 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002562 }
2563
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002564# check for && or || at the start of a line
2565 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2566 CHK("LOGICAL_CONTINUATIONS",
2567 "Logical continuations should be on the previous line\n" . $hereprev);
2568 }
2569
2570# check multi-line statement indentation matches previous line
2571 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002572 $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 -07002573 $prevline =~ /^\+(\t*)(.*)$/;
2574 my $oldindent = $1;
2575 my $rest = $2;
2576
2577 my $pos = pos_last_openparen($rest);
2578 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002579 $line =~ /^(\+| )([ \t]*)/;
2580 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002581
2582 my $goodtabindent = $oldindent .
2583 "\t" x ($pos / 8) .
2584 " " x ($pos % 8);
2585 my $goodspaceindent = $oldindent . " " x $pos;
2586
2587 if ($newindent ne $goodtabindent &&
2588 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002589
2590 if (CHK("PARENTHESIS_ALIGNMENT",
2591 "Alignment should match open parenthesis\n" . $hereprev) &&
2592 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002593 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002594 s/^\+[ \t]*/\+$goodtabindent/;
2595 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002596 }
2597 }
2598 }
2599
Joe Perches6ab3a972015-04-16 12:44:05 -07002600# check for space after cast like "(int) foo" or "(struct foo) bar"
2601# avoid checking a few false positives:
2602# "sizeof(<type>)" or "__alignof__(<type>)"
2603# function pointer declarations like "(*foo)(int) = bar;"
2604# structure definitions like "(struct foo) { 0 };"
2605# multiline macros that define functions
2606# known attributes or the __attribute__ keyword
2607 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2608 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002609 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002610 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002611 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002612 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002613 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002614 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002615 }
2616
Joe Perches05880602012-10-04 17:13:35 -07002617 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002618 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002619 $rawline =~ /^\+[ \t]*\*/ &&
2620 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002621 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2622 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2623 }
2624
2625 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002626 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2627 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002628 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002629 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2630 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2631 "networking block comments start with * on subsequent lines\n" . $hereprev);
2632 }
2633
2634 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002635 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2636 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2637 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2638 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002639 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2640 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2641 }
2642
Joe Perches7f619192014-08-06 16:10:39 -07002643# check for missing blank lines after struct/union declarations
2644# with exceptions for various attributes and macros
2645 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2646 $line =~ /^\+/ &&
2647 !($line =~ /^\+\s*$/ ||
2648 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2649 $line =~ /^\+\s*MODULE_/i ||
2650 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2651 $line =~ /^\+[a-z_]*init/ ||
2652 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2653 $line =~ /^\+\s*DECLARE/ ||
2654 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002655 if (CHK("LINE_SPACING",
2656 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2657 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002658 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002659 }
Joe Perches7f619192014-08-06 16:10:39 -07002660 }
2661
Joe Perches365dd4e2014-08-06 16:10:42 -07002662# check for multiple consecutive blank lines
2663 if ($prevline =~ /^[\+ ]\s*$/ &&
2664 $line =~ /^\+\s*$/ &&
2665 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002666 if (CHK("LINE_SPACING",
2667 "Please don't use multiple blank lines\n" . $hereprev) &&
2668 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002669 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002670 }
2671
Joe Perches365dd4e2014-08-06 16:10:42 -07002672 $last_blank_line = $linenr;
2673 }
2674
Joe Perches3b617e32014-04-03 14:49:28 -07002675# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002676 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2677 # actual declarations
2678 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002679 # function pointer declarations
2680 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002681 # foo bar; where foo is some local typedef or #define
2682 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2683 # known declaration macros
2684 $prevline =~ /^\+\s+$declaration_macros/) &&
2685 # for "else if" which can look like "$Ident $Ident"
2686 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2687 # other possible extensions of declaration lines
2688 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2689 # not starting a section or a macro "\" extended line
2690 $prevline =~ /(?:\{\s*|\\)$/) &&
2691 # looks like a declaration
2692 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002693 # function pointer declarations
2694 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002695 # foo bar; where foo is some local typedef or #define
2696 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2697 # known declaration macros
2698 $sline =~ /^\+\s+$declaration_macros/ ||
2699 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002700 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002701 # start or end of block or continuation of declaration
2702 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2703 # bitfield continuation
2704 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2705 # other possible extensions of declaration lines
2706 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2707 # indentation of previous and current line are the same
2708 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002709 if (WARN("LINE_SPACING",
2710 "Missing a blank line after declarations\n" . $hereprev) &&
2711 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002712 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002713 }
Joe Perches3b617e32014-04-03 14:49:28 -07002714 }
2715
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002716# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002717# Exceptions:
2718# 1) within comments
2719# 2) indented preprocessor commands
2720# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002721 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002722 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002723 if (WARN("LEADING_SPACE",
2724 "please, no spaces at the start of a line\n" . $herevet) &&
2725 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002726 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002727 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002728 }
2729
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002730# check we are in a valid C source file if not then ignore this hunk
2731 next if ($realfile !~ /\.(h|c)$/);
2732
Joe Perches032a4c02014-08-06 16:10:29 -07002733# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002734# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002735# if the previous line is a break or return and is indented 1 tab more...
2736 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2737 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002738 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2739 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2740 defined $lines[$linenr] &&
2741 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002742 WARN("UNNECESSARY_ELSE",
2743 "else is not generally useful after a break or return\n" . $hereprev);
2744 }
2745 }
2746
Joe Perchesc00df192014-08-06 16:11:01 -07002747# check indentation of a line with a break;
2748# if the previous line is a goto or return and is indented the same # of tabs
2749 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2750 my $tabs = $1;
2751 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2752 WARN("UNNECESSARY_BREAK",
2753 "break is not useful after a goto or return\n" . $hereprev);
2754 }
2755 }
2756
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002757# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2758 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2759 WARN("CONFIG_EXPERIMENTAL",
2760 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2761 }
2762
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002763# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002764 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002765 WARN("CVS_KEYWORD",
2766 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002767 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002768
Mike Frysinger42e41c52009-09-21 17:04:40 -07002769# Blackfin: don't use __builtin_bfin_[cs]sync
2770 if ($line =~ /__builtin_bfin_csync/) {
2771 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002772 ERROR("CSYNC",
2773 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002774 }
2775 if ($line =~ /__builtin_bfin_ssync/) {
2776 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002777 ERROR("SSYNC",
2778 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002779 }
2780
Joe Perches56e77d72013-02-21 16:44:14 -08002781# check for old HOTPLUG __dev<foo> section markings
2782 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2783 WARN("HOTPLUG_SECTION",
2784 "Using $1 is unnecessary\n" . $herecurr);
2785 }
2786
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002787# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002788 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2789 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002790#print "LINE<$line>\n";
2791 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002792 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002793 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002794 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002795 $stat =~ s/\n./\n /g;
2796 $cond =~ s/\n./\n /g;
2797
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002798#print "linenr<$linenr> <$stat>\n";
2799 # If this statement has no statement boundaries within
2800 # it there is no point in retrying a statement scan
2801 # until we hit end of it.
2802 my $frag = $stat; $frag =~ s/;+\s*$//;
2803 if ($frag !~ /(?:{|;)/) {
2804#print "skip<$line_nr_next>\n";
2805 $suppress_statement = $line_nr_next;
2806 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002807
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002808 # Find the real next line.
2809 $realline_next = $line_nr_next;
2810 if (defined $realline_next &&
2811 (!defined $lines[$realline_next - 1] ||
2812 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2813 $realline_next++;
2814 }
2815
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002816 my $s = $stat;
2817 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002818
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002819 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002820 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002821
2822 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002823 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002824
Andy Whitcroft463f2862009-09-21 17:04:34 -07002825 } elsif ($s =~ /^.\s*else\b/s) {
2826
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002827 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002828 } 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 -07002829 my $type = $1;
2830 $type =~ s/\s+/ /g;
2831 possible($type, "A:" . $s);
2832
Andy Whitcroft8905a672007-11-28 16:21:06 -08002833 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002834 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002835 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002836 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002837
2838 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002839 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002840 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002841 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002842
2843 # Check for any sort of function declaration.
2844 # int foo(something bar, other baz);
2845 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002846 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 -08002847 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002848
Andy Whitcroftcf655042008-03-04 14:28:20 -08002849 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002850 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002851 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002852
Andy Whitcroft8905a672007-11-28 16:21:06 -08002853 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002854 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002855
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002856 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002857 }
2858 }
2859 }
2860
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002861 }
2862
Andy Whitcroft653d4872007-06-23 17:16:34 -07002863#
2864# Checks which may be anchored in the context.
2865#
2866
2867# Check for switch () and associated case and default
2868# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002869 if ($line=~/\bswitch\s*\(.*\)/) {
2870 my $err = '';
2871 my $sep = '';
2872 my @ctx = ctx_block_outer($linenr, $realcnt);
2873 shift(@ctx);
2874 for my $ctx (@ctx) {
2875 my ($clen, $cindent) = line_stats($ctx);
2876 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2877 $indent != $cindent) {
2878 $err .= "$sep$ctx\n";
2879 $sep = '';
2880 } else {
2881 $sep = "[...]\n";
2882 }
2883 }
2884 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002885 ERROR("SWITCH_CASE_INDENT_LEVEL",
2886 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002887 }
2888 }
2889
2890# if/while/etc brace do not go on next line, unless defining a do while loop,
2891# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002892 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 -07002893 my $pre_ctx = "$1$2";
2894
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002895 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002896
2897 if ($line =~ /^\+\t{6,}/) {
2898 WARN("DEEP_INDENTATION",
2899 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2900 }
2901
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002902 my $ctx_cnt = $realcnt - $#ctx - 1;
2903 my $ctx = join("\n", @ctx);
2904
Andy Whitcroft548596d2008-07-23 21:29:01 -07002905 my $ctx_ln = $linenr;
2906 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002907
Andy Whitcroft548596d2008-07-23 21:29:01 -07002908 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2909 defined $lines[$ctx_ln - 1] &&
2910 $lines[$ctx_ln - 1] =~ /^-/)) {
2911 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2912 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002913 $ctx_ln++;
2914 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002915
Andy Whitcroft53210162008-07-23 21:29:03 -07002916 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2917 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002918
Joe Perchesd752fcc2014-08-06 16:11:05 -07002919 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002920 ERROR("OPEN_BRACE",
2921 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002922 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002923 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002924 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2925 $ctx =~ /\)\s*\;\s*$/ &&
2926 defined $lines[$ctx_ln - 1])
2927 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002928 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2929 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002930 WARN("TRAILING_SEMICOLON",
2931 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002932 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002933 }
2934 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002935 }
2936
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002937# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002938 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 -08002939 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2940 ctx_statement_block($linenr, $realcnt, 0)
2941 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002942 my ($s, $c) = ($stat, $cond);
2943
2944 substr($s, 0, length($c), '');
2945
2946 # Make sure we remove the line prefixes as we have
2947 # none on the first line, and are going to readd them
2948 # where necessary.
2949 $s =~ s/\n./\n/gs;
2950
2951 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002952 my @newlines = ($c =~ /\n/gs);
2953 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002954
2955 # We want to check the first line inside the block
2956 # starting at the end of the conditional, so remove:
2957 # 1) any blank line termination
2958 # 2) any opening brace { on end of the line
2959 # 3) any do (...) {
2960 my $continuation = 0;
2961 my $check = 0;
2962 $s =~ s/^.*\bdo\b//;
2963 $s =~ s/^\s*{//;
2964 if ($s =~ s/^\s*\\//) {
2965 $continuation = 1;
2966 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002967 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002968 $check = 1;
2969 $cond_lines++;
2970 }
2971
2972 # Also ignore a loop construct at the end of a
2973 # preprocessor statement.
2974 if (($prevline =~ /^.\s*#\s*define\s/ ||
2975 $prevline =~ /\\\s*$/) && $continuation == 0) {
2976 $check = 0;
2977 }
2978
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002979 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002980 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002981 while ($cond_ptr != $cond_lines) {
2982 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002983
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002984 # If we see an #else/#elif then the code
2985 # is not linear.
2986 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2987 $check = 0;
2988 }
2989
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002990 # Ignore:
2991 # 1) blank lines, they should be at 0,
2992 # 2) preprocessor lines, and
2993 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002994 if ($continuation ||
2995 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002996 $s =~ /^\s*#\s*?/ ||
2997 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002998 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002999 if ($s =~ s/^.*?\n//) {
3000 $cond_lines++;
3001 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003002 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003003 }
3004
3005 my (undef, $sindent) = line_stats("+" . $s);
3006 my $stat_real = raw_line($linenr, $cond_lines);
3007
3008 # Check if either of these lines are modified, else
3009 # this is not this patch's fault.
3010 if (!defined($stat_real) ||
3011 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3012 $check = 0;
3013 }
3014 if (defined($stat_real) && $cond_lines > 1) {
3015 $stat_real = "[...]\n$stat_real";
3016 }
3017
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003018 #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 -07003019
3020 if ($check && (($sindent % 8) != 0 ||
3021 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003022 WARN("SUSPECT_CODE_INDENT",
3023 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003024 }
3025 }
3026
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003027 # Track the 'values' across context and added lines.
3028 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003029 my ($curr_values, $curr_vars) =
3030 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003031 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003032 if ($dbg_values) {
3033 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003034 print "$linenr > .$outline\n";
3035 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003036 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003037 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003038 $prev_values = substr($curr_values, -1);
3039
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003040#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003041 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003042
Andy Whitcroft653d4872007-06-23 17:16:34 -07003043# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003044 if ($dbg_type) {
3045 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003046 ERROR("TEST_TYPE",
3047 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003048 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003049 ERROR("TEST_NOT_TYPE",
3050 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003051 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003052 next;
3053 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003054# TEST: allow direct testing of the attribute matcher.
3055 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003056 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003057 ERROR("TEST_ATTR",
3058 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003059 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003060 ERROR("TEST_NOT_ATTR",
3061 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003062 }
3063 next;
3064 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003065
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003066# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003067 if ($line =~ /^.\s*{/ &&
3068 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003069 if (ERROR("OPEN_BRACE",
3070 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003071 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3072 fix_delete_line($fixlinenr - 1, $prevrawline);
3073 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003074 my $fixedline = $prevrawline;
3075 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003076 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003077 $fixedline = $line;
3078 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003079 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003080 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003081 }
3082
Andy Whitcroft653d4872007-06-23 17:16:34 -07003083#
3084# Checks which are anchored on the added line.
3085#
3086
3087# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003088 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003089 my $path = $1;
3090 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003091 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003092 "malformed #include filename\n" . $herecurr);
3093 }
3094 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3095 ERROR("UAPI_INCLUDE",
3096 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003097 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003098 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003099
3100# no C99 // comments
3101 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003102 if (ERROR("C99_COMMENTS",
3103 "do not use C99 // comments\n" . $herecurr) &&
3104 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003105 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003106 if ($line =~ /\/\/(.*)$/) {
3107 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003108 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003109 }
3110 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003111 }
3112 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003113 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003114 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003115
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003116# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3117# the whole statement.
3118#print "APW <$lines[$realline_next - 1]>\n";
3119 if (defined $realline_next &&
3120 exists $lines[$realline_next - 1] &&
3121 !defined $suppress_export{$realline_next} &&
3122 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3123 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003124 # Handle definitions which produce identifiers with
3125 # a prefix:
3126 # XXX(foo);
3127 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003128 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003129 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003130 $name =~ /^${Ident}_$2/) {
3131#print "FOO C name<$name>\n";
3132 $suppress_export{$realline_next} = 1;
3133
3134 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003135 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003136 ^.DEFINE_$Ident\(\Q$name\E\)|
3137 ^.DECLARE_$Ident\(\Q$name\E\)|
3138 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003139 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3140 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003141 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003142#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3143 $suppress_export{$realline_next} = 2;
3144 } else {
3145 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003146 }
3147 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003148 if (!defined $suppress_export{$linenr} &&
3149 $prevline =~ /^.\s*$/ &&
3150 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3151 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3152#print "FOO B <$lines[$linenr - 1]>\n";
3153 $suppress_export{$linenr} = 2;
3154 }
3155 if (defined $suppress_export{$linenr} &&
3156 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003157 WARN("EXPORT_SYMBOL",
3158 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003159 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003160
Joe Eloff5150bda2010-08-09 17:21:00 -07003161# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003162 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3163 if (ERROR("GLOBAL_INITIALISERS",
3164 "do not initialise globals to 0 or NULL\n" .
3165 $herecurr) &&
3166 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003167 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003168 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003169 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003170# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003171 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3172 if (ERROR("INITIALISED_STATIC",
3173 "do not initialise statics to 0 or NULL\n" .
3174 $herecurr) &&
3175 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003176 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003177 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003178 }
3179
Joe Perches18130872014-08-06 16:11:22 -07003180# check for misordered declarations of char/short/int/long with signed/unsigned
3181 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3182 my $tmp = trim($1);
3183 WARN("MISORDERED_TYPE",
3184 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3185 }
3186
Joe Perchescb710ec2010-10-26 14:23:20 -07003187# check for static const char * arrays.
3188 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003189 WARN("STATIC_CONST_CHAR_ARRAY",
3190 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003191 $herecurr);
3192 }
3193
3194# check for static char foo[] = "bar" declarations.
3195 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003196 WARN("STATIC_CONST_CHAR_ARRAY",
3197 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003198 $herecurr);
3199 }
3200
Joe Perchesab7e23f2015-04-16 12:44:22 -07003201# check for const <foo> const where <foo> is not a pointer or array type
3202 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3203 my $found = $1;
3204 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3205 WARN("CONST_CONST",
3206 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3207 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3208 WARN("CONST_CONST",
3209 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3210 }
3211 }
3212
Joe Perches9b0fa602014-04-03 14:49:18 -07003213# check for non-global char *foo[] = {"bar", ...} declarations.
3214 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3215 WARN("STATIC_CONST_CHAR_ARRAY",
3216 "char * array declaration might be better as static const\n" .
3217 $herecurr);
3218 }
3219
Joe Perchesb36190c2014-01-27 17:07:18 -08003220# check for function declarations without arguments like "int foo()"
3221 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3222 if (ERROR("FUNCTION_WITHOUT_ARGS",
3223 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3224 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003225 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003226 }
3227 }
3228
Joe Perches92e112f2013-12-13 11:36:22 -07003229# check for uses of DEFINE_PCI_DEVICE_TABLE
3230 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3231 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3232 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3233 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003234 $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 -07003235 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003236 }
3237
Andy Whitcroft653d4872007-06-23 17:16:34 -07003238# check for new typedefs, only function parameters and sparse annotations
3239# make sense.
3240 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003241 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003242 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003243 $line !~ /\b$typeTypedefs\b/ &&
Joe Perches021158b2015-02-13 14:38:43 -08003244 $line !~ /\b$typeOtherOSTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003245 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003246 WARN("NEW_TYPEDEFS",
3247 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003248 }
3249
3250# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003251 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003252 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3253 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003254 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003255
Andy Whitcroft65863862009-01-06 14:41:21 -08003256 # Should start with a space.
3257 $to =~ s/^(\S)/ $1/;
3258 # Should not end with a space.
3259 $to =~ s/\s+$//;
3260 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003261 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003262 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003263
Joe Perches3705ce52013-07-03 15:05:31 -07003264## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003265 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003266 if (ERROR("POINTER_LOCATION",
3267 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3268 $fix) {
3269 my $sub_from = $ident;
3270 my $sub_to = $ident;
3271 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003272 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003273 s@\Q$sub_from\E@$sub_to@;
3274 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003275 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003276 }
3277 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3278 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003279 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003280
Andy Whitcroft65863862009-01-06 14:41:21 -08003281 # Should start with a space.
3282 $to =~ s/^(\S)/ $1/;
3283 # Should not end with a space.
3284 $to =~ s/\s+$//;
3285 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003286 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003287 }
3288 # Modifiers should have spaces.
3289 $to =~ s/(\b$Modifier$)/$1 /;
3290
Joe Perches3705ce52013-07-03 15:05:31 -07003291## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003292 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003293 if (ERROR("POINTER_LOCATION",
3294 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3295 $fix) {
3296
3297 my $sub_from = $match;
3298 my $sub_to = $match;
3299 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003300 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003301 s@\Q$sub_from\E@$sub_to@;
3302 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003303 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003304 }
3305
3306# # no BUG() or BUG_ON()
3307# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3308# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3309# print "$herecurr";
3310# $clean = 0;
3311# }
3312
Andy Whitcroft8905a672007-11-28 16:21:06 -08003313 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003314 WARN("LINUX_VERSION_CODE",
3315 "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 -08003316 }
3317
Joe Perches17441222011-06-15 15:08:17 -07003318# check for uses of printk_ratelimit
3319 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003320 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003321 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003322 }
3323
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003324# printk should use KERN_* levels. Note that follow on printk's on the
3325# same line do not need a level, so we use the current block context
3326# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003327# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003328# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003329 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003330 my $ok = 0;
3331 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3332 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003333 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003334 # with "\n" ignore it, else it is to blame
3335 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3336 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3337 $ok = 1;
3338 }
3339 last;
3340 }
3341 }
3342 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003343 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3344 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003345 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003346 }
3347
Joe Perches243f3802012-05-31 16:26:09 -07003348 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3349 my $orig = $1;
3350 my $level = lc($orig);
3351 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003352 my $level2 = $level;
3353 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003354 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003355 "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 -07003356 }
3357
3358 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003359 if (WARN("PREFER_PR_LEVEL",
3360 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3361 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003362 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003363 s/\bpr_warning\b/pr_warn/;
3364 }
Joe Perches243f3802012-05-31 16:26:09 -07003365 }
3366
Joe Perchesdc139312013-02-21 16:44:13 -08003367 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3368 my $orig = $1;
3369 my $level = lc($orig);
3370 $level = "warn" if ($level eq "warning");
3371 $level = "dbg" if ($level eq "debug");
3372 WARN("PREFER_DEV_LEVEL",
3373 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3374 }
3375
Andy Whitcroft653d4872007-06-23 17:16:34 -07003376# function brace can't be on same line, except for #defines of do while,
3377# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003378 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003379 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003380 if (ERROR("OPEN_BRACE",
3381 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3382 $fix) {
3383 fix_delete_line($fixlinenr, $rawline);
3384 my $fixed_line = $rawline;
3385 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3386 my $line1 = $1;
3387 my $line2 = $2;
3388 fix_insert_line($fixlinenr, ltrim($line1));
3389 fix_insert_line($fixlinenr, "\+{");
3390 if ($line2 !~ /^\s*$/) {
3391 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3392 }
3393 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003394 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003395
Andy Whitcroft8905a672007-11-28 16:21:06 -08003396# open braces for enum, union and struct go on the same line.
3397 if ($line =~ /^.\s*{/ &&
3398 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003399 if (ERROR("OPEN_BRACE",
3400 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3401 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3402 fix_delete_line($fixlinenr - 1, $prevrawline);
3403 fix_delete_line($fixlinenr, $rawline);
3404 my $fixedline = rtrim($prevrawline) . " {";
3405 fix_insert_line($fixlinenr, $fixedline);
3406 $fixedline = $rawline;
3407 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3408 if ($fixedline !~ /^\+\s*$/) {
3409 fix_insert_line($fixlinenr, $fixedline);
3410 }
3411 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003412 }
3413
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003414# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003415 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3416 if (WARN("SPACING",
3417 "missing space after $1 definition\n" . $herecurr) &&
3418 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003419 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003420 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3421 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003422 }
3423
Joe Perches31070b52014-01-23 15:54:49 -08003424# Function pointer declarations
3425# check spacing between type, funcptr, and args
3426# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003427 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003428 my $declare = $1;
3429 my $pre_pointer_space = $2;
3430 my $post_pointer_space = $3;
3431 my $funcname = $4;
3432 my $post_funcname_space = $5;
3433 my $pre_args_space = $6;
3434
Joe Perches91f72e92014-04-03 14:49:12 -07003435# the $Declare variable will capture all spaces after the type
3436# so check it for a missing trailing missing space but pointer return types
3437# don't need a space so don't warn for those.
3438 my $post_declare_space = "";
3439 if ($declare =~ /(\s+)$/) {
3440 $post_declare_space = $1;
3441 $declare = rtrim($declare);
3442 }
3443 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003444 WARN("SPACING",
3445 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003446 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003447 }
3448
3449# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003450# This test is not currently implemented because these declarations are
3451# equivalent to
3452# int foo(int bar, ...)
3453# and this is form shouldn't/doesn't generate a checkpatch warning.
3454#
3455# elsif ($declare =~ /\s{2,}$/) {
3456# WARN("SPACING",
3457# "Multiple spaces after return type\n" . $herecurr);
3458# }
Joe Perches31070b52014-01-23 15:54:49 -08003459
3460# unnecessary space "type ( *funcptr)(args...)"
3461 if (defined $pre_pointer_space &&
3462 $pre_pointer_space =~ /^\s/) {
3463 WARN("SPACING",
3464 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3465 }
3466
3467# unnecessary space "type (* funcptr)(args...)"
3468 if (defined $post_pointer_space &&
3469 $post_pointer_space =~ /^\s/) {
3470 WARN("SPACING",
3471 "Unnecessary space before function pointer name\n" . $herecurr);
3472 }
3473
3474# unnecessary space "type (*funcptr )(args...)"
3475 if (defined $post_funcname_space &&
3476 $post_funcname_space =~ /^\s/) {
3477 WARN("SPACING",
3478 "Unnecessary space after function pointer name\n" . $herecurr);
3479 }
3480
3481# unnecessary space "type (*funcptr) (args...)"
3482 if (defined $pre_args_space &&
3483 $pre_args_space =~ /^\s/) {
3484 WARN("SPACING",
3485 "Unnecessary space before function pointer arguments\n" . $herecurr);
3486 }
3487
3488 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003489 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003490 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003491 }
3492 }
3493
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003494# check for spacing round square brackets; allowed:
3495# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003496# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3497# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003498 while ($line =~ /(.*?\s)\[/g) {
3499 my ($where, $prefix) = ($-[1], $1);
3500 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003501 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003502 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003503 if (ERROR("BRACKET_SPACE",
3504 "space prohibited before open square bracket '['\n" . $herecurr) &&
3505 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003506 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003507 s/^(\+.*?)\s+\[/$1\[/;
3508 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003509 }
3510 }
3511
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003512# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003513 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003514 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003515 my $ctx_before = substr($line, 0, $-[1]);
3516 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003517
3518 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003519 if ($name =~ /^(?:
3520 if|for|while|switch|return|case|
3521 volatile|__volatile__|
3522 __attribute__|format|__extension__|
3523 asm|__asm__)$/x)
3524 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003525 # cpp #define statements have non-optional spaces, ie
3526 # if there is a space between the name and the open
3527 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003528 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003529
3530 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003531 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003532
3533 # If this whole things ends with a type its most
3534 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003535 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003536
3537 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003538 if (WARN("SPACING",
3539 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3540 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003541 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003542 s/\b$name\s+\(/$name\(/;
3543 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003544 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003545 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003546
Andy Whitcroft653d4872007-06-23 17:16:34 -07003547# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003548 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003549 my $fixed_line = "";
3550 my $line_fixed = 0;
3551
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003552 my $ops = qr{
3553 <<=|>>=|<=|>=|==|!=|
3554 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3555 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003556 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003557 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003558 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003559 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003560
3561## print("element count: <" . $#elements . ">\n");
3562## foreach my $el (@elements) {
3563## print("el: <$el>\n");
3564## }
3565
3566 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003567 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003568
Joe Perches3705ce52013-07-03 15:05:31 -07003569 foreach my $el (@elements) {
3570 push(@fix_elements, substr($rawline, $off, length($el)));
3571 $off += length($el);
3572 }
3573
3574 $off = 0;
3575
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003576 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003577 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003578
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003579 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003580
3581 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3582
3583## print("n: <$n> good: <$good>\n");
3584
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003585 $off += length($elements[$n]);
3586
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003587 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003588 my $ca = substr($opline, 0, $off);
3589 my $cc = '';
3590 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3591 $cc = substr($opline, $off + length($elements[$n + 1]));
3592 }
3593 my $cb = "$ca$;$cc";
3594
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003595 my $a = '';
3596 $a = 'V' if ($elements[$n] ne '');
3597 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003598 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003599 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3600 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003601 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003602
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003603 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003604
3605 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003606 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003607 $c = 'V' if ($elements[$n + 2] ne '');
3608 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003609 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003610 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3611 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003612 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003613 } else {
3614 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003615 }
3616
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003617 my $ctx = "${a}x${c}";
3618
3619 my $at = "(ctx:$ctx)";
3620
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003621 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003622 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003623
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003624 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003625 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003626
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003627 # Get the full operator variant.
3628 my $opv = $op . substr($curr_vars, $off, 1);
3629
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003630 # Ignore operators passed as parameters.
3631 if ($op_type ne 'V' &&
3632 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3633
Andy Whitcroftcf655042008-03-04 14:28:20 -08003634# # Ignore comments
3635# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003636
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003637 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003638 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003639 if ($ctx !~ /.x[WEBC]/ &&
3640 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003641 if (ERROR("SPACING",
3642 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003643 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003644 $line_fixed = 1;
3645 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003646 }
3647
3648 # // is a comment
3649 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003650
Joe Perchesb00e4812014-04-03 14:49:33 -07003651 # : when part of a bitfield
3652 } elsif ($opv eq ':B') {
3653 # skip the bitfield test for now
3654
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003655 # No spaces for:
3656 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003657 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003658 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003659 if (ERROR("SPACING",
3660 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003661 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003662 if (defined $fix_elements[$n + 2]) {
3663 $fix_elements[$n + 2] =~ s/^\s+//;
3664 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003665 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003666 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003667 }
3668
Joe Perches23810972014-12-10 15:51:32 -08003669 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003670 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003671 my $rtrim_before = 0;
3672 my $space_after = 0;
3673 if ($ctx =~ /Wx./) {
3674 if (ERROR("SPACING",
3675 "space prohibited before that '$op' $at\n" . $hereptr)) {
3676 $line_fixed = 1;
3677 $rtrim_before = 1;
3678 }
3679 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003680 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003681 if (ERROR("SPACING",
3682 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003683 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003684 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003685 $space_after = 1;
3686 }
3687 }
3688 if ($rtrim_before || $space_after) {
3689 if ($rtrim_before) {
3690 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3691 } else {
3692 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3693 }
3694 if ($space_after) {
3695 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003696 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003697 }
3698
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003699 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003700 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003701 #warn "'*' is part of type\n";
3702
3703 # unary operators should have a space before and
3704 # none after. May be left adjacent to another
3705 # unary operator, or a cast
3706 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003707 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003708 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003709 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003710 if (ERROR("SPACING",
3711 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003712 if ($n != $last_after + 2) {
3713 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3714 $line_fixed = 1;
3715 }
Joe Perches3705ce52013-07-03 15:05:31 -07003716 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003717 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003718 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003719 # A unary '*' may be const
3720
3721 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003722 if (ERROR("SPACING",
3723 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003724 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003725 if (defined $fix_elements[$n + 2]) {
3726 $fix_elements[$n + 2] =~ s/^\s+//;
3727 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003728 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003729 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003730 }
3731
3732 # unary ++ and unary -- are allowed no space on one side.
3733 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003734 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003735 if (ERROR("SPACING",
3736 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003737 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003738 $line_fixed = 1;
3739 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003740 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003741 if ($ctx =~ /Wx[BE]/ ||
3742 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003743 if (ERROR("SPACING",
3744 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003745 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003746 $line_fixed = 1;
3747 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003748 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003749 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003750 if (ERROR("SPACING",
3751 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003752 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003753 if (defined $fix_elements[$n + 2]) {
3754 $fix_elements[$n + 2] =~ s/^\s+//;
3755 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003756 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003757 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003758 }
3759
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003760 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003761 } elsif ($op eq '<<' or $op eq '>>' or
3762 $op eq '&' or $op eq '^' or $op eq '|' or
3763 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003764 $op eq '*' or $op eq '/' or
3765 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003766 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003767 if ($check) {
3768 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3769 if (CHK("SPACING",
3770 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3771 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3772 $fix_elements[$n + 2] =~ s/^\s+//;
3773 $line_fixed = 1;
3774 }
3775 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3776 if (CHK("SPACING",
3777 "space preferred before that '$op' $at\n" . $hereptr)) {
3778 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3779 $line_fixed = 1;
3780 }
3781 }
3782 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003783 if (ERROR("SPACING",
3784 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003785 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3786 if (defined $fix_elements[$n + 2]) {
3787 $fix_elements[$n + 2] =~ s/^\s+//;
3788 }
Joe Perches3705ce52013-07-03 15:05:31 -07003789 $line_fixed = 1;
3790 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003791 }
3792
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003793 # A colon needs no spaces before when it is
3794 # terminating a case value or a label.
3795 } elsif ($opv eq ':C' || $opv eq ':L') {
3796 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003797 if (ERROR("SPACING",
3798 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003799 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003800 $line_fixed = 1;
3801 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003802 }
3803
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003804 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003805 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003806 my $ok = 0;
3807
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003808 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003809 if (($op eq '<' &&
3810 $cc =~ /^\S+\@\S+>/) ||
3811 ($op eq '>' &&
3812 $ca =~ /<\S+\@\S+$/))
3813 {
3814 $ok = 1;
3815 }
3816
Joe Perches84731622013-11-12 15:10:05 -08003817 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003818 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003819 my $msg_type = \&ERROR;
3820 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3821
3822 if (&{$msg_type}("SPACING",
3823 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003824 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3825 if (defined $fix_elements[$n + 2]) {
3826 $fix_elements[$n + 2] =~ s/^\s+//;
3827 }
Joe Perches3705ce52013-07-03 15:05:31 -07003828 $line_fixed = 1;
3829 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003830 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003831 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003832 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003833
3834## print("n: <$n> GOOD: <$good>\n");
3835
3836 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003837 }
Joe Perches3705ce52013-07-03 15:05:31 -07003838
3839 if (($#elements % 2) == 0) {
3840 $fixed_line = $fixed_line . $fix_elements[$#elements];
3841 }
3842
Joe Perches194f66f2014-08-06 16:11:03 -07003843 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3844 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003845 }
3846
3847
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003848 }
3849
Joe Perches786b6322013-07-03 15:05:32 -07003850# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003851 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003852 if (WARN("SPACING",
3853 "space prohibited before semicolon\n" . $herecurr) &&
3854 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003855 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003856 s/^(\+.*\S)\s+;/$1;/;
3857 }
3858 }
3859
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003860# check for multiple assignments
3861 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003862 CHK("MULTIPLE_ASSIGNMENTS",
3863 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003864 }
3865
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003866## # check for multiple declarations, allowing for a function declaration
3867## # continuation.
3868## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3869## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3870##
3871## # Remove any bracketed sections to ensure we do not
3872## # falsly report the parameters of functions.
3873## my $ln = $line;
3874## while ($ln =~ s/\([^\(\)]*\)//g) {
3875## }
3876## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003877## WARN("MULTIPLE_DECLARATION",
3878## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003879## }
3880## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003881
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003882#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003883 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3884 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003885 if (ERROR("SPACING",
3886 "space required before the open brace '{'\n" . $herecurr) &&
3887 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003888 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003889 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003890 }
3891
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003892## # check for blank lines before declarations
3893## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3894## $prevrawline =~ /^.\s*$/) {
3895## WARN("SPACING",
3896## "No blank lines before declarations\n" . $hereprev);
3897## }
3898##
3899
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003900# closing brace should have a space following it when it has anything
3901# on the line
3902 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003903 if (ERROR("SPACING",
3904 "space required after that close brace '}'\n" . $herecurr) &&
3905 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003906 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003907 s/}((?!(?:,|;|\)))\S)/} $1/;
3908 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003909 }
3910
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003911# check spacing on square brackets
3912 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003913 if (ERROR("SPACING",
3914 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3915 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003916 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003917 s/\[\s+/\[/;
3918 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003919 }
3920 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003921 if (ERROR("SPACING",
3922 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3923 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003924 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003925 s/\s+\]/\]/;
3926 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003927 }
3928
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003929# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003930 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3931 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003932 if (ERROR("SPACING",
3933 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3934 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003935 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003936 s/\(\s+/\(/;
3937 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003938 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003939 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003940 $line !~ /for\s*\(.*;\s+\)/ &&
3941 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003942 if (ERROR("SPACING",
3943 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3944 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003945 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003946 s/\s+\)/\)/;
3947 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003948 }
3949
Joe Perchese2826fd2014-08-06 16:10:48 -07003950# check unnecessary parentheses around addressof/dereference single $Lvals
3951# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3952
3953 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003954 my $var = $1;
3955 if (CHK("UNNECESSARY_PARENTHESES",
3956 "Unnecessary parentheses around $var\n" . $herecurr) &&
3957 $fix) {
3958 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3959 }
3960 }
3961
3962# check for unnecessary parentheses around function pointer uses
3963# ie: (foo->bar)(); should be foo->bar();
3964# but not "if (foo->bar) (" to avoid some false positives
3965 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3966 my $var = $2;
3967 if (CHK("UNNECESSARY_PARENTHESES",
3968 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3969 $fix) {
3970 my $var2 = deparenthesize($var);
3971 $var2 =~ s/\s//g;
3972 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3973 }
3974 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003975
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003976#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003977 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003978 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003979 if (WARN("INDENTED_LABEL",
3980 "labels should not be indented\n" . $herecurr) &&
3981 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003982 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003983 s/^(.)\s+/$1/;
3984 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003985 }
3986
Joe Perches5b9553a2014-04-03 14:49:21 -07003987# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003988 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003989 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003990 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003991 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3992 my $value = $1;
3993 $value = deparenthesize($value);
3994 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3995 ERROR("RETURN_PARENTHESES",
3996 "return is not a function, parentheses are not required\n" . $herecurr);
3997 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003998 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003999 ERROR("SPACING",
4000 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004001 }
4002 }
Joe Perches507e5142013-11-12 15:10:13 -08004003
Joe Perchesb43ae212014-06-23 13:22:07 -07004004# unnecessary return in a void function
4005# at end-of-function, with the previous line a single leading tab, then return;
4006# and the line before that not a goto label target like "out:"
4007 if ($sline =~ /^[ \+]}\s*$/ &&
4008 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4009 $linenr >= 3 &&
4010 $lines[$linenr - 3] =~ /^[ +]/ &&
4011 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004012 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004013 "void function return statements are not generally useful\n" . $hereprev);
4014 }
Joe Perches9819cf22014-06-04 16:12:09 -07004015
Joe Perches189248d2014-01-23 15:54:47 -08004016# if statements using unnecessary parentheses - ie: if ((foo == bar))
4017 if ($^V && $^V ge 5.10.0 &&
4018 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4019 my $openparens = $1;
4020 my $count = $openparens =~ tr@\(@\(@;
4021 my $msg = "";
4022 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4023 my $comp = $4; #Not $1 because of $LvalOrFunc
4024 $msg = " - maybe == should be = ?" if ($comp eq "==");
4025 WARN("UNNECESSARY_PARENTHESES",
4026 "Unnecessary parentheses$msg\n" . $herecurr);
4027 }
4028 }
4029
Joe Perchesf34e4a42015-04-16 12:44:19 -07004030# Return of what appears to be an errno should normally be negative
4031 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004032 my $name = $1;
4033 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004034 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004035 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004036 }
4037 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004038
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004039# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004040 if ($line =~ /\b(if|while|for|switch)\(/) {
4041 if (ERROR("SPACING",
4042 "space required before the open parenthesis '('\n" . $herecurr) &&
4043 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004044 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004045 s/\b(if|while|for|switch)\(/$1 \(/;
4046 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004047 }
4048
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004049# Check for illegal assignment in if conditional -- and check for trailing
4050# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004051 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004052 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4053 ctx_statement_block($linenr, $realcnt, 0)
4054 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004055 my ($stat_next) = ctx_statement_block($line_nr_next,
4056 $remain_next, $off_next);
4057 $stat_next =~ s/\n./\n /g;
4058 ##print "stat<$stat> stat_next<$stat_next>\n";
4059
4060 if ($stat_next =~ /^\s*while\b/) {
4061 # If the statement carries leading newlines,
4062 # then count those as offsets.
4063 my ($whitespace) =
4064 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4065 my $offset =
4066 statement_rawlines($whitespace) - 1;
4067
4068 $suppress_whiletrailers{$line_nr_next +
4069 $offset} = 1;
4070 }
4071 }
4072 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004073 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004074 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004075 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004076
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004077 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004078 ERROR("ASSIGN_IN_IF",
4079 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004080 }
4081
4082 # Find out what is on the end of the line after the
4083 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004084 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004085 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004086 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004087 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4088 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004089 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004090 # Find out how long the conditional actually is.
4091 my @newlines = ($c =~ /\n/gs);
4092 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004093 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004094
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004095 $stat_real = raw_line($linenr, $cond_lines)
4096 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004097 if (defined($stat_real) && $cond_lines > 1) {
4098 $stat_real = "[...]\n$stat_real";
4099 }
4100
Joe Perches000d1cc12011-07-25 17:13:25 -07004101 ERROR("TRAILING_STATEMENTS",
4102 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004103 }
4104 }
4105
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004106# Check for bitwise tests written as boolean
4107 if ($line =~ /
4108 (?:
4109 (?:\[|\(|\&\&|\|\|)
4110 \s*0[xX][0-9]+\s*
4111 (?:\&\&|\|\|)
4112 |
4113 (?:\&\&|\|\|)
4114 \s*0[xX][0-9]+\s*
4115 (?:\&\&|\|\||\)|\])
4116 )/x)
4117 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004118 WARN("HEXADECIMAL_BOOLEAN_TEST",
4119 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004120 }
4121
Andy Whitcroft8905a672007-11-28 16:21:06 -08004122# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004123 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4124 my $s = $1;
4125 $s =~ s/$;//g; # Remove any comments
4126 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004127 ERROR("TRAILING_STATEMENTS",
4128 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004129 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004130 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004131# if should not continue a brace
4132 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004133 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004134 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004135 $herecurr);
4136 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004137# case and default should not have general statements after them
4138 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4139 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004140 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004141 \s*return\s+
4142 )/xg)
4143 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004144 ERROR("TRAILING_STATEMENTS",
4145 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004146 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004147
4148 # Check for }<nl>else {, these must be at the same
4149 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004150 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4151 $previndent == $indent) {
4152 if (ERROR("ELSE_AFTER_BRACE",
4153 "else should follow close brace '}'\n" . $hereprev) &&
4154 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4155 fix_delete_line($fixlinenr - 1, $prevrawline);
4156 fix_delete_line($fixlinenr, $rawline);
4157 my $fixedline = $prevrawline;
4158 $fixedline =~ s/}\s*$//;
4159 if ($fixedline !~ /^\+\s*$/) {
4160 fix_insert_line($fixlinenr, $fixedline);
4161 }
4162 $fixedline = $rawline;
4163 $fixedline =~ s/^(.\s*)else/$1} else/;
4164 fix_insert_line($fixlinenr, $fixedline);
4165 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004166 }
4167
Joe Perches8b8856f2014-08-06 16:11:14 -07004168 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4169 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004170 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4171
4172 # Find out what is on the end of the line after the
4173 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004174 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004175 $s =~ s/\n.*//g;
4176
4177 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004178 if (ERROR("WHILE_AFTER_BRACE",
4179 "while should follow close brace '}'\n" . $hereprev) &&
4180 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4181 fix_delete_line($fixlinenr - 1, $prevrawline);
4182 fix_delete_line($fixlinenr, $rawline);
4183 my $fixedline = $prevrawline;
4184 my $trailing = $rawline;
4185 $trailing =~ s/^\+//;
4186 $trailing = trim($trailing);
4187 $fixedline =~ s/}\s*$/} $trailing/;
4188 fix_insert_line($fixlinenr, $fixedline);
4189 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004190 }
4191 }
4192
Joe Perches95e2c602013-07-03 15:05:20 -07004193#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004194 while ($line =~ m{($Constant|$Lval)}g) {
4195 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004196
4197#gcc binary extension
4198 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004199 if (WARN("GCC_BINARY_CONSTANT",
4200 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4201 $fix) {
4202 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004203 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004204 s/\b$var\b/$hexval/;
4205 }
Joe Perches95e2c602013-07-03 15:05:20 -07004206 }
4207
4208#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004209 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004210 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004211#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004212 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004213#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004214 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4215#Ignore some three character SI units explicitly, like MiB and KHz
4216 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004217 while ($var =~ m{($Ident)}g) {
4218 my $word = $1;
4219 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004220 if ($check) {
4221 seed_camelcase_includes();
4222 if (!$file && !$camelcase_file_seeded) {
4223 seed_camelcase_file($realfile);
4224 $camelcase_file_seeded = 1;
4225 }
4226 }
Joe Perches7e781f62013-09-11 14:23:55 -07004227 if (!defined $camelcase{$word}) {
4228 $camelcase{$word} = 1;
4229 CHK("CAMELCASE",
4230 "Avoid CamelCase: <$word>\n" . $herecurr);
4231 }
Joe Perches34456862013-07-03 15:05:34 -07004232 }
Joe Perches323c1262012-12-17 16:02:07 -08004233 }
4234 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004235
4236#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004237 if ($line =~ /\#\s*define.*\\\s+$/) {
4238 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4239 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4240 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004241 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004242 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004243 }
4244
Fabian Frederick0e212e02015-04-16 12:44:25 -07004245# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4246# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004247 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004248 my $file = "$1.h";
4249 my $checkfile = "include/linux/$file";
4250 if (-f "$root/$checkfile" &&
4251 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004252 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004253 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004254 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4255 if ($asminclude > 0) {
4256 if ($realfile =~ m{^arch/}) {
4257 CHK("ARCH_INCLUDE_LINUX",
4258 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4259 } else {
4260 WARN("INCLUDE_LINUX",
4261 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4262 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004263 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004264 }
4265 }
4266
Andy Whitcroft653d4872007-06-23 17:16:34 -07004267# multi-statement macros should be enclosed in a do while loop, grab the
4268# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004269# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004270 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4271 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004272 my $ln = $linenr;
4273 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004274 my ($off, $dstat, $dcond, $rest);
4275 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004276 my $has_flow_statement = 0;
4277 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004278 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004279 ctx_statement_block($linenr, $realcnt, 0);
4280 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004281 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004282 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004283
Joe Perches08a28432014-10-13 15:51:55 -07004284 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4285 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4286
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004287 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004288 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004289 $dstat =~ s/\\\n.//g;
4290 $dstat =~ s/^\s*//s;
4291 $dstat =~ s/\s*$//s;
4292
4293 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004294 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4295 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004296 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004297 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004298 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004299
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004300 # Flatten any obvious string concatentation.
4301 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4302 $dstat =~ s/$Ident\s*("X*")/$1/)
4303 {
4304 }
4305
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004306 my $exceptions = qr{
4307 $Declare|
4308 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004309 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004310 DECLARE_PER_CPU|
4311 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004312 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004313 union|
4314 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004315 \.$Ident\s*=\s*|
4316 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004317 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004318 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004319 if ($dstat ne '' &&
4320 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4321 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004322 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004323 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004324 $dstat !~ /$exceptions/ &&
4325 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004326 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004327 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004328 $dstat !~ /^for\s*$Constant$/ && # for (...)
4329 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4330 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004331 $dstat !~ /^\({/ && # ({...
4332 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004333 {
4334 $ctx =~ s/\n*$//;
4335 my $herectx = $here . "\n";
4336 my $cnt = statement_rawlines($ctx);
4337
4338 for (my $n = 0; $n < $cnt; $n++) {
4339 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004340 }
4341
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004342 if ($dstat =~ /;/) {
4343 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4344 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4345 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004346 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004347 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004348 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004349 }
Joe Perches5023d342012-12-17 16:01:47 -08004350
Joe Perches08a28432014-10-13 15:51:55 -07004351# check for macros with flow control, but without ## concatenation
4352# ## concatenation is commonly a macro that defines a function so ignore those
4353 if ($has_flow_statement && !$has_arg_concat) {
4354 my $herectx = $here . "\n";
4355 my $cnt = statement_rawlines($ctx);
4356
4357 for (my $n = 0; $n < $cnt; $n++) {
4358 $herectx .= raw_line($linenr, $n) . "\n";
4359 }
4360 WARN("MACRO_WITH_FLOW_CONTROL",
4361 "Macros with flow control statements should be avoided\n" . "$herectx");
4362 }
4363
Joe Perches481eb482012-12-17 16:01:56 -08004364# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004365
4366 } else {
4367 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004368 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4369 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004370 $line =~ /^\+.*\\$/) {
4371 WARN("LINE_CONTINUATIONS",
4372 "Avoid unnecessary line continuations\n" . $herecurr);
4373 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004374 }
4375
Joe Perchesb13edf72012-07-30 14:41:24 -07004376# do {} while (0) macro tests:
4377# single-statement macros do not need to be enclosed in do while (0) loop,
4378# macro should not end with a semicolon
4379 if ($^V && $^V ge 5.10.0 &&
4380 $realfile !~ m@/vmlinux.lds.h$@ &&
4381 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4382 my $ln = $linenr;
4383 my $cnt = $realcnt;
4384 my ($off, $dstat, $dcond, $rest);
4385 my $ctx = '';
4386 ($dstat, $dcond, $ln, $cnt, $off) =
4387 ctx_statement_block($linenr, $realcnt, 0);
4388 $ctx = $dstat;
4389
4390 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004391 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004392
4393 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4394 my $stmts = $2;
4395 my $semis = $3;
4396
4397 $ctx =~ s/\n*$//;
4398 my $cnt = statement_rawlines($ctx);
4399 my $herectx = $here . "\n";
4400
4401 for (my $n = 0; $n < $cnt; $n++) {
4402 $herectx .= raw_line($linenr, $n) . "\n";
4403 }
4404
Joe Perchesac8e97f2012-08-21 16:15:53 -07004405 if (($stmts =~ tr/;/;/) == 1 &&
4406 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004407 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4408 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4409 }
4410 if (defined $semis && $semis ne "") {
4411 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4412 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4413 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004414 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4415 $ctx =~ s/\n*$//;
4416 my $cnt = statement_rawlines($ctx);
4417 my $herectx = $here . "\n";
4418
4419 for (my $n = 0; $n < $cnt; $n++) {
4420 $herectx .= raw_line($linenr, $n) . "\n";
4421 }
4422
4423 WARN("TRAILING_SEMICOLON",
4424 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004425 }
4426 }
4427
Mike Frysinger080ba922009-01-06 14:41:25 -08004428# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4429# all assignments may have only one of the following with an assignment:
4430# .
4431# ALIGN(...)
4432# VMLINUX_SYMBOL(...)
4433 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004434 WARN("MISSING_VMLINUX_SYMBOL",
4435 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004436 }
4437
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004438# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004439 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4440 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004441 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004442 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004443 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4444 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004445 my @allowed = ();
4446 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004447 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004448 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004449 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004450 for my $chunk (@chunks) {
4451 my ($cond, $block) = @{$chunk};
4452
Andy Whitcroft773647a2008-03-28 14:15:58 -07004453 # If the condition carries leading newlines, then count those as offsets.
4454 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4455 my $offset = statement_rawlines($whitespace) - 1;
4456
Joe Perchesaad4f612012-03-23 15:02:19 -07004457 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004458 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4459
4460 # We have looked at and allowed this specific line.
4461 $suppress_ifbraces{$ln + $offset} = 1;
4462
4463 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004464 $ln += statement_rawlines($block) - 1;
4465
Andy Whitcroft773647a2008-03-28 14:15:58 -07004466 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004467
4468 $seen++ if ($block =~ /^\s*{/);
4469
Joe Perchesaad4f612012-03-23 15:02:19 -07004470 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004471 if (statement_lines($cond) > 1) {
4472 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004473 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004474 }
4475 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004476 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004477 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004478 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004479 if (statement_block_size($block) > 1) {
4480 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004481 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004482 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004483 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004484 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004485 if ($seen) {
4486 my $sum_allowed = 0;
4487 foreach (@allowed) {
4488 $sum_allowed += $_;
4489 }
4490 if ($sum_allowed == 0) {
4491 WARN("BRACES",
4492 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4493 } elsif ($sum_allowed != $allow &&
4494 $seen != $allow) {
4495 CHK("BRACES",
4496 "braces {} should be used on all arms of this statement\n" . $herectx);
4497 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004498 }
4499 }
4500 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004501 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004502 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004503 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004504
Andy Whitcroftcf655042008-03-04 14:28:20 -08004505 # Check the pre-context.
4506 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4507 #print "APW: ALLOWED: pre<$1>\n";
4508 $allowed = 1;
4509 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004510
4511 my ($level, $endln, @chunks) =
4512 ctx_statement_full($linenr, $realcnt, $-[0]);
4513
Andy Whitcroftcf655042008-03-04 14:28:20 -08004514 # Check the condition.
4515 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004516 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004517 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004518 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004519 }
4520 if (statement_lines($cond) > 1) {
4521 #print "APW: ALLOWED: cond<$cond>\n";
4522 $allowed = 1;
4523 }
4524 if ($block =~/\b(?:if|for|while)\b/) {
4525 #print "APW: ALLOWED: block<$block>\n";
4526 $allowed = 1;
4527 }
4528 if (statement_block_size($block) > 1) {
4529 #print "APW: ALLOWED: lines block<$block>\n";
4530 $allowed = 1;
4531 }
4532 # Check the post-context.
4533 if (defined $chunks[1]) {
4534 my ($cond, $block) = @{$chunks[1]};
4535 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004536 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004537 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004538 if ($block =~ /^\s*\{/) {
4539 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4540 $allowed = 1;
4541 }
4542 }
4543 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004544 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004545 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004546
Andy Whitcroftf0556632008-10-15 22:02:23 -07004547 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004548 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004549 }
4550
Joe Perches000d1cc12011-07-25 17:13:25 -07004551 WARN("BRACES",
4552 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004553 }
4554 }
4555
Joe Perches0979ae62012-12-17 16:01:59 -08004556# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004557 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004558 if (CHK("BRACES",
4559 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4560 $fix && $prevrawline =~ /^\+/) {
4561 fix_delete_line($fixlinenr - 1, $prevrawline);
4562 }
Joe Perches0979ae62012-12-17 16:01:59 -08004563 }
Joe Perches77b9a532013-07-03 15:05:29 -07004564 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004565 if (CHK("BRACES",
4566 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4567 $fix) {
4568 fix_delete_line($fixlinenr, $rawline);
4569 }
Joe Perches0979ae62012-12-17 16:01:59 -08004570 }
4571
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004572# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004573 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4574 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004575 WARN("VOLATILE",
4576 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004577 }
4578
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004579# Check for user-visible strings broken across lines, which breaks the ability
4580# to grep for the string. Make exceptions when the previous string ends in a
4581# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4582# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4583 if ($line =~ /^\+\s*"[X\t]*"/ &&
4584 $prevline =~ /"\s*$/ &&
4585 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4586 if (WARN("SPLIT_STRING",
4587 "quoted string split across lines\n" . $hereprev) &&
4588 $fix &&
4589 $prevrawline =~ /^\+.*"\s*$/ &&
4590 $last_coalesced_string_linenr != $linenr - 1) {
4591 my $extracted_string = get_quoted_string($line, $rawline);
4592 my $comma_close = "";
4593 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4594 $comma_close = $1;
4595 }
4596
4597 fix_delete_line($fixlinenr - 1, $prevrawline);
4598 fix_delete_line($fixlinenr, $rawline);
4599 my $fixedline = $prevrawline;
4600 $fixedline =~ s/"\s*$//;
4601 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4602 fix_insert_line($fixlinenr - 1, $fixedline);
4603 $fixedline = $rawline;
4604 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4605 if ($fixedline !~ /\+\s*$/) {
4606 fix_insert_line($fixlinenr, $fixedline);
4607 }
4608 $last_coalesced_string_linenr = $linenr;
4609 }
4610 }
4611
4612# check for missing a space in a string concatenation
4613 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4614 WARN('MISSING_SPACE',
4615 "break quoted strings at a space character\n" . $hereprev);
4616 }
4617
4618# check for spaces before a quoted newline
4619 if ($rawline =~ /^.*\".*\s\\n/) {
4620 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4621 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4622 $fix) {
4623 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4624 }
4625
4626 }
4627
Joe Perchesf17dba42014-10-13 15:51:51 -07004628# concatenated string without spaces between elements
4629 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4630 CHK("CONCATENATED_STRING",
4631 "Concatenated strings should use spaces between elements\n" . $herecurr);
4632 }
4633
Joe Perches90ad30e2014-12-10 15:51:59 -08004634# uncoalesced string fragments
4635 if ($line =~ /"X*"\s*"/) {
4636 WARN("STRING_FRAGMENTS",
4637 "Consecutive strings are generally better as a single string\n" . $herecurr);
4638 }
4639
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004640# check for %L{u,d,i} in strings
4641 my $string;
4642 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4643 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4644 $string =~ s/%%/__/g;
4645 if ($string =~ /(?<!%)%L[udi]/) {
4646 WARN("PRINTF_L",
4647 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4648 last;
4649 }
4650 }
4651
4652# check for line continuations in quoted strings with odd counts of "
4653 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4654 WARN("LINE_CONTINUATIONS",
4655 "Avoid line continuations in quoted strings\n" . $herecurr);
4656 }
4657
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004658# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004659 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004660 CHK("REDUNDANT_CODE",
4661 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004662 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004663 }
4664
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004665# check for needless "if (<foo>) fn(<foo>)" uses
4666 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4667 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4668 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4669 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004670 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004671 }
4672 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004673
Joe Perchesebfdc402014-08-06 16:10:27 -07004674# check for unnecessary "Out of Memory" messages
4675 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4676 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4677 (defined $1 || defined $3) &&
4678 $linenr > 3) {
4679 my $testval = $2;
4680 my $testline = $lines[$linenr - 3];
4681
4682 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4683# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4684
4685 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4686 WARN("OOM_MESSAGE",
4687 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4688 }
4689 }
4690
Joe Perchesf78d98f2014-10-13 15:52:01 -07004691# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004692 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004693 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4694 my $level = $1;
4695 if (WARN("UNNECESSARY_KERN_LEVEL",
4696 "Possible unnecessary $level\n" . $herecurr) &&
4697 $fix) {
4698 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4699 }
4700 }
4701
Joe Perchesabb08a52014-12-10 15:51:46 -08004702# check for mask then right shift without a parentheses
4703 if ($^V && $^V ge 5.10.0 &&
4704 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4705 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4706 WARN("MASK_THEN_SHIFT",
4707 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4708 }
4709
Joe Perchesb75ac612014-12-10 15:52:02 -08004710# check for pointer comparisons to NULL
4711 if ($^V && $^V ge 5.10.0) {
4712 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4713 my $val = $1;
4714 my $equal = "!";
4715 $equal = "" if ($4 eq "!=");
4716 if (CHK("COMPARISON_TO_NULL",
4717 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4718 $fix) {
4719 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4720 }
4721 }
4722 }
4723
Joe Perches8716de32013-09-11 14:24:05 -07004724# check for bad placement of section $InitAttribute (e.g.: __initdata)
4725 if ($line =~ /(\b$InitAttribute\b)/) {
4726 my $attr = $1;
4727 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4728 my $ptr = $1;
4729 my $var = $2;
4730 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4731 ERROR("MISPLACED_INIT",
4732 "$attr should be placed after $var\n" . $herecurr)) ||
4733 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4734 WARN("MISPLACED_INIT",
4735 "$attr should be placed after $var\n" . $herecurr))) &&
4736 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004737 $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 -07004738 }
4739 }
4740 }
4741
Joe Perchese970b8842013-11-12 15:10:10 -08004742# check for $InitAttributeData (ie: __initdata) with const
4743 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4744 my $attr = $1;
4745 $attr =~ /($InitAttributePrefix)(.*)/;
4746 my $attr_prefix = $1;
4747 my $attr_type = $2;
4748 if (ERROR("INIT_ATTRIBUTE",
4749 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4750 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004751 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004752 s/$InitAttributeData/${attr_prefix}initconst/;
4753 }
4754 }
4755
4756# check for $InitAttributeConst (ie: __initconst) without const
4757 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4758 my $attr = $1;
4759 if (ERROR("INIT_ATTRIBUTE",
4760 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4761 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004762 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004763 /(^\+\s*(?:static\s+))/;
4764 $lead = rtrim($1);
4765 $lead = "$lead " if ($lead !~ /^\+$/);
4766 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004767 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004768 }
4769 }
4770
Joe Perchesfbdb8132014-04-03 14:49:14 -07004771# don't use __constant_<foo> functions outside of include/uapi/
4772 if ($realfile !~ m@^include/uapi/@ &&
4773 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4774 my $constant_func = $1;
4775 my $func = $constant_func;
4776 $func =~ s/^__constant_//;
4777 if (WARN("CONSTANT_CONVERSION",
4778 "$constant_func should be $func\n" . $herecurr) &&
4779 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004780 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004781 }
4782 }
4783
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004784# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004785 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004786 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004787 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004788 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004789 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004790 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4791 }
4792 if ($delay > 2000) {
4793 WARN("LONG_UDELAY",
4794 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004795 }
4796 }
4797
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004798# warn about unexpectedly long msleep's
4799 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4800 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004801 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004802 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004803 }
4804 }
4805
Joe Perches36ec1932013-07-03 15:05:25 -07004806# check for comparisons of jiffies
4807 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4808 WARN("JIFFIES_COMPARISON",
4809 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4810 }
4811
Joe Perches9d7a34a2013-07-03 15:05:26 -07004812# check for comparisons of get_jiffies_64()
4813 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4814 WARN("JIFFIES_COMPARISON",
4815 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4816 }
4817
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004818# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004819# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004820# print "#ifdef in C files should be avoided\n";
4821# print "$herecurr";
4822# $clean = 0;
4823# }
4824
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004825# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004826 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004827 if (ERROR("SPACING",
4828 "exactly one space required after that #$1\n" . $herecurr) &&
4829 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004830 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004831 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4832 }
4833
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004834 }
4835
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004836# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004837 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4838 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004839 my $which = $1;
4840 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004841 CHK("UNCOMMENTED_DEFINITION",
4842 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004843 }
4844 }
4845# check for memory barriers without a comment.
4846 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4847 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004848 WARN("MEMORY_BARRIER",
4849 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004850 }
4851 }
4852# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004853 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004854 CHK("ARCH_DEFINES",
4855 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004856 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004857
Tobias Klauserd4977c72010-05-24 14:33:30 -07004858# Check that the storage class is at the beginning of a declaration
4859 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004860 WARN("STORAGE_CLASS",
4861 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004862 }
4863
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004864# check the location of the inline attribute, that it is between
4865# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004866 if ($line =~ /\b$Type\s+$Inline\b/ ||
4867 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004868 ERROR("INLINE_LOCATION",
4869 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004870 }
4871
Andy Whitcroft8905a672007-11-28 16:21:06 -08004872# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004873 if ($realfile !~ m@\binclude/uapi/@ &&
4874 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004875 if (WARN("INLINE",
4876 "plain inline is preferred over $1\n" . $herecurr) &&
4877 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004878 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004879
4880 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004881 }
4882
Joe Perches3d130fd2011-01-12 17:00:00 -08004883# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004884 if ($realfile !~ m@\binclude/uapi/@ &&
4885 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004886 WARN("PREFER_PACKED",
4887 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004888 }
4889
Joe Perches39b7e282011-07-25 17:13:24 -07004890# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004891 if ($realfile !~ m@\binclude/uapi/@ &&
4892 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004893 WARN("PREFER_ALIGNED",
4894 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004895 }
4896
Joe Perches5f14d3b2012-01-10 15:09:52 -08004897# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004898 if ($realfile !~ m@\binclude/uapi/@ &&
4899 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004900 if (WARN("PREFER_PRINTF",
4901 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4902 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004903 $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 -07004904
4905 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004906 }
4907
Joe Perches6061d942012-03-23 15:02:16 -07004908# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004909 if ($realfile !~ m@\binclude/uapi/@ &&
4910 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004911 if (WARN("PREFER_SCANF",
4912 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4913 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004914 $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 -07004915 }
Joe Perches6061d942012-03-23 15:02:16 -07004916 }
4917
Joe Perches619a9082014-12-10 15:51:35 -08004918# Check for __attribute__ weak, or __weak declarations (may have link issues)
4919 if ($^V && $^V ge 5.10.0 &&
4920 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4921 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4922 $line =~ /\b__weak\b/)) {
4923 ERROR("WEAK_DECLARATION",
4924 "Using weak declarations can have unintended link defects\n" . $herecurr);
4925 }
4926
Joe Perches8f53a9b2010-03-05 13:43:48 -08004927# check for sizeof(&)
4928 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004929 WARN("SIZEOF_ADDRESS",
4930 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004931 }
4932
Joe Perches66c80b62012-07-30 14:41:22 -07004933# check for sizeof without parenthesis
4934 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004935 if (WARN("SIZEOF_PARENTHESIS",
4936 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4937 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004938 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004939 }
Joe Perches66c80b62012-07-30 14:41:22 -07004940 }
4941
Joe Perches88982fe2012-12-17 16:02:00 -08004942# check for struct spinlock declarations
4943 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4944 WARN("USE_SPINLOCK_T",
4945 "struct spinlock should be spinlock_t\n" . $herecurr);
4946 }
4947
Joe Perchesa6962d72013-04-29 16:18:13 -07004948# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004949 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004950 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08004951 $fmt =~ s/%%//g;
4952 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004953 if (WARN("PREFER_SEQ_PUTS",
4954 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4955 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004956 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004957 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004958 }
4959 }
4960
Andy Whitcroft554e1652012-01-10 15:09:57 -08004961# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004962 if ($^V && $^V ge 5.10.0 &&
4963 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004964 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004965
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004966 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004967 my $ms_val = $7;
4968 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004969
Andy Whitcroft554e1652012-01-10 15:09:57 -08004970 if ($ms_size =~ /^(0x|)0$/i) {
4971 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004972 "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 -08004973 } elsif ($ms_size =~ /^(0x|)1$/i) {
4974 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004975 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4976 }
4977 }
4978
Joe Perches98a9bba2014-01-23 15:54:52 -08004979# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4980 if ($^V && $^V ge 5.10.0 &&
4981 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4982 if (WARN("PREFER_ETHER_ADDR_COPY",
4983 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4984 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004985 $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 -08004986 }
4987 }
4988
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004989# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004990 if ($^V && $^V ge 5.10.0 &&
4991 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004992 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004993 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004994 my $call = $1;
4995 my $cast1 = deparenthesize($2);
4996 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004997 my $cast2 = deparenthesize($7);
4998 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004999 my $cast;
5000
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005001 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005002 $cast = "$cast1 or $cast2";
5003 } elsif ($cast1 ne "") {
5004 $cast = $cast1;
5005 } else {
5006 $cast = $cast2;
5007 }
5008 WARN("MINMAX",
5009 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005010 }
5011 }
5012
Joe Perches4a273192012-07-30 14:41:20 -07005013# check usleep_range arguments
5014 if ($^V && $^V ge 5.10.0 &&
5015 defined $stat &&
5016 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5017 my $min = $1;
5018 my $max = $7;
5019 if ($min eq $max) {
5020 WARN("USLEEP_RANGE",
5021 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5022 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5023 $min > $max) {
5024 WARN("USLEEP_RANGE",
5025 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5026 }
5027 }
5028
Joe Perches823b7942013-11-12 15:10:15 -08005029# check for naked sscanf
5030 if ($^V && $^V ge 5.10.0 &&
5031 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005032 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005033 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5034 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5035 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5036 my $lc = $stat =~ tr@\n@@;
5037 $lc = $lc + $linenr;
5038 my $stat_real = raw_line($linenr, 0);
5039 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5040 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5041 }
5042 WARN("NAKED_SSCANF",
5043 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5044 }
5045
Joe Perchesafc819a2014-06-04 16:12:08 -07005046# check for simple sscanf that should be kstrto<foo>
5047 if ($^V && $^V ge 5.10.0 &&
5048 defined $stat &&
5049 $line =~ /\bsscanf\b/) {
5050 my $lc = $stat =~ tr@\n@@;
5051 $lc = $lc + $linenr;
5052 my $stat_real = raw_line($linenr, 0);
5053 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5054 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5055 }
5056 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5057 my $format = $6;
5058 my $count = $format =~ tr@%@%@;
5059 if ($count == 1 &&
5060 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5061 WARN("SSCANF_TO_KSTRTO",
5062 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5063 }
5064 }
5065 }
5066
Joe Perches70dc8a42013-09-11 14:23:58 -07005067# check for new externs in .h files.
5068 if ($realfile =~ /\.h$/ &&
5069 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005070 if (CHK("AVOID_EXTERNS",
5071 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005072 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005073 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005074 }
5075 }
5076
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005077# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005078 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005079 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005080 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005081 my $function_name = $1;
5082 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005083
5084 my $s = $stat;
5085 if (defined $cond) {
5086 substr($s, 0, length($cond), '');
5087 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005088 if ($s =~ /^\s*;/ &&
5089 $function_name ne 'uninitialized_var')
5090 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005091 WARN("AVOID_EXTERNS",
5092 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005093 }
5094
5095 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005096 WARN("FUNCTION_ARGUMENTS",
5097 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005098 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005099
5100 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5101 $stat =~ /^.\s*extern\s+/)
5102 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005103 WARN("AVOID_EXTERNS",
5104 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005105 }
5106
5107# checks for new __setup's
5108 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5109 my $name = $1;
5110
5111 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005112 CHK("UNDOCUMENTED_SETUP",
5113 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005114 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005115 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005116
5117# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005118 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005119 WARN("UNNECESSARY_CASTS",
5120 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005121 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005122
Joe Perchesa640d252013-07-03 15:05:21 -07005123# alloc style
5124# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5125 if ($^V && $^V ge 5.10.0 &&
5126 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5127 CHK("ALLOC_SIZEOF_STRUCT",
5128 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5129 }
5130
Joe Perches60a55362014-06-04 16:12:07 -07005131# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5132 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005133 $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 -07005134 my $oldfunc = $3;
5135 my $a1 = $4;
5136 my $a2 = $10;
5137 my $newfunc = "kmalloc_array";
5138 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005139 my $r1 = $a1;
5140 my $r2 = $a2;
5141 if ($a1 =~ /^sizeof\s*\S/) {
5142 $r1 = $a2;
5143 $r2 = $a1;
5144 }
5145 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5146 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005147 if (WARN("ALLOC_WITH_MULTIPLY",
5148 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5149 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005150 $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 -07005151
5152 }
5153 }
5154 }
5155
Joe Perches972fdea2013-04-29 16:18:12 -07005156# check for krealloc arg reuse
5157 if ($^V && $^V ge 5.10.0 &&
5158 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5159 WARN("KREALLOC_ARG_REUSE",
5160 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5161 }
5162
Joe Perches5ce59ae2013-02-21 16:44:18 -08005163# check for alloc argument mismatch
5164 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5165 WARN("ALLOC_ARRAY_ARGS",
5166 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5167 }
5168
Joe Perchescaf2a542011-01-12 16:59:56 -08005169# check for multiple semicolons
5170 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005171 if (WARN("ONE_SEMICOLON",
5172 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5173 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005174 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005175 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005176 }
5177
Joe Perches0ab90192014-12-10 15:51:57 -08005178# check for #defines like: 1 << <digit> that could be BIT(digit)
5179 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5180 my $ull = "";
5181 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5182 if (CHK("BIT_MACRO",
5183 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5184 $fix) {
5185 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5186 }
5187 }
5188
Joe Perchese81f2392014-08-06 16:11:25 -07005189# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005190 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5191 my $has_break = 0;
5192 my $has_statement = 0;
5193 my $count = 0;
5194 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005195 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005196 $prevline--;
5197 my $rline = $rawlines[$prevline - 1];
5198 my $fline = $lines[$prevline - 1];
5199 last if ($fline =~ /^\@\@/);
5200 next if ($fline =~ /^\-/);
5201 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5202 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5203 next if ($fline =~ /^.[\s$;]*$/);
5204 $has_statement = 1;
5205 $count++;
5206 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5207 }
5208 if (!$has_break && $has_statement) {
5209 WARN("MISSING_BREAK",
5210 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5211 }
5212 }
5213
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005214# check for switch/default statements without a break;
5215 if ($^V && $^V ge 5.10.0 &&
5216 defined $stat &&
5217 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5218 my $ctx = '';
5219 my $herectx = $here . "\n";
5220 my $cnt = statement_rawlines($stat);
5221 for (my $n = 0; $n < $cnt; $n++) {
5222 $herectx .= raw_line($linenr, $n) . "\n";
5223 }
5224 WARN("DEFAULT_NO_BREAK",
5225 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005226 }
5227
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005228# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005229 if ($line =~ /\b__FUNCTION__\b/) {
5230 if (WARN("USE_FUNC",
5231 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5232 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005233 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005234 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005235 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005236
Joe Perches62ec8182015-02-13 14:38:18 -08005237# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5238 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5239 ERROR("DATE_TIME",
5240 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5241 }
5242
Joe Perches2c924882012-03-23 15:02:20 -07005243# check for use of yield()
5244 if ($line =~ /\byield\s*\(\s*\)/) {
5245 WARN("YIELD",
5246 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5247 }
5248
Joe Perches179f8f42013-07-03 15:05:30 -07005249# check for comparisons against true and false
5250 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5251 my $lead = $1;
5252 my $arg = $2;
5253 my $test = $3;
5254 my $otype = $4;
5255 my $trail = $5;
5256 my $op = "!";
5257
5258 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5259
5260 my $type = lc($otype);
5261 if ($type =~ /^(?:true|false)$/) {
5262 if (("$test" eq "==" && "$type" eq "true") ||
5263 ("$test" eq "!=" && "$type" eq "false")) {
5264 $op = "";
5265 }
5266
5267 CHK("BOOL_COMPARISON",
5268 "Using comparison to $otype is error prone\n" . $herecurr);
5269
5270## maybe suggesting a correct construct would better
5271## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5272
5273 }
5274 }
5275
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005276# check for semaphores initialized locked
5277 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005278 WARN("CONSIDER_COMPLETION",
5279 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005280 }
Joe Perches6712d852012-03-23 15:02:20 -07005281
Joe Perches67d0a072011-10-31 17:13:10 -07005282# recommend kstrto* over simple_strto* and strict_strto*
5283 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005284 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005285 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005286 }
Joe Perches6712d852012-03-23 15:02:20 -07005287
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005288# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005289 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005290 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005291 "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 -07005292 }
Joe Perches6712d852012-03-23 15:02:20 -07005293
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005294# check for various structs that are normally const (ops, kgdb, device_tree)
5295 my $const_structs = qr{
5296 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005297 address_space_operations|
5298 backlight_ops|
5299 block_device_operations|
5300 dentry_operations|
5301 dev_pm_ops|
5302 dma_map_ops|
5303 extent_io_ops|
5304 file_lock_operations|
5305 file_operations|
5306 hv_ops|
5307 ide_dma_ops|
5308 intel_dvo_dev_ops|
5309 item_operations|
5310 iwl_ops|
5311 kgdb_arch|
5312 kgdb_io|
5313 kset_uevent_ops|
5314 lock_manager_operations|
5315 microcode_ops|
5316 mtrr_ops|
5317 neigh_ops|
5318 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005319 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005320 pci_raw_ops|
5321 pipe_buf_operations|
5322 platform_hibernation_ops|
5323 platform_suspend_ops|
5324 proto_ops|
5325 rpc_pipe_ops|
5326 seq_operations|
5327 snd_ac97_build_ops|
5328 soc_pcmcia_socket_ops|
5329 stacktrace_ops|
5330 sysfs_ops|
5331 tty_operations|
5332 usb_mon_operations|
5333 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005334 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005335 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005336 WARN("CONST_STRUCT",
5337 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005338 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005339 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005340
5341# use of NR_CPUS is usually wrong
5342# ignore definitions of NR_CPUS and usage to define arrays as likely right
5343 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005344 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5345 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005346 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5347 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5348 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005349 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005350 WARN("NR_CPUS",
5351 "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 -07005352 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005353
Joe Perches52ea8502013-11-12 15:10:09 -08005354# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5355 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5356 ERROR("DEFINE_ARCH_HAS",
5357 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5358 }
5359
Joe Perchesacd93622015-02-13 14:38:38 -08005360# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5361 if ($^V && $^V ge 5.10.0 &&
5362 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5363 WARN("LIKELY_MISUSE",
5364 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5365 }
5366
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005367# whine mightly about in_atomic
5368 if ($line =~ /\bin_atomic\s*\(/) {
5369 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005370 ERROR("IN_ATOMIC",
5371 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005372 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005373 WARN("IN_ATOMIC",
5374 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005375 }
5376 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005377
5378# check for lockdep_set_novalidate_class
5379 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5380 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5381 if ($realfile !~ m@^kernel/lockdep@ &&
5382 $realfile !~ m@^include/linux/lockdep@ &&
5383 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005384 ERROR("LOCKDEP",
5385 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005386 }
5387 }
Dave Jones88f88312011-01-12 16:59:59 -08005388
Joe Perchesb392c642015-04-16 12:44:16 -07005389 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5390 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005391 WARN("EXPORTED_WORLD_WRITABLE",
5392 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005393 }
Joe Perches24358802014-04-03 14:49:13 -07005394
Joe Perches515a2352014-04-03 14:49:24 -07005395# Mode permission misuses where it seems decimal should be octal
5396# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5397 if ($^V && $^V ge 5.10.0 &&
5398 $line =~ /$mode_perms_search/) {
5399 foreach my $entry (@mode_permission_funcs) {
5400 my $func = $entry->[0];
5401 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005402
Joe Perches515a2352014-04-03 14:49:24 -07005403 my $skip_args = "";
5404 if ($arg_pos > 1) {
5405 $arg_pos--;
5406 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5407 }
5408 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5409 if ($line =~ /$test/) {
5410 my $val = $1;
5411 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005412
Joe Perches515a2352014-04-03 14:49:24 -07005413 if ($val !~ /^0$/ &&
5414 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5415 length($val) ne 4)) {
5416 ERROR("NON_OCTAL_PERMISSIONS",
5417 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005418 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5419 ERROR("EXPORTED_WORLD_WRITABLE",
5420 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005421 }
Joe Perches24358802014-04-03 14:49:13 -07005422 }
5423 }
5424 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005425 }
5426
5427 # If we have no input at all, then there is nothing to report on
5428 # so just keep quiet.
5429 if ($#rawlines == -1) {
5430 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005431 }
5432
Andy Whitcroft8905a672007-11-28 16:21:06 -08005433 # In mailback mode only produce a report in the negative, for
5434 # things that appear to be patches.
5435 if ($mailback && ($clean == 1 || !$is_patch)) {
5436 exit(0);
5437 }
5438
5439 # This is not a patch, and we are are in 'no-patch' mode so
5440 # just keep quiet.
5441 if (!$chk_patch && !$is_patch) {
5442 exit(0);
5443 }
5444
5445 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005446 ERROR("NOT_UNIFIED_DIFF",
5447 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005448 }
5449 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005450 ERROR("MISSING_SIGN_OFF",
5451 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005452 }
5453
Andy Whitcroft8905a672007-11-28 16:21:06 -08005454 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005455 if ($summary && !($clean == 1 && $quiet == 1)) {
5456 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005457 print "total: $cnt_error errors, $cnt_warn warnings, " .
5458 (($check)? "$cnt_chk checks, " : "") .
5459 "$cnt_lines lines checked\n";
5460 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005461 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005462
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005463 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005464
5465 if ($^V lt 5.10.0) {
5466 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5467 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5468 }
5469
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005470 # If there were whitespace errors which cleanpatch can fix
5471 # then suggest that.
5472 if ($rpt_cleaners) {
5473 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5474 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005475 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005476 }
5477 }
5478
Joe Perches91bfe482013-09-11 14:23:59 -07005479 hash_show_words(\%use_type, "Used");
5480 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005481
Joe Perchesd752fcc2014-08-06 16:11:05 -07005482 if ($clean == 0 && $fix &&
5483 ("@rawlines" ne "@fixed" ||
5484 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005485 my $newfile = $filename;
5486 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005487 my $linecount = 0;
5488 my $f;
5489
Joe Perchesd752fcc2014-08-06 16:11:05 -07005490 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5491
Joe Perches3705ce52013-07-03 15:05:31 -07005492 open($f, '>', $newfile)
5493 or die "$P: Can't open $newfile for write\n";
5494 foreach my $fixed_line (@fixed) {
5495 $linecount++;
5496 if ($file) {
5497 if ($linecount > 3) {
5498 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005499 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005500 }
5501 } else {
5502 print $f $fixed_line . "\n";
5503 }
5504 }
5505 close($f);
5506
5507 if (!$quiet) {
5508 print << "EOM";
5509Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5510
5511Do _NOT_ trust the results written to this file.
5512Do _NOT_ submit these changes without inspecting them for correctness.
5513
5514This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5515No warranties, expressed or implied...
5516
5517EOM
5518 }
5519 }
5520
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005521 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005522 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005523 }
5524 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005525 print << "EOM";
5526$vname has style problems, please review.
5527
5528If any of these errors are false positives, please report
5529them to the maintainer, see CHECKPATCH in MAINTAINERS.
5530EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005531 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005532
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005533 return $clean;
5534}