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