Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Dave Jones | dbf004d | 2010-01-12 16:59:52 -0500 | [diff] [blame] | 2 | # (c) 2001, Dave Jones. (the file handling bit) |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
Andy Whitcroft | 2a5a2c2 | 2009-01-06 14:41:23 -0800 | [diff] [blame] | 4 | # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite) |
Andy Whitcroft | 015830b | 2010-10-26 14:23:17 -0700 | [diff] [blame] | 5 | # (c) 2008-2010 Andy Whitcroft <apw@canonical.com> |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 6 | # Licensed under the terms of the GNU GPL License version 2 |
| 7 | |
| 8 | use strict; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 9 | use POSIX; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 10 | |
| 11 | my $P = $0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 12 | $P =~ s@.*/@@g; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 13 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 14 | my $V = '0.32'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 15 | |
| 16 | use Getopt::Long qw(:config no_auto_abbrev); |
| 17 | |
| 18 | my $quiet = 0; |
| 19 | my $tree = 1; |
| 20 | my $chk_signoff = 1; |
| 21 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 22 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 23 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 24 | my $terse = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 25 | my $file = 0; |
| 26 | my $check = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 27 | my $summary = 1; |
| 28 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 29 | my $summary_file = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 30 | my $show_types = 0; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 31 | my $fix = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 32 | my $root; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 33 | my %debug; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 34 | my %camelcase = (); |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 35 | my %use_type = (); |
| 36 | my @use = (); |
| 37 | my %ignore_type = (); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 38 | my @ignore = (); |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 39 | my $help = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 40 | my $configuration_file = ".checkpatch.conf"; |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 41 | my $max_line_length = 80; |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 42 | my $ignore_perl_version = 0; |
| 43 | my $minimum_perl_version = 5.10.0; |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 44 | |
| 45 | sub help { |
| 46 | my ($exitcode) = @_; |
| 47 | |
| 48 | print << "EOM"; |
| 49 | Usage: $P [OPTION]... [FILE]... |
| 50 | Version: $V |
| 51 | |
| 52 | Options: |
| 53 | -q, --quiet quiet |
| 54 | --no-tree run without a kernel tree |
| 55 | --no-signoff do not check for 'Signed-off-by' line |
| 56 | --patch treat FILE as patchfile (default) |
| 57 | --emacs emacs compile window format |
| 58 | --terse one line per report |
| 59 | -f, --file treat FILE as regular source file |
| 60 | --subjective, --strict enable more subjective tests |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 61 | --types TYPE(,TYPE2...) show only these comma separated message types |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 62 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 63 | --max-line-length=n set the maximum line length, if exceeded, warn |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 64 | --show-types show the message "types" in the output |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 65 | --root=PATH PATH to the kernel tree root |
| 66 | --no-summary suppress the per-file summary |
| 67 | --mailback only produce a report in case of warnings/errors |
| 68 | --summary-file include the filename in summary |
| 69 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 70 | 'values', 'possible', 'type', and 'attr' (default |
| 71 | is all off) |
| 72 | --test-only=WORD report only warnings/errors containing WORD |
| 73 | literally |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 74 | --fix EXPERIMENTAL - may create horrible results |
| 75 | If correctable single-line errors exist, create |
| 76 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 77 | with potential errors corrected to the preferred |
| 78 | checkpatch style |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 79 | --ignore-perl-version override checking of perl version. expect |
| 80 | runtime errors. |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 81 | -h, --help, --version display this help and exit |
| 82 | |
| 83 | When FILE is - read standard input. |
| 84 | EOM |
| 85 | |
| 86 | exit($exitcode); |
| 87 | } |
| 88 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 89 | my $conf = which_conf($configuration_file); |
| 90 | if (-f $conf) { |
| 91 | my @conf_args; |
| 92 | open(my $conffile, '<', "$conf") |
| 93 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 94 | |
| 95 | while (<$conffile>) { |
| 96 | my $line = $_; |
| 97 | |
| 98 | $line =~ s/\s*\n?$//g; |
| 99 | $line =~ s/^\s*//g; |
| 100 | $line =~ s/\s+/ /g; |
| 101 | |
| 102 | next if ($line =~ m/^\s*#/); |
| 103 | next if ($line =~ m/^\s*$/); |
| 104 | |
| 105 | my @words = split(" ", $line); |
| 106 | foreach my $word (@words) { |
| 107 | last if ($word =~ m/^#/); |
| 108 | push (@conf_args, $word); |
| 109 | } |
| 110 | } |
| 111 | close($conffile); |
| 112 | unshift(@ARGV, @conf_args) if @conf_args; |
| 113 | } |
| 114 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 115 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 116 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 117 | 'tree!' => \$tree, |
| 118 | 'signoff!' => \$chk_signoff, |
| 119 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 120 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 121 | 'terse!' => \$terse, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 122 | 'f|file!' => \$file, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 123 | 'subjective!' => \$check, |
| 124 | 'strict!' => \$check, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 125 | 'ignore=s' => \@ignore, |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 126 | 'types=s' => \@use, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 127 | 'show-types!' => \$show_types, |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 128 | 'max-line-length=i' => \$max_line_length, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 129 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 130 | 'summary!' => \$summary, |
| 131 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 132 | 'summary-file!' => \$summary_file, |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 133 | 'fix!' => \$fix, |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 134 | 'ignore-perl-version!' => \$ignore_perl_version, |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 135 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 136 | 'test-only=s' => \$tst_only, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 137 | 'h|help' => \$help, |
| 138 | 'version' => \$help |
| 139 | ) or help(1); |
| 140 | |
| 141 | help(0) if ($help); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 142 | |
| 143 | my $exit = 0; |
| 144 | |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 145 | if ($^V && $^V lt $minimum_perl_version) { |
| 146 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
| 147 | if (!$ignore_perl_version) { |
| 148 | exit(1); |
| 149 | } |
| 150 | } |
| 151 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 152 | if ($#ARGV < 0) { |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 153 | print "$P: no input files\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 154 | exit(1); |
| 155 | } |
| 156 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 157 | sub hash_save_array_words { |
| 158 | my ($hashRef, $arrayRef) = @_; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 159 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 160 | my @array = split(/,/, join(',', @$arrayRef)); |
| 161 | foreach my $word (@array) { |
| 162 | $word =~ s/\s*\n?$//g; |
| 163 | $word =~ s/^\s*//g; |
| 164 | $word =~ s/\s+/ /g; |
| 165 | $word =~ tr/[a-z]/[A-Z]/; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 166 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 167 | next if ($word =~ m/^\s*#/); |
| 168 | next if ($word =~ m/^\s*$/); |
| 169 | |
| 170 | $hashRef->{$word}++; |
| 171 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 174 | sub hash_show_words { |
| 175 | my ($hashRef, $prefix) = @_; |
| 176 | |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 177 | if ($quiet == 0 && keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 178 | print "NOTE: $prefix message types:"; |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 179 | foreach my $word (sort keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 180 | print " $word"; |
| 181 | } |
| 182 | print "\n\n"; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | hash_save_array_words(\%ignore_type, \@ignore); |
| 187 | hash_save_array_words(\%use_type, \@use); |
| 188 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 189 | my $dbg_values = 0; |
| 190 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 191 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 192 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 193 | for my $key (keys %debug) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 194 | ## no critic |
| 195 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 196 | die "$@" if ($@); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 199 | my $rpt_cleaners = 0; |
| 200 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 201 | if ($terse) { |
| 202 | $emacs = 1; |
| 203 | $quiet++; |
| 204 | } |
| 205 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 206 | if ($tree) { |
| 207 | if (defined $root) { |
| 208 | if (!top_of_kernel_tree($root)) { |
| 209 | die "$P: $root: --root does not point at a valid tree\n"; |
| 210 | } |
| 211 | } else { |
| 212 | if (top_of_kernel_tree('.')) { |
| 213 | $root = '.'; |
| 214 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 215 | top_of_kernel_tree($1)) { |
| 216 | $root = $1; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (!defined $root) { |
| 221 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 222 | exit(2); |
| 223 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 226 | my $emitted_corrupt = 0; |
| 227 | |
Andy Whitcroft | 2ceb532 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 228 | our $Ident = qr{ |
| 229 | [A-Za-z_][A-Za-z\d_]* |
| 230 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 231 | }x; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 232 | our $Storage = qr{extern|static|asmlinkage}; |
| 233 | our $Sparse = qr{ |
| 234 | __user| |
| 235 | __kernel| |
| 236 | __force| |
| 237 | __iomem| |
| 238 | __must_check| |
| 239 | __init_refok| |
Andy Whitcroft | 417495e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 240 | __kprobes| |
Sven Eckelmann | 165e72a | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 241 | __ref| |
| 242 | __rcu |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 243 | }x; |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 244 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 245 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 246 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 247 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 248 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 249 | |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 250 | # Notes to $Attribute: |
| 251 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 252 | our $Attribute = qr{ |
| 253 | const| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 254 | __percpu| |
| 255 | __nocast| |
| 256 | __safe| |
| 257 | __bitwise__| |
| 258 | __packed__| |
| 259 | __packed2__| |
| 260 | __naked| |
| 261 | __maybe_unused| |
| 262 | __always_unused| |
| 263 | __noreturn| |
| 264 | __used| |
| 265 | __cold| |
| 266 | __noclone| |
| 267 | __deprecated| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 268 | __read_mostly| |
| 269 | __kprobes| |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 270 | $InitAttribute| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 271 | ____cacheline_aligned| |
| 272 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 273 | ____cacheline_internodealigned_in_smp| |
| 274 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 275 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 276 | our $Modifier; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 277 | our $Inline = qr{inline|__always_inline|noinline}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 278 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 279 | our $Lval = qr{$Ident(?:$Member)*}; |
| 280 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 281 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 282 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 283 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 284 | our $Int = qr{[0-9]+$Int_type?}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 285 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 286 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 287 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
Joe Perches | 74349bc | 2012-12-17 16:02:05 -0800 | [diff] [blame] | 288 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 289 | our $Constant = qr{$Float|$Binary|$Hex|$Int}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 290 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 291 | our $Compare = qr{<=|>=|==|!=|<|>}; |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 292 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 293 | our $Operators = qr{ |
| 294 | <=|>=|==|!=| |
| 295 | =>|->|<<|>>|<|>|!|~| |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 296 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 297 | }x; |
| 298 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 299 | our $NonptrType; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 300 | our $NonptrTypeWithAttr; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 301 | our $Type; |
| 302 | our $Declare; |
| 303 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 304 | our $NON_ASCII_UTF8 = qr{ |
| 305 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 306 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 307 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 308 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 309 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 310 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 311 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 312 | }x; |
| 313 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 314 | our $UTF8 = qr{ |
| 315 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 316 | | $NON_ASCII_UTF8 |
| 317 | }x; |
| 318 | |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 319 | our $typeTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 320 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 321 | atomic_t |
| 322 | )}; |
| 323 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 324 | our $logFunctions = qr{(?x: |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 325 | printk(?:_ratelimited|_once|)| |
Jacob Keller | 7d0b659 | 2013-07-03 15:05:35 -0700 | [diff] [blame] | 326 | (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 327 | WARN(?:_RATELIMIT|_ONCE|)| |
Joe Perches | b053172 | 2011-05-24 17:13:40 -0700 | [diff] [blame] | 328 | panic| |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 329 | MODULE_[A-Z_]+| |
| 330 | seq_vprintf|seq_printf|seq_puts |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 331 | )}; |
| 332 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 333 | our $signature_tags = qr{(?xi: |
| 334 | Signed-off-by:| |
| 335 | Acked-by:| |
| 336 | Tested-by:| |
| 337 | Reviewed-by:| |
| 338 | Reported-by:| |
Mugunthan V N | 8543ae1 | 2013-04-29 16:18:17 -0700 | [diff] [blame] | 339 | Suggested-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 340 | To:| |
| 341 | Cc: |
| 342 | )}; |
| 343 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 344 | our @typeList = ( |
| 345 | qr{void}, |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 346 | qr{(?:unsigned\s+)?char}, |
| 347 | qr{(?:unsigned\s+)?short}, |
| 348 | qr{(?:unsigned\s+)?int}, |
| 349 | qr{(?:unsigned\s+)?long}, |
| 350 | qr{(?:unsigned\s+)?long\s+int}, |
| 351 | qr{(?:unsigned\s+)?long\s+long}, |
| 352 | qr{(?:unsigned\s+)?long\s+long\s+int}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 353 | qr{unsigned}, |
| 354 | qr{float}, |
| 355 | qr{double}, |
| 356 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 357 | qr{struct\s+$Ident}, |
| 358 | qr{union\s+$Ident}, |
| 359 | qr{enum\s+$Ident}, |
| 360 | qr{${Ident}_t}, |
| 361 | qr{${Ident}_handler}, |
| 362 | qr{${Ident}_handler_fn}, |
| 363 | ); |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 364 | our @typeListWithAttr = ( |
| 365 | @typeList, |
| 366 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 367 | qr{union\s+$InitAttribute\s+$Ident}, |
| 368 | ); |
| 369 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 370 | our @modifierList = ( |
| 371 | qr{fastcall}, |
| 372 | ); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 373 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 374 | our $allowed_asm_includes = qr{(?x: |
| 375 | irq| |
| 376 | memory |
| 377 | )}; |
| 378 | # memory.h: ARM has a custom one |
| 379 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 380 | sub build_types { |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 381 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
| 382 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 383 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 384 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 385 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 386 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 387 | (?: |
Andy Whitcroft | 6b48db2 | 2012-01-10 15:10:13 -0800 | [diff] [blame] | 388 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 389 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 390 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 391 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 392 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 393 | }x; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 394 | $NonptrTypeWithAttr = qr{ |
| 395 | (?:$Modifier\s+|const\s+)* |
| 396 | (?: |
| 397 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 398 | (?:$typeTypedefs\b)| |
| 399 | (?:${allWithAttr}\b) |
| 400 | ) |
| 401 | (?:\s+$Modifier|\s+const)* |
| 402 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 403 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 404 | $NonptrType |
Andy Whitcroft | b337d8b | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 405 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 406 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 407 | }x; |
| 408 | $Declare = qr{(?:$Storage\s+)?$Type}; |
| 409 | } |
| 410 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 411 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 412 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 413 | |
| 414 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 415 | # requires at least perl version v5.10.0 |
| 416 | # Any use must be runtime checked with $^V |
| 417 | |
| 418 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
| 419 | our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 420 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 421 | |
| 422 | sub deparenthesize { |
| 423 | my ($string) = @_; |
| 424 | return "" if (!defined($string)); |
| 425 | $string =~ s@^\s*\(\s*@@g; |
| 426 | $string =~ s@\s*\)\s*$@@g; |
| 427 | $string =~ s@\s+@ @g; |
| 428 | return $string; |
| 429 | } |
| 430 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 431 | sub seed_camelcase_file { |
| 432 | my ($file) = @_; |
| 433 | |
| 434 | return if (!(-f $file)); |
| 435 | |
| 436 | local $/; |
| 437 | |
| 438 | open(my $include_file, '<', "$file") |
| 439 | or warn "$P: Can't read '$file' $!\n"; |
| 440 | my $text = <$include_file>; |
| 441 | close($include_file); |
| 442 | |
| 443 | my @lines = split('\n', $text); |
| 444 | |
| 445 | foreach my $line (@lines) { |
| 446 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 447 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 448 | $camelcase{$1} = 1; |
Joe Perches | 11ea516 | 2013-11-12 15:10:08 -0800 | [diff] [blame] | 449 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 450 | $camelcase{$1} = 1; |
| 451 | } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 452 | $camelcase{$1} = 1; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | my $camelcase_seeded = 0; |
| 458 | sub seed_camelcase_includes { |
| 459 | return if ($camelcase_seeded); |
| 460 | |
| 461 | my $files; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 462 | my $camelcase_cache = ""; |
| 463 | my @include_files = (); |
| 464 | |
| 465 | $camelcase_seeded = 1; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 466 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 467 | if (-d ".git") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 468 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; |
| 469 | chomp $git_last_include_commit; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 470 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 471 | } else { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 472 | my $last_mod_date = 0; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 473 | $files = `find $root/include -name "*.h"`; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 474 | @include_files = split('\n', $files); |
| 475 | foreach my $file (@include_files) { |
| 476 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 477 | localtime((stat $file)[9])); |
| 478 | $last_mod_date = $date if ($last_mod_date < $date); |
| 479 | } |
| 480 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 481 | } |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 482 | |
| 483 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 484 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 485 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 486 | while (<$camelcase_file>) { |
| 487 | chomp; |
| 488 | $camelcase{$_} = 1; |
| 489 | } |
| 490 | close($camelcase_file); |
| 491 | |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | if (-d ".git") { |
| 496 | $files = `git ls-files "include/*.h"`; |
| 497 | @include_files = split('\n', $files); |
| 498 | } |
| 499 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 500 | foreach my $file (@include_files) { |
| 501 | seed_camelcase_file($file); |
| 502 | } |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 503 | |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 504 | if ($camelcase_cache ne "") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 505 | unlink glob ".checkpatch-camelcase.*"; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 506 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 507 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 508 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 509 | print $camelcase_file ("$_\n"); |
| 510 | } |
| 511 | close($camelcase_file); |
| 512 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 515 | $chk_signoff = 0 if ($file); |
| 516 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 517 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 518 | my @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 519 | my @fixed = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 520 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 521 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 522 | my $FILE; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 523 | if ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 524 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 525 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 526 | } elsif ($filename eq '-') { |
| 527 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 528 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 529 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 530 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 531 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 532 | if ($filename eq '-') { |
| 533 | $vname = 'Your patch'; |
| 534 | } else { |
| 535 | $vname = $filename; |
| 536 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 537 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 538 | chomp; |
| 539 | push(@rawlines, $_); |
| 540 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 541 | close($FILE); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 542 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 543 | $exit = 1; |
| 544 | } |
| 545 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 546 | @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 547 | @fixed = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | exit($exit); |
| 551 | |
| 552 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 553 | my ($root) = @_; |
| 554 | |
| 555 | my @tree_check = ( |
| 556 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 557 | "README", "Documentation", "arch", "include", "drivers", |
| 558 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 559 | ); |
| 560 | |
| 561 | foreach my $check (@tree_check) { |
| 562 | if (! -e $root . '/' . $check) { |
| 563 | return 0; |
| 564 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 565 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 566 | return 1; |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 567 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 568 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 569 | sub parse_email { |
| 570 | my ($formatted_email) = @_; |
| 571 | |
| 572 | my $name = ""; |
| 573 | my $address = ""; |
| 574 | my $comment = ""; |
| 575 | |
| 576 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 577 | $name = $1; |
| 578 | $address = $2; |
| 579 | $comment = $3 if defined $3; |
| 580 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 581 | $address = $1; |
| 582 | $comment = $2 if defined $2; |
| 583 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 584 | $address = $1; |
| 585 | $comment = $2 if defined $2; |
| 586 | $formatted_email =~ s/$address.*$//; |
| 587 | $name = $formatted_email; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 588 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 589 | $name =~ s/^\"|\"$//g; |
| 590 | # If there's a name left after stripping spaces and |
| 591 | # leading quotes, and the address doesn't have both |
| 592 | # leading and trailing angle brackets, the address |
| 593 | # is invalid. ie: |
| 594 | # "joe smith joe@smith.com" bad |
| 595 | # "joe smith <joe@smith.com" bad |
| 596 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 597 | $name = ""; |
| 598 | $address = ""; |
| 599 | $comment = ""; |
| 600 | } |
| 601 | } |
| 602 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 603 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 604 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 605 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 606 | $address =~ s/^\<|\>$//g; |
| 607 | |
| 608 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 609 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 610 | $name = "\"$name\""; |
| 611 | } |
| 612 | |
| 613 | return ($name, $address, $comment); |
| 614 | } |
| 615 | |
| 616 | sub format_email { |
| 617 | my ($name, $address) = @_; |
| 618 | |
| 619 | my $formatted_email; |
| 620 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 621 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 622 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 623 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 624 | |
| 625 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 626 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 627 | $name = "\"$name\""; |
| 628 | } |
| 629 | |
| 630 | if ("$name" eq "") { |
| 631 | $formatted_email = "$address"; |
| 632 | } else { |
| 633 | $formatted_email = "$name <$address>"; |
| 634 | } |
| 635 | |
| 636 | return $formatted_email; |
| 637 | } |
| 638 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 639 | sub which_conf { |
| 640 | my ($conf) = @_; |
| 641 | |
| 642 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 643 | if (-e "$path/$conf") { |
| 644 | return "$path/$conf"; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return ""; |
| 649 | } |
| 650 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 651 | sub expand_tabs { |
| 652 | my ($str) = @_; |
| 653 | |
| 654 | my $res = ''; |
| 655 | my $n = 0; |
| 656 | for my $c (split(//, $str)) { |
| 657 | if ($c eq "\t") { |
| 658 | $res .= ' '; |
| 659 | $n++; |
| 660 | for (; ($n % 8) != 0; $n++) { |
| 661 | $res .= ' '; |
| 662 | } |
| 663 | next; |
| 664 | } |
| 665 | $res .= $c; |
| 666 | $n++; |
| 667 | } |
| 668 | |
| 669 | return $res; |
| 670 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 671 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 672 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 673 | return $res; |
| 674 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 675 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 676 | sub line_stats { |
| 677 | my ($line) = @_; |
| 678 | |
| 679 | # Drop the diff line leader and expand tabs |
| 680 | $line =~ s/^.//; |
| 681 | $line = expand_tabs($line); |
| 682 | |
| 683 | # Pick the indent from the front of the line. |
| 684 | my ($white) = ($line =~ /^(\s*)/); |
| 685 | |
| 686 | return (length($line), length($white)); |
| 687 | } |
| 688 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 689 | my $sanitise_quote = ''; |
| 690 | |
| 691 | sub sanitise_line_reset { |
| 692 | my ($in_comment) = @_; |
| 693 | |
| 694 | if ($in_comment) { |
| 695 | $sanitise_quote = '*/'; |
| 696 | } else { |
| 697 | $sanitise_quote = ''; |
| 698 | } |
| 699 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 700 | sub sanitise_line { |
| 701 | my ($line) = @_; |
| 702 | |
| 703 | my $res = ''; |
| 704 | my $l = ''; |
| 705 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 706 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 707 | my $off = 0; |
| 708 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 709 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 710 | # Always copy over the diff marker. |
| 711 | $res = substr($line, 0, 1); |
| 712 | |
| 713 | for ($off = 1; $off < length($line); $off++) { |
| 714 | $c = substr($line, $off, 1); |
| 715 | |
| 716 | # Comments we are wacking completly including the begin |
| 717 | # and end, all to $;. |
| 718 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 719 | $sanitise_quote = '*/'; |
| 720 | |
| 721 | substr($res, $off, 2, "$;$;"); |
| 722 | $off++; |
| 723 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 724 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 725 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 726 | $sanitise_quote = ''; |
| 727 | substr($res, $off, 2, "$;$;"); |
| 728 | $off++; |
| 729 | next; |
| 730 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 731 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 732 | $sanitise_quote = '//'; |
| 733 | |
| 734 | substr($res, $off, 2, $sanitise_quote); |
| 735 | $off++; |
| 736 | next; |
| 737 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 738 | |
| 739 | # A \ in a string means ignore the next character. |
| 740 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 741 | $c eq "\\") { |
| 742 | substr($res, $off, 2, 'XX'); |
| 743 | $off++; |
| 744 | next; |
| 745 | } |
| 746 | # Regular quotes. |
| 747 | if ($c eq "'" || $c eq '"') { |
| 748 | if ($sanitise_quote eq '') { |
| 749 | $sanitise_quote = $c; |
| 750 | |
| 751 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 752 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 753 | } elsif ($sanitise_quote eq $c) { |
| 754 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 757 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 758 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 759 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 760 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 761 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 762 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 763 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 764 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 765 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 766 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 767 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 770 | if ($sanitise_quote eq '//') { |
| 771 | $sanitise_quote = ''; |
| 772 | } |
| 773 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 774 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 775 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 776 | my $clean = 'X' x length($1); |
| 777 | $res =~ s@\<.*\>@<$clean>@; |
| 778 | |
| 779 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 780 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 781 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 782 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 785 | return $res; |
| 786 | } |
| 787 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 788 | sub get_quoted_string { |
| 789 | my ($line, $rawline) = @_; |
| 790 | |
| 791 | return "" if ($line !~ m/(\"[X]+\")/g); |
| 792 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 793 | } |
| 794 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 795 | sub ctx_statement_block { |
| 796 | my ($linenr, $remain, $off) = @_; |
| 797 | my $line = $linenr - 1; |
| 798 | my $blk = ''; |
| 799 | my $soff = $off; |
| 800 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 801 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 802 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 803 | my $loff = 0; |
| 804 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 805 | my $type = ''; |
| 806 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 807 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 808 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 809 | my $c; |
| 810 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 811 | |
| 812 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 813 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 814 | @stack = (['', 0]) if ($#stack == -1); |
| 815 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 816 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 817 | # If we are about to drop off the end, pull in more |
| 818 | # context. |
| 819 | if ($off >= $len) { |
| 820 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 821 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 822 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 823 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 824 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 825 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 826 | $len = length($blk); |
| 827 | $line++; |
| 828 | last; |
| 829 | } |
| 830 | # Bail if there is no further context. |
| 831 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 832 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 833 | last; |
| 834 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 835 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 836 | $level++; |
| 837 | $type = '#'; |
| 838 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 839 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 840 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 841 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 842 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 843 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 844 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 845 | |
| 846 | # Handle nested #if/#else. |
| 847 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 848 | push(@stack, [ $type, $level ]); |
| 849 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 850 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 851 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 852 | ($type, $level) = @{pop(@stack)}; |
| 853 | } |
| 854 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 855 | # Statement ends at the ';' or a close '}' at the |
| 856 | # outermost level. |
| 857 | if ($level == 0 && $c eq ';') { |
| 858 | last; |
| 859 | } |
| 860 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 861 | # An else is really a conditional as long as its not else if |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 862 | if ($level == 0 && $coff_set == 0 && |
| 863 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 864 | $remainder =~ /^(else)(?:\s|{)/ && |
| 865 | $remainder !~ /^else\s+if\b/) { |
| 866 | $coff = $off + length($1) - 1; |
| 867 | $coff_set = 1; |
| 868 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 869 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 870 | } |
| 871 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 872 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 873 | $level++; |
| 874 | $type = '('; |
| 875 | } |
| 876 | if ($type eq '(' && $c eq ')') { |
| 877 | $level--; |
| 878 | $type = ($level != 0)? '(' : ''; |
| 879 | |
| 880 | if ($level == 0 && $coff < $soff) { |
| 881 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 882 | $coff_set = 1; |
| 883 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 887 | $level++; |
| 888 | $type = '{'; |
| 889 | } |
| 890 | if ($type eq '{' && $c eq '}') { |
| 891 | $level--; |
| 892 | $type = ($level != 0)? '{' : ''; |
| 893 | |
| 894 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 895 | if (substr($blk, $off + 1, 1) eq ';') { |
| 896 | $off++; |
| 897 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 898 | last; |
| 899 | } |
| 900 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 901 | # Preprocessor commands end at the newline unless escaped. |
| 902 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 903 | $level--; |
| 904 | $type = ''; |
| 905 | $off++; |
| 906 | last; |
| 907 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 908 | $off++; |
| 909 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 910 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 911 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 912 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 913 | $line++; |
| 914 | $remain--; |
| 915 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 916 | |
| 917 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 918 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 919 | |
| 920 | #warn "STATEMENT<$statement>\n"; |
| 921 | #warn "CONDITION<$condition>\n"; |
| 922 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 923 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 924 | |
| 925 | return ($statement, $condition, |
| 926 | $line, $remain + 1, $off - $loff + 1, $level); |
| 927 | } |
| 928 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 929 | sub statement_lines { |
| 930 | my ($stmt) = @_; |
| 931 | |
| 932 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 933 | $stmt =~ s/(^|\n)./$1/g; |
| 934 | $stmt =~ s/^\s*//; |
| 935 | $stmt =~ s/\s*$//; |
| 936 | |
| 937 | my @stmt_lines = ($stmt =~ /\n/g); |
| 938 | |
| 939 | return $#stmt_lines + 2; |
| 940 | } |
| 941 | |
| 942 | sub statement_rawlines { |
| 943 | my ($stmt) = @_; |
| 944 | |
| 945 | my @stmt_lines = ($stmt =~ /\n/g); |
| 946 | |
| 947 | return $#stmt_lines + 2; |
| 948 | } |
| 949 | |
| 950 | sub statement_block_size { |
| 951 | my ($stmt) = @_; |
| 952 | |
| 953 | $stmt =~ s/(^|\n)./$1/g; |
| 954 | $stmt =~ s/^\s*{//; |
| 955 | $stmt =~ s/}\s*$//; |
| 956 | $stmt =~ s/^\s*//; |
| 957 | $stmt =~ s/\s*$//; |
| 958 | |
| 959 | my @stmt_lines = ($stmt =~ /\n/g); |
| 960 | my @stmt_statements = ($stmt =~ /;/g); |
| 961 | |
| 962 | my $stmt_lines = $#stmt_lines + 2; |
| 963 | my $stmt_statements = $#stmt_statements + 1; |
| 964 | |
| 965 | if ($stmt_lines > $stmt_statements) { |
| 966 | return $stmt_lines; |
| 967 | } else { |
| 968 | return $stmt_statements; |
| 969 | } |
| 970 | } |
| 971 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 972 | sub ctx_statement_full { |
| 973 | my ($linenr, $remain, $off) = @_; |
| 974 | my ($statement, $condition, $level); |
| 975 | |
| 976 | my (@chunks); |
| 977 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 978 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 979 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 980 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 981 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 982 | push(@chunks, [ $condition, $statement ]); |
| 983 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 984 | return ($level, $linenr, @chunks); |
| 985 | } |
| 986 | |
| 987 | # Pull in the following conditional/block pairs and see if they |
| 988 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 989 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 990 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 991 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 992 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 993 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 994 | #print "C: push\n"; |
| 995 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1001 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1002 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1003 | my $line; |
| 1004 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1005 | my $blk = ''; |
| 1006 | my @o; |
| 1007 | my @c; |
| 1008 | my @res = (); |
| 1009 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1010 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1011 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1012 | for ($line = $start; $remain > 0; $line++) { |
| 1013 | next if ($rawlines[$line] =~ /^-/); |
| 1014 | $remain--; |
| 1015 | |
| 1016 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1017 | |
| 1018 | # Handle nested #if/#else. |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1019 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1020 | push(@stack, $level); |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1021 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1022 | $level = $stack[$#stack - 1]; |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1023 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1024 | $level = pop(@stack); |
| 1025 | } |
| 1026 | |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1027 | foreach my $c (split(//, $lines[$line])) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1028 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1029 | if ($off > 0) { |
| 1030 | $off--; |
| 1031 | next; |
| 1032 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1033 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1034 | if ($c eq $close && $level > 0) { |
| 1035 | $level--; |
| 1036 | last if ($level == 0); |
| 1037 | } elsif ($c eq $open) { |
| 1038 | $level++; |
| 1039 | } |
| 1040 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1041 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1042 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1043 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1046 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1049 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1050 | } |
| 1051 | sub ctx_block_outer { |
| 1052 | my ($linenr, $remain) = @_; |
| 1053 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1054 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1055 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1056 | } |
| 1057 | sub ctx_block { |
| 1058 | my ($linenr, $remain) = @_; |
| 1059 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1060 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1061 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1062 | } |
| 1063 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1064 | my ($linenr, $remain, $off) = @_; |
| 1065 | |
| 1066 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1067 | return @r; |
| 1068 | } |
| 1069 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1070 | my ($linenr, $remain) = @_; |
| 1071 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1072 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1073 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1074 | sub ctx_statement_level { |
| 1075 | my ($linenr, $remain, $off) = @_; |
| 1076 | |
| 1077 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1078 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1079 | |
| 1080 | sub ctx_locate_comment { |
| 1081 | my ($first_line, $end_line) = @_; |
| 1082 | |
| 1083 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 1084 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1085 | return $current_comment if (defined $current_comment); |
| 1086 | |
| 1087 | # Look through the context and try and figure out if there is a |
| 1088 | # comment. |
| 1089 | my $in_comment = 0; |
| 1090 | $current_comment = ''; |
| 1091 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1092 | my $line = $rawlines[$linenr - 1]; |
| 1093 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1094 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1095 | $in_comment = 1; |
| 1096 | } |
| 1097 | if ($line =~ m@/\*@) { |
| 1098 | $in_comment = 1; |
| 1099 | } |
| 1100 | if (!$in_comment && $current_comment ne '') { |
| 1101 | $current_comment = ''; |
| 1102 | } |
| 1103 | $current_comment .= $line . "\n" if ($in_comment); |
| 1104 | if ($line =~ m@\*/@) { |
| 1105 | $in_comment = 0; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | chomp($current_comment); |
| 1110 | return($current_comment); |
| 1111 | } |
| 1112 | sub ctx_has_comment { |
| 1113 | my ($first_line, $end_line) = @_; |
| 1114 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1115 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1116 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1117 | ##print "CMMT: $cmt\n"; |
| 1118 | |
| 1119 | return ($cmt ne ''); |
| 1120 | } |
| 1121 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1122 | sub raw_line { |
| 1123 | my ($linenr, $cnt) = @_; |
| 1124 | |
| 1125 | my $offset = $linenr - 1; |
| 1126 | $cnt++; |
| 1127 | |
| 1128 | my $line; |
| 1129 | while ($cnt) { |
| 1130 | $line = $rawlines[$offset++]; |
| 1131 | next if (defined($line) && $line =~ /^-/); |
| 1132 | $cnt--; |
| 1133 | } |
| 1134 | |
| 1135 | return $line; |
| 1136 | } |
| 1137 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1138 | sub cat_vet { |
| 1139 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1140 | my ($res, $coded); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1141 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1142 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1143 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1144 | $res .= $1; |
| 1145 | if ($2 ne '') { |
| 1146 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1147 | $res .= $coded; |
| 1148 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1149 | } |
| 1150 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1151 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1152 | return $res; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1155 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1156 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1157 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1158 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1159 | |
| 1160 | sub annotate_reset { |
| 1161 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1162 | $av_pending = '_'; |
| 1163 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1164 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1167 | sub annotate_values { |
| 1168 | my ($stream, $type) = @_; |
| 1169 | |
| 1170 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1171 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1172 | my $cur = $stream; |
| 1173 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1174 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1175 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1176 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1177 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1178 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1179 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1180 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1181 | print "WS($1)\n" if ($dbg_values > 1); |
| 1182 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1183 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1184 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Florian Mickler | c023e473 | 2011-01-12 16:59:58 -0800 | [diff] [blame] | 1187 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1188 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1189 | push(@av_paren_type, $type); |
Andy Whitcroft | addcdce | 2012-01-10 15:10:11 -0800 | [diff] [blame] | 1190 | $type = 'c'; |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1191 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1192 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1193 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1194 | $type = 'T'; |
| 1195 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 1196 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1197 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1198 | $type = 'T'; |
| 1199 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1200 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1201 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1202 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1203 | push(@av_paren_type, $type); |
| 1204 | if ($2 ne '') { |
| 1205 | $av_pending = 'N'; |
| 1206 | } |
| 1207 | $type = 'E'; |
| 1208 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1209 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1210 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1211 | $av_preprocessor = 1; |
| 1212 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1213 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1214 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1215 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1216 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1217 | |
| 1218 | push(@av_paren_type, $type); |
| 1219 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1220 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1221 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1222 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1223 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1224 | $av_preprocessor = 1; |
| 1225 | |
| 1226 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1227 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1228 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1229 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1230 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1231 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1232 | |
| 1233 | $av_preprocessor = 1; |
| 1234 | |
| 1235 | # Assume all arms of the conditional end as this |
| 1236 | # one does, and continue as if the #endif was not here. |
| 1237 | pop(@av_paren_type); |
| 1238 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1239 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1240 | |
| 1241 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1242 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1243 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1244 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1245 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1246 | $av_pending = $type; |
| 1247 | $type = 'N'; |
| 1248 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1249 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1250 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1251 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1252 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1253 | } |
| 1254 | $type = 'N'; |
| 1255 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1256 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1257 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1258 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1259 | $type = 'N'; |
| 1260 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1261 | } elsif ($cur =~/^(case)/o) { |
| 1262 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1263 | $av_pend_colon = 'C'; |
| 1264 | $type = 'N'; |
| 1265 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1266 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1267 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1268 | $type = 'N'; |
| 1269 | |
| 1270 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1271 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1272 | push(@av_paren_type, $av_pending); |
| 1273 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1274 | $type = 'N'; |
| 1275 | |
| 1276 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1277 | my $new_type = pop(@av_paren_type); |
| 1278 | if ($new_type ne '_') { |
| 1279 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1280 | print "PAREN('$1') -> $type\n" |
| 1281 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1282 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1283 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1286 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1287 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1288 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1289 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1290 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1291 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1292 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1293 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1294 | } elsif ($type eq 'E') { |
| 1295 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1296 | } |
| 1297 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1298 | $type = 'V'; |
| 1299 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1300 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1301 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1302 | $type = 'V'; |
| 1303 | |
| 1304 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1305 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1306 | $type = 'N'; |
| 1307 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1308 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1309 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1310 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1311 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1312 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1313 | } elsif ($cur =~/^(,)/) { |
| 1314 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1315 | $type = 'C'; |
| 1316 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1317 | } elsif ($cur =~ /^(\?)/o) { |
| 1318 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1319 | $type = 'N'; |
| 1320 | |
| 1321 | } elsif ($cur =~ /^(:)/o) { |
| 1322 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1323 | |
| 1324 | substr($var, length($res), 1, $av_pend_colon); |
| 1325 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1326 | $type = 'E'; |
| 1327 | } else { |
| 1328 | $type = 'N'; |
| 1329 | } |
| 1330 | $av_pend_colon = 'O'; |
| 1331 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1332 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1333 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1334 | $type = 'N'; |
| 1335 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1336 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1337 | my $variant; |
| 1338 | |
| 1339 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1340 | if ($type eq 'V') { |
| 1341 | $variant = 'B'; |
| 1342 | } else { |
| 1343 | $variant = 'U'; |
| 1344 | } |
| 1345 | |
| 1346 | substr($var, length($res), 1, $variant); |
| 1347 | $type = 'N'; |
| 1348 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1349 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1350 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1351 | if ($1 ne '++' && $1 ne '--') { |
| 1352 | $type = 'N'; |
| 1353 | } |
| 1354 | |
| 1355 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1356 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1357 | } |
| 1358 | if (defined $1) { |
| 1359 | $cur = substr($cur, length($1)); |
| 1360 | $res .= $type x length($1); |
| 1361 | } |
| 1362 | } |
| 1363 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1364 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1367 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1368 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1369 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1370 | ^(?: |
| 1371 | $Modifier| |
| 1372 | $Storage| |
| 1373 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1374 | DEFINE_\S+ |
| 1375 | )$| |
| 1376 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1377 | goto| |
| 1378 | return| |
| 1379 | case| |
| 1380 | else| |
| 1381 | asm|__asm__| |
Andy Whitcroft | 89a8835 | 2012-01-10 15:10:00 -0800 | [diff] [blame] | 1382 | do| |
| 1383 | \#| |
| 1384 | \#\#| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1385 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1386 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1387 | )}x; |
| 1388 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1389 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1390 | # Check for modifiers. |
| 1391 | $possible =~ s/\s*$Storage\s*//g; |
| 1392 | $possible =~ s/\s*$Sparse\s*//g; |
| 1393 | if ($possible =~ /^\s*$/) { |
| 1394 | |
| 1395 | } elsif ($possible =~ /\s/) { |
| 1396 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1397 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1398 | if ($modifier !~ $notPermitted) { |
| 1399 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
| 1400 | push(@modifierList, $modifier); |
| 1401 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1402 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1403 | |
| 1404 | } else { |
| 1405 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
| 1406 | push(@typeList, $possible); |
| 1407 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1408 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1409 | } else { |
| 1410 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1414 | my $prefix = ''; |
| 1415 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1416 | sub show_type { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 1417 | return defined $use_type{$_[0]} if (scalar keys %use_type > 0); |
| 1418 | |
| 1419 | return !defined $ignore_type{$_[0]}; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1422 | sub report { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1423 | if (!show_type($_[1]) || |
| 1424 | (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1425 | return 0; |
| 1426 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1427 | my $line; |
| 1428 | if ($show_types) { |
| 1429 | $line = "$prefix$_[0]:$_[1]: $_[2]\n"; |
| 1430 | } else { |
| 1431 | $line = "$prefix$_[0]: $_[2]\n"; |
| 1432 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1433 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
| 1434 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1435 | push(our @report, $line); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1436 | |
| 1437 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1438 | } |
| 1439 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1440 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1441 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1442 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1443 | sub ERROR { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1444 | if (report("ERROR", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1445 | our $clean = 0; |
| 1446 | our $cnt_error++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1447 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1448 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1449 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1450 | } |
| 1451 | sub WARN { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1452 | if (report("WARNING", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1453 | our $clean = 0; |
| 1454 | our $cnt_warn++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1455 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1456 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1457 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1458 | } |
| 1459 | sub CHK { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1460 | if ($check && report("CHECK", $_[0], $_[1])) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1461 | our $clean = 0; |
| 1462 | our $cnt_chk++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1463 | return 1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1464 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1465 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1468 | sub check_absolute_file { |
| 1469 | my ($absolute, $herecurr) = @_; |
| 1470 | my $file = $absolute; |
| 1471 | |
| 1472 | ##print "absolute<$absolute>\n"; |
| 1473 | |
| 1474 | # See if any suffix of this path is a path within the tree. |
| 1475 | while ($file =~ s@^[^/]*/@@) { |
| 1476 | if (-f "$root/$file") { |
| 1477 | ##print "file<$file>\n"; |
| 1478 | last; |
| 1479 | } |
| 1480 | } |
| 1481 | if (! -f _) { |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | # It is, so see if the prefix is acceptable. |
| 1486 | my $prefix = $absolute; |
| 1487 | substr($prefix, -length($file)) = ''; |
| 1488 | |
| 1489 | ##print "prefix<$prefix>\n"; |
| 1490 | if ($prefix ne ".../") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1491 | WARN("USE_RELATIVE_PATH", |
| 1492 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1496 | sub trim { |
| 1497 | my ($string) = @_; |
| 1498 | |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 1499 | $string =~ s/^\s+|\s+$//g; |
| 1500 | |
| 1501 | return $string; |
| 1502 | } |
| 1503 | |
| 1504 | sub ltrim { |
| 1505 | my ($string) = @_; |
| 1506 | |
| 1507 | $string =~ s/^\s+//; |
| 1508 | |
| 1509 | return $string; |
| 1510 | } |
| 1511 | |
| 1512 | sub rtrim { |
| 1513 | my ($string) = @_; |
| 1514 | |
| 1515 | $string =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1516 | |
| 1517 | return $string; |
| 1518 | } |
| 1519 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 1520 | sub string_find_replace { |
| 1521 | my ($string, $find, $replace) = @_; |
| 1522 | |
| 1523 | $string =~ s/$find/$replace/g; |
| 1524 | |
| 1525 | return $string; |
| 1526 | } |
| 1527 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1528 | sub tabify { |
| 1529 | my ($leading) = @_; |
| 1530 | |
| 1531 | my $source_indent = 8; |
| 1532 | my $max_spaces_before_tab = $source_indent - 1; |
| 1533 | my $spaces_to_tab = " " x $source_indent; |
| 1534 | |
| 1535 | #convert leading spaces to tabs |
| 1536 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 1537 | #Remove spaces before a tab |
| 1538 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 1539 | |
| 1540 | return "$leading"; |
| 1541 | } |
| 1542 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1543 | sub pos_last_openparen { |
| 1544 | my ($line) = @_; |
| 1545 | |
| 1546 | my $pos = 0; |
| 1547 | |
| 1548 | my $opens = $line =~ tr/\(/\(/; |
| 1549 | my $closes = $line =~ tr/\)/\)/; |
| 1550 | |
| 1551 | my $last_openparen = 0; |
| 1552 | |
| 1553 | if (($opens == 0) || ($closes >= $opens)) { |
| 1554 | return -1; |
| 1555 | } |
| 1556 | |
| 1557 | my $len = length($line); |
| 1558 | |
| 1559 | for ($pos = 0; $pos < $len; $pos++) { |
| 1560 | my $string = substr($line, $pos); |
| 1561 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 1562 | $pos += length($1) - 1; |
| 1563 | } elsif (substr($line, $pos, 1) eq '(') { |
| 1564 | $last_openparen = $pos; |
| 1565 | } elsif (index($string, '(') == -1) { |
| 1566 | last; |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | return $last_openparen + 1; |
| 1571 | } |
| 1572 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1573 | sub process { |
| 1574 | my $filename = shift; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1575 | |
| 1576 | my $linenr=0; |
| 1577 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1578 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1579 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1580 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1581 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1582 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1583 | my $indent; |
| 1584 | my $previndent=0; |
| 1585 | my $stashindent=0; |
| 1586 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1587 | our $clean = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1588 | my $signoff = 0; |
| 1589 | my $is_patch = 0; |
| 1590 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1591 | my $in_header_lines = 1; |
| 1592 | my $in_commit_log = 0; #Scanning lines before patch |
| 1593 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1594 | my $non_utf8_charset = 0; |
| 1595 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1596 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1597 | our $cnt_lines = 0; |
| 1598 | our $cnt_error = 0; |
| 1599 | our $cnt_warn = 0; |
| 1600 | our $cnt_chk = 0; |
| 1601 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1602 | # Trace the real file/line as we go. |
| 1603 | my $realfile = ''; |
| 1604 | my $realline = 0; |
| 1605 | my $realcnt = 0; |
| 1606 | my $here = ''; |
| 1607 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1608 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1609 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1610 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1611 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1612 | my $prev_values = 'E'; |
| 1613 | |
| 1614 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1615 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1616 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1617 | my %suppress_export; |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1618 | my $suppress_statement = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1619 | |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1620 | my %signatures = (); |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 1621 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1622 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1623 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1624 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1625 | my @setup_docs = (); |
| 1626 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1627 | |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 1628 | my $camelcase_file_seeded = 0; |
| 1629 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1630 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1631 | my $line; |
| 1632 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1633 | $linenr++; |
| 1634 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1635 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1636 | push(@fixed, $rawline) if ($fix); |
| 1637 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1638 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1639 | $setup_docs = 0; |
| 1640 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 1641 | $setup_docs = 1; |
| 1642 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1643 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1644 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1645 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 1646 | $realline=$1-1; |
| 1647 | if (defined $2) { |
| 1648 | $realcnt=$3+1; |
| 1649 | } else { |
| 1650 | $realcnt=1+1; |
| 1651 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1652 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1653 | |
| 1654 | # Guestimate if this is a continuing comment. Run |
| 1655 | # the context looking for a comment "edge". If this |
| 1656 | # edge is a close comment then we must be in a comment |
| 1657 | # at context start. |
| 1658 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 1659 | my $cnt = $realcnt; |
| 1660 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 1661 | next if (defined $rawlines[$ln - 1] && |
| 1662 | $rawlines[$ln - 1] =~ /^-/); |
| 1663 | $cnt--; |
| 1664 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 1665 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1666 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 1667 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 1668 | ($edge) = $1; |
| 1669 | last; |
| 1670 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1671 | } |
| 1672 | if (defined $edge && $edge eq '*/') { |
| 1673 | $in_comment = 1; |
| 1674 | } |
| 1675 | |
| 1676 | # Guestimate if this is a continuing comment. If this |
| 1677 | # is the start of a diff block and this line starts |
| 1678 | # ' *' then it is very likely a comment. |
| 1679 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 1680 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1681 | { |
| 1682 | $in_comment = 1; |
| 1683 | } |
| 1684 | |
| 1685 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 1686 | sanitise_line_reset($in_comment); |
| 1687 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1688 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1689 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1690 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1691 | $line = sanitise_line($rawline); |
| 1692 | } |
| 1693 | push(@lines, $line); |
| 1694 | |
| 1695 | if ($realcnt > 1) { |
| 1696 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 1697 | } else { |
| 1698 | $realcnt = 0; |
| 1699 | } |
| 1700 | |
| 1701 | #print "==>$rawline\n"; |
| 1702 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1703 | |
| 1704 | if ($setup_docs && $line =~ /^\+/) { |
| 1705 | push(@setup_docs, $line); |
| 1706 | } |
| 1707 | } |
| 1708 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1709 | $prefix = ''; |
| 1710 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1711 | $realcnt = 0; |
| 1712 | $linenr = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1713 | foreach my $line (@lines) { |
| 1714 | $linenr++; |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 1715 | my $sline = $line; #copy of $line |
| 1716 | $sline =~ s/$;/ /g; #with comments as spaces |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1717 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1718 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1719 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1720 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1721 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1722 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1723 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1724 | $realline=$1-1; |
| 1725 | if (defined $2) { |
| 1726 | $realcnt=$3+1; |
| 1727 | } else { |
| 1728 | $realcnt=1+1; |
| 1729 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1730 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1731 | $prev_values = 'E'; |
| 1732 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1733 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1734 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1735 | %suppress_export = (); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1736 | $suppress_statement = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1737 | next; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1738 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1739 | # track the line number as we move through the hunk, note that |
| 1740 | # new versions of GNU diff omit the leading space on completely |
| 1741 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1742 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1743 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1744 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1745 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1746 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1747 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1748 | |
| 1749 | # Track the previous line. |
| 1750 | ($prevline, $stashline) = ($stashline, $line); |
| 1751 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1752 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 1753 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1754 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1755 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1756 | } elsif ($realcnt == 1) { |
| 1757 | $realcnt--; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 1760 | my $hunk_line = ($realcnt != 0); |
| 1761 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1762 | #make up the handle for any error we report on this line |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1763 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 1764 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 1765 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1766 | $here = "#$linenr: " if (!$file); |
| 1767 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1768 | |
| 1769 | # extract the filename as it passes |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1770 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 1771 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 1772 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1773 | $in_commit_log = 0; |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1774 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1775 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 1776 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1777 | $in_commit_log = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1778 | |
| 1779 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 1780 | if (!$file && $tree && $p1_prefix ne '' && |
| 1781 | -e "$root/$p1_prefix") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1782 | WARN("PATCH_PREFIX", |
| 1783 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1784 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1785 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 1786 | if ($realfile =~ m@^include/asm/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1787 | ERROR("MODIFIED_INCLUDE_ASM", |
| 1788 | "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1789 | } |
| 1790 | next; |
| 1791 | } |
| 1792 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 1793 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1794 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1795 | my $hereline = "$here\n$rawline\n"; |
| 1796 | my $herecurr = "$here\n$rawline\n"; |
| 1797 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1798 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1799 | $cnt_lines++ if ($realcnt != 0); |
| 1800 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1801 | # Check for incorrect file permissions |
| 1802 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 1803 | my $permhere = $here . "FILE: $realfile\n"; |
Joe Perches | 04db4d2 | 2013-04-29 16:18:14 -0700 | [diff] [blame] | 1804 | if ($realfile !~ m@scripts/@ && |
| 1805 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1806 | ERROR("EXECUTE_PERMISSIONS", |
| 1807 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1811 | # Check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1812 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1813 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1814 | $in_commit_log = 0; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1818 | if (!$in_header_lines && |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1819 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1820 | my $space_before = $1; |
| 1821 | my $sign_off = $2; |
| 1822 | my $space_after = $3; |
| 1823 | my $email = $4; |
| 1824 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 1825 | |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1826 | if ($sign_off !~ /$signature_tags/) { |
| 1827 | WARN("BAD_SIGN_OFF", |
| 1828 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 1829 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1830 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1831 | if (WARN("BAD_SIGN_OFF", |
| 1832 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 1833 | $fix) { |
| 1834 | $fixed[$linenr - 1] = |
| 1835 | "$ucfirst_sign_off $email"; |
| 1836 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1837 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1838 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1839 | if (WARN("BAD_SIGN_OFF", |
| 1840 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 1841 | $fix) { |
| 1842 | $fixed[$linenr - 1] = |
| 1843 | "$ucfirst_sign_off $email"; |
| 1844 | } |
| 1845 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1846 | } |
| 1847 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1848 | if (WARN("BAD_SIGN_OFF", |
| 1849 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 1850 | $fix) { |
| 1851 | $fixed[$linenr - 1] = |
| 1852 | "$ucfirst_sign_off $email"; |
| 1853 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 1857 | my $suggested_email = format_email(($email_name, $email_address)); |
| 1858 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1859 | ERROR("BAD_SIGN_OFF", |
| 1860 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1861 | } else { |
| 1862 | my $dequoted = $suggested_email; |
| 1863 | $dequoted =~ s/^"//; |
| 1864 | $dequoted =~ s/" </ </; |
| 1865 | # Don't force email to have quotes |
| 1866 | # Allow just an angle bracketed address |
| 1867 | if ("$dequoted$comment" ne $email && |
| 1868 | "<$email_address>$comment" ne $email && |
| 1869 | "$suggested_email$comment" ne $email) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1870 | WARN("BAD_SIGN_OFF", |
| 1871 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1872 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1873 | } |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1874 | |
| 1875 | # Check for duplicate signatures |
| 1876 | my $sig_nospace = $line; |
| 1877 | $sig_nospace =~ s/\s//g; |
| 1878 | $sig_nospace = lc($sig_nospace); |
| 1879 | if (defined $signatures{$sig_nospace}) { |
| 1880 | WARN("BAD_SIGN_OFF", |
| 1881 | "Duplicate signature\n" . $herecurr); |
| 1882 | } else { |
| 1883 | $signatures{$sig_nospace} = 1; |
| 1884 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1887 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1888 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1889 | ERROR("CORRUPTED_PATCH", |
| 1890 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1891 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1894 | # Check for absolute kernel paths. |
| 1895 | if ($tree) { |
| 1896 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 1897 | my $file = $1; |
| 1898 | |
| 1899 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 1900 | check_absolute_file($1, $herecurr)) { |
| 1901 | # |
| 1902 | } else { |
| 1903 | check_absolute_file($file, $herecurr); |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1908 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 1909 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1910 | $rawline !~ m/^$UTF8*$/) { |
| 1911 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 1912 | |
| 1913 | my $blank = copy_spacing($rawline); |
| 1914 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 1915 | my $hereptr = "$hereline$ptr\n"; |
| 1916 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 1917 | CHK("INVALID_UTF8", |
| 1918 | "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1919 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1920 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1921 | # Check if it's the start of a commit log |
| 1922 | # (not a header line and we haven't seen the patch filename) |
| 1923 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1924 | $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1925 | $in_header_lines = 0; |
| 1926 | $in_commit_log = 1; |
| 1927 | } |
| 1928 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1929 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 1930 | # declined it, i.e defined some charset where it is missing. |
| 1931 | if ($in_header_lines && |
| 1932 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 1933 | $1 !~ /utf-8/i) { |
| 1934 | $non_utf8_charset = 1; |
| 1935 | } |
| 1936 | |
| 1937 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1938 | $rawline =~ /$NON_ASCII_UTF8/) { |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1939 | WARN("UTF8_BEFORE_PATCH", |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1940 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 1941 | } |
| 1942 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1943 | # ignore non-hunk lines and lines being removed |
| 1944 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1945 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1946 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1947 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1948 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 1949 | if (ERROR("DOS_LINE_ENDINGS", |
| 1950 | "DOS line endings\n" . $herevet) && |
| 1951 | $fix) { |
| 1952 | $fixed[$linenr - 1] =~ s/[\s\015]+$//; |
| 1953 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1954 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1955 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1956 | if (ERROR("TRAILING_WHITESPACE", |
| 1957 | "trailing whitespace\n" . $herevet) && |
| 1958 | $fix) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 1959 | $fixed[$linenr - 1] =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1962 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1963 | } |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1964 | |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 1965 | # Check for FSF mailing addresses. |
| 1966 | if ($rawline =~ /You should have received a copy/ || |
| 1967 | $rawline =~ /write to the Free Software/ || |
| 1968 | $rawline =~ /59 Temple Place/ || |
| 1969 | $rawline =~ /51 Franklin Street/) { |
| 1970 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 1971 | my $msg_type = \&ERROR; |
| 1972 | $msg_type = \&CHK if ($file); |
| 1973 | &{$msg_type}("FSF_MAILING_ADDRESS", |
| 1974 | "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) |
| 1975 | } |
| 1976 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1977 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1978 | # Only applies when adding the entry originally, after that we do not have |
| 1979 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1980 | if ($realfile =~ /Kconfig/ && |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1981 | $line =~ /.\s*config\s+/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1982 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1983 | my $cnt = $realcnt; |
| 1984 | my $ln = $linenr + 1; |
| 1985 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1986 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1987 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1988 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1989 | $f = $lines[$ln - 1]; |
| 1990 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 1991 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1992 | |
| 1993 | next if ($f =~ /^-/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1994 | |
| 1995 | if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { |
| 1996 | $is_start = 1; |
| 1997 | } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { |
| 1998 | $length = -1; |
| 1999 | } |
| 2000 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2001 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2002 | $f =~ s/#.*//; |
| 2003 | $f =~ s/^\s+//; |
| 2004 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2005 | if ($f =~ /^\s*config\s/) { |
| 2006 | $is_end = 1; |
| 2007 | last; |
| 2008 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2009 | $length++; |
| 2010 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2011 | WARN("CONFIG_DESCRIPTION", |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2012 | "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); |
| 2013 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2016 | # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. |
| 2017 | if ($realfile =~ /Kconfig/ && |
| 2018 | $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { |
| 2019 | WARN("CONFIG_EXPERIMENTAL", |
| 2020 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2021 | } |
| 2022 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 2023 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 2024 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 2025 | my $flag = $1; |
| 2026 | my $replacement = { |
| 2027 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 2028 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 2029 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 2030 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 2031 | }; |
| 2032 | |
| 2033 | WARN("DEPRECATED_VARIABLE", |
| 2034 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 2035 | } |
| 2036 | |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2037 | # check we are in a valid source file if not then ignore this hunk |
| 2038 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
| 2039 | |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2040 | #line length limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2041 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2042 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
Joe Perches | 0fccc62 | 2011-05-24 17:13:41 -0700 | [diff] [blame] | 2043 | !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || |
Joe Perches | 8bbea96 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 2044 | $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2045 | $length > $max_line_length) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2046 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2047 | WARN("LONG_LINE", |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2048 | "line over $max_line_length characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
Josh Triplett | ca56dc0 | 2012-03-23 15:02:21 -0700 | [diff] [blame] | 2051 | # Check for user-visible strings broken across lines, which breaks the ability |
| 2052 | # to grep for the string. Limited to strings used as parameters (those |
| 2053 | # following an open parenthesis), which almost completely eliminates false |
| 2054 | # positives, as well as warning only once per parameter rather than once per |
| 2055 | # line of the string. Make an exception when the previous string ends in a |
| 2056 | # newline (multiple lines in one string constant) or \n\t (common in inline |
| 2057 | # assembly to indent the instruction on the following line). |
| 2058 | if ($line =~ /^\+\s*"/ && |
| 2059 | $prevline =~ /"\s*$/ && |
| 2060 | $prevline =~ /\(/ && |
| 2061 | $prevrawline !~ /\\n(?:\\t)*"\s*$/) { |
| 2062 | WARN("SPLIT_STRING", |
| 2063 | "quoted string split across lines\n" . $hereprev); |
| 2064 | } |
| 2065 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 2066 | # check for spaces before a quoted newline |
| 2067 | if ($rawline =~ /^.*\".*\s\\n/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2068 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 2069 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 2070 | $fix) { |
| 2071 | $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 2072 | } |
| 2073 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2076 | # check for adding lines without a newline. |
| 2077 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2078 | WARN("MISSING_EOF_NEWLINE", |
| 2079 | "adding a line without newline at end of file\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2080 | } |
| 2081 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2082 | # Blackfin: use hi/lo macros |
| 2083 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 2084 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 2085 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2086 | ERROR("LO_MACRO", |
| 2087 | "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2088 | } |
| 2089 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 2090 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2091 | ERROR("HI_MACRO", |
| 2092 | "use the HI() macro, not (... >> 16)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2096 | # check we are in a valid source file C or perl if not then ignore this hunk |
| 2097 | next if ($realfile !~ /\.(h|c|pl)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2098 | |
| 2099 | # at the beginning of a line any tabs must come first and anything |
| 2100 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2101 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 2102 | $rawline =~ /^\+\s* \s*/) { |
| 2103 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2104 | $rpt_cleaners = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2105 | if (ERROR("CODE_INDENT", |
| 2106 | "code indent should use tabs where possible\n" . $herevet) && |
| 2107 | $fix) { |
| 2108 | $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 2109 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2112 | # check for space before tabs. |
| 2113 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 2114 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2115 | if (WARN("SPACE_BEFORE_TAB", |
| 2116 | "please, no space before tabs\n" . $herevet) && |
| 2117 | $fix) { |
| 2118 | $fixed[$linenr - 1] =~ |
| 2119 | s/(^\+.*) +\t/$1\t/; |
| 2120 | } |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2121 | } |
| 2122 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2123 | # check for && or || at the start of a line |
| 2124 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 2125 | CHK("LOGICAL_CONTINUATIONS", |
| 2126 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 2127 | } |
| 2128 | |
| 2129 | # check multi-line statement indentation matches previous line |
| 2130 | if ($^V && $^V ge 5.10.0 && |
| 2131 | $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { |
| 2132 | $prevline =~ /^\+(\t*)(.*)$/; |
| 2133 | my $oldindent = $1; |
| 2134 | my $rest = $2; |
| 2135 | |
| 2136 | my $pos = pos_last_openparen($rest); |
| 2137 | if ($pos >= 0) { |
Joe Perches | b34a26f | 2012-07-30 14:41:16 -0700 | [diff] [blame] | 2138 | $line =~ /^(\+| )([ \t]*)/; |
| 2139 | my $newindent = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2140 | |
| 2141 | my $goodtabindent = $oldindent . |
| 2142 | "\t" x ($pos / 8) . |
| 2143 | " " x ($pos % 8); |
| 2144 | my $goodspaceindent = $oldindent . " " x $pos; |
| 2145 | |
| 2146 | if ($newindent ne $goodtabindent && |
| 2147 | $newindent ne $goodspaceindent) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2148 | |
| 2149 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 2150 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 2151 | $fix && $line =~ /^\+/) { |
| 2152 | $fixed[$linenr - 1] =~ |
| 2153 | s/^\+[ \t]*/\+$goodtabindent/; |
| 2154 | } |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | } |
| 2158 | |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2159 | if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2160 | if (CHK("SPACING", |
| 2161 | "No space is necessary after a cast\n" . $hereprev) && |
| 2162 | $fix) { |
| 2163 | $fixed[$linenr - 1] =~ |
| 2164 | s/^(\+.*\*[ \t]*\))[ \t]+/$1/; |
| 2165 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2168 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | fdb4bcd | 2013-07-03 15:05:23 -0700 | [diff] [blame] | 2169 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
| 2170 | $rawline =~ /^\+[ \t]*\*/) { |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2171 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2172 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 2173 | } |
| 2174 | |
| 2175 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2176 | $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* |
| 2177 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
Joe Perches | 61135e9 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 2178 | $rawline =~ /^\+/ && #line is new |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2179 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
| 2180 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2181 | "networking block comments start with * on subsequent lines\n" . $hereprev); |
| 2182 | } |
| 2183 | |
| 2184 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | c24f9f1 | 2012-11-08 15:53:29 -0800 | [diff] [blame] | 2185 | $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
| 2186 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 2187 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 2188 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2189 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2190 | "networking block comments put the trailing */ on a separate line\n" . $herecurr); |
| 2191 | } |
| 2192 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2193 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 2194 | # Exceptions: |
| 2195 | # 1) within comments |
| 2196 | # 2) indented preprocessor commands |
| 2197 | # 3) hanging labels |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2198 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2199 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2200 | if (WARN("LEADING_SPACE", |
| 2201 | "please, no spaces at the start of a line\n" . $herevet) && |
| 2202 | $fix) { |
| 2203 | $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 2204 | } |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2207 | # check we are in a valid C source file if not then ignore this hunk |
| 2208 | next if ($realfile !~ /\.(h|c)$/); |
| 2209 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2210 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). |
| 2211 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { |
| 2212 | WARN("CONFIG_EXPERIMENTAL", |
| 2213 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2214 | } |
| 2215 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2216 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2217 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2218 | WARN("CVS_KEYWORD", |
| 2219 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2220 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2221 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2222 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 2223 | if ($line =~ /__builtin_bfin_csync/) { |
| 2224 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2225 | ERROR("CSYNC", |
| 2226 | "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2227 | } |
| 2228 | if ($line =~ /__builtin_bfin_ssync/) { |
| 2229 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2230 | ERROR("SSYNC", |
| 2231 | "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Joe Perches | 56e77d7 | 2013-02-21 16:44:14 -0800 | [diff] [blame] | 2234 | # check for old HOTPLUG __dev<foo> section markings |
| 2235 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 2236 | WARN("HOTPLUG_SECTION", |
| 2237 | "Using $1 is unnecessary\n" . $herecurr); |
| 2238 | } |
| 2239 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2240 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2241 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 2242 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2243 | #print "LINE<$line>\n"; |
| 2244 | if ($linenr >= $suppress_statement && |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2245 | $realcnt && $sline =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2246 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2247 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2248 | $stat =~ s/\n./\n /g; |
| 2249 | $cond =~ s/\n./\n /g; |
| 2250 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2251 | #print "linenr<$linenr> <$stat>\n"; |
| 2252 | # If this statement has no statement boundaries within |
| 2253 | # it there is no point in retrying a statement scan |
| 2254 | # until we hit end of it. |
| 2255 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 2256 | if ($frag !~ /(?:{|;)/) { |
| 2257 | #print "skip<$line_nr_next>\n"; |
| 2258 | $suppress_statement = $line_nr_next; |
| 2259 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 2260 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2261 | # Find the real next line. |
| 2262 | $realline_next = $line_nr_next; |
| 2263 | if (defined $realline_next && |
| 2264 | (!defined $lines[$realline_next - 1] || |
| 2265 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 2266 | $realline_next++; |
| 2267 | } |
| 2268 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2269 | my $s = $stat; |
| 2270 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2271 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2272 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2273 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2274 | |
| 2275 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2276 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2277 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 2278 | } elsif ($s =~ /^.\s*else\b/s) { |
| 2279 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2280 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2281 | } 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 Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2282 | my $type = $1; |
| 2283 | $type =~ s/\s+/ /g; |
| 2284 | possible($type, "A:" . $s); |
| 2285 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2286 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2287 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2288 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2289 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2290 | |
| 2291 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2292 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2293 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2294 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2295 | |
| 2296 | # Check for any sort of function declaration. |
| 2297 | # int foo(something bar, other baz); |
| 2298 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2299 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2300 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2301 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2302 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2303 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2304 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2305 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2306 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2307 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2308 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2309 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2314 | } |
| 2315 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2316 | # |
| 2317 | # Checks which may be anchored in the context. |
| 2318 | # |
| 2319 | |
| 2320 | # Check for switch () and associated case and default |
| 2321 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2322 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 2323 | my $err = ''; |
| 2324 | my $sep = ''; |
| 2325 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 2326 | shift(@ctx); |
| 2327 | for my $ctx (@ctx) { |
| 2328 | my ($clen, $cindent) = line_stats($ctx); |
| 2329 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 2330 | $indent != $cindent) { |
| 2331 | $err .= "$sep$ctx\n"; |
| 2332 | $sep = ''; |
| 2333 | } else { |
| 2334 | $sep = "[...]\n"; |
| 2335 | } |
| 2336 | } |
| 2337 | if ($err ne '') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2338 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 2339 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 2344 | # or if that brace on the next line is for something else |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2345 | if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2346 | my $pre_ctx = "$1$2"; |
| 2347 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2348 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 2349 | |
| 2350 | if ($line =~ /^\+\t{6,}/) { |
| 2351 | WARN("DEEP_INDENTATION", |
| 2352 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 2353 | } |
| 2354 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2355 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 2356 | my $ctx = join("\n", @ctx); |
| 2357 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2358 | my $ctx_ln = $linenr; |
| 2359 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2360 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2361 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 2362 | defined $lines[$ctx_ln - 1] && |
| 2363 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 2364 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 2365 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2366 | $ctx_ln++; |
| 2367 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2368 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2369 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 2370 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2371 | |
| 2372 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2373 | ERROR("OPEN_BRACE", |
| 2374 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2375 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2376 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2377 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 2378 | $ctx =~ /\)\s*\;\s*$/ && |
| 2379 | defined $lines[$ctx_ln - 1]) |
| 2380 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2381 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 2382 | if ($nindent > $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2383 | WARN("TRAILING_SEMICOLON", |
| 2384 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2385 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2386 | } |
| 2387 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2388 | } |
| 2389 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2390 | # Check relative indent for conditionals and blocks. |
| 2391 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2392 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2393 | ctx_statement_block($linenr, $realcnt, 0) |
| 2394 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2395 | my ($s, $c) = ($stat, $cond); |
| 2396 | |
| 2397 | substr($s, 0, length($c), ''); |
| 2398 | |
| 2399 | # Make sure we remove the line prefixes as we have |
| 2400 | # none on the first line, and are going to readd them |
| 2401 | # where necessary. |
| 2402 | $s =~ s/\n./\n/gs; |
| 2403 | |
| 2404 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2405 | my @newlines = ($c =~ /\n/gs); |
| 2406 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2407 | |
| 2408 | # We want to check the first line inside the block |
| 2409 | # starting at the end of the conditional, so remove: |
| 2410 | # 1) any blank line termination |
| 2411 | # 2) any opening brace { on end of the line |
| 2412 | # 3) any do (...) { |
| 2413 | my $continuation = 0; |
| 2414 | my $check = 0; |
| 2415 | $s =~ s/^.*\bdo\b//; |
| 2416 | $s =~ s/^\s*{//; |
| 2417 | if ($s =~ s/^\s*\\//) { |
| 2418 | $continuation = 1; |
| 2419 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2420 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2421 | $check = 1; |
| 2422 | $cond_lines++; |
| 2423 | } |
| 2424 | |
| 2425 | # Also ignore a loop construct at the end of a |
| 2426 | # preprocessor statement. |
| 2427 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 2428 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 2429 | $check = 0; |
| 2430 | } |
| 2431 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2432 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2433 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2434 | while ($cond_ptr != $cond_lines) { |
| 2435 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2436 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2437 | # If we see an #else/#elif then the code |
| 2438 | # is not linear. |
| 2439 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 2440 | $check = 0; |
| 2441 | } |
| 2442 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2443 | # Ignore: |
| 2444 | # 1) blank lines, they should be at 0, |
| 2445 | # 2) preprocessor lines, and |
| 2446 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2447 | if ($continuation || |
| 2448 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2449 | $s =~ /^\s*#\s*?/ || |
| 2450 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2451 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 2452 | if ($s =~ s/^.*?\n//) { |
| 2453 | $cond_lines++; |
| 2454 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2455 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | my (undef, $sindent) = line_stats("+" . $s); |
| 2459 | my $stat_real = raw_line($linenr, $cond_lines); |
| 2460 | |
| 2461 | # Check if either of these lines are modified, else |
| 2462 | # this is not this patch's fault. |
| 2463 | if (!defined($stat_real) || |
| 2464 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 2465 | $check = 0; |
| 2466 | } |
| 2467 | if (defined($stat_real) && $cond_lines > 1) { |
| 2468 | $stat_real = "[...]\n$stat_real"; |
| 2469 | } |
| 2470 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2471 | #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 Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2472 | |
| 2473 | if ($check && (($sindent % 8) != 0 || |
| 2474 | ($sindent <= $indent && $s ne ''))) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2475 | WARN("SUSPECT_CODE_INDENT", |
| 2476 | "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2480 | # Track the 'values' across context and added lines. |
| 2481 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2482 | my ($curr_values, $curr_vars) = |
| 2483 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2484 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2485 | if ($dbg_values) { |
| 2486 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2487 | print "$linenr > .$outline\n"; |
| 2488 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2489 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2490 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2491 | $prev_values = substr($curr_values, -1); |
| 2492 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2493 | #ignore lines not being added |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2494 | next if ($line =~ /^[^\+]/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2495 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2496 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2497 | if ($dbg_type) { |
| 2498 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2499 | ERROR("TEST_TYPE", |
| 2500 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2501 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2502 | ERROR("TEST_NOT_TYPE", |
| 2503 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2504 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2505 | next; |
| 2506 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2507 | # TEST: allow direct testing of the attribute matcher. |
| 2508 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2509 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2510 | ERROR("TEST_ATTR", |
| 2511 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2512 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2513 | ERROR("TEST_NOT_ATTR", |
| 2514 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2515 | } |
| 2516 | next; |
| 2517 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2518 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2519 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 2520 | if ($line =~ /^.\s*{/ && |
| 2521 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2522 | ERROR("OPEN_BRACE", |
| 2523 | "that open brace { should be on the previous line\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2524 | } |
| 2525 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2526 | # |
| 2527 | # Checks which are anchored on the added line. |
| 2528 | # |
| 2529 | |
| 2530 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2531 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2532 | my $path = $1; |
| 2533 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2534 | ERROR("MALFORMED_INCLUDE", |
Joe Perches | 495e9d8 | 2012-12-20 15:05:37 -0800 | [diff] [blame] | 2535 | "malformed #include filename\n" . $herecurr); |
| 2536 | } |
| 2537 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 2538 | ERROR("UAPI_INCLUDE", |
| 2539 | "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2540 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2541 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2542 | |
| 2543 | # no C99 // comments |
| 2544 | if ($line =~ m{//}) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2545 | if (ERROR("C99_COMMENTS", |
| 2546 | "do not use C99 // comments\n" . $herecurr) && |
| 2547 | $fix) { |
| 2548 | my $line = $fixed[$linenr - 1]; |
| 2549 | if ($line =~ /\/\/(.*)$/) { |
| 2550 | my $comment = trim($1); |
| 2551 | $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; |
| 2552 | } |
| 2553 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2554 | } |
| 2555 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2556 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2557 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2558 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2559 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 2560 | # the whole statement. |
| 2561 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 2562 | if (defined $realline_next && |
| 2563 | exists $lines[$realline_next - 1] && |
| 2564 | !defined $suppress_export{$realline_next} && |
| 2565 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2566 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2567 | # Handle definitions which produce identifiers with |
| 2568 | # a prefix: |
| 2569 | # XXX(foo); |
| 2570 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2571 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 2572 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2573 | $name =~ /^${Ident}_$2/) { |
| 2574 | #print "FOO C name<$name>\n"; |
| 2575 | $suppress_export{$realline_next} = 1; |
| 2576 | |
| 2577 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2578 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2579 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 2580 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 2581 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2582 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 2583 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2584 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2585 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 2586 | $suppress_export{$realline_next} = 2; |
| 2587 | } else { |
| 2588 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2589 | } |
| 2590 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2591 | if (!defined $suppress_export{$linenr} && |
| 2592 | $prevline =~ /^.\s*$/ && |
| 2593 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2594 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 2595 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 2596 | $suppress_export{$linenr} = 2; |
| 2597 | } |
| 2598 | if (defined $suppress_export{$linenr} && |
| 2599 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2600 | WARN("EXPORT_SYMBOL", |
| 2601 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2602 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2603 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 2604 | # check for global initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2605 | if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { |
| 2606 | if (ERROR("GLOBAL_INITIALISERS", |
| 2607 | "do not initialise globals to 0 or NULL\n" . |
| 2608 | $herecurr) && |
| 2609 | $fix) { |
| 2610 | $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; |
| 2611 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2612 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2613 | # check for static initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2614 | if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
| 2615 | if (ERROR("INITIALISED_STATIC", |
| 2616 | "do not initialise statics to 0 or NULL\n" . |
| 2617 | $herecurr) && |
| 2618 | $fix) { |
| 2619 | $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; |
| 2620 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2623 | # check for static const char * arrays. |
| 2624 | if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2625 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2626 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2627 | $herecurr); |
| 2628 | } |
| 2629 | |
| 2630 | # check for static char foo[] = "bar" declarations. |
| 2631 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2632 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2633 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2634 | $herecurr); |
| 2635 | } |
| 2636 | |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2637 | # check for declarations of struct pci_device_id |
| 2638 | if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2639 | WARN("DEFINE_PCI_DEVICE_TABLE", |
| 2640 | "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2643 | # check for new typedefs, only function parameters and sparse annotations |
| 2644 | # make sense. |
| 2645 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 2646 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2647 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2648 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2649 | $line !~ /\b__bitwise(?:__|)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2650 | WARN("NEW_TYPEDEFS", |
| 2651 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2655 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2656 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 2657 | #print "AA<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2658 | my ($ident, $from, $to) = ($1, $2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2659 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2660 | # Should start with a space. |
| 2661 | $to =~ s/^(\S)/ $1/; |
| 2662 | # Should not end with a space. |
| 2663 | $to =~ s/\s+$//; |
| 2664 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2665 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2666 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2667 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2668 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2669 | if ($from ne $to) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2670 | if (ERROR("POINTER_LOCATION", |
| 2671 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 2672 | $fix) { |
| 2673 | my $sub_from = $ident; |
| 2674 | my $sub_to = $ident; |
| 2675 | $sub_to =~ s/\Q$from\E/$to/; |
| 2676 | $fixed[$linenr - 1] =~ |
| 2677 | s@\Q$sub_from\E@$sub_to@; |
| 2678 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2679 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2680 | } |
| 2681 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 2682 | #print "BB<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2683 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2684 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2685 | # Should start with a space. |
| 2686 | $to =~ s/^(\S)/ $1/; |
| 2687 | # Should not end with a space. |
| 2688 | $to =~ s/\s+$//; |
| 2689 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2690 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2691 | } |
| 2692 | # Modifiers should have spaces. |
| 2693 | $to =~ s/(\b$Modifier$)/$1 /; |
| 2694 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2695 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2696 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2697 | if (ERROR("POINTER_LOCATION", |
| 2698 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 2699 | $fix) { |
| 2700 | |
| 2701 | my $sub_from = $match; |
| 2702 | my $sub_to = $match; |
| 2703 | $sub_to =~ s/\Q$from\E/$to/; |
| 2704 | $fixed[$linenr - 1] =~ |
| 2705 | s@\Q$sub_from\E@$sub_to@; |
| 2706 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2707 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | # # no BUG() or BUG_ON() |
| 2711 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 2712 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 2713 | # print "$herecurr"; |
| 2714 | # $clean = 0; |
| 2715 | # } |
| 2716 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2717 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2718 | WARN("LINUX_VERSION_CODE", |
| 2719 | "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2720 | } |
| 2721 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2722 | # check for uses of printk_ratelimit |
| 2723 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2724 | WARN("PRINTK_RATELIMITED", |
| 2725 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2728 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 2729 | # same line do not need a level, so we use the current block context |
| 2730 | # to try and find and validate the current printk. In summary the current |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2731 | # printk includes all preceding printk's which have no newline on the end. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2732 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2733 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2734 | my $ok = 0; |
| 2735 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 2736 | #print "CHECK<$lines[$ln - 1]\n"; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2737 | # we have a preceding printk if it ends |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2738 | # with "\n" ignore it, else it is to blame |
| 2739 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 2740 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 2741 | $ok = 1; |
| 2742 | } |
| 2743 | last; |
| 2744 | } |
| 2745 | } |
| 2746 | if ($ok == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2747 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 2748 | "printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2749 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2750 | } |
| 2751 | |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2752 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 2753 | my $orig = $1; |
| 2754 | my $level = lc($orig); |
| 2755 | $level = "warn" if ($level eq "warning"); |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2756 | my $level2 = $level; |
| 2757 | $level2 = "dbg" if ($level eq "debug"); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2758 | WARN("PREFER_PR_LEVEL", |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2759 | "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | if ($line =~ /\bpr_warning\s*\(/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2763 | if (WARN("PREFER_PR_LEVEL", |
| 2764 | "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && |
| 2765 | $fix) { |
| 2766 | $fixed[$linenr - 1] =~ |
| 2767 | s/\bpr_warning\b/pr_warn/; |
| 2768 | } |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Joe Perches | dc13931 | 2013-02-21 16:44:13 -0800 | [diff] [blame] | 2771 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 2772 | my $orig = $1; |
| 2773 | my $level = lc($orig); |
| 2774 | $level = "warn" if ($level eq "warning"); |
| 2775 | $level = "dbg" if ($level eq "debug"); |
| 2776 | WARN("PREFER_DEV_LEVEL", |
| 2777 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 2778 | } |
| 2779 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2780 | # function brace can't be on same line, except for #defines of do while, |
| 2781 | # or if closed on same line |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2782 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
| 2783 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2784 | ERROR("OPEN_BRACE", |
| 2785 | "open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2786 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2787 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2788 | # open braces for enum, union and struct go on the same line. |
| 2789 | if ($line =~ /^.\s*{/ && |
| 2790 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2791 | ERROR("OPEN_BRACE", |
| 2792 | "open brace '{' following $1 go on the same line\n" . $hereprev); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2793 | } |
| 2794 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2795 | # missing space after union, struct or enum definition |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2796 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 2797 | if (WARN("SPACING", |
| 2798 | "missing space after $1 definition\n" . $herecurr) && |
| 2799 | $fix) { |
| 2800 | $fixed[$linenr - 1] =~ |
| 2801 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 2802 | } |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2803 | } |
| 2804 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2805 | # check for spacing round square brackets; allowed: |
| 2806 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2807 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 2808 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2809 | while ($line =~ /(.*?\s)\[/g) { |
| 2810 | my ($where, $prefix) = ($-[1], $1); |
| 2811 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2812 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Andy Whitcroft | daebc53 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 2813 | $prefix !~ /[{,]\s+$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2814 | if (ERROR("BRACKET_SPACE", |
| 2815 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 2816 | $fix) { |
| 2817 | $fixed[$linenr - 1] =~ |
| 2818 | s/^(\+.*?)\s+\[/$1\[/; |
| 2819 | } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2820 | } |
| 2821 | } |
| 2822 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2823 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2824 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2825 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2826 | my $ctx_before = substr($line, 0, $-[1]); |
| 2827 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2828 | |
| 2829 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2830 | if ($name =~ /^(?: |
| 2831 | if|for|while|switch|return|case| |
| 2832 | volatile|__volatile__| |
| 2833 | __attribute__|format|__extension__| |
| 2834 | asm|__asm__)$/x) |
| 2835 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2836 | # cpp #define statements have non-optional spaces, ie |
| 2837 | # if there is a space between the name and the open |
| 2838 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2839 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2840 | |
| 2841 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2842 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2843 | |
| 2844 | # If this whole things ends with a type its most |
| 2845 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2846 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2847 | |
| 2848 | } else { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2849 | if (WARN("SPACING", |
| 2850 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 2851 | $fix) { |
| 2852 | $fixed[$linenr - 1] =~ |
| 2853 | s/\b$name\s+\(/$name\(/; |
| 2854 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2855 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2856 | } |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2857 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2858 | # Check operator spacing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2859 | if (!($line=~/\#\s*include/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2860 | my $fixed_line = ""; |
| 2861 | my $line_fixed = 0; |
| 2862 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2863 | my $ops = qr{ |
| 2864 | <<=|>>=|<=|>=|==|!=| |
| 2865 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 2866 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2867 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 2868 | \?:|\?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2869 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2870 | my @elements = split(/($ops|;)/, $opline); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2871 | |
| 2872 | ## print("element count: <" . $#elements . ">\n"); |
| 2873 | ## foreach my $el (@elements) { |
| 2874 | ## print("el: <$el>\n"); |
| 2875 | ## } |
| 2876 | |
| 2877 | my @fix_elements = (); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2878 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2879 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2880 | foreach my $el (@elements) { |
| 2881 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 2882 | $off += length($el); |
| 2883 | } |
| 2884 | |
| 2885 | $off = 0; |
| 2886 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2887 | my $blank = copy_spacing($opline); |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2888 | my $last_after = -1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2889 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2890 | for (my $n = 0; $n < $#elements; $n += 2) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2891 | |
| 2892 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 2893 | |
| 2894 | ## print("n: <$n> good: <$good>\n"); |
| 2895 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2896 | $off += length($elements[$n]); |
| 2897 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2898 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2899 | my $ca = substr($opline, 0, $off); |
| 2900 | my $cc = ''; |
| 2901 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 2902 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 2903 | } |
| 2904 | my $cb = "$ca$;$cc"; |
| 2905 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2906 | my $a = ''; |
| 2907 | $a = 'V' if ($elements[$n] ne ''); |
| 2908 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2909 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2910 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 2911 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2912 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2913 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2914 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2915 | |
| 2916 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2917 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2918 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 2919 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2920 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2921 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 2922 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 2923 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2924 | } else { |
| 2925 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2926 | } |
| 2927 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2928 | my $ctx = "${a}x${c}"; |
| 2929 | |
| 2930 | my $at = "(ctx:$ctx)"; |
| 2931 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2932 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2933 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2934 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2935 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2936 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2937 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2938 | # Get the full operator variant. |
| 2939 | my $opv = $op . substr($curr_vars, $off, 1); |
| 2940 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2941 | # Ignore operators passed as parameters. |
| 2942 | if ($op_type ne 'V' && |
| 2943 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { |
| 2944 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2945 | # # Ignore comments |
| 2946 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2947 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2948 | # ; should have either the end of line or a space or \ after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2949 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2950 | if ($ctx !~ /.x[WEBC]/ && |
| 2951 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2952 | if (ERROR("SPACING", |
| 2953 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2954 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2955 | $line_fixed = 1; |
| 2956 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | # // is a comment |
| 2960 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2961 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2962 | # No spaces for: |
| 2963 | # -> |
| 2964 | # : when part of a bitfield |
| 2965 | } elsif ($op eq '->' || $opv eq ':B') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2966 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2967 | if (ERROR("SPACING", |
| 2968 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2969 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2970 | if (defined $fix_elements[$n + 2]) { |
| 2971 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 2972 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2973 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2974 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | # , must have a space on the right. |
| 2978 | } elsif ($op eq ',') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2979 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2980 | if (ERROR("SPACING", |
| 2981 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2982 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2983 | $line_fixed = 1; |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2984 | $last_after = $n; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2985 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2986 | } |
| 2987 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2988 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2989 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2990 | #warn "'*' is part of type\n"; |
| 2991 | |
| 2992 | # unary operators should have a space before and |
| 2993 | # none after. May be left adjacent to another |
| 2994 | # unary operator, or a cast |
| 2995 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2996 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2997 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2998 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2999 | if (ERROR("SPACING", |
| 3000 | "space required before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3001 | if ($n != $last_after + 2) { |
| 3002 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 3003 | $line_fixed = 1; |
| 3004 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3005 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3006 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 3007 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3008 | # A unary '*' may be const |
| 3009 | |
| 3010 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3011 | if (ERROR("SPACING", |
| 3012 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3013 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3014 | if (defined $fix_elements[$n + 2]) { |
| 3015 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3016 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3017 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3018 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | # unary ++ and unary -- are allowed no space on one side. |
| 3022 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3023 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3024 | if (ERROR("SPACING", |
| 3025 | "space required one side of that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3026 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3027 | $line_fixed = 1; |
| 3028 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3029 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3030 | if ($ctx =~ /Wx[BE]/ || |
| 3031 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3032 | if (ERROR("SPACING", |
| 3033 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3034 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3035 | $line_fixed = 1; |
| 3036 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3037 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3038 | if ($ctx =~ /ExW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3039 | if (ERROR("SPACING", |
| 3040 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3041 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3042 | if (defined $fix_elements[$n + 2]) { |
| 3043 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3044 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3045 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3046 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3049 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3050 | } elsif ($op eq '<<' or $op eq '>>' or |
| 3051 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 3052 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3053 | $op eq '*' or $op eq '/' or |
| 3054 | $op eq '%') |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3055 | { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3056 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3057 | if (ERROR("SPACING", |
| 3058 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3059 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3060 | if (defined $fix_elements[$n + 2]) { |
| 3061 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3062 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3063 | $line_fixed = 1; |
| 3064 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3067 | # A colon needs no spaces before when it is |
| 3068 | # terminating a case value or a label. |
| 3069 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 3070 | if ($ctx =~ /Wx./) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3071 | if (ERROR("SPACING", |
| 3072 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3073 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3074 | $line_fixed = 1; |
| 3075 | } |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3078 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3079 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3080 | my $ok = 0; |
| 3081 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3082 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3083 | if (($op eq '<' && |
| 3084 | $cc =~ /^\S+\@\S+>/) || |
| 3085 | ($op eq '>' && |
| 3086 | $ca =~ /<\S+\@\S+$/)) |
| 3087 | { |
| 3088 | $ok = 1; |
| 3089 | } |
| 3090 | |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3091 | # messages are ERROR, but ?: are CHK |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3092 | if ($ok == 0) { |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3093 | my $msg_type = \&ERROR; |
| 3094 | $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
| 3095 | |
| 3096 | if (&{$msg_type}("SPACING", |
| 3097 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3098 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3099 | if (defined $fix_elements[$n + 2]) { |
| 3100 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3101 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3102 | $line_fixed = 1; |
| 3103 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3104 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3105 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3106 | $off += length($elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3107 | |
| 3108 | ## print("n: <$n> GOOD: <$good>\n"); |
| 3109 | |
| 3110 | $fixed_line = $fixed_line . $good; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3111 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3112 | |
| 3113 | if (($#elements % 2) == 0) { |
| 3114 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 3115 | } |
| 3116 | |
| 3117 | if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { |
| 3118 | $fixed[$linenr - 1] = $fixed_line; |
| 3119 | } |
| 3120 | |
| 3121 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 3124 | # check for whitespace before a non-naked semicolon |
| 3125 | if ($line =~ /^\+.*\S\s+;/) { |
| 3126 | if (WARN("SPACING", |
| 3127 | "space prohibited before semicolon\n" . $herecurr) && |
| 3128 | $fix) { |
| 3129 | 1 while $fixed[$linenr - 1] =~ |
| 3130 | s/^(\+.*\S)\s+;/$1;/; |
| 3131 | } |
| 3132 | } |
| 3133 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3134 | # check for multiple assignments |
| 3135 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3136 | CHK("MULTIPLE_ASSIGNMENTS", |
| 3137 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3140 | ## # check for multiple declarations, allowing for a function declaration |
| 3141 | ## # continuation. |
| 3142 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 3143 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 3144 | ## |
| 3145 | ## # Remove any bracketed sections to ensure we do not |
| 3146 | ## # falsly report the parameters of functions. |
| 3147 | ## my $ln = $line; |
| 3148 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 3149 | ## } |
| 3150 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3151 | ## WARN("MULTIPLE_DECLARATION", |
| 3152 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3153 | ## } |
| 3154 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3155 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3156 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3157 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 3158 | $line =~ /do{/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3159 | if (ERROR("SPACING", |
| 3160 | "space required before the open brace '{'\n" . $herecurr) && |
| 3161 | $fix) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3162 | $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3163 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
Joe Perches | c4a62ef | 2013-07-03 15:05:28 -0700 | [diff] [blame] | 3166 | ## # check for blank lines before declarations |
| 3167 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 3168 | ## $prevrawline =~ /^.\s*$/) { |
| 3169 | ## WARN("SPACING", |
| 3170 | ## "No blank lines before declarations\n" . $hereprev); |
| 3171 | ## } |
| 3172 | ## |
| 3173 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3174 | # closing brace should have a space following it when it has anything |
| 3175 | # on the line |
| 3176 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3177 | if (ERROR("SPACING", |
| 3178 | "space required after that close brace '}'\n" . $herecurr) && |
| 3179 | $fix) { |
| 3180 | $fixed[$linenr - 1] =~ |
| 3181 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 3182 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3185 | # check spacing on square brackets |
| 3186 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3187 | if (ERROR("SPACING", |
| 3188 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 3189 | $fix) { |
| 3190 | $fixed[$linenr - 1] =~ |
| 3191 | s/\[\s+/\[/; |
| 3192 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3193 | } |
| 3194 | if ($line =~ /\s\]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3195 | if (ERROR("SPACING", |
| 3196 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 3197 | $fix) { |
| 3198 | $fixed[$linenr - 1] =~ |
| 3199 | s/\s+\]/\]/; |
| 3200 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3203 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3204 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 3205 | $line !~ /for\s*\(\s+;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3206 | if (ERROR("SPACING", |
| 3207 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 3208 | $fix) { |
| 3209 | $fixed[$linenr - 1] =~ |
| 3210 | s/\(\s+/\(/; |
| 3211 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3212 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3213 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3214 | $line !~ /for\s*\(.*;\s+\)/ && |
| 3215 | $line !~ /:\s+\)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3216 | if (ERROR("SPACING", |
| 3217 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 3218 | $fix) { |
| 3219 | $fixed[$linenr - 1] =~ |
| 3220 | s/\s+\)/\)/; |
| 3221 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3222 | } |
| 3223 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3224 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3225 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3226 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3227 | if (WARN("INDENTED_LABEL", |
| 3228 | "labels should not be indented\n" . $herecurr) && |
| 3229 | $fix) { |
| 3230 | $fixed[$linenr - 1] =~ |
| 3231 | s/^(.)\s+/$1/; |
| 3232 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3233 | } |
| 3234 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3235 | # Return is not a function. |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 3236 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3237 | my $spacing = $1; |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 3238 | if ($^V && $^V ge 5.10.0 && |
| 3239 | $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3240 | ERROR("RETURN_PARENTHESES", |
| 3241 | "return is not a function, parentheses are not required\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3242 | |
| 3243 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3244 | ERROR("SPACING", |
| 3245 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3246 | } |
| 3247 | } |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 3248 | |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 3249 | # Return of what appears to be an errno should normally be -'ve |
| 3250 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
| 3251 | my $name = $1; |
| 3252 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3253 | WARN("USE_NEGATIVE_ERRNO", |
| 3254 | "return of an errno should typically be -ve (return -$1)\n" . $herecurr); |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 3255 | } |
| 3256 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3257 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3258 | # Need a space before open parenthesis after if, while etc |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3259 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 3260 | if (ERROR("SPACING", |
| 3261 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 3262 | $fix) { |
| 3263 | $fixed[$linenr - 1] =~ |
| 3264 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 3265 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3268 | # Check for illegal assignment in if conditional -- and check for trailing |
| 3269 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3270 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 3271 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3272 | ctx_statement_block($linenr, $realcnt, 0) |
| 3273 | if (!defined $stat); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3274 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 3275 | $remain_next, $off_next); |
| 3276 | $stat_next =~ s/\n./\n /g; |
| 3277 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 3278 | |
| 3279 | if ($stat_next =~ /^\s*while\b/) { |
| 3280 | # If the statement carries leading newlines, |
| 3281 | # then count those as offsets. |
| 3282 | my ($whitespace) = |
| 3283 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3284 | my $offset = |
| 3285 | statement_rawlines($whitespace) - 1; |
| 3286 | |
| 3287 | $suppress_whiletrailers{$line_nr_next + |
| 3288 | $offset} = 1; |
| 3289 | } |
| 3290 | } |
| 3291 | if (!defined $suppress_whiletrailers{$linenr} && |
| 3292 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3293 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3294 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 3295 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3296 | ERROR("ASSIGN_IN_IF", |
| 3297 | "do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | # Find out what is on the end of the line after the |
| 3301 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3302 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3303 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3304 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3305 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 3306 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3307 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3308 | # Find out how long the conditional actually is. |
| 3309 | my @newlines = ($c =~ /\n/gs); |
| 3310 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 3311 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3312 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 3313 | $stat_real = raw_line($linenr, $cond_lines) |
| 3314 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3315 | if (defined($stat_real) && $cond_lines > 1) { |
| 3316 | $stat_real = "[...]\n$stat_real"; |
| 3317 | } |
| 3318 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3319 | ERROR("TRAILING_STATEMENTS", |
| 3320 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3321 | } |
| 3322 | } |
| 3323 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3324 | # Check for bitwise tests written as boolean |
| 3325 | if ($line =~ / |
| 3326 | (?: |
| 3327 | (?:\[|\(|\&\&|\|\|) |
| 3328 | \s*0[xX][0-9]+\s* |
| 3329 | (?:\&\&|\|\|) |
| 3330 | | |
| 3331 | (?:\&\&|\|\|) |
| 3332 | \s*0[xX][0-9]+\s* |
| 3333 | (?:\&\&|\|\||\)|\]) |
| 3334 | )/x) |
| 3335 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3336 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 3337 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3338 | } |
| 3339 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3340 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3341 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 3342 | my $s = $1; |
| 3343 | $s =~ s/$;//g; # Remove any comments |
| 3344 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3345 | ERROR("TRAILING_STATEMENTS", |
| 3346 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3347 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3348 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3349 | # if should not continue a brace |
| 3350 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3351 | ERROR("TRAILING_STATEMENTS", |
| 3352 | "trailing statements should be on next line\n" . |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3353 | $herecurr); |
| 3354 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3355 | # case and default should not have general statements after them |
| 3356 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 3357 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 3358 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3359 | \s*return\s+ |
| 3360 | )/xg) |
| 3361 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3362 | ERROR("TRAILING_STATEMENTS", |
| 3363 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3364 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3365 | |
| 3366 | # Check for }<nl>else {, these must be at the same |
| 3367 | # indent level to be relevant to each other. |
| 3368 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 3369 | $previndent == $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3370 | ERROR("ELSE_AFTER_BRACE", |
| 3371 | "else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3372 | } |
| 3373 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3374 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
| 3375 | $previndent == $indent) { |
| 3376 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 3377 | |
| 3378 | # Find out what is on the end of the line after the |
| 3379 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3380 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3381 | $s =~ s/\n.*//g; |
| 3382 | |
| 3383 | if ($s =~ /^\s*;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3384 | ERROR("WHILE_AFTER_BRACE", |
| 3385 | "while should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3389 | #Specific variable tests |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 3390 | while ($line =~ m{($Constant|$Lval)}g) { |
| 3391 | my $var = $1; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3392 | |
| 3393 | #gcc binary extension |
| 3394 | if ($var =~ /^$Binary$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3395 | if (WARN("GCC_BINARY_CONSTANT", |
| 3396 | "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && |
| 3397 | $fix) { |
| 3398 | my $hexval = sprintf("0x%x", oct($var)); |
| 3399 | $fixed[$linenr - 1] =~ |
| 3400 | s/\b$var\b/$hexval/; |
| 3401 | } |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | #CamelCase |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 3405 | if ($var !~ /^$Constant$/ && |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3406 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 3407 | #Ignore Page<foo> variants |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 3408 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 3409 | #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 3410 | $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 3411 | while ($var =~ m{($Ident)}g) { |
| 3412 | my $word = $1; |
| 3413 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 3414 | if ($check) { |
| 3415 | seed_camelcase_includes(); |
| 3416 | if (!$file && !$camelcase_file_seeded) { |
| 3417 | seed_camelcase_file($realfile); |
| 3418 | $camelcase_file_seeded = 1; |
| 3419 | } |
| 3420 | } |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 3421 | if (!defined $camelcase{$word}) { |
| 3422 | $camelcase{$word} = 1; |
| 3423 | CHK("CAMELCASE", |
| 3424 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 3425 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 3426 | } |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 3427 | } |
| 3428 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3429 | |
| 3430 | #no spaces allowed after \ in define |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3431 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 3432 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 3433 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 3434 | $fix) { |
| 3435 | $fixed[$linenr - 1] =~ s/\s+$//; |
| 3436 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3439 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3440 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3441 | my $file = "$1.h"; |
| 3442 | my $checkfile = "include/linux/$file"; |
| 3443 | if (-f "$root/$checkfile" && |
| 3444 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 3445 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3446 | { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3447 | if ($realfile =~ m{^arch/}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3448 | CHK("ARCH_INCLUDE_LINUX", |
| 3449 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3450 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3451 | WARN("INCLUDE_LINUX", |
| 3452 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3453 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3457 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 3458 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3459 | # in a known good container |
Andy Whitcroft | b8f96a3 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 3460 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 3461 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3462 | my $ln = $linenr; |
| 3463 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3464 | my ($off, $dstat, $dcond, $rest); |
| 3465 | my $ctx = ''; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3466 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3467 | ctx_statement_block($linenr, $realcnt, 0); |
| 3468 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3469 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 3470 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3471 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3472 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 3473 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3474 | $dstat =~ s/\\\n.//g; |
| 3475 | $dstat =~ s/^\s*//s; |
| 3476 | $dstat =~ s/\s*$//s; |
| 3477 | |
| 3478 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3479 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 3480 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Andy Whitcroft | c81769f | 2012-01-10 15:10:10 -0800 | [diff] [blame] | 3481 | $dstat =~ s/\[[^\[\]]*\]/1/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3482 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3483 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3484 | |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 3485 | # Flatten any obvious string concatentation. |
| 3486 | while ($dstat =~ s/("X*")\s*$Ident/$1/ || |
| 3487 | $dstat =~ s/$Ident\s*("X*")/$1/) |
| 3488 | { |
| 3489 | } |
| 3490 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3491 | my $exceptions = qr{ |
| 3492 | $Declare| |
| 3493 | module_param_named| |
Kees Cook | a0a0a7a | 2012-10-04 17:13:38 -0700 | [diff] [blame] | 3494 | MODULE_PARM_DESC| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3495 | DECLARE_PER_CPU| |
| 3496 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 3497 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 3498 | union| |
| 3499 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 3500 | \.$Ident\s*=\s*| |
| 3501 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3502 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3503 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3504 | if ($dstat ne '' && |
| 3505 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 3506 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Joe Perches | 3cc4b1c | 2013-07-03 15:05:27 -0700 | [diff] [blame] | 3507 | $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz |
Andy Whitcroft | b9df76a | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3508 | $dstat !~ /^'X'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3509 | $dstat !~ /$exceptions/ && |
| 3510 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Joe Perches | e942e2c | 2013-04-17 15:58:26 -0700 | [diff] [blame] | 3511 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 3512 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3513 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 3514 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 3515 | $dstat !~ /^do\s*{/ && # do {... |
Joe Perches | f95a7e6 | 2013-09-11 14:24:00 -0700 | [diff] [blame] | 3516 | $dstat !~ /^\({/ && # ({... |
| 3517 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3518 | { |
| 3519 | $ctx =~ s/\n*$//; |
| 3520 | my $herectx = $here . "\n"; |
| 3521 | my $cnt = statement_rawlines($ctx); |
| 3522 | |
| 3523 | for (my $n = 0; $n < $cnt; $n++) { |
| 3524 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3525 | } |
| 3526 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3527 | if ($dstat =~ /;/) { |
| 3528 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 3529 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 3530 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3531 | ERROR("COMPLEX_MACRO", |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3532 | "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3533 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3534 | } |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3535 | |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3536 | # check for line continuations outside of #defines, preprocessor #, and asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3537 | |
| 3538 | } else { |
| 3539 | if ($prevline !~ /^..*\\$/ && |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3540 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 3541 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3542 | $line =~ /^\+.*\\$/) { |
| 3543 | WARN("LINE_CONTINUATIONS", |
| 3544 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 3545 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3546 | } |
| 3547 | |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3548 | # do {} while (0) macro tests: |
| 3549 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 3550 | # macro should not end with a semicolon |
| 3551 | if ($^V && $^V ge 5.10.0 && |
| 3552 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 3553 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 3554 | my $ln = $linenr; |
| 3555 | my $cnt = $realcnt; |
| 3556 | my ($off, $dstat, $dcond, $rest); |
| 3557 | my $ctx = ''; |
| 3558 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 3559 | ctx_statement_block($linenr, $realcnt, 0); |
| 3560 | $ctx = $dstat; |
| 3561 | |
| 3562 | $dstat =~ s/\\\n.//g; |
| 3563 | |
| 3564 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 3565 | my $stmts = $2; |
| 3566 | my $semis = $3; |
| 3567 | |
| 3568 | $ctx =~ s/\n*$//; |
| 3569 | my $cnt = statement_rawlines($ctx); |
| 3570 | my $herectx = $here . "\n"; |
| 3571 | |
| 3572 | for (my $n = 0; $n < $cnt; $n++) { |
| 3573 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 3574 | } |
| 3575 | |
Joe Perches | ac8e97f | 2012-08-21 16:15:53 -0700 | [diff] [blame] | 3576 | if (($stmts =~ tr/;/;/) == 1 && |
| 3577 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3578 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 3579 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 3580 | } |
| 3581 | if (defined $semis && $semis ne "") { |
| 3582 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 3583 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 3584 | } |
| 3585 | } |
| 3586 | } |
| 3587 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3588 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 3589 | # all assignments may have only one of the following with an assignment: |
| 3590 | # . |
| 3591 | # ALIGN(...) |
| 3592 | # VMLINUX_SYMBOL(...) |
| 3593 | if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3594 | WARN("MISSING_VMLINUX_SYMBOL", |
| 3595 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3596 | } |
| 3597 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3598 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3599 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 3600 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3601 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3602 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3603 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 3604 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3605 | my @allowed = (); |
| 3606 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3607 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3608 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3609 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3610 | for my $chunk (@chunks) { |
| 3611 | my ($cond, $block) = @{$chunk}; |
| 3612 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3613 | # If the condition carries leading newlines, then count those as offsets. |
| 3614 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3615 | my $offset = statement_rawlines($whitespace) - 1; |
| 3616 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3617 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3618 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 3619 | |
| 3620 | # We have looked at and allowed this specific line. |
| 3621 | $suppress_ifbraces{$ln + $offset} = 1; |
| 3622 | |
| 3623 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3624 | $ln += statement_rawlines($block) - 1; |
| 3625 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3626 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3627 | |
| 3628 | $seen++ if ($block =~ /^\s*{/); |
| 3629 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3630 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3631 | if (statement_lines($cond) > 1) { |
| 3632 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3633 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3634 | } |
| 3635 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3636 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3637 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3638 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3639 | if (statement_block_size($block) > 1) { |
| 3640 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3641 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3642 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3643 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3644 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3645 | if ($seen) { |
| 3646 | my $sum_allowed = 0; |
| 3647 | foreach (@allowed) { |
| 3648 | $sum_allowed += $_; |
| 3649 | } |
| 3650 | if ($sum_allowed == 0) { |
| 3651 | WARN("BRACES", |
| 3652 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 3653 | } elsif ($sum_allowed != $allow && |
| 3654 | $seen != $allow) { |
| 3655 | CHK("BRACES", |
| 3656 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 3657 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3658 | } |
| 3659 | } |
| 3660 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3661 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3662 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3663 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3664 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3665 | # Check the pre-context. |
| 3666 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 3667 | #print "APW: ALLOWED: pre<$1>\n"; |
| 3668 | $allowed = 1; |
| 3669 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3670 | |
| 3671 | my ($level, $endln, @chunks) = |
| 3672 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 3673 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3674 | # Check the condition. |
| 3675 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3676 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3677 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3678 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3679 | } |
| 3680 | if (statement_lines($cond) > 1) { |
| 3681 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 3682 | $allowed = 1; |
| 3683 | } |
| 3684 | if ($block =~/\b(?:if|for|while)\b/) { |
| 3685 | #print "APW: ALLOWED: block<$block>\n"; |
| 3686 | $allowed = 1; |
| 3687 | } |
| 3688 | if (statement_block_size($block) > 1) { |
| 3689 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 3690 | $allowed = 1; |
| 3691 | } |
| 3692 | # Check the post-context. |
| 3693 | if (defined $chunks[1]) { |
| 3694 | my ($cond, $block) = @{$chunks[1]}; |
| 3695 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3696 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3697 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3698 | if ($block =~ /^\s*\{/) { |
| 3699 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 3700 | $allowed = 1; |
| 3701 | } |
| 3702 | } |
| 3703 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3704 | my $herectx = $here . "\n"; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3705 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3706 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3707 | for (my $n = 0; $n < $cnt; $n++) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3708 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3709 | } |
| 3710 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3711 | WARN("BRACES", |
| 3712 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3713 | } |
| 3714 | } |
| 3715 | |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3716 | # check for unnecessary blank lines around braces |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 3717 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3718 | CHK("BRACES", |
| 3719 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); |
| 3720 | } |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 3721 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3722 | CHK("BRACES", |
| 3723 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); |
| 3724 | } |
| 3725 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3726 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3727 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 3728 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3729 | WARN("VOLATILE", |
| 3730 | "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3733 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3734 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3735 | CHK("REDUNDANT_CODE", |
| 3736 | "if this code is redundant consider removing it\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3737 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3738 | } |
| 3739 | |
Andy Whitcroft | 03df4b5 | 2012-12-17 16:01:52 -0800 | [diff] [blame] | 3740 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 3741 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
| 3742 | my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; |
| 3743 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { |
| 3744 | WARN('NEEDLESS_IF', |
| 3745 | "$1(NULL) is safe this check is probably not required\n" . $hereprev); |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 3746 | } |
| 3747 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3748 | |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 3749 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 3750 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 3751 | my $attr = $1; |
| 3752 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 3753 | my $ptr = $1; |
| 3754 | my $var = $2; |
| 3755 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 3756 | ERROR("MISPLACED_INIT", |
| 3757 | "$attr should be placed after $var\n" . $herecurr)) || |
| 3758 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 3759 | WARN("MISPLACED_INIT", |
| 3760 | "$attr should be placed after $var\n" . $herecurr))) && |
| 3761 | $fix) { |
| 3762 | $fixed[$linenr - 1] =~ 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; |
| 3763 | } |
| 3764 | } |
| 3765 | } |
| 3766 | |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 3767 | # check for $InitAttributeData (ie: __initdata) with const |
| 3768 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 3769 | my $attr = $1; |
| 3770 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 3771 | my $attr_prefix = $1; |
| 3772 | my $attr_type = $2; |
| 3773 | if (ERROR("INIT_ATTRIBUTE", |
| 3774 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 3775 | $fix) { |
| 3776 | $fixed[$linenr - 1] =~ |
| 3777 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | # check for $InitAttributeConst (ie: __initconst) without const |
| 3782 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 3783 | my $attr = $1; |
| 3784 | if (ERROR("INIT_ATTRIBUTE", |
| 3785 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 3786 | $fix) { |
| 3787 | my $lead = $fixed[$linenr - 1] =~ |
| 3788 | /(^\+\s*(?:static\s+))/; |
| 3789 | $lead = rtrim($1); |
| 3790 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 3791 | $lead = "${lead}const "; |
| 3792 | $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/; |
| 3793 | } |
| 3794 | } |
| 3795 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3796 | # prefer usleep_range over udelay |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3797 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3798 | # ignore udelay's < 10, however |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3799 | if (! ($1 < 10) ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3800 | CHK("USLEEP_RANGE", |
| 3801 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3802 | } |
| 3803 | } |
| 3804 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3805 | # warn about unexpectedly long msleep's |
| 3806 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 3807 | if ($1 < 20) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3808 | WARN("MSLEEP", |
| 3809 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | |
Joe Perches | 36ec193 | 2013-07-03 15:05:25 -0700 | [diff] [blame] | 3813 | # check for comparisons of jiffies |
| 3814 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 3815 | WARN("JIFFIES_COMPARISON", |
| 3816 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 3817 | } |
| 3818 | |
Joe Perches | 9d7a34a | 2013-07-03 15:05:26 -0700 | [diff] [blame] | 3819 | # check for comparisons of get_jiffies_64() |
| 3820 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 3821 | WARN("JIFFIES_COMPARISON", |
| 3822 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 3823 | } |
| 3824 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3825 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3826 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3827 | # print "#ifdef in C files should be avoided\n"; |
| 3828 | # print "$herecurr"; |
| 3829 | # $clean = 0; |
| 3830 | # } |
| 3831 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3832 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3833 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3834 | if (ERROR("SPACING", |
| 3835 | "exactly one space required after that #$1\n" . $herecurr) && |
| 3836 | $fix) { |
| 3837 | $fixed[$linenr - 1] =~ |
| 3838 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 3839 | } |
| 3840 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3841 | } |
| 3842 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3843 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3844 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 3845 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3846 | my $which = $1; |
| 3847 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3848 | CHK("UNCOMMENTED_DEFINITION", |
| 3849 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3850 | } |
| 3851 | } |
| 3852 | # check for memory barriers without a comment. |
| 3853 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 3854 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | c1fd7bb | 2013-11-12 15:10:11 -0800 | [diff] [blame] | 3855 | WARN("MEMORY_BARRIER", |
| 3856 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3857 | } |
| 3858 | } |
| 3859 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3860 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3861 | CHK("ARCH_DEFINES", |
| 3862 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3863 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3864 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3865 | # Check that the storage class is at the beginning of a declaration |
| 3866 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3867 | WARN("STORAGE_CLASS", |
| 3868 | "storage class should be at the beginning of the declaration\n" . $herecurr) |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3869 | } |
| 3870 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3871 | # check the location of the inline attribute, that it is between |
| 3872 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3873 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 3874 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3875 | ERROR("INLINE_LOCATION", |
| 3876 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3879 | # Check for __inline__ and __inline, prefer inline |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 3880 | if ($realfile !~ m@\binclude/uapi/@ && |
| 3881 | $line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3882 | if (WARN("INLINE", |
| 3883 | "plain inline is preferred over $1\n" . $herecurr) && |
| 3884 | $fix) { |
| 3885 | $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/; |
| 3886 | |
| 3887 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3888 | } |
| 3889 | |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3890 | # Check for __attribute__ packed, prefer __packed |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 3891 | if ($realfile !~ m@\binclude/uapi/@ && |
| 3892 | $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3893 | WARN("PREFER_PACKED", |
| 3894 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3895 | } |
| 3896 | |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3897 | # Check for __attribute__ aligned, prefer __aligned |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 3898 | if ($realfile !~ m@\binclude/uapi/@ && |
| 3899 | $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3900 | WARN("PREFER_ALIGNED", |
| 3901 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3902 | } |
| 3903 | |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 3904 | # Check for __attribute__ format(printf, prefer __printf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 3905 | if ($realfile !~ m@\binclude/uapi/@ && |
| 3906 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3907 | if (WARN("PREFER_PRINTF", |
| 3908 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && |
| 3909 | $fix) { |
| 3910 | $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; |
| 3911 | |
| 3912 | } |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 3913 | } |
| 3914 | |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3915 | # Check for __attribute__ format(scanf, prefer __scanf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 3916 | if ($realfile !~ m@\binclude/uapi/@ && |
| 3917 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3918 | if (WARN("PREFER_SCANF", |
| 3919 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && |
| 3920 | $fix) { |
| 3921 | $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; |
| 3922 | } |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3923 | } |
| 3924 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3925 | # check for sizeof(&) |
| 3926 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3927 | WARN("SIZEOF_ADDRESS", |
| 3928 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3929 | } |
| 3930 | |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 3931 | # check for sizeof without parenthesis |
| 3932 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3933 | if (WARN("SIZEOF_PARENTHESIS", |
| 3934 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 3935 | $fix) { |
| 3936 | $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; |
| 3937 | } |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 3938 | } |
| 3939 | |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3940 | # check for line continuations in quoted strings with odd counts of " |
| 3941 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3942 | WARN("LINE_CONTINUATIONS", |
| 3943 | "Avoid line continuations in quoted strings\n" . $herecurr); |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3944 | } |
| 3945 | |
Joe Perches | 88982fe | 2012-12-17 16:02:00 -0800 | [diff] [blame] | 3946 | # check for struct spinlock declarations |
| 3947 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 3948 | WARN("USE_SPINLOCK_T", |
| 3949 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 3950 | } |
| 3951 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 3952 | # check for seq_printf uses that could be seq_puts |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 3953 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 3954 | my $fmt = get_quoted_string($line, $rawline); |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 3955 | if ($fmt ne "" && $fmt !~ /[^\\]\%/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3956 | if (WARN("PREFER_SEQ_PUTS", |
| 3957 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 3958 | $fix) { |
| 3959 | $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/; |
| 3960 | } |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 3961 | } |
| 3962 | } |
| 3963 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3964 | # Check for misused memsets |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3965 | if ($^V && $^V ge 5.10.0 && |
| 3966 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3967 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3968 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3969 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3970 | my $ms_val = $7; |
| 3971 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3972 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3973 | if ($ms_size =~ /^(0x|)0$/i) { |
| 3974 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3975 | "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3976 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 3977 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3978 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | # typecasts on min/max could be min_t/max_t |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3983 | if ($^V && $^V ge 5.10.0 && |
| 3984 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3985 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3986 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3987 | my $call = $1; |
| 3988 | my $cast1 = deparenthesize($2); |
| 3989 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3990 | my $cast2 = deparenthesize($7); |
| 3991 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3992 | my $cast; |
| 3993 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3994 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3995 | $cast = "$cast1 or $cast2"; |
| 3996 | } elsif ($cast1 ne "") { |
| 3997 | $cast = $cast1; |
| 3998 | } else { |
| 3999 | $cast = $cast2; |
| 4000 | } |
| 4001 | WARN("MINMAX", |
| 4002 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 4006 | # check usleep_range arguments |
| 4007 | if ($^V && $^V ge 5.10.0 && |
| 4008 | defined $stat && |
| 4009 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 4010 | my $min = $1; |
| 4011 | my $max = $7; |
| 4012 | if ($min eq $max) { |
| 4013 | WARN("USLEEP_RANGE", |
| 4014 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 4015 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 4016 | $min > $max) { |
| 4017 | WARN("USLEEP_RANGE", |
| 4018 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 4019 | } |
| 4020 | } |
| 4021 | |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 4022 | # check for naked sscanf |
| 4023 | if ($^V && $^V ge 5.10.0 && |
| 4024 | defined $stat && |
| 4025 | $stat =~ /\bsscanf\b/ && |
| 4026 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 4027 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 4028 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 4029 | my $lc = $stat =~ tr@\n@@; |
| 4030 | $lc = $lc + $linenr; |
| 4031 | my $stat_real = raw_line($linenr, 0); |
| 4032 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 4033 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 4034 | } |
| 4035 | WARN("NAKED_SSCANF", |
| 4036 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 4037 | } |
| 4038 | |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 4039 | # check for new externs in .h files. |
| 4040 | if ($realfile =~ /\.h$/ && |
| 4041 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
Joe Perches | d1d8578 | 2013-09-24 15:27:46 -0700 | [diff] [blame] | 4042 | if (CHK("AVOID_EXTERNS", |
| 4043 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 4044 | $fix) { |
| 4045 | $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
| 4046 | } |
| 4047 | } |
| 4048 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4049 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4050 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4051 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4052 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4053 | my $function_name = $1; |
| 4054 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4055 | |
| 4056 | my $s = $stat; |
| 4057 | if (defined $cond) { |
| 4058 | substr($s, 0, length($cond), ''); |
| 4059 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4060 | if ($s =~ /^\s*;/ && |
| 4061 | $function_name ne 'uninitialized_var') |
| 4062 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4063 | WARN("AVOID_EXTERNS", |
| 4064 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4068 | WARN("FUNCTION_ARGUMENTS", |
| 4069 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4070 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4071 | |
| 4072 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 4073 | $stat =~ /^.\s*extern\s+/) |
| 4074 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4075 | WARN("AVOID_EXTERNS", |
| 4076 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | # checks for new __setup's |
| 4080 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 4081 | my $name = $1; |
| 4082 | |
| 4083 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4084 | CHK("UNDOCUMENTED_SETUP", |
| 4085 | "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4086 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4087 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4088 | |
| 4089 | # check for pointless casting of kmalloc return |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4090 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4091 | WARN("UNNECESSARY_CASTS", |
| 4092 | "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4093 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4094 | |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 4095 | # alloc style |
| 4096 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
| 4097 | if ($^V && $^V ge 5.10.0 && |
| 4098 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
| 4099 | CHK("ALLOC_SIZEOF_STRUCT", |
| 4100 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 4101 | } |
| 4102 | |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 4103 | # check for krealloc arg reuse |
| 4104 | if ($^V && $^V ge 5.10.0 && |
| 4105 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { |
| 4106 | WARN("KREALLOC_ARG_REUSE", |
| 4107 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 4108 | } |
| 4109 | |
Joe Perches | 5ce59ae | 2013-02-21 16:44:18 -0800 | [diff] [blame] | 4110 | # check for alloc argument mismatch |
| 4111 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 4112 | WARN("ALLOC_ARRAY_ARGS", |
| 4113 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 4114 | } |
| 4115 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4116 | # check for multiple semicolons |
| 4117 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4118 | if (WARN("ONE_SEMICOLON", |
| 4119 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 4120 | $fix) { |
| 4121 | $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g; |
| 4122 | } |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | # check for switch/default statements without a break; |
| 4126 | if ($^V && $^V ge 5.10.0 && |
| 4127 | defined $stat && |
| 4128 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
| 4129 | my $ctx = ''; |
| 4130 | my $herectx = $here . "\n"; |
| 4131 | my $cnt = statement_rawlines($stat); |
| 4132 | for (my $n = 0; $n < $cnt; $n++) { |
| 4133 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4134 | } |
| 4135 | WARN("DEFAULT_NO_BREAK", |
| 4136 | "switch default: should use break\n" . $herectx); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4137 | } |
| 4138 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4139 | # check for gcc specific __FUNCTION__ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4140 | if ($line =~ /\b__FUNCTION__\b/) { |
| 4141 | if (WARN("USE_FUNC", |
| 4142 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 4143 | $fix) { |
| 4144 | $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g; |
| 4145 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4146 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4147 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4148 | # check for use of yield() |
| 4149 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 4150 | WARN("YIELD", |
| 4151 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 4152 | } |
| 4153 | |
Joe Perches | 179f8f4 | 2013-07-03 15:05:30 -0700 | [diff] [blame] | 4154 | # check for comparisons against true and false |
| 4155 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 4156 | my $lead = $1; |
| 4157 | my $arg = $2; |
| 4158 | my $test = $3; |
| 4159 | my $otype = $4; |
| 4160 | my $trail = $5; |
| 4161 | my $op = "!"; |
| 4162 | |
| 4163 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 4164 | |
| 4165 | my $type = lc($otype); |
| 4166 | if ($type =~ /^(?:true|false)$/) { |
| 4167 | if (("$test" eq "==" && "$type" eq "true") || |
| 4168 | ("$test" eq "!=" && "$type" eq "false")) { |
| 4169 | $op = ""; |
| 4170 | } |
| 4171 | |
| 4172 | CHK("BOOL_COMPARISON", |
| 4173 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 4174 | |
| 4175 | ## maybe suggesting a correct construct would better |
| 4176 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 4177 | |
| 4178 | } |
| 4179 | } |
| 4180 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 4181 | # check for semaphores initialized locked |
| 4182 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4183 | WARN("CONSIDER_COMPLETION", |
| 4184 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4185 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4186 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 4187 | # recommend kstrto* over simple_strto* and strict_strto* |
| 4188 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4189 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 4190 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4191 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4192 | |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 4193 | # check for __initcall(), use device_initcall() explicitly please |
| 4194 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4195 | WARN("USE_DEVICE_INITCALL", |
| 4196 | "please use device_initcall() instead of __initcall()\n" . $herecurr); |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 4197 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4198 | |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 4199 | # check for various ops structs, ensure they are const. |
| 4200 | my $struct_ops = qr{acpi_dock_ops| |
| 4201 | address_space_operations| |
| 4202 | backlight_ops| |
| 4203 | block_device_operations| |
| 4204 | dentry_operations| |
| 4205 | dev_pm_ops| |
| 4206 | dma_map_ops| |
| 4207 | extent_io_ops| |
| 4208 | file_lock_operations| |
| 4209 | file_operations| |
| 4210 | hv_ops| |
| 4211 | ide_dma_ops| |
| 4212 | intel_dvo_dev_ops| |
| 4213 | item_operations| |
| 4214 | iwl_ops| |
| 4215 | kgdb_arch| |
| 4216 | kgdb_io| |
| 4217 | kset_uevent_ops| |
| 4218 | lock_manager_operations| |
| 4219 | microcode_ops| |
| 4220 | mtrr_ops| |
| 4221 | neigh_ops| |
| 4222 | nlmsvc_binding| |
| 4223 | pci_raw_ops| |
| 4224 | pipe_buf_operations| |
| 4225 | platform_hibernation_ops| |
| 4226 | platform_suspend_ops| |
| 4227 | proto_ops| |
| 4228 | rpc_pipe_ops| |
| 4229 | seq_operations| |
| 4230 | snd_ac97_build_ops| |
| 4231 | soc_pcmcia_socket_ops| |
| 4232 | stacktrace_ops| |
| 4233 | sysfs_ops| |
| 4234 | tty_operations| |
| 4235 | usb_mon_operations| |
| 4236 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 4237 | if ($line !~ /\bconst\b/ && |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 4238 | $line =~ /\bstruct\s+($struct_ops)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4239 | WARN("CONST_STRUCT", |
| 4240 | "struct $1 should normally be const\n" . |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 4241 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 4242 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4243 | |
| 4244 | # use of NR_CPUS is usually wrong |
| 4245 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 4246 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4247 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 4248 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4249 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 4250 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 4251 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4252 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4253 | WARN("NR_CPUS", |
| 4254 | "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4255 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4256 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 4257 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 4258 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 4259 | ERROR("DEFINE_ARCH_HAS", |
| 4260 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 4261 | } |
| 4262 | |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4263 | # check for %L{u,d,i} in strings |
| 4264 | my $string; |
| 4265 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 4266 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Andy Whitcroft | 2a1bc5d | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 4267 | $string =~ s/%%/__/g; |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4268 | if ($string =~ /(?<!%)%L[udi]/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4269 | WARN("PRINTF_L", |
| 4270 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4271 | last; |
| 4272 | } |
| 4273 | } |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 4274 | |
| 4275 | # whine mightly about in_atomic |
| 4276 | if ($line =~ /\bin_atomic\s*\(/) { |
| 4277 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4278 | ERROR("IN_ATOMIC", |
| 4279 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 4280 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4281 | WARN("IN_ATOMIC", |
| 4282 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 4283 | } |
| 4284 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4285 | |
| 4286 | # check for lockdep_set_novalidate_class |
| 4287 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 4288 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 4289 | if ($realfile !~ m@^kernel/lockdep@ && |
| 4290 | $realfile !~ m@^include/linux/lockdep@ && |
| 4291 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4292 | ERROR("LOCKDEP", |
| 4293 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4294 | } |
| 4295 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 4296 | |
| 4297 | if ($line =~ /debugfs_create_file.*S_IWUGO/ || |
| 4298 | $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4299 | WARN("EXPORTED_WORLD_WRITABLE", |
| 4300 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 4301 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | # If we have no input at all, then there is nothing to report on |
| 4305 | # so just keep quiet. |
| 4306 | if ($#rawlines == -1) { |
| 4307 | exit(0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4308 | } |
| 4309 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4310 | # In mailback mode only produce a report in the negative, for |
| 4311 | # things that appear to be patches. |
| 4312 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 4313 | exit(0); |
| 4314 | } |
| 4315 | |
| 4316 | # This is not a patch, and we are are in 'no-patch' mode so |
| 4317 | # just keep quiet. |
| 4318 | if (!$chk_patch && !$is_patch) { |
| 4319 | exit(0); |
| 4320 | } |
| 4321 | |
| 4322 | if (!$is_patch) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4323 | ERROR("NOT_UNIFIED_DIFF", |
| 4324 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4325 | } |
| 4326 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4327 | ERROR("MISSING_SIGN_OFF", |
| 4328 | "Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4329 | } |
| 4330 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4331 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4332 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 4333 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4334 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 4335 | (($check)? "$cnt_chk checks, " : "") . |
| 4336 | "$cnt_lines lines checked\n"; |
| 4337 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4338 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4339 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4340 | if ($quiet == 0) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4341 | |
| 4342 | if ($^V lt 5.10.0) { |
| 4343 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); |
| 4344 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); |
| 4345 | } |
| 4346 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4347 | # If there were whitespace errors which cleanpatch can fix |
| 4348 | # then suggest that. |
| 4349 | if ($rpt_cleaners) { |
| 4350 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; |
| 4351 | print " scripts/cleanfile\n\n"; |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 4352 | $rpt_cleaners = 0; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4353 | } |
| 4354 | } |
| 4355 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 4356 | hash_show_words(\%use_type, "Used"); |
| 4357 | hash_show_words(\%ignore_type, "Ignored"); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4358 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4359 | if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { |
| 4360 | my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; |
| 4361 | my $linecount = 0; |
| 4362 | my $f; |
| 4363 | |
| 4364 | open($f, '>', $newfile) |
| 4365 | or die "$P: Can't open $newfile for write\n"; |
| 4366 | foreach my $fixed_line (@fixed) { |
| 4367 | $linecount++; |
| 4368 | if ($file) { |
| 4369 | if ($linecount > 3) { |
| 4370 | $fixed_line =~ s/^\+//; |
| 4371 | print $f $fixed_line. "\n"; |
| 4372 | } |
| 4373 | } else { |
| 4374 | print $f $fixed_line . "\n"; |
| 4375 | } |
| 4376 | } |
| 4377 | close($f); |
| 4378 | |
| 4379 | if (!$quiet) { |
| 4380 | print << "EOM"; |
| 4381 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 4382 | |
| 4383 | Do _NOT_ trust the results written to this file. |
| 4384 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 4385 | |
| 4386 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 4387 | No warranties, expressed or implied... |
| 4388 | |
| 4389 | EOM |
| 4390 | } |
| 4391 | } |
| 4392 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4393 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4394 | print "$vname has no obvious style problems and is ready for submission.\n" |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4395 | } |
| 4396 | if ($clean == 0 && $quiet == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4397 | print << "EOM"; |
| 4398 | $vname has style problems, please review. |
| 4399 | |
| 4400 | If any of these errors are false positives, please report |
| 4401 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 4402 | EOM |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4403 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4404 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4405 | return $clean; |
| 4406 | } |