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