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; |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 244 | |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 245 | our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)}; |
| 246 | |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 247 | # Notes to $Attribute: |
| 248 | # 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] | 249 | our $Attribute = qr{ |
| 250 | const| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 251 | __percpu| |
| 252 | __nocast| |
| 253 | __safe| |
| 254 | __bitwise__| |
| 255 | __packed__| |
| 256 | __packed2__| |
| 257 | __naked| |
| 258 | __maybe_unused| |
| 259 | __always_unused| |
| 260 | __noreturn| |
| 261 | __used| |
| 262 | __cold| |
| 263 | __noclone| |
| 264 | __deprecated| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 265 | __read_mostly| |
| 266 | __kprobes| |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 267 | $InitAttribute| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 268 | ____cacheline_aligned| |
| 269 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 270 | ____cacheline_internodealigned_in_smp| |
| 271 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 272 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 273 | our $Modifier; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 274 | our $Inline = qr{inline|__always_inline|noinline}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 275 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 276 | our $Lval = qr{$Ident(?:$Member)*}; |
| 277 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 278 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 279 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 280 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 281 | our $Int = qr{[0-9]+$Int_type?}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 282 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 283 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 284 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
Joe Perches | 74349bc | 2012-12-17 16:02:05 -0800 | [diff] [blame] | 285 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 286 | our $Constant = qr{$Float|$Binary|$Hex|$Int}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 287 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 288 | our $Compare = qr{<=|>=|==|!=|<|>}; |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 289 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 290 | our $Operators = qr{ |
| 291 | <=|>=|==|!=| |
| 292 | =>|->|<<|>>|<|>|!|~| |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 293 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 294 | }x; |
| 295 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 296 | our $NonptrType; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 297 | our $NonptrTypeWithAttr; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 298 | our $Type; |
| 299 | our $Declare; |
| 300 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 301 | our $NON_ASCII_UTF8 = qr{ |
| 302 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 303 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 304 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 305 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 306 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 307 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 308 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 309 | }x; |
| 310 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 311 | our $UTF8 = qr{ |
| 312 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 313 | | $NON_ASCII_UTF8 |
| 314 | }x; |
| 315 | |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 316 | our $typeTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 317 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 318 | atomic_t |
| 319 | )}; |
| 320 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 321 | our $logFunctions = qr{(?x: |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 322 | printk(?:_ratelimited|_once|)| |
Jacob Keller | 7d0b659 | 2013-07-03 15:05:35 -0700 | [diff] [blame] | 323 | (?:[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] | 324 | WARN(?:_RATELIMIT|_ONCE|)| |
Joe Perches | b053172 | 2011-05-24 17:13:40 -0700 | [diff] [blame] | 325 | panic| |
| 326 | MODULE_[A-Z_]+ |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 327 | )}; |
| 328 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 329 | our $signature_tags = qr{(?xi: |
| 330 | Signed-off-by:| |
| 331 | Acked-by:| |
| 332 | Tested-by:| |
| 333 | Reviewed-by:| |
| 334 | Reported-by:| |
Mugunthan V N | 8543ae1 | 2013-04-29 16:18:17 -0700 | [diff] [blame] | 335 | Suggested-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 336 | To:| |
| 337 | Cc: |
| 338 | )}; |
| 339 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 340 | our @typeList = ( |
| 341 | qr{void}, |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 342 | qr{(?:unsigned\s+)?char}, |
| 343 | qr{(?:unsigned\s+)?short}, |
| 344 | qr{(?:unsigned\s+)?int}, |
| 345 | qr{(?:unsigned\s+)?long}, |
| 346 | qr{(?:unsigned\s+)?long\s+int}, |
| 347 | qr{(?:unsigned\s+)?long\s+long}, |
| 348 | qr{(?:unsigned\s+)?long\s+long\s+int}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 349 | qr{unsigned}, |
| 350 | qr{float}, |
| 351 | qr{double}, |
| 352 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 353 | qr{struct\s+$Ident}, |
| 354 | qr{union\s+$Ident}, |
| 355 | qr{enum\s+$Ident}, |
| 356 | qr{${Ident}_t}, |
| 357 | qr{${Ident}_handler}, |
| 358 | qr{${Ident}_handler_fn}, |
| 359 | ); |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 360 | our @typeListWithAttr = ( |
| 361 | @typeList, |
| 362 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 363 | qr{union\s+$InitAttribute\s+$Ident}, |
| 364 | ); |
| 365 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 366 | our @modifierList = ( |
| 367 | qr{fastcall}, |
| 368 | ); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 369 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 370 | our $allowed_asm_includes = qr{(?x: |
| 371 | irq| |
| 372 | memory |
| 373 | )}; |
| 374 | # memory.h: ARM has a custom one |
| 375 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 376 | sub build_types { |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 377 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
| 378 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 379 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 380 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 381 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 382 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 383 | (?: |
Andy Whitcroft | 6b48db2 | 2012-01-10 15:10:13 -0800 | [diff] [blame] | 384 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 385 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 386 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 387 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 388 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 389 | }x; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 390 | $NonptrTypeWithAttr = qr{ |
| 391 | (?:$Modifier\s+|const\s+)* |
| 392 | (?: |
| 393 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 394 | (?:$typeTypedefs\b)| |
| 395 | (?:${allWithAttr}\b) |
| 396 | ) |
| 397 | (?:\s+$Modifier|\s+const)* |
| 398 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 399 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 400 | $NonptrType |
Andy Whitcroft | b337d8b | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 401 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 402 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 403 | }x; |
| 404 | $Declare = qr{(?:$Storage\s+)?$Type}; |
| 405 | } |
| 406 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 407 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 408 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 409 | |
| 410 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 411 | # requires at least perl version v5.10.0 |
| 412 | # Any use must be runtime checked with $^V |
| 413 | |
| 414 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
| 415 | our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 416 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 417 | |
| 418 | sub deparenthesize { |
| 419 | my ($string) = @_; |
| 420 | return "" if (!defined($string)); |
| 421 | $string =~ s@^\s*\(\s*@@g; |
| 422 | $string =~ s@\s*\)\s*$@@g; |
| 423 | $string =~ s@\s+@ @g; |
| 424 | return $string; |
| 425 | } |
| 426 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 427 | sub seed_camelcase_file { |
| 428 | my ($file) = @_; |
| 429 | |
| 430 | return if (!(-f $file)); |
| 431 | |
| 432 | local $/; |
| 433 | |
| 434 | open(my $include_file, '<', "$file") |
| 435 | or warn "$P: Can't read '$file' $!\n"; |
| 436 | my $text = <$include_file>; |
| 437 | close($include_file); |
| 438 | |
| 439 | my @lines = split('\n', $text); |
| 440 | |
| 441 | foreach my $line (@lines) { |
| 442 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 443 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 444 | $camelcase{$1} = 1; |
| 445 | } |
| 446 | elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) { |
| 447 | $camelcase{$1} = 1; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | my $camelcase_seeded = 0; |
| 453 | sub seed_camelcase_includes { |
| 454 | return if ($camelcase_seeded); |
| 455 | |
| 456 | my $files; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 457 | my $camelcase_cache = ""; |
| 458 | my @include_files = (); |
| 459 | |
| 460 | $camelcase_seeded = 1; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 461 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 462 | if (-d ".git") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 463 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; |
| 464 | chomp $git_last_include_commit; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 465 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 466 | } else { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 467 | my $last_mod_date = 0; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 468 | $files = `find $root/include -name "*.h"`; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 469 | @include_files = split('\n', $files); |
| 470 | foreach my $file (@include_files) { |
| 471 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 472 | localtime((stat $file)[9])); |
| 473 | $last_mod_date = $date if ($last_mod_date < $date); |
| 474 | } |
| 475 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 476 | } |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 477 | |
| 478 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 479 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 480 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 481 | while (<$camelcase_file>) { |
| 482 | chomp; |
| 483 | $camelcase{$_} = 1; |
| 484 | } |
| 485 | close($camelcase_file); |
| 486 | |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (-d ".git") { |
| 491 | $files = `git ls-files "include/*.h"`; |
| 492 | @include_files = split('\n', $files); |
| 493 | } |
| 494 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 495 | foreach my $file (@include_files) { |
| 496 | seed_camelcase_file($file); |
| 497 | } |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 498 | |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 499 | if ($camelcase_cache ne "") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 500 | unlink glob ".checkpatch-camelcase.*"; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 501 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 502 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 503 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 504 | print $camelcase_file ("$_\n"); |
| 505 | } |
| 506 | close($camelcase_file); |
| 507 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 510 | $chk_signoff = 0 if ($file); |
| 511 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 512 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 513 | my @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 514 | my @fixed = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 515 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 516 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 517 | my $FILE; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 518 | if ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 519 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 520 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 521 | } elsif ($filename eq '-') { |
| 522 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 523 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 524 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 525 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 526 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 527 | if ($filename eq '-') { |
| 528 | $vname = 'Your patch'; |
| 529 | } else { |
| 530 | $vname = $filename; |
| 531 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 532 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 533 | chomp; |
| 534 | push(@rawlines, $_); |
| 535 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 536 | close($FILE); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 537 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 538 | $exit = 1; |
| 539 | } |
| 540 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 541 | @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 542 | @fixed = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | exit($exit); |
| 546 | |
| 547 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 548 | my ($root) = @_; |
| 549 | |
| 550 | my @tree_check = ( |
| 551 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 552 | "README", "Documentation", "arch", "include", "drivers", |
| 553 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 554 | ); |
| 555 | |
| 556 | foreach my $check (@tree_check) { |
| 557 | if (! -e $root . '/' . $check) { |
| 558 | return 0; |
| 559 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 560 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 561 | return 1; |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 562 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 563 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 564 | sub parse_email { |
| 565 | my ($formatted_email) = @_; |
| 566 | |
| 567 | my $name = ""; |
| 568 | my $address = ""; |
| 569 | my $comment = ""; |
| 570 | |
| 571 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 572 | $name = $1; |
| 573 | $address = $2; |
| 574 | $comment = $3 if defined $3; |
| 575 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 576 | $address = $1; |
| 577 | $comment = $2 if defined $2; |
| 578 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 579 | $address = $1; |
| 580 | $comment = $2 if defined $2; |
| 581 | $formatted_email =~ s/$address.*$//; |
| 582 | $name = $formatted_email; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 583 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 584 | $name =~ s/^\"|\"$//g; |
| 585 | # If there's a name left after stripping spaces and |
| 586 | # leading quotes, and the address doesn't have both |
| 587 | # leading and trailing angle brackets, the address |
| 588 | # is invalid. ie: |
| 589 | # "joe smith joe@smith.com" bad |
| 590 | # "joe smith <joe@smith.com" bad |
| 591 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 592 | $name = ""; |
| 593 | $address = ""; |
| 594 | $comment = ""; |
| 595 | } |
| 596 | } |
| 597 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 598 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 599 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 600 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 601 | $address =~ s/^\<|\>$//g; |
| 602 | |
| 603 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 604 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 605 | $name = "\"$name\""; |
| 606 | } |
| 607 | |
| 608 | return ($name, $address, $comment); |
| 609 | } |
| 610 | |
| 611 | sub format_email { |
| 612 | my ($name, $address) = @_; |
| 613 | |
| 614 | my $formatted_email; |
| 615 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 616 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 617 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 618 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 619 | |
| 620 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 621 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 622 | $name = "\"$name\""; |
| 623 | } |
| 624 | |
| 625 | if ("$name" eq "") { |
| 626 | $formatted_email = "$address"; |
| 627 | } else { |
| 628 | $formatted_email = "$name <$address>"; |
| 629 | } |
| 630 | |
| 631 | return $formatted_email; |
| 632 | } |
| 633 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 634 | sub which_conf { |
| 635 | my ($conf) = @_; |
| 636 | |
| 637 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 638 | if (-e "$path/$conf") { |
| 639 | return "$path/$conf"; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | return ""; |
| 644 | } |
| 645 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 646 | sub expand_tabs { |
| 647 | my ($str) = @_; |
| 648 | |
| 649 | my $res = ''; |
| 650 | my $n = 0; |
| 651 | for my $c (split(//, $str)) { |
| 652 | if ($c eq "\t") { |
| 653 | $res .= ' '; |
| 654 | $n++; |
| 655 | for (; ($n % 8) != 0; $n++) { |
| 656 | $res .= ' '; |
| 657 | } |
| 658 | next; |
| 659 | } |
| 660 | $res .= $c; |
| 661 | $n++; |
| 662 | } |
| 663 | |
| 664 | return $res; |
| 665 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 666 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 667 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 668 | return $res; |
| 669 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 670 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 671 | sub line_stats { |
| 672 | my ($line) = @_; |
| 673 | |
| 674 | # Drop the diff line leader and expand tabs |
| 675 | $line =~ s/^.//; |
| 676 | $line = expand_tabs($line); |
| 677 | |
| 678 | # Pick the indent from the front of the line. |
| 679 | my ($white) = ($line =~ /^(\s*)/); |
| 680 | |
| 681 | return (length($line), length($white)); |
| 682 | } |
| 683 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 684 | my $sanitise_quote = ''; |
| 685 | |
| 686 | sub sanitise_line_reset { |
| 687 | my ($in_comment) = @_; |
| 688 | |
| 689 | if ($in_comment) { |
| 690 | $sanitise_quote = '*/'; |
| 691 | } else { |
| 692 | $sanitise_quote = ''; |
| 693 | } |
| 694 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 695 | sub sanitise_line { |
| 696 | my ($line) = @_; |
| 697 | |
| 698 | my $res = ''; |
| 699 | my $l = ''; |
| 700 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 701 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 702 | my $off = 0; |
| 703 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 704 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 705 | # Always copy over the diff marker. |
| 706 | $res = substr($line, 0, 1); |
| 707 | |
| 708 | for ($off = 1; $off < length($line); $off++) { |
| 709 | $c = substr($line, $off, 1); |
| 710 | |
| 711 | # Comments we are wacking completly including the begin |
| 712 | # and end, all to $;. |
| 713 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 714 | $sanitise_quote = '*/'; |
| 715 | |
| 716 | substr($res, $off, 2, "$;$;"); |
| 717 | $off++; |
| 718 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 719 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 720 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 721 | $sanitise_quote = ''; |
| 722 | substr($res, $off, 2, "$;$;"); |
| 723 | $off++; |
| 724 | next; |
| 725 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 726 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 727 | $sanitise_quote = '//'; |
| 728 | |
| 729 | substr($res, $off, 2, $sanitise_quote); |
| 730 | $off++; |
| 731 | next; |
| 732 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 733 | |
| 734 | # A \ in a string means ignore the next character. |
| 735 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 736 | $c eq "\\") { |
| 737 | substr($res, $off, 2, 'XX'); |
| 738 | $off++; |
| 739 | next; |
| 740 | } |
| 741 | # Regular quotes. |
| 742 | if ($c eq "'" || $c eq '"') { |
| 743 | if ($sanitise_quote eq '') { |
| 744 | $sanitise_quote = $c; |
| 745 | |
| 746 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 747 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 748 | } elsif ($sanitise_quote eq $c) { |
| 749 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 752 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 753 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 754 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 755 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 756 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 757 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 758 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 759 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 760 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 761 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 762 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 763 | } |
| 764 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 765 | if ($sanitise_quote eq '//') { |
| 766 | $sanitise_quote = ''; |
| 767 | } |
| 768 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 769 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 770 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 771 | my $clean = 'X' x length($1); |
| 772 | $res =~ s@\<.*\>@<$clean>@; |
| 773 | |
| 774 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 775 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 776 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 777 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 780 | return $res; |
| 781 | } |
| 782 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 783 | sub get_quoted_string { |
| 784 | my ($line, $rawline) = @_; |
| 785 | |
| 786 | return "" if ($line !~ m/(\"[X]+\")/g); |
| 787 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 788 | } |
| 789 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 790 | sub ctx_statement_block { |
| 791 | my ($linenr, $remain, $off) = @_; |
| 792 | my $line = $linenr - 1; |
| 793 | my $blk = ''; |
| 794 | my $soff = $off; |
| 795 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 796 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 797 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 798 | my $loff = 0; |
| 799 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 800 | my $type = ''; |
| 801 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 802 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 803 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 804 | my $c; |
| 805 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 806 | |
| 807 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 808 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 809 | @stack = (['', 0]) if ($#stack == -1); |
| 810 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 811 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 812 | # If we are about to drop off the end, pull in more |
| 813 | # context. |
| 814 | if ($off >= $len) { |
| 815 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 816 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 817 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 818 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 819 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 820 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 821 | $len = length($blk); |
| 822 | $line++; |
| 823 | last; |
| 824 | } |
| 825 | # Bail if there is no further context. |
| 826 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 827 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 828 | last; |
| 829 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 830 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 831 | $level++; |
| 832 | $type = '#'; |
| 833 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 834 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 835 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 836 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 837 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 838 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 839 | #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] | 840 | |
| 841 | # Handle nested #if/#else. |
| 842 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 843 | push(@stack, [ $type, $level ]); |
| 844 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 845 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 846 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 847 | ($type, $level) = @{pop(@stack)}; |
| 848 | } |
| 849 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 850 | # Statement ends at the ';' or a close '}' at the |
| 851 | # outermost level. |
| 852 | if ($level == 0 && $c eq ';') { |
| 853 | last; |
| 854 | } |
| 855 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 856 | # 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] | 857 | if ($level == 0 && $coff_set == 0 && |
| 858 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 859 | $remainder =~ /^(else)(?:\s|{)/ && |
| 860 | $remainder !~ /^else\s+if\b/) { |
| 861 | $coff = $off + length($1) - 1; |
| 862 | $coff_set = 1; |
| 863 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 864 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 867 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 868 | $level++; |
| 869 | $type = '('; |
| 870 | } |
| 871 | if ($type eq '(' && $c eq ')') { |
| 872 | $level--; |
| 873 | $type = ($level != 0)? '(' : ''; |
| 874 | |
| 875 | if ($level == 0 && $coff < $soff) { |
| 876 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 877 | $coff_set = 1; |
| 878 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 882 | $level++; |
| 883 | $type = '{'; |
| 884 | } |
| 885 | if ($type eq '{' && $c eq '}') { |
| 886 | $level--; |
| 887 | $type = ($level != 0)? '{' : ''; |
| 888 | |
| 889 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 890 | if (substr($blk, $off + 1, 1) eq ';') { |
| 891 | $off++; |
| 892 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 893 | last; |
| 894 | } |
| 895 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 896 | # Preprocessor commands end at the newline unless escaped. |
| 897 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 898 | $level--; |
| 899 | $type = ''; |
| 900 | $off++; |
| 901 | last; |
| 902 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 903 | $off++; |
| 904 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 905 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 906 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 907 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 908 | $line++; |
| 909 | $remain--; |
| 910 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 911 | |
| 912 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 913 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 914 | |
| 915 | #warn "STATEMENT<$statement>\n"; |
| 916 | #warn "CONDITION<$condition>\n"; |
| 917 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 918 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 919 | |
| 920 | return ($statement, $condition, |
| 921 | $line, $remain + 1, $off - $loff + 1, $level); |
| 922 | } |
| 923 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 924 | sub statement_lines { |
| 925 | my ($stmt) = @_; |
| 926 | |
| 927 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 928 | $stmt =~ s/(^|\n)./$1/g; |
| 929 | $stmt =~ s/^\s*//; |
| 930 | $stmt =~ s/\s*$//; |
| 931 | |
| 932 | my @stmt_lines = ($stmt =~ /\n/g); |
| 933 | |
| 934 | return $#stmt_lines + 2; |
| 935 | } |
| 936 | |
| 937 | sub statement_rawlines { |
| 938 | my ($stmt) = @_; |
| 939 | |
| 940 | my @stmt_lines = ($stmt =~ /\n/g); |
| 941 | |
| 942 | return $#stmt_lines + 2; |
| 943 | } |
| 944 | |
| 945 | sub statement_block_size { |
| 946 | my ($stmt) = @_; |
| 947 | |
| 948 | $stmt =~ s/(^|\n)./$1/g; |
| 949 | $stmt =~ s/^\s*{//; |
| 950 | $stmt =~ s/}\s*$//; |
| 951 | $stmt =~ s/^\s*//; |
| 952 | $stmt =~ s/\s*$//; |
| 953 | |
| 954 | my @stmt_lines = ($stmt =~ /\n/g); |
| 955 | my @stmt_statements = ($stmt =~ /;/g); |
| 956 | |
| 957 | my $stmt_lines = $#stmt_lines + 2; |
| 958 | my $stmt_statements = $#stmt_statements + 1; |
| 959 | |
| 960 | if ($stmt_lines > $stmt_statements) { |
| 961 | return $stmt_lines; |
| 962 | } else { |
| 963 | return $stmt_statements; |
| 964 | } |
| 965 | } |
| 966 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 967 | sub ctx_statement_full { |
| 968 | my ($linenr, $remain, $off) = @_; |
| 969 | my ($statement, $condition, $level); |
| 970 | |
| 971 | my (@chunks); |
| 972 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 973 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 974 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 975 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 976 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 977 | push(@chunks, [ $condition, $statement ]); |
| 978 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 979 | return ($level, $linenr, @chunks); |
| 980 | } |
| 981 | |
| 982 | # Pull in the following conditional/block pairs and see if they |
| 983 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 984 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 985 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 986 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 987 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 988 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 989 | #print "C: push\n"; |
| 990 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 994 | } |
| 995 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 996 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 997 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 998 | my $line; |
| 999 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1000 | my $blk = ''; |
| 1001 | my @o; |
| 1002 | my @c; |
| 1003 | my @res = (); |
| 1004 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1005 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1006 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1007 | for ($line = $start; $remain > 0; $line++) { |
| 1008 | next if ($rawlines[$line] =~ /^-/); |
| 1009 | $remain--; |
| 1010 | |
| 1011 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1012 | |
| 1013 | # Handle nested #if/#else. |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1014 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1015 | push(@stack, $level); |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1016 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1017 | $level = $stack[$#stack - 1]; |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1018 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1019 | $level = pop(@stack); |
| 1020 | } |
| 1021 | |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1022 | foreach my $c (split(//, $lines[$line])) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1023 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1024 | if ($off > 0) { |
| 1025 | $off--; |
| 1026 | next; |
| 1027 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1028 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1029 | if ($c eq $close && $level > 0) { |
| 1030 | $level--; |
| 1031 | last if ($level == 0); |
| 1032 | } elsif ($c eq $open) { |
| 1033 | $level++; |
| 1034 | } |
| 1035 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1036 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1037 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1038 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1041 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1044 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1045 | } |
| 1046 | sub ctx_block_outer { |
| 1047 | my ($linenr, $remain) = @_; |
| 1048 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1049 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1050 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1051 | } |
| 1052 | sub ctx_block { |
| 1053 | my ($linenr, $remain) = @_; |
| 1054 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1055 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1056 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1057 | } |
| 1058 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1059 | my ($linenr, $remain, $off) = @_; |
| 1060 | |
| 1061 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1062 | return @r; |
| 1063 | } |
| 1064 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1065 | my ($linenr, $remain) = @_; |
| 1066 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1067 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1068 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1069 | sub ctx_statement_level { |
| 1070 | my ($linenr, $remain, $off) = @_; |
| 1071 | |
| 1072 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1073 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1074 | |
| 1075 | sub ctx_locate_comment { |
| 1076 | my ($first_line, $end_line) = @_; |
| 1077 | |
| 1078 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 1079 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1080 | return $current_comment if (defined $current_comment); |
| 1081 | |
| 1082 | # Look through the context and try and figure out if there is a |
| 1083 | # comment. |
| 1084 | my $in_comment = 0; |
| 1085 | $current_comment = ''; |
| 1086 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1087 | my $line = $rawlines[$linenr - 1]; |
| 1088 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1089 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1090 | $in_comment = 1; |
| 1091 | } |
| 1092 | if ($line =~ m@/\*@) { |
| 1093 | $in_comment = 1; |
| 1094 | } |
| 1095 | if (!$in_comment && $current_comment ne '') { |
| 1096 | $current_comment = ''; |
| 1097 | } |
| 1098 | $current_comment .= $line . "\n" if ($in_comment); |
| 1099 | if ($line =~ m@\*/@) { |
| 1100 | $in_comment = 0; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | chomp($current_comment); |
| 1105 | return($current_comment); |
| 1106 | } |
| 1107 | sub ctx_has_comment { |
| 1108 | my ($first_line, $end_line) = @_; |
| 1109 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1110 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1111 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1112 | ##print "CMMT: $cmt\n"; |
| 1113 | |
| 1114 | return ($cmt ne ''); |
| 1115 | } |
| 1116 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1117 | sub raw_line { |
| 1118 | my ($linenr, $cnt) = @_; |
| 1119 | |
| 1120 | my $offset = $linenr - 1; |
| 1121 | $cnt++; |
| 1122 | |
| 1123 | my $line; |
| 1124 | while ($cnt) { |
| 1125 | $line = $rawlines[$offset++]; |
| 1126 | next if (defined($line) && $line =~ /^-/); |
| 1127 | $cnt--; |
| 1128 | } |
| 1129 | |
| 1130 | return $line; |
| 1131 | } |
| 1132 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1133 | sub cat_vet { |
| 1134 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1135 | my ($res, $coded); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1136 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1137 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1138 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1139 | $res .= $1; |
| 1140 | if ($2 ne '') { |
| 1141 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1142 | $res .= $coded; |
| 1143 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1144 | } |
| 1145 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1146 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1147 | return $res; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1150 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1151 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1152 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1153 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1154 | |
| 1155 | sub annotate_reset { |
| 1156 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1157 | $av_pending = '_'; |
| 1158 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1159 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1162 | sub annotate_values { |
| 1163 | my ($stream, $type) = @_; |
| 1164 | |
| 1165 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1166 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1167 | my $cur = $stream; |
| 1168 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1169 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1170 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1171 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1172 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1173 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1174 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1175 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1176 | print "WS($1)\n" if ($dbg_values > 1); |
| 1177 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1178 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1179 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Florian Mickler | c023e473 | 2011-01-12 16:59:58 -0800 | [diff] [blame] | 1182 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1183 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1184 | push(@av_paren_type, $type); |
Andy Whitcroft | addcdce | 2012-01-10 15:10:11 -0800 | [diff] [blame] | 1185 | $type = 'c'; |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1186 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1187 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1188 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1189 | $type = 'T'; |
| 1190 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 1191 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1192 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1193 | $type = 'T'; |
| 1194 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1195 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1196 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1197 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1198 | push(@av_paren_type, $type); |
| 1199 | if ($2 ne '') { |
| 1200 | $av_pending = 'N'; |
| 1201 | } |
| 1202 | $type = 'E'; |
| 1203 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1204 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1205 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1206 | $av_preprocessor = 1; |
| 1207 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1208 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1209 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1210 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1211 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1212 | |
| 1213 | push(@av_paren_type, $type); |
| 1214 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1215 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1216 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1217 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1218 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1219 | $av_preprocessor = 1; |
| 1220 | |
| 1221 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1222 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1223 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1224 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1225 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1226 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1227 | |
| 1228 | $av_preprocessor = 1; |
| 1229 | |
| 1230 | # Assume all arms of the conditional end as this |
| 1231 | # one does, and continue as if the #endif was not here. |
| 1232 | pop(@av_paren_type); |
| 1233 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1234 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1235 | |
| 1236 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1237 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1238 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1239 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1240 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1241 | $av_pending = $type; |
| 1242 | $type = 'N'; |
| 1243 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1244 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1245 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1246 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1247 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1248 | } |
| 1249 | $type = 'N'; |
| 1250 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1251 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1252 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1253 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1254 | $type = 'N'; |
| 1255 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1256 | } elsif ($cur =~/^(case)/o) { |
| 1257 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1258 | $av_pend_colon = 'C'; |
| 1259 | $type = 'N'; |
| 1260 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1261 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1262 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1263 | $type = 'N'; |
| 1264 | |
| 1265 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1266 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1267 | push(@av_paren_type, $av_pending); |
| 1268 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1269 | $type = 'N'; |
| 1270 | |
| 1271 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1272 | my $new_type = pop(@av_paren_type); |
| 1273 | if ($new_type ne '_') { |
| 1274 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1275 | print "PAREN('$1') -> $type\n" |
| 1276 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1277 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1278 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1281 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1282 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1283 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1284 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1285 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1286 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1287 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1288 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1289 | } elsif ($type eq 'E') { |
| 1290 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1291 | } |
| 1292 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1293 | $type = 'V'; |
| 1294 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1295 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1296 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1297 | $type = 'V'; |
| 1298 | |
| 1299 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1300 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1301 | $type = 'N'; |
| 1302 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1303 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1304 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1305 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1306 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1307 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1308 | } elsif ($cur =~/^(,)/) { |
| 1309 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1310 | $type = 'C'; |
| 1311 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1312 | } elsif ($cur =~ /^(\?)/o) { |
| 1313 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1314 | $type = 'N'; |
| 1315 | |
| 1316 | } elsif ($cur =~ /^(:)/o) { |
| 1317 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1318 | |
| 1319 | substr($var, length($res), 1, $av_pend_colon); |
| 1320 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1321 | $type = 'E'; |
| 1322 | } else { |
| 1323 | $type = 'N'; |
| 1324 | } |
| 1325 | $av_pend_colon = 'O'; |
| 1326 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1327 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1328 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1329 | $type = 'N'; |
| 1330 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1331 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1332 | my $variant; |
| 1333 | |
| 1334 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1335 | if ($type eq 'V') { |
| 1336 | $variant = 'B'; |
| 1337 | } else { |
| 1338 | $variant = 'U'; |
| 1339 | } |
| 1340 | |
| 1341 | substr($var, length($res), 1, $variant); |
| 1342 | $type = 'N'; |
| 1343 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1344 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1345 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1346 | if ($1 ne '++' && $1 ne '--') { |
| 1347 | $type = 'N'; |
| 1348 | } |
| 1349 | |
| 1350 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1351 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1352 | } |
| 1353 | if (defined $1) { |
| 1354 | $cur = substr($cur, length($1)); |
| 1355 | $res .= $type x length($1); |
| 1356 | } |
| 1357 | } |
| 1358 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1359 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1362 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1363 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1364 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1365 | ^(?: |
| 1366 | $Modifier| |
| 1367 | $Storage| |
| 1368 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1369 | DEFINE_\S+ |
| 1370 | )$| |
| 1371 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1372 | goto| |
| 1373 | return| |
| 1374 | case| |
| 1375 | else| |
| 1376 | asm|__asm__| |
Andy Whitcroft | 89a8835 | 2012-01-10 15:10:00 -0800 | [diff] [blame] | 1377 | do| |
| 1378 | \#| |
| 1379 | \#\#| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1380 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1381 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1382 | )}x; |
| 1383 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1384 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1385 | # Check for modifiers. |
| 1386 | $possible =~ s/\s*$Storage\s*//g; |
| 1387 | $possible =~ s/\s*$Sparse\s*//g; |
| 1388 | if ($possible =~ /^\s*$/) { |
| 1389 | |
| 1390 | } elsif ($possible =~ /\s/) { |
| 1391 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1392 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1393 | if ($modifier !~ $notPermitted) { |
| 1394 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
| 1395 | push(@modifierList, $modifier); |
| 1396 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1397 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1398 | |
| 1399 | } else { |
| 1400 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
| 1401 | push(@typeList, $possible); |
| 1402 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1403 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1404 | } else { |
| 1405 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1409 | my $prefix = ''; |
| 1410 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1411 | sub show_type { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 1412 | return defined $use_type{$_[0]} if (scalar keys %use_type > 0); |
| 1413 | |
| 1414 | return !defined $ignore_type{$_[0]}; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1417 | sub report { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1418 | if (!show_type($_[1]) || |
| 1419 | (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1422 | my $line; |
| 1423 | if ($show_types) { |
| 1424 | $line = "$prefix$_[0]:$_[1]: $_[2]\n"; |
| 1425 | } else { |
| 1426 | $line = "$prefix$_[0]: $_[2]\n"; |
| 1427 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1428 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
| 1429 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1430 | push(our @report, $line); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1431 | |
| 1432 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1433 | } |
| 1434 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1435 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1436 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1437 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1438 | sub ERROR { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1439 | if (report("ERROR", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1440 | our $clean = 0; |
| 1441 | our $cnt_error++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1442 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1443 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1444 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1445 | } |
| 1446 | sub WARN { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1447 | if (report("WARNING", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1448 | our $clean = 0; |
| 1449 | our $cnt_warn++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1450 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1451 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1452 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1453 | } |
| 1454 | sub CHK { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1455 | if ($check && report("CHECK", $_[0], $_[1])) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1456 | our $clean = 0; |
| 1457 | our $cnt_chk++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1458 | return 1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1459 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1460 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1463 | sub check_absolute_file { |
| 1464 | my ($absolute, $herecurr) = @_; |
| 1465 | my $file = $absolute; |
| 1466 | |
| 1467 | ##print "absolute<$absolute>\n"; |
| 1468 | |
| 1469 | # See if any suffix of this path is a path within the tree. |
| 1470 | while ($file =~ s@^[^/]*/@@) { |
| 1471 | if (-f "$root/$file") { |
| 1472 | ##print "file<$file>\n"; |
| 1473 | last; |
| 1474 | } |
| 1475 | } |
| 1476 | if (! -f _) { |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | # It is, so see if the prefix is acceptable. |
| 1481 | my $prefix = $absolute; |
| 1482 | substr($prefix, -length($file)) = ''; |
| 1483 | |
| 1484 | ##print "prefix<$prefix>\n"; |
| 1485 | if ($prefix ne ".../") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1486 | WARN("USE_RELATIVE_PATH", |
| 1487 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1491 | sub trim { |
| 1492 | my ($string) = @_; |
| 1493 | |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 1494 | $string =~ s/^\s+|\s+$//g; |
| 1495 | |
| 1496 | return $string; |
| 1497 | } |
| 1498 | |
| 1499 | sub ltrim { |
| 1500 | my ($string) = @_; |
| 1501 | |
| 1502 | $string =~ s/^\s+//; |
| 1503 | |
| 1504 | return $string; |
| 1505 | } |
| 1506 | |
| 1507 | sub rtrim { |
| 1508 | my ($string) = @_; |
| 1509 | |
| 1510 | $string =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1511 | |
| 1512 | return $string; |
| 1513 | } |
| 1514 | |
| 1515 | sub tabify { |
| 1516 | my ($leading) = @_; |
| 1517 | |
| 1518 | my $source_indent = 8; |
| 1519 | my $max_spaces_before_tab = $source_indent - 1; |
| 1520 | my $spaces_to_tab = " " x $source_indent; |
| 1521 | |
| 1522 | #convert leading spaces to tabs |
| 1523 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 1524 | #Remove spaces before a tab |
| 1525 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 1526 | |
| 1527 | return "$leading"; |
| 1528 | } |
| 1529 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1530 | sub pos_last_openparen { |
| 1531 | my ($line) = @_; |
| 1532 | |
| 1533 | my $pos = 0; |
| 1534 | |
| 1535 | my $opens = $line =~ tr/\(/\(/; |
| 1536 | my $closes = $line =~ tr/\)/\)/; |
| 1537 | |
| 1538 | my $last_openparen = 0; |
| 1539 | |
| 1540 | if (($opens == 0) || ($closes >= $opens)) { |
| 1541 | return -1; |
| 1542 | } |
| 1543 | |
| 1544 | my $len = length($line); |
| 1545 | |
| 1546 | for ($pos = 0; $pos < $len; $pos++) { |
| 1547 | my $string = substr($line, $pos); |
| 1548 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 1549 | $pos += length($1) - 1; |
| 1550 | } elsif (substr($line, $pos, 1) eq '(') { |
| 1551 | $last_openparen = $pos; |
| 1552 | } elsif (index($string, '(') == -1) { |
| 1553 | last; |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | return $last_openparen + 1; |
| 1558 | } |
| 1559 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1560 | sub process { |
| 1561 | my $filename = shift; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1562 | |
| 1563 | my $linenr=0; |
| 1564 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1565 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1566 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1567 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1568 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1569 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1570 | my $indent; |
| 1571 | my $previndent=0; |
| 1572 | my $stashindent=0; |
| 1573 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1574 | our $clean = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1575 | my $signoff = 0; |
| 1576 | my $is_patch = 0; |
| 1577 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1578 | my $in_header_lines = 1; |
| 1579 | my $in_commit_log = 0; #Scanning lines before patch |
| 1580 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1581 | my $non_utf8_charset = 0; |
| 1582 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1583 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1584 | our $cnt_lines = 0; |
| 1585 | our $cnt_error = 0; |
| 1586 | our $cnt_warn = 0; |
| 1587 | our $cnt_chk = 0; |
| 1588 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1589 | # Trace the real file/line as we go. |
| 1590 | my $realfile = ''; |
| 1591 | my $realline = 0; |
| 1592 | my $realcnt = 0; |
| 1593 | my $here = ''; |
| 1594 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1595 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1596 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1597 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1598 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1599 | my $prev_values = 'E'; |
| 1600 | |
| 1601 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1602 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1603 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1604 | my %suppress_export; |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1605 | my $suppress_statement = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1606 | |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1607 | my %signatures = (); |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 1608 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1609 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1610 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1611 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1612 | my @setup_docs = (); |
| 1613 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1614 | |
| 1615 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1616 | my $line; |
| 1617 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1618 | $linenr++; |
| 1619 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1620 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1621 | push(@fixed, $rawline) if ($fix); |
| 1622 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1623 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1624 | $setup_docs = 0; |
| 1625 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 1626 | $setup_docs = 1; |
| 1627 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1628 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1629 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1630 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 1631 | $realline=$1-1; |
| 1632 | if (defined $2) { |
| 1633 | $realcnt=$3+1; |
| 1634 | } else { |
| 1635 | $realcnt=1+1; |
| 1636 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1637 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1638 | |
| 1639 | # Guestimate if this is a continuing comment. Run |
| 1640 | # the context looking for a comment "edge". If this |
| 1641 | # edge is a close comment then we must be in a comment |
| 1642 | # at context start. |
| 1643 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 1644 | my $cnt = $realcnt; |
| 1645 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 1646 | next if (defined $rawlines[$ln - 1] && |
| 1647 | $rawlines[$ln - 1] =~ /^-/); |
| 1648 | $cnt--; |
| 1649 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 1650 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1651 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 1652 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 1653 | ($edge) = $1; |
| 1654 | last; |
| 1655 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1656 | } |
| 1657 | if (defined $edge && $edge eq '*/') { |
| 1658 | $in_comment = 1; |
| 1659 | } |
| 1660 | |
| 1661 | # Guestimate if this is a continuing comment. If this |
| 1662 | # is the start of a diff block and this line starts |
| 1663 | # ' *' then it is very likely a comment. |
| 1664 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 1665 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1666 | { |
| 1667 | $in_comment = 1; |
| 1668 | } |
| 1669 | |
| 1670 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 1671 | sanitise_line_reset($in_comment); |
| 1672 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1673 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1674 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1675 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1676 | $line = sanitise_line($rawline); |
| 1677 | } |
| 1678 | push(@lines, $line); |
| 1679 | |
| 1680 | if ($realcnt > 1) { |
| 1681 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 1682 | } else { |
| 1683 | $realcnt = 0; |
| 1684 | } |
| 1685 | |
| 1686 | #print "==>$rawline\n"; |
| 1687 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1688 | |
| 1689 | if ($setup_docs && $line =~ /^\+/) { |
| 1690 | push(@setup_docs, $line); |
| 1691 | } |
| 1692 | } |
| 1693 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1694 | $prefix = ''; |
| 1695 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1696 | $realcnt = 0; |
| 1697 | $linenr = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1698 | foreach my $line (@lines) { |
| 1699 | $linenr++; |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 1700 | my $sline = $line; #copy of $line |
| 1701 | $sline =~ s/$;/ /g; #with comments as spaces |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1702 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1703 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1704 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1705 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1706 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1707 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1708 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1709 | $realline=$1-1; |
| 1710 | if (defined $2) { |
| 1711 | $realcnt=$3+1; |
| 1712 | } else { |
| 1713 | $realcnt=1+1; |
| 1714 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1715 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1716 | $prev_values = 'E'; |
| 1717 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1718 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1719 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1720 | %suppress_export = (); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1721 | $suppress_statement = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1722 | next; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1723 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1724 | # track the line number as we move through the hunk, note that |
| 1725 | # new versions of GNU diff omit the leading space on completely |
| 1726 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1727 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1728 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1729 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1730 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1731 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1732 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1733 | |
| 1734 | # Track the previous line. |
| 1735 | ($prevline, $stashline) = ($stashline, $line); |
| 1736 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1737 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 1738 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1739 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1740 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1741 | } elsif ($realcnt == 1) { |
| 1742 | $realcnt--; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 1745 | my $hunk_line = ($realcnt != 0); |
| 1746 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1747 | #make up the handle for any error we report on this line |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1748 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 1749 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 1750 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1751 | $here = "#$linenr: " if (!$file); |
| 1752 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1753 | |
| 1754 | # extract the filename as it passes |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1755 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 1756 | $realfile = $1; |
| 1757 | $realfile =~ s@^([^/]*)/@@; |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1758 | $in_commit_log = 0; |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1759 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1760 | $realfile = $1; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1761 | $realfile =~ s@^([^/]*)/@@; |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1762 | $in_commit_log = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1763 | |
| 1764 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 1765 | if (!$file && $tree && $p1_prefix ne '' && |
| 1766 | -e "$root/$p1_prefix") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1767 | WARN("PATCH_PREFIX", |
| 1768 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1769 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1770 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 1771 | if ($realfile =~ m@^include/asm/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1772 | ERROR("MODIFIED_INCLUDE_ASM", |
| 1773 | "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] | 1774 | } |
| 1775 | next; |
| 1776 | } |
| 1777 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 1778 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1779 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1780 | my $hereline = "$here\n$rawline\n"; |
| 1781 | my $herecurr = "$here\n$rawline\n"; |
| 1782 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1783 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1784 | $cnt_lines++ if ($realcnt != 0); |
| 1785 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1786 | # Check for incorrect file permissions |
| 1787 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 1788 | my $permhere = $here . "FILE: $realfile\n"; |
Joe Perches | 04db4d2 | 2013-04-29 16:18:14 -0700 | [diff] [blame] | 1789 | if ($realfile !~ m@scripts/@ && |
| 1790 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1791 | ERROR("EXECUTE_PERMISSIONS", |
| 1792 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1796 | # Check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1797 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1798 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1799 | $in_commit_log = 0; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1803 | if (!$in_header_lines && |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1804 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1805 | my $space_before = $1; |
| 1806 | my $sign_off = $2; |
| 1807 | my $space_after = $3; |
| 1808 | my $email = $4; |
| 1809 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 1810 | |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1811 | if ($sign_off !~ /$signature_tags/) { |
| 1812 | WARN("BAD_SIGN_OFF", |
| 1813 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 1814 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1815 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1816 | if (WARN("BAD_SIGN_OFF", |
| 1817 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 1818 | $fix) { |
| 1819 | $fixed[$linenr - 1] = |
| 1820 | "$ucfirst_sign_off $email"; |
| 1821 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1822 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1823 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1824 | if (WARN("BAD_SIGN_OFF", |
| 1825 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 1826 | $fix) { |
| 1827 | $fixed[$linenr - 1] = |
| 1828 | "$ucfirst_sign_off $email"; |
| 1829 | } |
| 1830 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1831 | } |
| 1832 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1833 | if (WARN("BAD_SIGN_OFF", |
| 1834 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 1835 | $fix) { |
| 1836 | $fixed[$linenr - 1] = |
| 1837 | "$ucfirst_sign_off $email"; |
| 1838 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 1842 | my $suggested_email = format_email(($email_name, $email_address)); |
| 1843 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1844 | ERROR("BAD_SIGN_OFF", |
| 1845 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1846 | } else { |
| 1847 | my $dequoted = $suggested_email; |
| 1848 | $dequoted =~ s/^"//; |
| 1849 | $dequoted =~ s/" </ </; |
| 1850 | # Don't force email to have quotes |
| 1851 | # Allow just an angle bracketed address |
| 1852 | if ("$dequoted$comment" ne $email && |
| 1853 | "<$email_address>$comment" ne $email && |
| 1854 | "$suggested_email$comment" ne $email) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1855 | WARN("BAD_SIGN_OFF", |
| 1856 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1857 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1858 | } |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1859 | |
| 1860 | # Check for duplicate signatures |
| 1861 | my $sig_nospace = $line; |
| 1862 | $sig_nospace =~ s/\s//g; |
| 1863 | $sig_nospace = lc($sig_nospace); |
| 1864 | if (defined $signatures{$sig_nospace}) { |
| 1865 | WARN("BAD_SIGN_OFF", |
| 1866 | "Duplicate signature\n" . $herecurr); |
| 1867 | } else { |
| 1868 | $signatures{$sig_nospace} = 1; |
| 1869 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1872 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1873 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1874 | ERROR("CORRUPTED_PATCH", |
| 1875 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1876 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1879 | # Check for absolute kernel paths. |
| 1880 | if ($tree) { |
| 1881 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 1882 | my $file = $1; |
| 1883 | |
| 1884 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 1885 | check_absolute_file($1, $herecurr)) { |
| 1886 | # |
| 1887 | } else { |
| 1888 | check_absolute_file($file, $herecurr); |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1893 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 1894 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1895 | $rawline !~ m/^$UTF8*$/) { |
| 1896 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 1897 | |
| 1898 | my $blank = copy_spacing($rawline); |
| 1899 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 1900 | my $hereptr = "$hereline$ptr\n"; |
| 1901 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 1902 | CHK("INVALID_UTF8", |
| 1903 | "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] | 1904 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1905 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1906 | # Check if it's the start of a commit log |
| 1907 | # (not a header line and we haven't seen the patch filename) |
| 1908 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1909 | $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1910 | $in_header_lines = 0; |
| 1911 | $in_commit_log = 1; |
| 1912 | } |
| 1913 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1914 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 1915 | # declined it, i.e defined some charset where it is missing. |
| 1916 | if ($in_header_lines && |
| 1917 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 1918 | $1 !~ /utf-8/i) { |
| 1919 | $non_utf8_charset = 1; |
| 1920 | } |
| 1921 | |
| 1922 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1923 | $rawline =~ /$NON_ASCII_UTF8/) { |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1924 | WARN("UTF8_BEFORE_PATCH", |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1925 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 1926 | } |
| 1927 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1928 | # ignore non-hunk lines and lines being removed |
| 1929 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1930 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1931 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1932 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1933 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 1934 | if (ERROR("DOS_LINE_ENDINGS", |
| 1935 | "DOS line endings\n" . $herevet) && |
| 1936 | $fix) { |
| 1937 | $fixed[$linenr - 1] =~ s/[\s\015]+$//; |
| 1938 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1939 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1940 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1941 | if (ERROR("TRAILING_WHITESPACE", |
| 1942 | "trailing whitespace\n" . $herevet) && |
| 1943 | $fix) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 1944 | $fixed[$linenr - 1] =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1947 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1948 | } |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1949 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1950 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1951 | # Only applies when adding the entry originally, after that we do not have |
| 1952 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1953 | if ($realfile =~ /Kconfig/ && |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1954 | $line =~ /.\s*config\s+/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1955 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1956 | my $cnt = $realcnt; |
| 1957 | my $ln = $linenr + 1; |
| 1958 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1959 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1960 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1961 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1962 | $f = $lines[$ln - 1]; |
| 1963 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 1964 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1965 | |
| 1966 | next if ($f =~ /^-/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1967 | |
| 1968 | if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { |
| 1969 | $is_start = 1; |
| 1970 | } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { |
| 1971 | $length = -1; |
| 1972 | } |
| 1973 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1974 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1975 | $f =~ s/#.*//; |
| 1976 | $f =~ s/^\s+//; |
| 1977 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1978 | if ($f =~ /^\s*config\s/) { |
| 1979 | $is_end = 1; |
| 1980 | last; |
| 1981 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1982 | $length++; |
| 1983 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1984 | WARN("CONFIG_DESCRIPTION", |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1985 | "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); |
| 1986 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 1989 | # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. |
| 1990 | if ($realfile =~ /Kconfig/ && |
| 1991 | $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { |
| 1992 | WARN("CONFIG_EXPERIMENTAL", |
| 1993 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 1994 | } |
| 1995 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 1996 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 1997 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 1998 | my $flag = $1; |
| 1999 | my $replacement = { |
| 2000 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 2001 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 2002 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 2003 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 2004 | }; |
| 2005 | |
| 2006 | WARN("DEPRECATED_VARIABLE", |
| 2007 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 2008 | } |
| 2009 | |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2010 | # check we are in a valid source file if not then ignore this hunk |
| 2011 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
| 2012 | |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2013 | #line length limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2014 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2015 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
Joe Perches | 0fccc62 | 2011-05-24 17:13:41 -0700 | [diff] [blame] | 2016 | !($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] | 2017 | $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2018 | $length > $max_line_length) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2019 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2020 | WARN("LONG_LINE", |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2021 | "line over $max_line_length characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Josh Triplett | ca56dc0 | 2012-03-23 15:02:21 -0700 | [diff] [blame] | 2024 | # Check for user-visible strings broken across lines, which breaks the ability |
| 2025 | # to grep for the string. Limited to strings used as parameters (those |
| 2026 | # following an open parenthesis), which almost completely eliminates false |
| 2027 | # positives, as well as warning only once per parameter rather than once per |
| 2028 | # line of the string. Make an exception when the previous string ends in a |
| 2029 | # newline (multiple lines in one string constant) or \n\t (common in inline |
| 2030 | # assembly to indent the instruction on the following line). |
| 2031 | if ($line =~ /^\+\s*"/ && |
| 2032 | $prevline =~ /"\s*$/ && |
| 2033 | $prevline =~ /\(/ && |
| 2034 | $prevrawline !~ /\\n(?:\\t)*"\s*$/) { |
| 2035 | WARN("SPLIT_STRING", |
| 2036 | "quoted string split across lines\n" . $hereprev); |
| 2037 | } |
| 2038 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 2039 | # check for spaces before a quoted newline |
| 2040 | if ($rawline =~ /^.*\".*\s\\n/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2041 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 2042 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 2043 | $fix) { |
| 2044 | $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 2045 | } |
| 2046 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 2047 | } |
| 2048 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2049 | # check for adding lines without a newline. |
| 2050 | 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] | 2051 | WARN("MISSING_EOF_NEWLINE", |
| 2052 | "adding a line without newline at end of file\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2053 | } |
| 2054 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2055 | # Blackfin: use hi/lo macros |
| 2056 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 2057 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 2058 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2059 | ERROR("LO_MACRO", |
| 2060 | "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2061 | } |
| 2062 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 2063 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2064 | ERROR("HI_MACRO", |
| 2065 | "use the HI() macro, not (... >> 16)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2066 | } |
| 2067 | } |
| 2068 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2069 | # check we are in a valid source file C or perl if not then ignore this hunk |
| 2070 | next if ($realfile !~ /\.(h|c|pl)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2071 | |
| 2072 | # at the beginning of a line any tabs must come first and anything |
| 2073 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2074 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 2075 | $rawline =~ /^\+\s* \s*/) { |
| 2076 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2077 | $rpt_cleaners = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2078 | if (ERROR("CODE_INDENT", |
| 2079 | "code indent should use tabs where possible\n" . $herevet) && |
| 2080 | $fix) { |
| 2081 | $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 2082 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2085 | # check for space before tabs. |
| 2086 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 2087 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2088 | if (WARN("SPACE_BEFORE_TAB", |
| 2089 | "please, no space before tabs\n" . $herevet) && |
| 2090 | $fix) { |
| 2091 | $fixed[$linenr - 1] =~ |
| 2092 | s/(^\+.*) +\t/$1\t/; |
| 2093 | } |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2096 | # check for && or || at the start of a line |
| 2097 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 2098 | CHK("LOGICAL_CONTINUATIONS", |
| 2099 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 2100 | } |
| 2101 | |
| 2102 | # check multi-line statement indentation matches previous line |
| 2103 | if ($^V && $^V ge 5.10.0 && |
| 2104 | $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { |
| 2105 | $prevline =~ /^\+(\t*)(.*)$/; |
| 2106 | my $oldindent = $1; |
| 2107 | my $rest = $2; |
| 2108 | |
| 2109 | my $pos = pos_last_openparen($rest); |
| 2110 | if ($pos >= 0) { |
Joe Perches | b34a26f | 2012-07-30 14:41:16 -0700 | [diff] [blame] | 2111 | $line =~ /^(\+| )([ \t]*)/; |
| 2112 | my $newindent = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2113 | |
| 2114 | my $goodtabindent = $oldindent . |
| 2115 | "\t" x ($pos / 8) . |
| 2116 | " " x ($pos % 8); |
| 2117 | my $goodspaceindent = $oldindent . " " x $pos; |
| 2118 | |
| 2119 | if ($newindent ne $goodtabindent && |
| 2120 | $newindent ne $goodspaceindent) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2121 | |
| 2122 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 2123 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 2124 | $fix && $line =~ /^\+/) { |
| 2125 | $fixed[$linenr - 1] =~ |
| 2126 | s/^\+[ \t]*/\+$goodtabindent/; |
| 2127 | } |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2132 | if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2133 | if (CHK("SPACING", |
| 2134 | "No space is necessary after a cast\n" . $hereprev) && |
| 2135 | $fix) { |
| 2136 | $fixed[$linenr - 1] =~ |
| 2137 | s/^(\+.*\*[ \t]*\))[ \t]+/$1/; |
| 2138 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2141 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | fdb4bcd | 2013-07-03 15:05:23 -0700 | [diff] [blame] | 2142 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
| 2143 | $rawline =~ /^\+[ \t]*\*/) { |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2144 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2145 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 2146 | } |
| 2147 | |
| 2148 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2149 | $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* |
| 2150 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
Joe Perches | 61135e9 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 2151 | $rawline =~ /^\+/ && #line is new |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2152 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
| 2153 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2154 | "networking block comments start with * on subsequent lines\n" . $hereprev); |
| 2155 | } |
| 2156 | |
| 2157 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | c24f9f1 | 2012-11-08 15:53:29 -0800 | [diff] [blame] | 2158 | $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
| 2159 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 2160 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 2161 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2162 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2163 | "networking block comments put the trailing */ on a separate line\n" . $herecurr); |
| 2164 | } |
| 2165 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2166 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 2167 | # Exceptions: |
| 2168 | # 1) within comments |
| 2169 | # 2) indented preprocessor commands |
| 2170 | # 3) hanging labels |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2171 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2172 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2173 | if (WARN("LEADING_SPACE", |
| 2174 | "please, no spaces at the start of a line\n" . $herevet) && |
| 2175 | $fix) { |
| 2176 | $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 2177 | } |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2180 | # check we are in a valid C source file if not then ignore this hunk |
| 2181 | next if ($realfile !~ /\.(h|c)$/); |
| 2182 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2183 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). |
| 2184 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { |
| 2185 | WARN("CONFIG_EXPERIMENTAL", |
| 2186 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2187 | } |
| 2188 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2189 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2190 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2191 | WARN("CVS_KEYWORD", |
| 2192 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2193 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2194 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2195 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 2196 | if ($line =~ /__builtin_bfin_csync/) { |
| 2197 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2198 | ERROR("CSYNC", |
| 2199 | "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2200 | } |
| 2201 | if ($line =~ /__builtin_bfin_ssync/) { |
| 2202 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2203 | ERROR("SSYNC", |
| 2204 | "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
Joe Perches | 56e77d7 | 2013-02-21 16:44:14 -0800 | [diff] [blame] | 2207 | # check for old HOTPLUG __dev<foo> section markings |
| 2208 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 2209 | WARN("HOTPLUG_SECTION", |
| 2210 | "Using $1 is unnecessary\n" . $herecurr); |
| 2211 | } |
| 2212 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2213 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2214 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 2215 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2216 | #print "LINE<$line>\n"; |
| 2217 | if ($linenr >= $suppress_statement && |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2218 | $realcnt && $sline =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2219 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2220 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2221 | $stat =~ s/\n./\n /g; |
| 2222 | $cond =~ s/\n./\n /g; |
| 2223 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2224 | #print "linenr<$linenr> <$stat>\n"; |
| 2225 | # If this statement has no statement boundaries within |
| 2226 | # it there is no point in retrying a statement scan |
| 2227 | # until we hit end of it. |
| 2228 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 2229 | if ($frag !~ /(?:{|;)/) { |
| 2230 | #print "skip<$line_nr_next>\n"; |
| 2231 | $suppress_statement = $line_nr_next; |
| 2232 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 2233 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2234 | # Find the real next line. |
| 2235 | $realline_next = $line_nr_next; |
| 2236 | if (defined $realline_next && |
| 2237 | (!defined $lines[$realline_next - 1] || |
| 2238 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 2239 | $realline_next++; |
| 2240 | } |
| 2241 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2242 | my $s = $stat; |
| 2243 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2244 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2245 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2246 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2247 | |
| 2248 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2249 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2250 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 2251 | } elsif ($s =~ /^.\s*else\b/s) { |
| 2252 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2253 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2254 | } 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] | 2255 | my $type = $1; |
| 2256 | $type =~ s/\s+/ /g; |
| 2257 | possible($type, "A:" . $s); |
| 2258 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2259 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2260 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2261 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2262 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2263 | |
| 2264 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2265 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2266 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2267 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2268 | |
| 2269 | # Check for any sort of function declaration. |
| 2270 | # int foo(something bar, other baz); |
| 2271 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2272 | 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] | 2273 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2274 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2275 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2276 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2277 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2278 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2279 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2280 | 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] | 2281 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2282 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | } |
| 2286 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2289 | # |
| 2290 | # Checks which may be anchored in the context. |
| 2291 | # |
| 2292 | |
| 2293 | # Check for switch () and associated case and default |
| 2294 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2295 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 2296 | my $err = ''; |
| 2297 | my $sep = ''; |
| 2298 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 2299 | shift(@ctx); |
| 2300 | for my $ctx (@ctx) { |
| 2301 | my ($clen, $cindent) = line_stats($ctx); |
| 2302 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 2303 | $indent != $cindent) { |
| 2304 | $err .= "$sep$ctx\n"; |
| 2305 | $sep = ''; |
| 2306 | } else { |
| 2307 | $sep = "[...]\n"; |
| 2308 | } |
| 2309 | } |
| 2310 | if ($err ne '') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2311 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 2312 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 2317 | # or if that brace on the next line is for something else |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2318 | 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] | 2319 | my $pre_ctx = "$1$2"; |
| 2320 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2321 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 2322 | |
| 2323 | if ($line =~ /^\+\t{6,}/) { |
| 2324 | WARN("DEEP_INDENTATION", |
| 2325 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 2326 | } |
| 2327 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2328 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 2329 | my $ctx = join("\n", @ctx); |
| 2330 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2331 | my $ctx_ln = $linenr; |
| 2332 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2333 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2334 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 2335 | defined $lines[$ctx_ln - 1] && |
| 2336 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 2337 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 2338 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2339 | $ctx_ln++; |
| 2340 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2341 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2342 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 2343 | #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] | 2344 | |
| 2345 | 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] | 2346 | ERROR("OPEN_BRACE", |
| 2347 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2348 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2349 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2350 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 2351 | $ctx =~ /\)\s*\;\s*$/ && |
| 2352 | defined $lines[$ctx_ln - 1]) |
| 2353 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2354 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 2355 | if ($nindent > $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2356 | WARN("TRAILING_SEMICOLON", |
| 2357 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2358 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2359 | } |
| 2360 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2363 | # Check relative indent for conditionals and blocks. |
| 2364 | 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] | 2365 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2366 | ctx_statement_block($linenr, $realcnt, 0) |
| 2367 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2368 | my ($s, $c) = ($stat, $cond); |
| 2369 | |
| 2370 | substr($s, 0, length($c), ''); |
| 2371 | |
| 2372 | # Make sure we remove the line prefixes as we have |
| 2373 | # none on the first line, and are going to readd them |
| 2374 | # where necessary. |
| 2375 | $s =~ s/\n./\n/gs; |
| 2376 | |
| 2377 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2378 | my @newlines = ($c =~ /\n/gs); |
| 2379 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2380 | |
| 2381 | # We want to check the first line inside the block |
| 2382 | # starting at the end of the conditional, so remove: |
| 2383 | # 1) any blank line termination |
| 2384 | # 2) any opening brace { on end of the line |
| 2385 | # 3) any do (...) { |
| 2386 | my $continuation = 0; |
| 2387 | my $check = 0; |
| 2388 | $s =~ s/^.*\bdo\b//; |
| 2389 | $s =~ s/^\s*{//; |
| 2390 | if ($s =~ s/^\s*\\//) { |
| 2391 | $continuation = 1; |
| 2392 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2393 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2394 | $check = 1; |
| 2395 | $cond_lines++; |
| 2396 | } |
| 2397 | |
| 2398 | # Also ignore a loop construct at the end of a |
| 2399 | # preprocessor statement. |
| 2400 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 2401 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 2402 | $check = 0; |
| 2403 | } |
| 2404 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2405 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2406 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2407 | while ($cond_ptr != $cond_lines) { |
| 2408 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2409 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2410 | # If we see an #else/#elif then the code |
| 2411 | # is not linear. |
| 2412 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 2413 | $check = 0; |
| 2414 | } |
| 2415 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2416 | # Ignore: |
| 2417 | # 1) blank lines, they should be at 0, |
| 2418 | # 2) preprocessor lines, and |
| 2419 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2420 | if ($continuation || |
| 2421 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2422 | $s =~ /^\s*#\s*?/ || |
| 2423 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2424 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 2425 | if ($s =~ s/^.*?\n//) { |
| 2426 | $cond_lines++; |
| 2427 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2428 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | my (undef, $sindent) = line_stats("+" . $s); |
| 2432 | my $stat_real = raw_line($linenr, $cond_lines); |
| 2433 | |
| 2434 | # Check if either of these lines are modified, else |
| 2435 | # this is not this patch's fault. |
| 2436 | if (!defined($stat_real) || |
| 2437 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 2438 | $check = 0; |
| 2439 | } |
| 2440 | if (defined($stat_real) && $cond_lines > 1) { |
| 2441 | $stat_real = "[...]\n$stat_real"; |
| 2442 | } |
| 2443 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2444 | #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] | 2445 | |
| 2446 | if ($check && (($sindent % 8) != 0 || |
| 2447 | ($sindent <= $indent && $s ne ''))) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2448 | WARN("SUSPECT_CODE_INDENT", |
| 2449 | "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] | 2450 | } |
| 2451 | } |
| 2452 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2453 | # Track the 'values' across context and added lines. |
| 2454 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2455 | my ($curr_values, $curr_vars) = |
| 2456 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2457 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2458 | if ($dbg_values) { |
| 2459 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2460 | print "$linenr > .$outline\n"; |
| 2461 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2462 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2463 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2464 | $prev_values = substr($curr_values, -1); |
| 2465 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2466 | #ignore lines not being added |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2467 | next if ($line =~ /^[^\+]/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2468 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2469 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2470 | if ($dbg_type) { |
| 2471 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2472 | ERROR("TEST_TYPE", |
| 2473 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2474 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2475 | ERROR("TEST_NOT_TYPE", |
| 2476 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2477 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2478 | next; |
| 2479 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2480 | # TEST: allow direct testing of the attribute matcher. |
| 2481 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2482 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2483 | ERROR("TEST_ATTR", |
| 2484 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2485 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2486 | ERROR("TEST_NOT_ATTR", |
| 2487 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2488 | } |
| 2489 | next; |
| 2490 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2491 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2492 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 2493 | if ($line =~ /^.\s*{/ && |
| 2494 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2495 | ERROR("OPEN_BRACE", |
| 2496 | "that open brace { should be on the previous line\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2499 | # |
| 2500 | # Checks which are anchored on the added line. |
| 2501 | # |
| 2502 | |
| 2503 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2504 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2505 | my $path = $1; |
| 2506 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2507 | ERROR("MALFORMED_INCLUDE", |
Joe Perches | 495e9d8 | 2012-12-20 15:05:37 -0800 | [diff] [blame] | 2508 | "malformed #include filename\n" . $herecurr); |
| 2509 | } |
| 2510 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 2511 | ERROR("UAPI_INCLUDE", |
| 2512 | "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] | 2513 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2514 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2515 | |
| 2516 | # no C99 // comments |
| 2517 | if ($line =~ m{//}) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2518 | if (ERROR("C99_COMMENTS", |
| 2519 | "do not use C99 // comments\n" . $herecurr) && |
| 2520 | $fix) { |
| 2521 | my $line = $fixed[$linenr - 1]; |
| 2522 | if ($line =~ /\/\/(.*)$/) { |
| 2523 | my $comment = trim($1); |
| 2524 | $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; |
| 2525 | } |
| 2526 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2527 | } |
| 2528 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2529 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2530 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2531 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2532 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 2533 | # the whole statement. |
| 2534 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 2535 | if (defined $realline_next && |
| 2536 | exists $lines[$realline_next - 1] && |
| 2537 | !defined $suppress_export{$realline_next} && |
| 2538 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2539 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2540 | # Handle definitions which produce identifiers with |
| 2541 | # a prefix: |
| 2542 | # XXX(foo); |
| 2543 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2544 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 2545 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2546 | $name =~ /^${Ident}_$2/) { |
| 2547 | #print "FOO C name<$name>\n"; |
| 2548 | $suppress_export{$realline_next} = 1; |
| 2549 | |
| 2550 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2551 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2552 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 2553 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 2554 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2555 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 2556 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2557 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2558 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 2559 | $suppress_export{$realline_next} = 2; |
| 2560 | } else { |
| 2561 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2562 | } |
| 2563 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2564 | if (!defined $suppress_export{$linenr} && |
| 2565 | $prevline =~ /^.\s*$/ && |
| 2566 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2567 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 2568 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 2569 | $suppress_export{$linenr} = 2; |
| 2570 | } |
| 2571 | if (defined $suppress_export{$linenr} && |
| 2572 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2573 | WARN("EXPORT_SYMBOL", |
| 2574 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2575 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2576 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 2577 | # check for global initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2578 | if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { |
| 2579 | if (ERROR("GLOBAL_INITIALISERS", |
| 2580 | "do not initialise globals to 0 or NULL\n" . |
| 2581 | $herecurr) && |
| 2582 | $fix) { |
| 2583 | $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; |
| 2584 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2585 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2586 | # check for static initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2587 | if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
| 2588 | if (ERROR("INITIALISED_STATIC", |
| 2589 | "do not initialise statics to 0 or NULL\n" . |
| 2590 | $herecurr) && |
| 2591 | $fix) { |
| 2592 | $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; |
| 2593 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2596 | # check for static const char * arrays. |
| 2597 | 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] | 2598 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2599 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2600 | $herecurr); |
| 2601 | } |
| 2602 | |
| 2603 | # check for static char foo[] = "bar" declarations. |
| 2604 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2605 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2606 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2607 | $herecurr); |
| 2608 | } |
| 2609 | |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2610 | # check for declarations of struct pci_device_id |
| 2611 | 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] | 2612 | WARN("DEFINE_PCI_DEVICE_TABLE", |
| 2613 | "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2616 | # check for new typedefs, only function parameters and sparse annotations |
| 2617 | # make sense. |
| 2618 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 2619 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2620 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2621 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2622 | $line !~ /\b__bitwise(?:__|)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2623 | WARN("NEW_TYPEDEFS", |
| 2624 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2628 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2629 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 2630 | #print "AA<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2631 | my ($ident, $from, $to) = ($1, $2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2632 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2633 | # Should start with a space. |
| 2634 | $to =~ s/^(\S)/ $1/; |
| 2635 | # Should not end with a space. |
| 2636 | $to =~ s/\s+$//; |
| 2637 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2638 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2639 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2640 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2641 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2642 | if ($from ne $to) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2643 | if (ERROR("POINTER_LOCATION", |
| 2644 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 2645 | $fix) { |
| 2646 | my $sub_from = $ident; |
| 2647 | my $sub_to = $ident; |
| 2648 | $sub_to =~ s/\Q$from\E/$to/; |
| 2649 | $fixed[$linenr - 1] =~ |
| 2650 | s@\Q$sub_from\E@$sub_to@; |
| 2651 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2652 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2653 | } |
| 2654 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 2655 | #print "BB<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2656 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2657 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2658 | # Should start with a space. |
| 2659 | $to =~ s/^(\S)/ $1/; |
| 2660 | # Should not end with a space. |
| 2661 | $to =~ s/\s+$//; |
| 2662 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2663 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2664 | } |
| 2665 | # Modifiers should have spaces. |
| 2666 | $to =~ s/(\b$Modifier$)/$1 /; |
| 2667 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2668 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2669 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2670 | if (ERROR("POINTER_LOCATION", |
| 2671 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 2672 | $fix) { |
| 2673 | |
| 2674 | my $sub_from = $match; |
| 2675 | my $sub_to = $match; |
| 2676 | $sub_to =~ s/\Q$from\E/$to/; |
| 2677 | $fixed[$linenr - 1] =~ |
| 2678 | s@\Q$sub_from\E@$sub_to@; |
| 2679 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2680 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | # # no BUG() or BUG_ON() |
| 2684 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 2685 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 2686 | # print "$herecurr"; |
| 2687 | # $clean = 0; |
| 2688 | # } |
| 2689 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2690 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2691 | WARN("LINUX_VERSION_CODE", |
| 2692 | "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] | 2693 | } |
| 2694 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2695 | # check for uses of printk_ratelimit |
| 2696 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2697 | WARN("PRINTK_RATELIMITED", |
| 2698 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2701 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 2702 | # same line do not need a level, so we use the current block context |
| 2703 | # 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] | 2704 | # 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] | 2705 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2706 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2707 | my $ok = 0; |
| 2708 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 2709 | #print "CHECK<$lines[$ln - 1]\n"; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2710 | # we have a preceding printk if it ends |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2711 | # with "\n" ignore it, else it is to blame |
| 2712 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 2713 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 2714 | $ok = 1; |
| 2715 | } |
| 2716 | last; |
| 2717 | } |
| 2718 | } |
| 2719 | if ($ok == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2720 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 2721 | "printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2722 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2725 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 2726 | my $orig = $1; |
| 2727 | my $level = lc($orig); |
| 2728 | $level = "warn" if ($level eq "warning"); |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2729 | my $level2 = $level; |
| 2730 | $level2 = "dbg" if ($level eq "debug"); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2731 | WARN("PREFER_PR_LEVEL", |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2732 | "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] | 2733 | } |
| 2734 | |
| 2735 | if ($line =~ /\bpr_warning\s*\(/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2736 | if (WARN("PREFER_PR_LEVEL", |
| 2737 | "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && |
| 2738 | $fix) { |
| 2739 | $fixed[$linenr - 1] =~ |
| 2740 | s/\bpr_warning\b/pr_warn/; |
| 2741 | } |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2742 | } |
| 2743 | |
Joe Perches | dc13931 | 2013-02-21 16:44:13 -0800 | [diff] [blame] | 2744 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 2745 | my $orig = $1; |
| 2746 | my $level = lc($orig); |
| 2747 | $level = "warn" if ($level eq "warning"); |
| 2748 | $level = "dbg" if ($level eq "debug"); |
| 2749 | WARN("PREFER_DEV_LEVEL", |
| 2750 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 2751 | } |
| 2752 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2753 | # function brace can't be on same line, except for #defines of do while, |
| 2754 | # or if closed on same line |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2755 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
| 2756 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2757 | ERROR("OPEN_BRACE", |
| 2758 | "open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2759 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2760 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2761 | # open braces for enum, union and struct go on the same line. |
| 2762 | if ($line =~ /^.\s*{/ && |
| 2763 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2764 | ERROR("OPEN_BRACE", |
| 2765 | "open brace '{' following $1 go on the same line\n" . $hereprev); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2766 | } |
| 2767 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2768 | # missing space after union, struct or enum definition |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2769 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 2770 | if (WARN("SPACING", |
| 2771 | "missing space after $1 definition\n" . $herecurr) && |
| 2772 | $fix) { |
| 2773 | $fixed[$linenr - 1] =~ |
| 2774 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 2775 | } |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2778 | # check for spacing round square brackets; allowed: |
| 2779 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2780 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 2781 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2782 | while ($line =~ /(.*?\s)\[/g) { |
| 2783 | my ($where, $prefix) = ($-[1], $1); |
| 2784 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2785 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Andy Whitcroft | daebc53 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 2786 | $prefix !~ /[{,]\s+$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2787 | if (ERROR("BRACKET_SPACE", |
| 2788 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 2789 | $fix) { |
| 2790 | $fixed[$linenr - 1] =~ |
| 2791 | s/^(\+.*?)\s+\[/$1\[/; |
| 2792 | } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2793 | } |
| 2794 | } |
| 2795 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2796 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2797 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2798 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2799 | my $ctx_before = substr($line, 0, $-[1]); |
| 2800 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2801 | |
| 2802 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2803 | if ($name =~ /^(?: |
| 2804 | if|for|while|switch|return|case| |
| 2805 | volatile|__volatile__| |
| 2806 | __attribute__|format|__extension__| |
| 2807 | asm|__asm__)$/x) |
| 2808 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2809 | # cpp #define statements have non-optional spaces, ie |
| 2810 | # if there is a space between the name and the open |
| 2811 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2812 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2813 | |
| 2814 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2815 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2816 | |
| 2817 | # If this whole things ends with a type its most |
| 2818 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2819 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2820 | |
| 2821 | } else { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2822 | if (WARN("SPACING", |
| 2823 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 2824 | $fix) { |
| 2825 | $fixed[$linenr - 1] =~ |
| 2826 | s/\b$name\s+\(/$name\(/; |
| 2827 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2828 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2829 | } |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2830 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2831 | # Check operator spacing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2832 | if (!($line=~/\#\s*include/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2833 | my $fixed_line = ""; |
| 2834 | my $line_fixed = 0; |
| 2835 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2836 | my $ops = qr{ |
| 2837 | <<=|>>=|<=|>=|==|!=| |
| 2838 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 2839 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2840 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
| 2841 | \?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2842 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2843 | my @elements = split(/($ops|;)/, $opline); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2844 | |
| 2845 | ## print("element count: <" . $#elements . ">\n"); |
| 2846 | ## foreach my $el (@elements) { |
| 2847 | ## print("el: <$el>\n"); |
| 2848 | ## } |
| 2849 | |
| 2850 | my @fix_elements = (); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2851 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2852 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2853 | foreach my $el (@elements) { |
| 2854 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 2855 | $off += length($el); |
| 2856 | } |
| 2857 | |
| 2858 | $off = 0; |
| 2859 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2860 | my $blank = copy_spacing($opline); |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2861 | my $last_after = -1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2862 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2863 | for (my $n = 0; $n < $#elements; $n += 2) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2864 | |
| 2865 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 2866 | |
| 2867 | ## print("n: <$n> good: <$good>\n"); |
| 2868 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2869 | $off += length($elements[$n]); |
| 2870 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2871 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2872 | my $ca = substr($opline, 0, $off); |
| 2873 | my $cc = ''; |
| 2874 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 2875 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 2876 | } |
| 2877 | my $cb = "$ca$;$cc"; |
| 2878 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2879 | my $a = ''; |
| 2880 | $a = 'V' if ($elements[$n] ne ''); |
| 2881 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2882 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2883 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 2884 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2885 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2886 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2887 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2888 | |
| 2889 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2890 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2891 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 2892 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2893 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2894 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 2895 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 2896 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2897 | } else { |
| 2898 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2899 | } |
| 2900 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2901 | my $ctx = "${a}x${c}"; |
| 2902 | |
| 2903 | my $at = "(ctx:$ctx)"; |
| 2904 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2905 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2906 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2907 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2908 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2909 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2910 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2911 | # Get the full operator variant. |
| 2912 | my $opv = $op . substr($curr_vars, $off, 1); |
| 2913 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2914 | # Ignore operators passed as parameters. |
| 2915 | if ($op_type ne 'V' && |
| 2916 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { |
| 2917 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2918 | # # Ignore comments |
| 2919 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2920 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2921 | # ; 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] | 2922 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2923 | if ($ctx !~ /.x[WEBC]/ && |
| 2924 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2925 | if (ERROR("SPACING", |
| 2926 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2927 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2928 | $line_fixed = 1; |
| 2929 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | # // is a comment |
| 2933 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2934 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2935 | # No spaces for: |
| 2936 | # -> |
| 2937 | # : when part of a bitfield |
| 2938 | } elsif ($op eq '->' || $opv eq ':B') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2939 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2940 | if (ERROR("SPACING", |
| 2941 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2942 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2943 | if (defined $fix_elements[$n + 2]) { |
| 2944 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 2945 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2946 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2947 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | # , must have a space on the right. |
| 2951 | } elsif ($op eq ',') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2952 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2953 | if (ERROR("SPACING", |
| 2954 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2955 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2956 | $line_fixed = 1; |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2957 | $last_after = $n; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2958 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2961 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2962 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2963 | #warn "'*' is part of type\n"; |
| 2964 | |
| 2965 | # unary operators should have a space before and |
| 2966 | # none after. May be left adjacent to another |
| 2967 | # unary operator, or a cast |
| 2968 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2969 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2970 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2971 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2972 | if (ERROR("SPACING", |
| 2973 | "space required before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2974 | if ($n != $last_after + 2) { |
| 2975 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 2976 | $line_fixed = 1; |
| 2977 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2978 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2979 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 2980 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2981 | # A unary '*' may be const |
| 2982 | |
| 2983 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2984 | if (ERROR("SPACING", |
| 2985 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2986 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2987 | if (defined $fix_elements[$n + 2]) { |
| 2988 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 2989 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2990 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2991 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | # unary ++ and unary -- are allowed no space on one side. |
| 2995 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2996 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2997 | if (ERROR("SPACING", |
| 2998 | "space required one side of that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2999 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3000 | $line_fixed = 1; |
| 3001 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3002 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3003 | if ($ctx =~ /Wx[BE]/ || |
| 3004 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3005 | if (ERROR("SPACING", |
| 3006 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3007 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3008 | $line_fixed = 1; |
| 3009 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3010 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3011 | if ($ctx =~ /ExW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3012 | if (ERROR("SPACING", |
| 3013 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3014 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3015 | if (defined $fix_elements[$n + 2]) { |
| 3016 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3017 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3018 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3019 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3020 | } |
| 3021 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3022 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3023 | } elsif ($op eq '<<' or $op eq '>>' or |
| 3024 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 3025 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3026 | $op eq '*' or $op eq '/' or |
| 3027 | $op eq '%') |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3028 | { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3029 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3030 | if (ERROR("SPACING", |
| 3031 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3032 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3033 | if (defined $fix_elements[$n + 2]) { |
| 3034 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3035 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3036 | $line_fixed = 1; |
| 3037 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3040 | # A colon needs no spaces before when it is |
| 3041 | # terminating a case value or a label. |
| 3042 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 3043 | if ($ctx =~ /Wx./) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3044 | if (ERROR("SPACING", |
| 3045 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3046 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3047 | $line_fixed = 1; |
| 3048 | } |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3049 | } |
| 3050 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3051 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3052 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3053 | my $ok = 0; |
| 3054 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3055 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3056 | if (($op eq '<' && |
| 3057 | $cc =~ /^\S+\@\S+>/) || |
| 3058 | ($op eq '>' && |
| 3059 | $ca =~ /<\S+\@\S+$/)) |
| 3060 | { |
| 3061 | $ok = 1; |
| 3062 | } |
| 3063 | |
| 3064 | # Ignore ?: |
| 3065 | if (($opv eq ':O' && $ca =~ /\?$/) || |
| 3066 | ($op eq '?' && $cc =~ /^:/)) { |
| 3067 | $ok = 1; |
| 3068 | } |
| 3069 | |
| 3070 | if ($ok == 0) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3071 | if (ERROR("SPACING", |
| 3072 | "spaces required around 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]) . " "; |
| 3074 | if (defined $fix_elements[$n + 2]) { |
| 3075 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3076 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3077 | $line_fixed = 1; |
| 3078 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3079 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3080 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3081 | $off += length($elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3082 | |
| 3083 | ## print("n: <$n> GOOD: <$good>\n"); |
| 3084 | |
| 3085 | $fixed_line = $fixed_line . $good; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3086 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3087 | |
| 3088 | if (($#elements % 2) == 0) { |
| 3089 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 3090 | } |
| 3091 | |
| 3092 | if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { |
| 3093 | $fixed[$linenr - 1] = $fixed_line; |
| 3094 | } |
| 3095 | |
| 3096 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3097 | } |
| 3098 | |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 3099 | # check for whitespace before a non-naked semicolon |
| 3100 | if ($line =~ /^\+.*\S\s+;/) { |
| 3101 | if (WARN("SPACING", |
| 3102 | "space prohibited before semicolon\n" . $herecurr) && |
| 3103 | $fix) { |
| 3104 | 1 while $fixed[$linenr - 1] =~ |
| 3105 | s/^(\+.*\S)\s+;/$1;/; |
| 3106 | } |
| 3107 | } |
| 3108 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3109 | # check for multiple assignments |
| 3110 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3111 | CHK("MULTIPLE_ASSIGNMENTS", |
| 3112 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3113 | } |
| 3114 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3115 | ## # check for multiple declarations, allowing for a function declaration |
| 3116 | ## # continuation. |
| 3117 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 3118 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 3119 | ## |
| 3120 | ## # Remove any bracketed sections to ensure we do not |
| 3121 | ## # falsly report the parameters of functions. |
| 3122 | ## my $ln = $line; |
| 3123 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 3124 | ## } |
| 3125 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3126 | ## WARN("MULTIPLE_DECLARATION", |
| 3127 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3128 | ## } |
| 3129 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3130 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3131 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3132 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 3133 | $line =~ /do{/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3134 | if (ERROR("SPACING", |
| 3135 | "space required before the open brace '{'\n" . $herecurr) && |
| 3136 | $fix) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3137 | $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3138 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3139 | } |
| 3140 | |
Joe Perches | c4a62ef | 2013-07-03 15:05:28 -0700 | [diff] [blame] | 3141 | ## # check for blank lines before declarations |
| 3142 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 3143 | ## $prevrawline =~ /^.\s*$/) { |
| 3144 | ## WARN("SPACING", |
| 3145 | ## "No blank lines before declarations\n" . $hereprev); |
| 3146 | ## } |
| 3147 | ## |
| 3148 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3149 | # closing brace should have a space following it when it has anything |
| 3150 | # on the line |
| 3151 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3152 | if (ERROR("SPACING", |
| 3153 | "space required after that close brace '}'\n" . $herecurr) && |
| 3154 | $fix) { |
| 3155 | $fixed[$linenr - 1] =~ |
| 3156 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 3157 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3158 | } |
| 3159 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3160 | # check spacing on square brackets |
| 3161 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3162 | if (ERROR("SPACING", |
| 3163 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 3164 | $fix) { |
| 3165 | $fixed[$linenr - 1] =~ |
| 3166 | s/\[\s+/\[/; |
| 3167 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3168 | } |
| 3169 | if ($line =~ /\s\]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3170 | if (ERROR("SPACING", |
| 3171 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 3172 | $fix) { |
| 3173 | $fixed[$linenr - 1] =~ |
| 3174 | s/\s+\]/\]/; |
| 3175 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3178 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3179 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 3180 | $line !~ /for\s*\(\s+;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3181 | if (ERROR("SPACING", |
| 3182 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 3183 | $fix) { |
| 3184 | $fixed[$linenr - 1] =~ |
| 3185 | s/\(\s+/\(/; |
| 3186 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3187 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3188 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3189 | $line !~ /for\s*\(.*;\s+\)/ && |
| 3190 | $line !~ /:\s+\)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3191 | if (ERROR("SPACING", |
| 3192 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 3193 | $fix) { |
| 3194 | $fixed[$linenr - 1] =~ |
| 3195 | s/\s+\)/\)/; |
| 3196 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3197 | } |
| 3198 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3199 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3200 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3201 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3202 | if (WARN("INDENTED_LABEL", |
| 3203 | "labels should not be indented\n" . $herecurr) && |
| 3204 | $fix) { |
| 3205 | $fixed[$linenr - 1] =~ |
| 3206 | s/^(.)\s+/$1/; |
| 3207 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3208 | } |
| 3209 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3210 | # Return is not a function. |
| 3211 | if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { |
| 3212 | my $spacing = $1; |
| 3213 | my $value = $2; |
| 3214 | |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 3215 | # Flatten any parentheses |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3216 | $value =~ s/\(/ \(/g; |
| 3217 | $value =~ s/\)/\) /g; |
Andy Whitcroft | e01886a | 2012-01-10 15:10:08 -0800 | [diff] [blame] | 3218 | while ($value =~ s/\[[^\[\]]*\]/1/ || |
Andy Whitcroft | 63f17f8 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3219 | $value !~ /(?:$Ident|-?$Constant)\s* |
| 3220 | $Compare\s* |
| 3221 | (?:$Ident|-?$Constant)/x && |
| 3222 | $value =~ s/\([^\(\)]*\)/1/) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3223 | } |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3224 | #print "value<$value>\n"; |
| 3225 | if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3226 | ERROR("RETURN_PARENTHESES", |
| 3227 | "return is not a function, parentheses are not required\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3228 | |
| 3229 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3230 | ERROR("SPACING", |
| 3231 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3232 | } |
| 3233 | } |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 3234 | # Return of what appears to be an errno should normally be -'ve |
| 3235 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
| 3236 | my $name = $1; |
| 3237 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3238 | WARN("USE_NEGATIVE_ERRNO", |
| 3239 | "return of an errno should typically be -ve (return -$1)\n" . $herecurr); |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 3240 | } |
| 3241 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3242 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3243 | # Need a space before open parenthesis after if, while etc |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3244 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 3245 | if (ERROR("SPACING", |
| 3246 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 3247 | $fix) { |
| 3248 | $fixed[$linenr - 1] =~ |
| 3249 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 3250 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3253 | # Check for illegal assignment in if conditional -- and check for trailing |
| 3254 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3255 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 3256 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3257 | ctx_statement_block($linenr, $realcnt, 0) |
| 3258 | if (!defined $stat); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3259 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 3260 | $remain_next, $off_next); |
| 3261 | $stat_next =~ s/\n./\n /g; |
| 3262 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 3263 | |
| 3264 | if ($stat_next =~ /^\s*while\b/) { |
| 3265 | # If the statement carries leading newlines, |
| 3266 | # then count those as offsets. |
| 3267 | my ($whitespace) = |
| 3268 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3269 | my $offset = |
| 3270 | statement_rawlines($whitespace) - 1; |
| 3271 | |
| 3272 | $suppress_whiletrailers{$line_nr_next + |
| 3273 | $offset} = 1; |
| 3274 | } |
| 3275 | } |
| 3276 | if (!defined $suppress_whiletrailers{$linenr} && |
| 3277 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3278 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3279 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 3280 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3281 | ERROR("ASSIGN_IN_IF", |
| 3282 | "do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | # Find out what is on the end of the line after the |
| 3286 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3287 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3288 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3289 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3290 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 3291 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3292 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3293 | # Find out how long the conditional actually is. |
| 3294 | my @newlines = ($c =~ /\n/gs); |
| 3295 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 3296 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3297 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 3298 | $stat_real = raw_line($linenr, $cond_lines) |
| 3299 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3300 | if (defined($stat_real) && $cond_lines > 1) { |
| 3301 | $stat_real = "[...]\n$stat_real"; |
| 3302 | } |
| 3303 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3304 | ERROR("TRAILING_STATEMENTS", |
| 3305 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3306 | } |
| 3307 | } |
| 3308 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3309 | # Check for bitwise tests written as boolean |
| 3310 | if ($line =~ / |
| 3311 | (?: |
| 3312 | (?:\[|\(|\&\&|\|\|) |
| 3313 | \s*0[xX][0-9]+\s* |
| 3314 | (?:\&\&|\|\|) |
| 3315 | | |
| 3316 | (?:\&\&|\|\|) |
| 3317 | \s*0[xX][0-9]+\s* |
| 3318 | (?:\&\&|\|\||\)|\]) |
| 3319 | )/x) |
| 3320 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3321 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 3322 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3323 | } |
| 3324 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3325 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3326 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 3327 | my $s = $1; |
| 3328 | $s =~ s/$;//g; # Remove any comments |
| 3329 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3330 | ERROR("TRAILING_STATEMENTS", |
| 3331 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3332 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3333 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3334 | # if should not continue a brace |
| 3335 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3336 | ERROR("TRAILING_STATEMENTS", |
| 3337 | "trailing statements should be on next line\n" . |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3338 | $herecurr); |
| 3339 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3340 | # case and default should not have general statements after them |
| 3341 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 3342 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 3343 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3344 | \s*return\s+ |
| 3345 | )/xg) |
| 3346 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3347 | ERROR("TRAILING_STATEMENTS", |
| 3348 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3349 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3350 | |
| 3351 | # Check for }<nl>else {, these must be at the same |
| 3352 | # indent level to be relevant to each other. |
| 3353 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 3354 | $previndent == $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3355 | ERROR("ELSE_AFTER_BRACE", |
| 3356 | "else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3359 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
| 3360 | $previndent == $indent) { |
| 3361 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 3362 | |
| 3363 | # Find out what is on the end of the line after the |
| 3364 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3365 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3366 | $s =~ s/\n.*//g; |
| 3367 | |
| 3368 | if ($s =~ /^\s*;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3369 | ERROR("WHILE_AFTER_BRACE", |
| 3370 | "while should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3371 | } |
| 3372 | } |
| 3373 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3374 | #Specific variable tests |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 3375 | while ($line =~ m{($Constant|$Lval)}g) { |
| 3376 | my $var = $1; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3377 | |
| 3378 | #gcc binary extension |
| 3379 | if ($var =~ /^$Binary$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3380 | if (WARN("GCC_BINARY_CONSTANT", |
| 3381 | "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && |
| 3382 | $fix) { |
| 3383 | my $hexval = sprintf("0x%x", oct($var)); |
| 3384 | $fixed[$linenr - 1] =~ |
| 3385 | s/\b$var\b/$hexval/; |
| 3386 | } |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | #CamelCase |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 3390 | if ($var !~ /^$Constant$/ && |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3391 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 3392 | #Ignore Page<foo> variants |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 3393 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 3394 | #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] | 3395 | $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 3396 | while ($var =~ m{($Ident)}g) { |
| 3397 | my $word = $1; |
| 3398 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
| 3399 | seed_camelcase_includes() if ($check); |
| 3400 | if (!defined $camelcase{$word}) { |
| 3401 | $camelcase{$word} = 1; |
| 3402 | CHK("CAMELCASE", |
| 3403 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 3404 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 3405 | } |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 3406 | } |
| 3407 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3408 | |
| 3409 | #no spaces allowed after \ in define |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3410 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 3411 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 3412 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 3413 | $fix) { |
| 3414 | $fixed[$linenr - 1] =~ s/\s+$//; |
| 3415 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3418 | #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] | 3419 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3420 | my $file = "$1.h"; |
| 3421 | my $checkfile = "include/linux/$file"; |
| 3422 | if (-f "$root/$checkfile" && |
| 3423 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 3424 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3425 | { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3426 | if ($realfile =~ m{^arch/}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3427 | CHK("ARCH_INCLUDE_LINUX", |
| 3428 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3429 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3430 | WARN("INCLUDE_LINUX", |
| 3431 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3432 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3436 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 3437 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3438 | # in a known good container |
Andy Whitcroft | b8f96a3 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 3439 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 3440 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3441 | my $ln = $linenr; |
| 3442 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3443 | my ($off, $dstat, $dcond, $rest); |
| 3444 | my $ctx = ''; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3445 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3446 | ctx_statement_block($linenr, $realcnt, 0); |
| 3447 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3448 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 3449 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3450 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3451 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 3452 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3453 | $dstat =~ s/\\\n.//g; |
| 3454 | $dstat =~ s/^\s*//s; |
| 3455 | $dstat =~ s/\s*$//s; |
| 3456 | |
| 3457 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3458 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 3459 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Andy Whitcroft | c81769f | 2012-01-10 15:10:10 -0800 | [diff] [blame] | 3460 | $dstat =~ s/\[[^\[\]]*\]/1/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3461 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3462 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3463 | |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 3464 | # Flatten any obvious string concatentation. |
| 3465 | while ($dstat =~ s/("X*")\s*$Ident/$1/ || |
| 3466 | $dstat =~ s/$Ident\s*("X*")/$1/) |
| 3467 | { |
| 3468 | } |
| 3469 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3470 | my $exceptions = qr{ |
| 3471 | $Declare| |
| 3472 | module_param_named| |
Kees Cook | a0a0a7a | 2012-10-04 17:13:38 -0700 | [diff] [blame] | 3473 | MODULE_PARM_DESC| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3474 | DECLARE_PER_CPU| |
| 3475 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 3476 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 3477 | union| |
| 3478 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 3479 | \.$Ident\s*=\s*| |
| 3480 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3481 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3482 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3483 | if ($dstat ne '' && |
| 3484 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 3485 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Joe Perches | 3cc4b1c | 2013-07-03 15:05:27 -0700 | [diff] [blame] | 3486 | $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] | 3487 | $dstat !~ /^'X'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3488 | $dstat !~ /$exceptions/ && |
| 3489 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Joe Perches | e942e2c | 2013-04-17 15:58:26 -0700 | [diff] [blame] | 3490 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 3491 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3492 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 3493 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 3494 | $dstat !~ /^do\s*{/ && # do {... |
Joe Perches | f95a7e6 | 2013-09-11 14:24:00 -0700 | [diff] [blame] | 3495 | $dstat !~ /^\({/ && # ({... |
| 3496 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3497 | { |
| 3498 | $ctx =~ s/\n*$//; |
| 3499 | my $herectx = $here . "\n"; |
| 3500 | my $cnt = statement_rawlines($ctx); |
| 3501 | |
| 3502 | for (my $n = 0; $n < $cnt; $n++) { |
| 3503 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3506 | if ($dstat =~ /;/) { |
| 3507 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 3508 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 3509 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3510 | ERROR("COMPLEX_MACRO", |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3511 | "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3512 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3513 | } |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3514 | |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3515 | # check for line continuations outside of #defines, preprocessor #, and asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3516 | |
| 3517 | } else { |
| 3518 | if ($prevline !~ /^..*\\$/ && |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3519 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 3520 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3521 | $line =~ /^\+.*\\$/) { |
| 3522 | WARN("LINE_CONTINUATIONS", |
| 3523 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 3524 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3525 | } |
| 3526 | |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3527 | # do {} while (0) macro tests: |
| 3528 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 3529 | # macro should not end with a semicolon |
| 3530 | if ($^V && $^V ge 5.10.0 && |
| 3531 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 3532 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 3533 | my $ln = $linenr; |
| 3534 | my $cnt = $realcnt; |
| 3535 | my ($off, $dstat, $dcond, $rest); |
| 3536 | my $ctx = ''; |
| 3537 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 3538 | ctx_statement_block($linenr, $realcnt, 0); |
| 3539 | $ctx = $dstat; |
| 3540 | |
| 3541 | $dstat =~ s/\\\n.//g; |
| 3542 | |
| 3543 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 3544 | my $stmts = $2; |
| 3545 | my $semis = $3; |
| 3546 | |
| 3547 | $ctx =~ s/\n*$//; |
| 3548 | my $cnt = statement_rawlines($ctx); |
| 3549 | my $herectx = $here . "\n"; |
| 3550 | |
| 3551 | for (my $n = 0; $n < $cnt; $n++) { |
| 3552 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 3553 | } |
| 3554 | |
Joe Perches | ac8e97f | 2012-08-21 16:15:53 -0700 | [diff] [blame] | 3555 | if (($stmts =~ tr/;/;/) == 1 && |
| 3556 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3557 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 3558 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 3559 | } |
| 3560 | if (defined $semis && $semis ne "") { |
| 3561 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 3562 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 3563 | } |
| 3564 | } |
| 3565 | } |
| 3566 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3567 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 3568 | # all assignments may have only one of the following with an assignment: |
| 3569 | # . |
| 3570 | # ALIGN(...) |
| 3571 | # VMLINUX_SYMBOL(...) |
| 3572 | 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] | 3573 | WARN("MISSING_VMLINUX_SYMBOL", |
| 3574 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3575 | } |
| 3576 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3577 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3578 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 3579 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3580 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3581 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3582 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 3583 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3584 | my @allowed = (); |
| 3585 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3586 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3587 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3588 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3589 | for my $chunk (@chunks) { |
| 3590 | my ($cond, $block) = @{$chunk}; |
| 3591 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3592 | # If the condition carries leading newlines, then count those as offsets. |
| 3593 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3594 | my $offset = statement_rawlines($whitespace) - 1; |
| 3595 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3596 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3597 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 3598 | |
| 3599 | # We have looked at and allowed this specific line. |
| 3600 | $suppress_ifbraces{$ln + $offset} = 1; |
| 3601 | |
| 3602 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3603 | $ln += statement_rawlines($block) - 1; |
| 3604 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3605 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3606 | |
| 3607 | $seen++ if ($block =~ /^\s*{/); |
| 3608 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3609 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3610 | if (statement_lines($cond) > 1) { |
| 3611 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3612 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3613 | } |
| 3614 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3615 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3616 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3617 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3618 | if (statement_block_size($block) > 1) { |
| 3619 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3620 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3621 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3622 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3623 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3624 | if ($seen) { |
| 3625 | my $sum_allowed = 0; |
| 3626 | foreach (@allowed) { |
| 3627 | $sum_allowed += $_; |
| 3628 | } |
| 3629 | if ($sum_allowed == 0) { |
| 3630 | WARN("BRACES", |
| 3631 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 3632 | } elsif ($sum_allowed != $allow && |
| 3633 | $seen != $allow) { |
| 3634 | CHK("BRACES", |
| 3635 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 3636 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3640 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3641 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3642 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3643 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3644 | # Check the pre-context. |
| 3645 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 3646 | #print "APW: ALLOWED: pre<$1>\n"; |
| 3647 | $allowed = 1; |
| 3648 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3649 | |
| 3650 | my ($level, $endln, @chunks) = |
| 3651 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 3652 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3653 | # Check the condition. |
| 3654 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3655 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3656 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3657 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3658 | } |
| 3659 | if (statement_lines($cond) > 1) { |
| 3660 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 3661 | $allowed = 1; |
| 3662 | } |
| 3663 | if ($block =~/\b(?:if|for|while)\b/) { |
| 3664 | #print "APW: ALLOWED: block<$block>\n"; |
| 3665 | $allowed = 1; |
| 3666 | } |
| 3667 | if (statement_block_size($block) > 1) { |
| 3668 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 3669 | $allowed = 1; |
| 3670 | } |
| 3671 | # Check the post-context. |
| 3672 | if (defined $chunks[1]) { |
| 3673 | my ($cond, $block) = @{$chunks[1]}; |
| 3674 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3675 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3676 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3677 | if ($block =~ /^\s*\{/) { |
| 3678 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 3679 | $allowed = 1; |
| 3680 | } |
| 3681 | } |
| 3682 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3683 | my $herectx = $here . "\n"; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3684 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3685 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3686 | for (my $n = 0; $n < $cnt; $n++) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3687 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3688 | } |
| 3689 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3690 | WARN("BRACES", |
| 3691 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3692 | } |
| 3693 | } |
| 3694 | |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3695 | # check for unnecessary blank lines around braces |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 3696 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3697 | CHK("BRACES", |
| 3698 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); |
| 3699 | } |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 3700 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3701 | CHK("BRACES", |
| 3702 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); |
| 3703 | } |
| 3704 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3705 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3706 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 3707 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3708 | WARN("VOLATILE", |
| 3709 | "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] | 3710 | } |
| 3711 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3712 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3713 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3714 | CHK("REDUNDANT_CODE", |
| 3715 | "if this code is redundant consider removing it\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3716 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Andy Whitcroft | 03df4b5 | 2012-12-17 16:01:52 -0800 | [diff] [blame] | 3719 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 3720 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
| 3721 | my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; |
| 3722 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { |
| 3723 | WARN('NEEDLESS_IF', |
| 3724 | "$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] | 3725 | } |
| 3726 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3727 | |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 3728 | sub string_find_replace { |
| 3729 | my ($string, $find, $replace) = @_; |
| 3730 | |
| 3731 | $string =~ s/$find/$replace/g; |
| 3732 | |
| 3733 | return $string; |
| 3734 | } |
| 3735 | |
| 3736 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 3737 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 3738 | my $attr = $1; |
| 3739 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 3740 | my $ptr = $1; |
| 3741 | my $var = $2; |
| 3742 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 3743 | ERROR("MISPLACED_INIT", |
| 3744 | "$attr should be placed after $var\n" . $herecurr)) || |
| 3745 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 3746 | WARN("MISPLACED_INIT", |
| 3747 | "$attr should be placed after $var\n" . $herecurr))) && |
| 3748 | $fix) { |
| 3749 | $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; |
| 3750 | } |
| 3751 | } |
| 3752 | } |
| 3753 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3754 | # prefer usleep_range over udelay |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3755 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3756 | # ignore udelay's < 10, however |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3757 | if (! ($1 < 10) ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3758 | CHK("USLEEP_RANGE", |
| 3759 | "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] | 3760 | } |
| 3761 | } |
| 3762 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3763 | # warn about unexpectedly long msleep's |
| 3764 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 3765 | if ($1 < 20) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3766 | WARN("MSLEEP", |
| 3767 | "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] | 3768 | } |
| 3769 | } |
| 3770 | |
Joe Perches | 36ec193 | 2013-07-03 15:05:25 -0700 | [diff] [blame] | 3771 | # check for comparisons of jiffies |
| 3772 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 3773 | WARN("JIFFIES_COMPARISON", |
| 3774 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 3775 | } |
| 3776 | |
Joe Perches | 9d7a34a | 2013-07-03 15:05:26 -0700 | [diff] [blame] | 3777 | # check for comparisons of get_jiffies_64() |
| 3778 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 3779 | WARN("JIFFIES_COMPARISON", |
| 3780 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 3781 | } |
| 3782 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3783 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3784 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3785 | # print "#ifdef in C files should be avoided\n"; |
| 3786 | # print "$herecurr"; |
| 3787 | # $clean = 0; |
| 3788 | # } |
| 3789 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3790 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3791 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3792 | if (ERROR("SPACING", |
| 3793 | "exactly one space required after that #$1\n" . $herecurr) && |
| 3794 | $fix) { |
| 3795 | $fixed[$linenr - 1] =~ |
| 3796 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 3797 | } |
| 3798 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3801 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3802 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 3803 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3804 | my $which = $1; |
| 3805 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3806 | CHK("UNCOMMENTED_DEFINITION", |
| 3807 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | # check for memory barriers without a comment. |
| 3811 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 3812 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3813 | CHK("MEMORY_BARRIER", |
| 3814 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3815 | } |
| 3816 | } |
| 3817 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3818 | 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] | 3819 | CHK("ARCH_DEFINES", |
| 3820 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3821 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3822 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3823 | # Check that the storage class is at the beginning of a declaration |
| 3824 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3825 | WARN("STORAGE_CLASS", |
| 3826 | "storage class should be at the beginning of the declaration\n" . $herecurr) |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3829 | # check the location of the inline attribute, that it is between |
| 3830 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3831 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 3832 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3833 | ERROR("INLINE_LOCATION", |
| 3834 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3835 | } |
| 3836 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3837 | # Check for __inline__ and __inline, prefer inline |
| 3838 | if ($line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3839 | if (WARN("INLINE", |
| 3840 | "plain inline is preferred over $1\n" . $herecurr) && |
| 3841 | $fix) { |
| 3842 | $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/; |
| 3843 | |
| 3844 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3845 | } |
| 3846 | |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3847 | # Check for __attribute__ packed, prefer __packed |
| 3848 | if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3849 | WARN("PREFER_PACKED", |
| 3850 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3851 | } |
| 3852 | |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3853 | # Check for __attribute__ aligned, prefer __aligned |
| 3854 | if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3855 | WARN("PREFER_ALIGNED", |
| 3856 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3857 | } |
| 3858 | |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 3859 | # Check for __attribute__ format(printf, prefer __printf |
| 3860 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3861 | if (WARN("PREFER_PRINTF", |
| 3862 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && |
| 3863 | $fix) { |
| 3864 | $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; |
| 3865 | |
| 3866 | } |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 3867 | } |
| 3868 | |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3869 | # Check for __attribute__ format(scanf, prefer __scanf |
| 3870 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3871 | if (WARN("PREFER_SCANF", |
| 3872 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && |
| 3873 | $fix) { |
| 3874 | $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; |
| 3875 | } |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3876 | } |
| 3877 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3878 | # check for sizeof(&) |
| 3879 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3880 | WARN("SIZEOF_ADDRESS", |
| 3881 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3882 | } |
| 3883 | |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 3884 | # check for sizeof without parenthesis |
| 3885 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3886 | if (WARN("SIZEOF_PARENTHESIS", |
| 3887 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 3888 | $fix) { |
| 3889 | $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; |
| 3890 | } |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 3891 | } |
| 3892 | |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3893 | # check for line continuations in quoted strings with odd counts of " |
| 3894 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3895 | WARN("LINE_CONTINUATIONS", |
| 3896 | "Avoid line continuations in quoted strings\n" . $herecurr); |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3897 | } |
| 3898 | |
Joe Perches | 88982fe | 2012-12-17 16:02:00 -0800 | [diff] [blame] | 3899 | # check for struct spinlock declarations |
| 3900 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 3901 | WARN("USE_SPINLOCK_T", |
| 3902 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 3903 | } |
| 3904 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 3905 | # check for seq_printf uses that could be seq_puts |
| 3906 | if ($line =~ /\bseq_printf\s*\(/) { |
| 3907 | my $fmt = get_quoted_string($line, $rawline); |
| 3908 | if ($fmt !~ /[^\\]\%/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3909 | if (WARN("PREFER_SEQ_PUTS", |
| 3910 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 3911 | $fix) { |
| 3912 | $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/; |
| 3913 | } |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3917 | # Check for misused memsets |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3918 | if ($^V && $^V ge 5.10.0 && |
| 3919 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3920 | $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] | 3921 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3922 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3923 | my $ms_val = $7; |
| 3924 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3925 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3926 | if ($ms_size =~ /^(0x|)0$/i) { |
| 3927 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3928 | "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] | 3929 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 3930 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3931 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 3932 | } |
| 3933 | } |
| 3934 | |
| 3935 | # typecasts on min/max could be min_t/max_t |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3936 | if ($^V && $^V ge 5.10.0 && |
| 3937 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3938 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3939 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3940 | my $call = $1; |
| 3941 | my $cast1 = deparenthesize($2); |
| 3942 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3943 | my $cast2 = deparenthesize($7); |
| 3944 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3945 | my $cast; |
| 3946 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3947 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3948 | $cast = "$cast1 or $cast2"; |
| 3949 | } elsif ($cast1 ne "") { |
| 3950 | $cast = $cast1; |
| 3951 | } else { |
| 3952 | $cast = $cast2; |
| 3953 | } |
| 3954 | WARN("MINMAX", |
| 3955 | "$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] | 3956 | } |
| 3957 | } |
| 3958 | |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 3959 | # check usleep_range arguments |
| 3960 | if ($^V && $^V ge 5.10.0 && |
| 3961 | defined $stat && |
| 3962 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 3963 | my $min = $1; |
| 3964 | my $max = $7; |
| 3965 | if ($min eq $max) { |
| 3966 | WARN("USLEEP_RANGE", |
| 3967 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 3968 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 3969 | $min > $max) { |
| 3970 | WARN("USLEEP_RANGE", |
| 3971 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 3972 | } |
| 3973 | } |
| 3974 | |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 3975 | # check for new externs in .h files. |
| 3976 | if ($realfile =~ /\.h$/ && |
| 3977 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
| 3978 | if (WARN("AVOID_EXTERNS", |
| 3979 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
| 3980 | $fix) { |
| 3981 | $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
| 3982 | } |
| 3983 | } |
| 3984 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3985 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3986 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3987 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3988 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3989 | my $function_name = $1; |
| 3990 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3991 | |
| 3992 | my $s = $stat; |
| 3993 | if (defined $cond) { |
| 3994 | substr($s, 0, length($cond), ''); |
| 3995 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3996 | if ($s =~ /^\s*;/ && |
| 3997 | $function_name ne 'uninitialized_var') |
| 3998 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3999 | WARN("AVOID_EXTERNS", |
| 4000 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4004 | WARN("FUNCTION_ARGUMENTS", |
| 4005 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4006 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4007 | |
| 4008 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 4009 | $stat =~ /^.\s*extern\s+/) |
| 4010 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4011 | WARN("AVOID_EXTERNS", |
| 4012 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | # checks for new __setup's |
| 4016 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 4017 | my $name = $1; |
| 4018 | |
| 4019 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4020 | CHK("UNDOCUMENTED_SETUP", |
| 4021 | "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4022 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4023 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4024 | |
| 4025 | # check for pointless casting of kmalloc return |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4026 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4027 | WARN("UNNECESSARY_CASTS", |
| 4028 | "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] | 4029 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4030 | |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 4031 | # alloc style |
| 4032 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
| 4033 | if ($^V && $^V ge 5.10.0 && |
| 4034 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
| 4035 | CHK("ALLOC_SIZEOF_STRUCT", |
| 4036 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 4037 | } |
| 4038 | |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 4039 | # check for krealloc arg reuse |
| 4040 | if ($^V && $^V ge 5.10.0 && |
| 4041 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { |
| 4042 | WARN("KREALLOC_ARG_REUSE", |
| 4043 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 4044 | } |
| 4045 | |
Joe Perches | 5ce59ae | 2013-02-21 16:44:18 -0800 | [diff] [blame] | 4046 | # check for alloc argument mismatch |
| 4047 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 4048 | WARN("ALLOC_ARRAY_ARGS", |
| 4049 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 4050 | } |
| 4051 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4052 | # check for multiple semicolons |
| 4053 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4054 | if (WARN("ONE_SEMICOLON", |
| 4055 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 4056 | $fix) { |
| 4057 | $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g; |
| 4058 | } |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | # check for switch/default statements without a break; |
| 4062 | if ($^V && $^V ge 5.10.0 && |
| 4063 | defined $stat && |
| 4064 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
| 4065 | my $ctx = ''; |
| 4066 | my $herectx = $here . "\n"; |
| 4067 | my $cnt = statement_rawlines($stat); |
| 4068 | for (my $n = 0; $n < $cnt; $n++) { |
| 4069 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4070 | } |
| 4071 | WARN("DEFAULT_NO_BREAK", |
| 4072 | "switch default: should use break\n" . $herectx); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4073 | } |
| 4074 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4075 | # check for gcc specific __FUNCTION__ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4076 | if ($line =~ /\b__FUNCTION__\b/) { |
| 4077 | if (WARN("USE_FUNC", |
| 4078 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 4079 | $fix) { |
| 4080 | $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g; |
| 4081 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4082 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4083 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4084 | # check for use of yield() |
| 4085 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 4086 | WARN("YIELD", |
| 4087 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 4088 | } |
| 4089 | |
Joe Perches | 179f8f4 | 2013-07-03 15:05:30 -0700 | [diff] [blame] | 4090 | # check for comparisons against true and false |
| 4091 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 4092 | my $lead = $1; |
| 4093 | my $arg = $2; |
| 4094 | my $test = $3; |
| 4095 | my $otype = $4; |
| 4096 | my $trail = $5; |
| 4097 | my $op = "!"; |
| 4098 | |
| 4099 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 4100 | |
| 4101 | my $type = lc($otype); |
| 4102 | if ($type =~ /^(?:true|false)$/) { |
| 4103 | if (("$test" eq "==" && "$type" eq "true") || |
| 4104 | ("$test" eq "!=" && "$type" eq "false")) { |
| 4105 | $op = ""; |
| 4106 | } |
| 4107 | |
| 4108 | CHK("BOOL_COMPARISON", |
| 4109 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 4110 | |
| 4111 | ## maybe suggesting a correct construct would better |
| 4112 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 4113 | |
| 4114 | } |
| 4115 | } |
| 4116 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 4117 | # check for semaphores initialized locked |
| 4118 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4119 | WARN("CONSIDER_COMPLETION", |
| 4120 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4121 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4122 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 4123 | # recommend kstrto* over simple_strto* and strict_strto* |
| 4124 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4125 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 4126 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4127 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4128 | |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 4129 | # check for __initcall(), use device_initcall() explicitly please |
| 4130 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4131 | WARN("USE_DEVICE_INITCALL", |
| 4132 | "please use device_initcall() instead of __initcall()\n" . $herecurr); |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 4133 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4134 | |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 4135 | # check for various ops structs, ensure they are const. |
| 4136 | my $struct_ops = qr{acpi_dock_ops| |
| 4137 | address_space_operations| |
| 4138 | backlight_ops| |
| 4139 | block_device_operations| |
| 4140 | dentry_operations| |
| 4141 | dev_pm_ops| |
| 4142 | dma_map_ops| |
| 4143 | extent_io_ops| |
| 4144 | file_lock_operations| |
| 4145 | file_operations| |
| 4146 | hv_ops| |
| 4147 | ide_dma_ops| |
| 4148 | intel_dvo_dev_ops| |
| 4149 | item_operations| |
| 4150 | iwl_ops| |
| 4151 | kgdb_arch| |
| 4152 | kgdb_io| |
| 4153 | kset_uevent_ops| |
| 4154 | lock_manager_operations| |
| 4155 | microcode_ops| |
| 4156 | mtrr_ops| |
| 4157 | neigh_ops| |
| 4158 | nlmsvc_binding| |
| 4159 | pci_raw_ops| |
| 4160 | pipe_buf_operations| |
| 4161 | platform_hibernation_ops| |
| 4162 | platform_suspend_ops| |
| 4163 | proto_ops| |
| 4164 | rpc_pipe_ops| |
| 4165 | seq_operations| |
| 4166 | snd_ac97_build_ops| |
| 4167 | soc_pcmcia_socket_ops| |
| 4168 | stacktrace_ops| |
| 4169 | sysfs_ops| |
| 4170 | tty_operations| |
| 4171 | usb_mon_operations| |
| 4172 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 4173 | if ($line !~ /\bconst\b/ && |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 4174 | $line =~ /\bstruct\s+($struct_ops)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4175 | WARN("CONST_STRUCT", |
| 4176 | "struct $1 should normally be const\n" . |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 4177 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 4178 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4179 | |
| 4180 | # use of NR_CPUS is usually wrong |
| 4181 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 4182 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4183 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 4184 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4185 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 4186 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 4187 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4188 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4189 | WARN("NR_CPUS", |
| 4190 | "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] | 4191 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4192 | |
| 4193 | # check for %L{u,d,i} in strings |
| 4194 | my $string; |
| 4195 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 4196 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Andy Whitcroft | 2a1bc5d | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 4197 | $string =~ s/%%/__/g; |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4198 | if ($string =~ /(?<!%)%L[udi]/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4199 | WARN("PRINTF_L", |
| 4200 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4201 | last; |
| 4202 | } |
| 4203 | } |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 4204 | |
| 4205 | # whine mightly about in_atomic |
| 4206 | if ($line =~ /\bin_atomic\s*\(/) { |
| 4207 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4208 | ERROR("IN_ATOMIC", |
| 4209 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 4210 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4211 | WARN("IN_ATOMIC", |
| 4212 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 4213 | } |
| 4214 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4215 | |
| 4216 | # check for lockdep_set_novalidate_class |
| 4217 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 4218 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 4219 | if ($realfile !~ m@^kernel/lockdep@ && |
| 4220 | $realfile !~ m@^include/linux/lockdep@ && |
| 4221 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4222 | ERROR("LOCKDEP", |
| 4223 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4224 | } |
| 4225 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 4226 | |
| 4227 | if ($line =~ /debugfs_create_file.*S_IWUGO/ || |
| 4228 | $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4229 | WARN("EXPORTED_WORLD_WRITABLE", |
| 4230 | "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] | 4231 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | # If we have no input at all, then there is nothing to report on |
| 4235 | # so just keep quiet. |
| 4236 | if ($#rawlines == -1) { |
| 4237 | exit(0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4240 | # In mailback mode only produce a report in the negative, for |
| 4241 | # things that appear to be patches. |
| 4242 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 4243 | exit(0); |
| 4244 | } |
| 4245 | |
| 4246 | # This is not a patch, and we are are in 'no-patch' mode so |
| 4247 | # just keep quiet. |
| 4248 | if (!$chk_patch && !$is_patch) { |
| 4249 | exit(0); |
| 4250 | } |
| 4251 | |
| 4252 | if (!$is_patch) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4253 | ERROR("NOT_UNIFIED_DIFF", |
| 4254 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4255 | } |
| 4256 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4257 | ERROR("MISSING_SIGN_OFF", |
| 4258 | "Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4259 | } |
| 4260 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4261 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4262 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 4263 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4264 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 4265 | (($check)? "$cnt_chk checks, " : "") . |
| 4266 | "$cnt_lines lines checked\n"; |
| 4267 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4268 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4269 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4270 | if ($quiet == 0) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4271 | |
| 4272 | if ($^V lt 5.10.0) { |
| 4273 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); |
| 4274 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); |
| 4275 | } |
| 4276 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4277 | # If there were whitespace errors which cleanpatch can fix |
| 4278 | # then suggest that. |
| 4279 | if ($rpt_cleaners) { |
| 4280 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; |
| 4281 | print " scripts/cleanfile\n\n"; |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 4282 | $rpt_cleaners = 0; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4283 | } |
| 4284 | } |
| 4285 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 4286 | hash_show_words(\%use_type, "Used"); |
| 4287 | hash_show_words(\%ignore_type, "Ignored"); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4288 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4289 | if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { |
| 4290 | my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; |
| 4291 | my $linecount = 0; |
| 4292 | my $f; |
| 4293 | |
| 4294 | open($f, '>', $newfile) |
| 4295 | or die "$P: Can't open $newfile for write\n"; |
| 4296 | foreach my $fixed_line (@fixed) { |
| 4297 | $linecount++; |
| 4298 | if ($file) { |
| 4299 | if ($linecount > 3) { |
| 4300 | $fixed_line =~ s/^\+//; |
| 4301 | print $f $fixed_line. "\n"; |
| 4302 | } |
| 4303 | } else { |
| 4304 | print $f $fixed_line . "\n"; |
| 4305 | } |
| 4306 | } |
| 4307 | close($f); |
| 4308 | |
| 4309 | if (!$quiet) { |
| 4310 | print << "EOM"; |
| 4311 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 4312 | |
| 4313 | Do _NOT_ trust the results written to this file. |
| 4314 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 4315 | |
| 4316 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 4317 | No warranties, expressed or implied... |
| 4318 | |
| 4319 | EOM |
| 4320 | } |
| 4321 | } |
| 4322 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4323 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4324 | 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] | 4325 | } |
| 4326 | if ($clean == 0 && $quiet == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4327 | print << "EOM"; |
| 4328 | $vname has style problems, please review. |
| 4329 | |
| 4330 | If any of these errors are false positives, please report |
| 4331 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 4332 | EOM |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4333 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4334 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4335 | return $clean; |
| 4336 | } |