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 |
Michael Bohan | 01020ea | 2012-10-03 17:17:04 -0700 | [diff] [blame] | 1632 | $exec_file =~ /^.+\.dts$/ or |
| 1633 | $exec_file =~ /^.+\.dtsi$/ or |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1634 | $exec_file =~ /^.+defconfig$/ or |
| 1635 | $exec_file =~ /^Makefile$/ or |
| 1636 | $exec_file =~ /^Kconfig$/) && |
| 1637 | $rawline =~ /^new (file )?mode\s([0-9]+)$/ && |
| 1638 | (oct($2) & 0111)) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1639 | ERROR("EXECUTE_PERMISSIONS", |
| 1640 | "Source file has +x permissions: " . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1641 | "$exec_file\n"); |
| 1642 | } |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 1643 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1644 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1645 | my $hereline = "$here\n$rawline\n"; |
| 1646 | my $herecurr = "$here\n$rawline\n"; |
| 1647 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1648 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1649 | if ($shorttext != AFTER_SHORTTEXT) { |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1650 | if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) { |
| 1651 | # the subject line was just processed, |
| 1652 | # a blank line must be next |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1653 | WARN("NONBLANK_AFTER_SUMMARY", |
| 1654 | "non-blank line after summary line\n" . $herecurr); |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1655 | $shorttext = IN_SHORTTEXT; |
| 1656 | # this non-blank line may or may not be commit text - |
| 1657 | # a warning has been generated so assume it is commit |
| 1658 | # text and move on |
| 1659 | $commit_text_present = 1; |
| 1660 | # fall through and treat this line as IN_SHORTTEXT |
| 1661 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1662 | if ($shorttext == IN_SHORTTEXT) { |
| 1663 | if ($line=~/^---/ || $line=~/^diff.*/) { |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1664 | if ($commit_text_present == 0) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1665 | WARN("NO_COMMIT_TEXT", |
| 1666 | "please add commit text explaining " . |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1667 | "*why* the change is needed\n" . |
| 1668 | $herecurr); |
| 1669 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1670 | $shorttext = AFTER_SHORTTEXT; |
| 1671 | } elsif (length($line) > (SHORTTEXT_LIMIT + |
| 1672 | $shorttext_exspc) |
| 1673 | && $line !~ /^:([0-7]{6}\s){2} |
| 1674 | ([[:xdigit:]]+\.* |
| 1675 | \s){2}\w+\s\w+/xms) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1676 | WARN("LONG_COMMIT_TEXT", |
| 1677 | "commit text line over " . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1678 | SHORTTEXT_LIMIT . |
| 1679 | " characters\n" . $herecurr); |
Steve Muckle | 336a3ae | 2012-03-09 11:15:24 -0800 | [diff] [blame] | 1680 | } elsif ($line=~/^\s*change-id:/i || |
| 1681 | $line=~/^\s*signed-off-by:/i || |
| 1682 | $line=~/^\s*crs-fixed:/i || |
| 1683 | $line=~/^\s*acked-by:/i) { |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1684 | # this is a tag, there must be commit |
| 1685 | # text by now |
| 1686 | if ($commit_text_present == 0) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1687 | WARN("NO_COMMIT_TEXT", |
| 1688 | "please add commit text explaining " . |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1689 | "*why* the change is needed\n" . |
| 1690 | $herecurr); |
| 1691 | # prevent duplicate warnings |
| 1692 | $commit_text_present = 1; |
| 1693 | } |
| 1694 | } elsif ($line=~/\S/) { |
| 1695 | $commit_text_present = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1696 | } |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1697 | } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) { |
| 1698 | # case of non-blank line in this state handled above |
| 1699 | $shorttext = IN_SHORTTEXT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1700 | } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) { |
| 1701 | # The Subject line doesn't have to be the last header in the patch. |
| 1702 | # Avoid moving to the IN_SHORTTEXT state until clear of all headers. |
| 1703 | # Per RFC5322, continuation lines must be folded, so any left-justified |
| 1704 | # text which looks like a header is definitely a header. |
| 1705 | if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) { |
| 1706 | $shorttext = IN_SHORTTEXT; |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1707 | # Check for Subject line followed by a blank line. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1708 | if (length($line) != 0) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1709 | WARN("NONBLANK_AFTER_SUMMARY", |
| 1710 | "non-blank line after " . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1711 | "summary line\n" . |
| 1712 | $sublinenr . $here . |
| 1713 | "\n" . $subjectline . |
| 1714 | "\n" . $line . "\n"); |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1715 | # this non-blank line may or may not |
| 1716 | # be commit text - a warning has been |
| 1717 | # generated so assume it is commit |
| 1718 | # text and move on |
| 1719 | $commit_text_present = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1720 | } |
| 1721 | } |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1722 | # The next two cases are BEFORE_SHORTTEXT. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1723 | } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) { |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1724 | # This is the subject line. Go to |
| 1725 | # CHECK_NEXT_SHORTTEXT to wait for the commit |
| 1726 | # text to show up. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1727 | $shorttext = CHECK_NEXT_SHORTTEXT; |
| 1728 | $subjectline = $line; |
| 1729 | $sublinenr = "#$linenr & "; |
| 1730 | # Check for Subject line less than line limit |
Matt Wagantall | 3c09aeb | 2012-08-18 11:21:00 -0700 | [diff] [blame] | 1731 | if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1732 | WARN("LONG_SUMMARY_LINE", |
| 1733 | "summary line over " . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1734 | SHORTTEXT_LIMIT . |
| 1735 | " characters\n" . $herecurr); |
| 1736 | } |
| 1737 | } elsif ($line=~/^ (.*)/) { |
Steve Muckle | d3b3b64 | 2012-03-05 10:12:04 -0800 | [diff] [blame] | 1738 | # Indented format, this must be the summary |
| 1739 | # line (i.e. git show). There will be no more |
| 1740 | # headers so we are now in the shorttext. |
| 1741 | $shorttext = IN_SHORTTEXT_BLANKLINE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1742 | $shorttext_exspc = 4; |
Matt Wagantall | 3c09aeb | 2012-08-18 11:21:00 -0700 | [diff] [blame] | 1743 | if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1744 | WARN("LONG_SUMMARY_LINE", |
| 1745 | "summary line over " . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1746 | SHORTTEXT_LIMIT . |
| 1747 | " characters\n" . $herecurr); |
| 1748 | } |
| 1749 | } |
| 1750 | } |
| 1751 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1752 | $cnt_lines++ if ($realcnt != 0); |
| 1753 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1754 | # Check for incorrect file permissions |
| 1755 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 1756 | my $permhere = $here . "FILE: $realfile\n"; |
| 1757 | if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1758 | ERROR("EXECUTE_PERMISSIONS", |
| 1759 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1763 | # Check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1764 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1765 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1766 | $in_commit_log = 0; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1770 | if (!$in_header_lines && |
| 1771 | $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1772 | my $space_before = $1; |
| 1773 | my $sign_off = $2; |
| 1774 | my $space_after = $3; |
| 1775 | my $email = $4; |
| 1776 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 1777 | |
| 1778 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1779 | WARN("BAD_SIGN_OFF", |
| 1780 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1781 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1782 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1783 | WARN("BAD_SIGN_OFF", |
| 1784 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1785 | } |
| 1786 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1787 | WARN("BAD_SIGN_OFF", |
| 1788 | "Use a single space after $ucfirst_sign_off\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 1792 | my $suggested_email = format_email(($email_name, $email_address)); |
| 1793 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1794 | ERROR("BAD_SIGN_OFF", |
| 1795 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1796 | } else { |
| 1797 | my $dequoted = $suggested_email; |
| 1798 | $dequoted =~ s/^"//; |
| 1799 | $dequoted =~ s/" </ </; |
| 1800 | # Don't force email to have quotes |
| 1801 | # Allow just an angle bracketed address |
| 1802 | if ("$dequoted$comment" ne $email && |
| 1803 | "<$email_address>$comment" ne $email && |
| 1804 | "$suggested_email$comment" ne $email) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1805 | WARN("BAD_SIGN_OFF", |
| 1806 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1807 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1808 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1809 | if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1810 | WARN("BAD_SIGN_OFF", |
| 1811 | "invalid Signed-off-by identity\n" . $line ); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | #check the patch for invalid author credentials |
| 1816 | if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 1817 | WARN("BAD_AUTHOR", "invalid author identity\n" . $line ); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1820 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1821 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1822 | ERROR("CORRUPTED_PATCH", |
| 1823 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1824 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1827 | # Check for absolute kernel paths. |
| 1828 | if ($tree) { |
| 1829 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 1830 | my $file = $1; |
| 1831 | |
| 1832 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 1833 | check_absolute_file($1, $herecurr)) { |
| 1834 | # |
| 1835 | } else { |
| 1836 | check_absolute_file($file, $herecurr); |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1841 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 1842 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1843 | $rawline !~ m/^$UTF8*$/) { |
| 1844 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 1845 | |
| 1846 | my $blank = copy_spacing($rawline); |
| 1847 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 1848 | my $hereptr = "$hereline$ptr\n"; |
| 1849 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 1850 | CHK("INVALID_UTF8", |
| 1851 | "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] | 1852 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1853 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1854 | # Check if it's the start of a commit log |
| 1855 | # (not a header line and we haven't seen the patch filename) |
| 1856 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1857 | $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1858 | $in_header_lines = 0; |
| 1859 | $in_commit_log = 1; |
| 1860 | } |
| 1861 | |
| 1862 | # Still not yet in a patch, check for any UTF-8 |
| 1863 | if ($in_commit_log && $realfile =~ /^$/ && |
| 1864 | $rawline =~ /$NON_ASCII_UTF8/) { |
| 1865 | CHK("UTF8_BEFORE_PATCH", |
| 1866 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 1867 | } |
| 1868 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1869 | # ignore non-hunk lines and lines being removed |
| 1870 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1871 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1872 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1873 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1874 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1875 | ERROR("DOS_LINE_ENDINGS", |
| 1876 | "DOS line endings\n" . $herevet); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1877 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1878 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1879 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1880 | ERROR("TRAILING_WHITESPACE", |
| 1881 | "trailing whitespace\n" . $herevet); |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1882 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1883 | } |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1884 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1885 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1886 | # Only applies when adding the entry originally, after that we do not have |
| 1887 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1888 | if ($realfile =~ /Kconfig/ && |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1889 | $line =~ /.\s*config\s+/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1890 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1891 | my $cnt = $realcnt; |
| 1892 | my $ln = $linenr + 1; |
| 1893 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1894 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1895 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1896 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1897 | $f = $lines[$ln - 1]; |
| 1898 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 1899 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1900 | |
| 1901 | next if ($f =~ /^-/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1902 | |
| 1903 | if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { |
| 1904 | $is_start = 1; |
| 1905 | } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { |
| 1906 | $length = -1; |
| 1907 | } |
| 1908 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1909 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1910 | $f =~ s/#.*//; |
| 1911 | $f =~ s/^\s+//; |
| 1912 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1913 | if ($f =~ /^\s*config\s/) { |
| 1914 | $is_end = 1; |
| 1915 | last; |
| 1916 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1917 | $length++; |
| 1918 | } |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1919 | WARN("CONFIG_DESCRIPTION", |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1920 | "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); |
| 1921 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 1924 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 1925 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 1926 | my $flag = $1; |
| 1927 | my $replacement = { |
| 1928 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 1929 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 1930 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 1931 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 1932 | }; |
| 1933 | |
| 1934 | WARN("DEPRECATED_VARIABLE", |
| 1935 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 1936 | } |
| 1937 | |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1938 | # check we are in a valid source file if not then ignore this hunk |
| 1939 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
| 1940 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1941 | #80 column limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1942 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1943 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
Joe Perches | 0fccc62 | 2011-05-24 17:13:41 -0700 | [diff] [blame] | 1944 | !($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] | 1945 | $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1946 | $realfile ne "scripts/checkpatch.pl" && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1947 | $length > 80) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1948 | { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1949 | WARN("LONG_LINE", |
| 1950 | "line over 80 characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1951 | } |
| 1952 | |
Josh Triplett | ca56dc0 | 2012-03-23 15:02:21 -0700 | [diff] [blame] | 1953 | # Check for user-visible strings broken across lines, which breaks the ability |
| 1954 | # to grep for the string. Limited to strings used as parameters (those |
| 1955 | # following an open parenthesis), which almost completely eliminates false |
| 1956 | # positives, as well as warning only once per parameter rather than once per |
| 1957 | # line of the string. Make an exception when the previous string ends in a |
| 1958 | # newline (multiple lines in one string constant) or \n\t (common in inline |
| 1959 | # assembly to indent the instruction on the following line). |
| 1960 | if ($line =~ /^\+\s*"/ && |
| 1961 | $prevline =~ /"\s*$/ && |
| 1962 | $prevline =~ /\(/ && |
| 1963 | $prevrawline !~ /\\n(?:\\t)*"\s*$/) { |
| 1964 | WARN("SPLIT_STRING", |
| 1965 | "quoted string split across lines\n" . $hereprev); |
| 1966 | } |
| 1967 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 1968 | # check for spaces before a quoted newline |
| 1969 | if ($rawline =~ /^.*\".*\s\\n/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1970 | WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 1971 | "unnecessary whitespace before a quoted newline\n" . $herecurr); |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1974 | # check for adding lines without a newline. |
| 1975 | 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] | 1976 | WARN("MISSING_EOF_NEWLINE", |
| 1977 | "adding a line without newline at end of file\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1980 | # Blackfin: use hi/lo macros |
| 1981 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 1982 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 1983 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1984 | ERROR("LO_MACRO", |
| 1985 | "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1986 | } |
| 1987 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 1988 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1989 | ERROR("HI_MACRO", |
| 1990 | "use the HI() macro, not (... >> 16)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 1994 | # check we are in a valid source file C or perl if not then ignore this hunk |
| 1995 | next if ($realfile !~ /\.(h|c|pl)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1996 | |
| 1997 | # at the beginning of a line any tabs must come first and anything |
| 1998 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1999 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 2000 | $rawline =~ /^\+\s* \s*/) { |
| 2001 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2002 | ERROR("CODE_INDENT", |
| 2003 | "code indent should use tabs where possible\n" . $herevet); |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2004 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2007 | # check for space before tabs. |
| 2008 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 2009 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2010 | WARN("SPACE_BEFORE_TAB", |
| 2011 | "please, no space before tabs\n" . $herevet); |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2012 | } |
| 2013 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2014 | # check for && or || at the start of a line |
| 2015 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 2016 | CHK("LOGICAL_CONTINUATIONS", |
| 2017 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 2018 | } |
| 2019 | |
| 2020 | # check multi-line statement indentation matches previous line |
| 2021 | if ($^V && $^V ge 5.10.0 && |
| 2022 | $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { |
| 2023 | $prevline =~ /^\+(\t*)(.*)$/; |
| 2024 | my $oldindent = $1; |
| 2025 | my $rest = $2; |
| 2026 | |
| 2027 | my $pos = pos_last_openparen($rest); |
| 2028 | if ($pos >= 0) { |
| 2029 | $line =~ /^\+([ \t]*)/; |
| 2030 | my $newindent = $1; |
| 2031 | |
| 2032 | my $goodtabindent = $oldindent . |
| 2033 | "\t" x ($pos / 8) . |
| 2034 | " " x ($pos % 8); |
| 2035 | my $goodspaceindent = $oldindent . " " x $pos; |
| 2036 | |
| 2037 | if ($newindent ne $goodtabindent && |
| 2038 | $newindent ne $goodspaceindent) { |
| 2039 | CHK("PARENTHESIS_ALIGNMENT", |
| 2040 | "Alignment should match open parenthesis\n" . $hereprev); |
| 2041 | } |
| 2042 | } |
| 2043 | } |
| 2044 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 2045 | if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) { |
| 2046 | CHK("SPACING", |
| 2047 | "No space is necessary after a cast\n" . $hereprev); |
| 2048 | } |
| 2049 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2050 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 2051 | # Exceptions: |
| 2052 | # 1) within comments |
| 2053 | # 2) indented preprocessor commands |
| 2054 | # 3) hanging labels |
| 2055 | if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2056 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2057 | WARN("LEADING_SPACE", |
| 2058 | "please, no spaces at the start of a line\n" . $herevet); |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2061 | # check we are in a valid C source file if not then ignore this hunk |
| 2062 | next if ($realfile !~ /\.(h|c)$/); |
| 2063 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2064 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2065 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2066 | WARN("CVS_KEYWORD", |
| 2067 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2068 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2069 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2070 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 2071 | if ($line =~ /__builtin_bfin_csync/) { |
| 2072 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2073 | ERROR("CSYNC", |
| 2074 | "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2075 | } |
| 2076 | if ($line =~ /__builtin_bfin_ssync/) { |
| 2077 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2078 | ERROR("SSYNC", |
| 2079 | "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2080 | } |
| 2081 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2082 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2083 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 2084 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2085 | #print "LINE<$line>\n"; |
| 2086 | if ($linenr >= $suppress_statement && |
| 2087 | $realcnt && $line =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2088 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2089 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2090 | $stat =~ s/\n./\n /g; |
| 2091 | $cond =~ s/\n./\n /g; |
| 2092 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2093 | #print "linenr<$linenr> <$stat>\n"; |
| 2094 | # If this statement has no statement boundaries within |
| 2095 | # it there is no point in retrying a statement scan |
| 2096 | # until we hit end of it. |
| 2097 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 2098 | if ($frag !~ /(?:{|;)/) { |
| 2099 | #print "skip<$line_nr_next>\n"; |
| 2100 | $suppress_statement = $line_nr_next; |
| 2101 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 2102 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2103 | # Find the real next line. |
| 2104 | $realline_next = $line_nr_next; |
| 2105 | if (defined $realline_next && |
| 2106 | (!defined $lines[$realline_next - 1] || |
| 2107 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 2108 | $realline_next++; |
| 2109 | } |
| 2110 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2111 | my $s = $stat; |
| 2112 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2113 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2114 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2115 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2116 | |
| 2117 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2118 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2119 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 2120 | } elsif ($s =~ /^.\s*else\b/s) { |
| 2121 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2122 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2123 | } 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] | 2124 | my $type = $1; |
| 2125 | $type =~ s/\s+/ /g; |
| 2126 | possible($type, "A:" . $s); |
| 2127 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2128 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a8406 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2129 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2130 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2131 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2132 | |
| 2133 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2134 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2135 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2136 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2137 | |
| 2138 | # Check for any sort of function declaration. |
| 2139 | # int foo(something bar, other baz); |
| 2140 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2141 | 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] | 2142 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2143 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2144 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2145 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2146 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2147 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2148 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2149 | 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] | 2150 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2151 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2158 | # |
| 2159 | # Checks which may be anchored in the context. |
| 2160 | # |
| 2161 | |
| 2162 | # Check for switch () and associated case and default |
| 2163 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2164 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 2165 | my $err = ''; |
| 2166 | my $sep = ''; |
| 2167 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 2168 | shift(@ctx); |
| 2169 | for my $ctx (@ctx) { |
| 2170 | my ($clen, $cindent) = line_stats($ctx); |
| 2171 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 2172 | $indent != $cindent) { |
| 2173 | $err .= "$sep$ctx\n"; |
| 2174 | $sep = ''; |
| 2175 | } else { |
| 2176 | $sep = "[...]\n"; |
| 2177 | } |
| 2178 | } |
| 2179 | if ($err ne '') { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2180 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 2181 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 2186 | # or if that brace on the next line is for something else |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2187 | 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] | 2188 | my $pre_ctx = "$1$2"; |
| 2189 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2190 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 2191 | |
| 2192 | if ($line =~ /^\+\t{6,}/) { |
| 2193 | WARN("DEEP_INDENTATION", |
| 2194 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 2195 | } |
| 2196 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2197 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 2198 | my $ctx = join("\n", @ctx); |
| 2199 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2200 | my $ctx_ln = $linenr; |
| 2201 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2202 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2203 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 2204 | defined $lines[$ctx_ln - 1] && |
| 2205 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 2206 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 2207 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2208 | $ctx_ln++; |
| 2209 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2210 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2211 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 2212 | #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] | 2213 | |
| 2214 | 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] | 2215 | ERROR("OPEN_BRACE", |
| 2216 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2217 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2218 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2219 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 2220 | $ctx =~ /\)\s*\;\s*$/ && |
| 2221 | defined $lines[$ctx_ln - 1]) |
| 2222 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2223 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 2224 | if ($nindent > $indent) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2225 | WARN("TRAILING_SEMICOLON", |
| 2226 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2227 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2228 | } |
| 2229 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2232 | # Check relative indent for conditionals and blocks. |
| 2233 | 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] | 2234 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2235 | ctx_statement_block($linenr, $realcnt, 0) |
| 2236 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2237 | my ($s, $c) = ($stat, $cond); |
| 2238 | |
| 2239 | substr($s, 0, length($c), ''); |
| 2240 | |
| 2241 | # Make sure we remove the line prefixes as we have |
| 2242 | # none on the first line, and are going to readd them |
| 2243 | # where necessary. |
| 2244 | $s =~ s/\n./\n/gs; |
| 2245 | |
| 2246 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2247 | my @newlines = ($c =~ /\n/gs); |
| 2248 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2249 | |
| 2250 | # We want to check the first line inside the block |
| 2251 | # starting at the end of the conditional, so remove: |
| 2252 | # 1) any blank line termination |
| 2253 | # 2) any opening brace { on end of the line |
| 2254 | # 3) any do (...) { |
| 2255 | my $continuation = 0; |
| 2256 | my $check = 0; |
| 2257 | $s =~ s/^.*\bdo\b//; |
| 2258 | $s =~ s/^\s*{//; |
| 2259 | if ($s =~ s/^\s*\\//) { |
| 2260 | $continuation = 1; |
| 2261 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2262 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2263 | $check = 1; |
| 2264 | $cond_lines++; |
| 2265 | } |
| 2266 | |
| 2267 | # Also ignore a loop construct at the end of a |
| 2268 | # preprocessor statement. |
| 2269 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 2270 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 2271 | $check = 0; |
| 2272 | } |
| 2273 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2274 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2275 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2276 | while ($cond_ptr != $cond_lines) { |
| 2277 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2278 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2279 | # If we see an #else/#elif then the code |
| 2280 | # is not linear. |
| 2281 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 2282 | $check = 0; |
| 2283 | } |
| 2284 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2285 | # Ignore: |
| 2286 | # 1) blank lines, they should be at 0, |
| 2287 | # 2) preprocessor lines, and |
| 2288 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2289 | if ($continuation || |
| 2290 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2291 | $s =~ /^\s*#\s*?/ || |
| 2292 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2293 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 2294 | if ($s =~ s/^.*?\n//) { |
| 2295 | $cond_lines++; |
| 2296 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2297 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | my (undef, $sindent) = line_stats("+" . $s); |
| 2301 | my $stat_real = raw_line($linenr, $cond_lines); |
| 2302 | |
| 2303 | # Check if either of these lines are modified, else |
| 2304 | # this is not this patch's fault. |
| 2305 | if (!defined($stat_real) || |
| 2306 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 2307 | $check = 0; |
| 2308 | } |
| 2309 | if (defined($stat_real) && $cond_lines > 1) { |
| 2310 | $stat_real = "[...]\n$stat_real"; |
| 2311 | } |
| 2312 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2313 | #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] | 2314 | |
| 2315 | if ($check && (($sindent % 8) != 0 || |
| 2316 | ($sindent <= $indent && $s ne ''))) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2317 | WARN("SUSPECT_CODE_INDENT", |
| 2318 | "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] | 2319 | } |
| 2320 | } |
| 2321 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2322 | # Track the 'values' across context and added lines. |
| 2323 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2324 | my ($curr_values, $curr_vars) = |
| 2325 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2326 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2327 | if ($dbg_values) { |
| 2328 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2329 | print "$linenr > .$outline\n"; |
| 2330 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2331 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2332 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2333 | $prev_values = substr($curr_values, -1); |
| 2334 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2335 | #ignore lines not being added |
| 2336 | if ($line=~/^[^\+]/) {next;} |
| 2337 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2338 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2339 | if ($dbg_type) { |
| 2340 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2341 | ERROR("TEST_TYPE", |
| 2342 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2343 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2344 | ERROR("TEST_NOT_TYPE", |
| 2345 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2346 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2347 | next; |
| 2348 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2349 | # TEST: allow direct testing of the attribute matcher. |
| 2350 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2351 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2352 | ERROR("TEST_ATTR", |
| 2353 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2354 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2355 | ERROR("TEST_NOT_ATTR", |
| 2356 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2357 | } |
| 2358 | next; |
| 2359 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2360 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2361 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 2362 | if ($line =~ /^.\s*{/ && |
| 2363 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2364 | ERROR("OPEN_BRACE", |
| 2365 | "that open brace { should be on the previous line\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2368 | # |
| 2369 | # Checks which are anchored on the added line. |
| 2370 | # |
| 2371 | |
| 2372 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2373 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2374 | my $path = $1; |
| 2375 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2376 | ERROR("MALFORMED_INCLUDE", |
| 2377 | "malformed #include filename\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2378 | $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2379 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2380 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2381 | |
| 2382 | # no C99 // comments |
| 2383 | if ($line =~ m{//}) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2384 | ERROR("C99_COMMENTS", |
| 2385 | "do not use C99 // comments\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2386 | } |
| 2387 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2388 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2389 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2390 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2391 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 2392 | # the whole statement. |
| 2393 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 2394 | if (defined $realline_next && |
| 2395 | exists $lines[$realline_next - 1] && |
| 2396 | !defined $suppress_export{$realline_next} && |
| 2397 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2398 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2399 | # Handle definitions which produce identifiers with |
| 2400 | # a prefix: |
| 2401 | # XXX(foo); |
| 2402 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2403 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 2404 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2405 | $name =~ /^${Ident}_$2/) { |
| 2406 | #print "FOO C name<$name>\n"; |
| 2407 | $suppress_export{$realline_next} = 1; |
| 2408 | |
| 2409 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2410 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2411 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 2412 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 2413 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2414 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 2415 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2416 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2417 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 2418 | $suppress_export{$realline_next} = 2; |
| 2419 | } else { |
| 2420 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2421 | } |
| 2422 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2423 | if (!defined $suppress_export{$linenr} && |
| 2424 | $prevline =~ /^.\s*$/ && |
| 2425 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2426 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 2427 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 2428 | $suppress_export{$linenr} = 2; |
| 2429 | } |
| 2430 | if (defined $suppress_export{$linenr} && |
| 2431 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2432 | WARN("EXPORT_SYMBOL", |
| 2433 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2434 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2435 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 2436 | # check for global initialisers. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2437 | 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] | 2438 | ERROR("GLOBAL_INITIALISERS", |
| 2439 | "do not initialise globals to 0 or NULL\n" . |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2440 | $herecurr); |
| 2441 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2442 | # check for static initialisers. |
Andy Whitcroft | 2d1bafd | 2009-01-06 14:41:28 -0800 | [diff] [blame] | 2443 | if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2444 | ERROR("INITIALISED_STATIC", |
| 2445 | "do not initialise statics to 0 or NULL\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2446 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2449 | # check for static const char * arrays. |
| 2450 | 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] | 2451 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2452 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2453 | $herecurr); |
| 2454 | } |
| 2455 | |
| 2456 | # check for static char foo[] = "bar" declarations. |
| 2457 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2458 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2459 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2460 | $herecurr); |
| 2461 | } |
| 2462 | |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2463 | # check for declarations of struct pci_device_id |
| 2464 | 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] | 2465 | WARN("DEFINE_PCI_DEVICE_TABLE", |
| 2466 | "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2469 | # check for new typedefs, only function parameters and sparse annotations |
| 2470 | # make sense. |
| 2471 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 2472 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2473 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2474 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2475 | $line !~ /\b__bitwise(?:__|)\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2476 | WARN("NEW_TYPEDEFS", |
| 2477 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2481 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2482 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 2483 | #print "AA<$1>\n"; |
| 2484 | my ($from, $to) = ($2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2485 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2486 | # Should start with a space. |
| 2487 | $to =~ s/^(\S)/ $1/; |
| 2488 | # Should not end with a space. |
| 2489 | $to =~ s/\s+$//; |
| 2490 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2491 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2492 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2493 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2494 | #print "from<$from> to<$to>\n"; |
| 2495 | if ($from ne $to) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2496 | ERROR("POINTER_LOCATION", |
| 2497 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2498 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2499 | } |
| 2500 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 2501 | #print "BB<$1>\n"; |
| 2502 | my ($from, $to, $ident) = ($2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2503 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2504 | # Should start with a space. |
| 2505 | $to =~ s/^(\S)/ $1/; |
| 2506 | # Should not end with a space. |
| 2507 | $to =~ s/\s+$//; |
| 2508 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2509 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2510 | } |
| 2511 | # Modifiers should have spaces. |
| 2512 | $to =~ s/(\b$Modifier$)/$1 /; |
| 2513 | |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2514 | #print "from<$from> to<$to> ident<$ident>\n"; |
| 2515 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2516 | ERROR("POINTER_LOCATION", |
| 2517 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2518 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | # # no BUG() or BUG_ON() |
| 2522 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 2523 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 2524 | # print "$herecurr"; |
| 2525 | # $clean = 0; |
| 2526 | # } |
| 2527 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2528 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2529 | WARN("LINUX_VERSION_CODE", |
| 2530 | "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] | 2531 | } |
| 2532 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2533 | # check for uses of printk_ratelimit |
| 2534 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2535 | WARN("PRINTK_RATELIMITED", |
| 2536 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2539 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 2540 | # same line do not need a level, so we use the current block context |
| 2541 | # 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] | 2542 | # 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] | 2543 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2544 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2545 | my $ok = 0; |
| 2546 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 2547 | #print "CHECK<$lines[$ln - 1]\n"; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2548 | # we have a preceding printk if it ends |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2549 | # with "\n" ignore it, else it is to blame |
| 2550 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 2551 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 2552 | $ok = 1; |
| 2553 | } |
| 2554 | last; |
| 2555 | } |
| 2556 | } |
| 2557 | if ($ok == 0) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2558 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 2559 | "printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2560 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2563 | # function brace can't be on same line, except for #defines of do while, |
| 2564 | # or if closed on same line |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2565 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
| 2566 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2567 | ERROR("OPEN_BRACE", |
| 2568 | "open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2569 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2570 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2571 | # open braces for enum, union and struct go on the same line. |
| 2572 | if ($line =~ /^.\s*{/ && |
| 2573 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2574 | ERROR("OPEN_BRACE", |
| 2575 | "open brace '{' following $1 go on the same line\n" . $hereprev); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2578 | # missing space after union, struct or enum definition |
| 2579 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2580 | WARN("SPACING", |
| 2581 | "missing space after $1 definition\n" . $herecurr); |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2584 | # check for spacing round square brackets; allowed: |
| 2585 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2586 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 2587 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2588 | while ($line =~ /(.*?\s)\[/g) { |
| 2589 | my ($where, $prefix) = ($-[1], $1); |
| 2590 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2591 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Andy Whitcroft | daebc53 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 2592 | $prefix !~ /[{,]\s+$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2593 | ERROR("BRACKET_SPACE", |
| 2594 | "space prohibited before open square bracket '['\n" . $herecurr); |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2598 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2599 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2600 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2601 | my $ctx_before = substr($line, 0, $-[1]); |
| 2602 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2603 | |
| 2604 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2605 | if ($name =~ /^(?: |
| 2606 | if|for|while|switch|return|case| |
| 2607 | volatile|__volatile__| |
| 2608 | __attribute__|format|__extension__| |
| 2609 | asm|__asm__)$/x) |
| 2610 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2611 | |
| 2612 | # cpp #define statements have non-optional spaces, ie |
| 2613 | # if there is a space between the name and the open |
| 2614 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2615 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2616 | |
| 2617 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2618 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2619 | |
| 2620 | # If this whole things ends with a type its most |
| 2621 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2622 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2623 | |
| 2624 | } else { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2625 | WARN("SPACING", |
| 2626 | "space prohibited between function name and open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2627 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2628 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2629 | # Check operator spacing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2630 | if (!($line=~/\#\s*include/)) { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2631 | my $ops = qr{ |
| 2632 | <<=|>>=|<=|>=|==|!=| |
| 2633 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 2634 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2635 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
| 2636 | \?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2637 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2638 | my @elements = split(/($ops|;)/, $opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2639 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2640 | |
| 2641 | my $blank = copy_spacing($opline); |
| 2642 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2643 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2644 | $off += length($elements[$n]); |
| 2645 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2646 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2647 | my $ca = substr($opline, 0, $off); |
| 2648 | my $cc = ''; |
| 2649 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 2650 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 2651 | } |
| 2652 | my $cb = "$ca$;$cc"; |
| 2653 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2654 | my $a = ''; |
| 2655 | $a = 'V' if ($elements[$n] ne ''); |
| 2656 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2657 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2658 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 2659 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2660 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2661 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2662 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2663 | |
| 2664 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2665 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2666 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 2667 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2668 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2669 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 2670 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 2671 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2672 | } else { |
| 2673 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2674 | } |
| 2675 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2676 | my $ctx = "${a}x${c}"; |
| 2677 | |
| 2678 | my $at = "(ctx:$ctx)"; |
| 2679 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2680 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2681 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2682 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2683 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2684 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2685 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2686 | # Get the full operator variant. |
| 2687 | my $opv = $op . substr($curr_vars, $off, 1); |
| 2688 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2689 | # Ignore operators passed as parameters. |
| 2690 | if ($op_type ne 'V' && |
| 2691 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { |
| 2692 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2693 | # # Ignore comments |
| 2694 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2695 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2696 | # ; 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] | 2697 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2698 | if ($ctx !~ /.x[WEBC]/ && |
| 2699 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2700 | ERROR("SPACING", |
| 2701 | "space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | # // is a comment |
| 2705 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2706 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2707 | # No spaces for: |
| 2708 | # -> |
| 2709 | # : when part of a bitfield |
| 2710 | } elsif ($op eq '->' || $opv eq ':B') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2711 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2712 | ERROR("SPACING", |
| 2713 | "spaces prohibited around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | # , must have a space on the right. |
| 2717 | } elsif ($op eq ',') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2718 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2719 | ERROR("SPACING", |
| 2720 | "space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2721 | } |
| 2722 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2723 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2724 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2725 | #warn "'*' is part of type\n"; |
| 2726 | |
| 2727 | # unary operators should have a space before and |
| 2728 | # none after. May be left adjacent to another |
| 2729 | # unary operator, or a cast |
| 2730 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2731 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2732 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2733 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2734 | ERROR("SPACING", |
| 2735 | "space required before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2736 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 2737 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2738 | # A unary '*' may be const |
| 2739 | |
| 2740 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2741 | ERROR("SPACING", |
| 2742 | "space prohibited after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | # unary ++ and unary -- are allowed no space on one side. |
| 2746 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2747 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2748 | ERROR("SPACING", |
| 2749 | "space required one side of that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2750 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2751 | if ($ctx =~ /Wx[BE]/ || |
| 2752 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2753 | ERROR("SPACING", |
| 2754 | "space prohibited before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2755 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2756 | if ($ctx =~ /ExW/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2757 | ERROR("SPACING", |
| 2758 | "space prohibited after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2761 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2762 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2763 | } elsif ($op eq '<<' or $op eq '>>' or |
| 2764 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 2765 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2766 | $op eq '*' or $op eq '/' or |
| 2767 | $op eq '%') |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2768 | { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2769 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2770 | ERROR("SPACING", |
| 2771 | "need consistent spacing around '$op' $at\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2772 | $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2775 | # A colon needs no spaces before when it is |
| 2776 | # terminating a case value or a label. |
| 2777 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 2778 | if ($ctx =~ /Wx./) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2779 | ERROR("SPACING", |
| 2780 | "space prohibited before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2783 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2784 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2785 | my $ok = 0; |
| 2786 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2787 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2788 | if (($op eq '<' && |
| 2789 | $cc =~ /^\S+\@\S+>/) || |
| 2790 | ($op eq '>' && |
| 2791 | $ca =~ /<\S+\@\S+$/)) |
| 2792 | { |
| 2793 | $ok = 1; |
| 2794 | } |
| 2795 | |
| 2796 | # Ignore ?: |
| 2797 | if (($opv eq ':O' && $ca =~ /\?$/) || |
| 2798 | ($op eq '?' && $cc =~ /^:/)) { |
| 2799 | $ok = 1; |
| 2800 | } |
| 2801 | |
| 2802 | if ($ok == 0) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2803 | ERROR("SPACING", |
| 2804 | "spaces required around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2805 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2806 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2807 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2811 | # check for multiple assignments |
| 2812 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2813 | CHK("MULTIPLE_ASSIGNMENTS", |
| 2814 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2817 | ## # check for multiple declarations, allowing for a function declaration |
| 2818 | ## # continuation. |
| 2819 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 2820 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 2821 | ## |
| 2822 | ## # Remove any bracketed sections to ensure we do not |
| 2823 | ## # falsly report the parameters of functions. |
| 2824 | ## my $ln = $line; |
| 2825 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 2826 | ## } |
| 2827 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2828 | ## WARN("MULTIPLE_DECLARATION", |
| 2829 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2830 | ## } |
| 2831 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2832 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2833 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2834 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 2835 | $line =~ /do{/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2836 | ERROR("SPACING", |
| 2837 | "space required before the open brace '{'\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | # closing brace should have a space following it when it has anything |
| 2841 | # on the line |
| 2842 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2843 | ERROR("SPACING", |
| 2844 | "space required after that close brace '}'\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2845 | } |
| 2846 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2847 | # check spacing on square brackets |
| 2848 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2849 | ERROR("SPACING", |
| 2850 | "space prohibited after that open square bracket '['\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2851 | } |
| 2852 | if ($line =~ /\s\]/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2853 | ERROR("SPACING", |
| 2854 | "space prohibited before that close square bracket ']'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2857 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2858 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2859 | $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] | 2860 | ERROR("SPACING", |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2861 | "space prohibited after that open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2862 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2863 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2864 | $line !~ /for\s*\(.*;\s+\)/ && |
| 2865 | $line !~ /:\s+\)/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2866 | ERROR("SPACING", |
| 2867 | "space prohibited before that close parenthesis ')'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2868 | } |
| 2869 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2870 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2871 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2872 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2873 | WARN("INDENTED_LABEL", |
| 2874 | "labels should not be indented\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2877 | # Return is not a function. |
| 2878 | if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { |
| 2879 | my $spacing = $1; |
| 2880 | my $value = $2; |
| 2881 | |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2882 | # Flatten any parentheses |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2883 | $value =~ s/\(/ \(/g; |
| 2884 | $value =~ s/\)/\) /g; |
Andy Whitcroft | e01886a | 2012-01-10 15:10:08 -0800 | [diff] [blame] | 2885 | while ($value =~ s/\[[^\[\]]*\]/1/ || |
Andy Whitcroft | 63f17f8 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 2886 | $value !~ /(?:$Ident|-?$Constant)\s* |
| 2887 | $Compare\s* |
| 2888 | (?:$Ident|-?$Constant)/x && |
| 2889 | $value =~ s/\([^\(\)]*\)/1/) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2890 | } |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2891 | #print "value<$value>\n"; |
| 2892 | if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2893 | ERROR("RETURN_PARENTHESES", |
| 2894 | "return is not a function, parentheses are not required\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2895 | |
| 2896 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2897 | ERROR("SPACING", |
| 2898 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2899 | } |
| 2900 | } |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 2901 | # Return of what appears to be an errno should normally be -'ve |
| 2902 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
| 2903 | my $name = $1; |
| 2904 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2905 | WARN("USE_NEGATIVE_ERRNO", |
| 2906 | "return of an errno should typically be -ve (return -$1)\n" . $herecurr); |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 2907 | } |
| 2908 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2909 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2910 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2911 | if ($line=~/\b(if|while|for|switch)\(/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2912 | ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2913 | } |
| 2914 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2915 | # Check for illegal assignment in if conditional -- and check for trailing |
| 2916 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2917 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2918 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2919 | ctx_statement_block($linenr, $realcnt, 0) |
| 2920 | if (!defined $stat); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2921 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 2922 | $remain_next, $off_next); |
| 2923 | $stat_next =~ s/\n./\n /g; |
| 2924 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 2925 | |
| 2926 | if ($stat_next =~ /^\s*while\b/) { |
| 2927 | # If the statement carries leading newlines, |
| 2928 | # then count those as offsets. |
| 2929 | my ($whitespace) = |
| 2930 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 2931 | my $offset = |
| 2932 | statement_rawlines($whitespace) - 1; |
| 2933 | |
| 2934 | $suppress_whiletrailers{$line_nr_next + |
| 2935 | $offset} = 1; |
| 2936 | } |
| 2937 | } |
| 2938 | if (!defined $suppress_whiletrailers{$linenr} && |
| 2939 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2940 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2941 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 2942 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2943 | ERROR("ASSIGN_IN_IF", |
| 2944 | "do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | # Find out what is on the end of the line after the |
| 2948 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2949 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2950 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2951 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2952 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 2953 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2954 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2955 | # Find out how long the conditional actually is. |
| 2956 | my @newlines = ($c =~ /\n/gs); |
| 2957 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 2958 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2959 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 2960 | $stat_real = raw_line($linenr, $cond_lines) |
| 2961 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2962 | if (defined($stat_real) && $cond_lines > 1) { |
| 2963 | $stat_real = "[...]\n$stat_real"; |
| 2964 | } |
| 2965 | |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2966 | ERROR("TRAILING_STATEMENTS", |
| 2967 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2971 | # Check for bitwise tests written as boolean |
| 2972 | if ($line =~ / |
| 2973 | (?: |
| 2974 | (?:\[|\(|\&\&|\|\|) |
| 2975 | \s*0[xX][0-9]+\s* |
| 2976 | (?:\&\&|\|\|) |
| 2977 | | |
| 2978 | (?:\&\&|\|\|) |
| 2979 | \s*0[xX][0-9]+\s* |
| 2980 | (?:\&\&|\|\||\)|\]) |
| 2981 | )/x) |
| 2982 | { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2983 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 2984 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2985 | } |
| 2986 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2987 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2988 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 2989 | my $s = $1; |
| 2990 | $s =~ s/$;//g; # Remove any comments |
| 2991 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2992 | ERROR("TRAILING_STATEMENTS", |
| 2993 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2994 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2995 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 2996 | # if should not continue a brace |
| 2997 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2998 | ERROR("TRAILING_STATEMENTS", |
| 2999 | "trailing statements should be on next line\n" . |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3000 | $herecurr); |
| 3001 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3002 | # case and default should not have general statements after them |
| 3003 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 3004 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 3005 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3006 | \s*return\s+ |
| 3007 | )/xg) |
| 3008 | { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3009 | ERROR("TRAILING_STATEMENTS", |
| 3010 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3011 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3012 | |
| 3013 | # Check for }<nl>else {, these must be at the same |
| 3014 | # indent level to be relevant to each other. |
| 3015 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 3016 | $previndent == $indent) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3017 | ERROR("ELSE_AFTER_BRACE", |
| 3018 | "else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3019 | } |
| 3020 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3021 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
| 3022 | $previndent == $indent) { |
| 3023 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 3024 | |
| 3025 | # Find out what is on the end of the line after the |
| 3026 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3027 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3028 | $s =~ s/\n.*//g; |
| 3029 | |
| 3030 | if ($s =~ /^\s*;/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3031 | ERROR("WHILE_AFTER_BRACE", |
| 3032 | "while should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3033 | } |
| 3034 | } |
| 3035 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3036 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
| 3037 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
| 3038 | # print "No studly caps, use _\n"; |
| 3039 | # print "$herecurr"; |
| 3040 | # $clean = 0; |
| 3041 | # } |
| 3042 | |
| 3043 | #no spaces allowed after \ in define |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3044 | if ($line=~/\#\s*define.*\\\s$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3045 | WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 3046 | "Whitepspace after \\ makes next lines useless\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3049 | #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] | 3050 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3051 | my $file = "$1.h"; |
| 3052 | my $checkfile = "include/linux/$file"; |
| 3053 | if (-f "$root/$checkfile" && |
| 3054 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 3055 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3056 | { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3057 | if ($realfile =~ m{^arch/}) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3058 | CHK("ARCH_INCLUDE_LINUX", |
| 3059 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3060 | } else { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3061 | WARN("INCLUDE_LINUX", |
| 3062 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3063 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3067 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 3068 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3069 | # in a known good container |
Andy Whitcroft | b8f96a3 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 3070 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 3071 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3072 | my $ln = $linenr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3073 | my $cnt = $realcnt - 1; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3074 | my ($off, $dstat, $dcond, $rest); |
| 3075 | my $ctx = ''; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3076 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3077 | ctx_statement_block($linenr, $realcnt, 0); |
| 3078 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3079 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 3080 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3081 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3082 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 3083 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3084 | $dstat =~ s/\\\n.//g; |
| 3085 | $dstat =~ s/^\s*//s; |
| 3086 | $dstat =~ s/\s*$//s; |
| 3087 | |
| 3088 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3089 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 3090 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Andy Whitcroft | c81769f | 2012-01-10 15:10:10 -0800 | [diff] [blame] | 3091 | $dstat =~ s/\[[^\[\]]*\]/1/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3092 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3093 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3094 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3095 | # Extremely long macros may fall off the end of the |
| 3096 | # available context without closing. Give a dangling |
| 3097 | # backslash the benefit of the doubt and allow it |
| 3098 | # to gobble any hanging open-parens. |
| 3099 | $dstat =~ s/\(.+\\$/1/; |
| 3100 | |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 3101 | # Flatten any obvious string concatentation. |
| 3102 | while ($dstat =~ s/("X*")\s*$Ident/$1/ || |
| 3103 | $dstat =~ s/$Ident\s*("X*")/$1/) |
| 3104 | { |
| 3105 | } |
| 3106 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3107 | my $exceptions = qr{ |
| 3108 | $Declare| |
| 3109 | module_param_named| |
| 3110 | MODULE_PARAM_DESC| |
| 3111 | DECLARE_PER_CPU| |
| 3112 | DEFINE_PER_CPU| |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3113 | CLK_[A-Z\d_]+| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 3114 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 3115 | union| |
| 3116 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 3117 | \.$Ident\s*=\s*| |
| 3118 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3119 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3120 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3121 | if ($dstat ne '' && |
| 3122 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 3123 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Andy Whitcroft | fd1b57a | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 3124 | $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo |
Andy Whitcroft | b9df76a | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3125 | $dstat !~ /^'X'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3126 | $dstat !~ /$exceptions/ && |
| 3127 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 3128 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3129 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 3130 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 3131 | $dstat !~ /^do\s*{/ && # do {... |
| 3132 | $dstat !~ /^\({/) # ({... |
| 3133 | { |
| 3134 | $ctx =~ s/\n*$//; |
| 3135 | my $herectx = $here . "\n"; |
| 3136 | my $cnt = statement_rawlines($ctx); |
| 3137 | |
| 3138 | for (my $n = 0; $n < $cnt; $n++) { |
| 3139 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3140 | } |
| 3141 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3142 | if ($dstat =~ /;/) { |
| 3143 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 3144 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 3145 | } else { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3146 | ERROR("COMPLEX_MACRO", |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3147 | "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3148 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3149 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3150 | } |
| 3151 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3152 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 3153 | # all assignments may have only one of the following with an assignment: |
| 3154 | # . |
| 3155 | # ALIGN(...) |
| 3156 | # VMLINUX_SYMBOL(...) |
| 3157 | 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] | 3158 | WARN("MISSING_VMLINUX_SYMBOL", |
| 3159 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3160 | } |
| 3161 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3162 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3163 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 3164 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3165 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3166 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3167 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 3168 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3169 | my @allowed = (); |
| 3170 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3171 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3172 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3173 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3174 | for my $chunk (@chunks) { |
| 3175 | my ($cond, $block) = @{$chunk}; |
| 3176 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3177 | # If the condition carries leading newlines, then count those as offsets. |
| 3178 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3179 | my $offset = statement_rawlines($whitespace) - 1; |
| 3180 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3181 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3182 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 3183 | |
| 3184 | # We have looked at and allowed this specific line. |
| 3185 | $suppress_ifbraces{$ln + $offset} = 1; |
| 3186 | |
| 3187 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3188 | $ln += statement_rawlines($block) - 1; |
| 3189 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3190 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3191 | |
| 3192 | $seen++ if ($block =~ /^\s*{/); |
| 3193 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3194 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3195 | if (statement_lines($cond) > 1) { |
| 3196 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3197 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3198 | } |
| 3199 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3200 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3201 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3202 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3203 | if (statement_block_size($block) > 1) { |
| 3204 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3205 | $allowed[$allow] = 1; |
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 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3208 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3209 | if ($seen) { |
| 3210 | my $sum_allowed = 0; |
| 3211 | foreach (@allowed) { |
| 3212 | $sum_allowed += $_; |
| 3213 | } |
| 3214 | if ($sum_allowed == 0) { |
| 3215 | WARN("BRACES", |
| 3216 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 3217 | } elsif ($sum_allowed != $allow && |
| 3218 | $seen != $allow) { |
| 3219 | CHK("BRACES", |
| 3220 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 3221 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3222 | } |
| 3223 | } |
| 3224 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3225 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3226 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3227 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3228 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3229 | # Check the pre-context. |
| 3230 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 3231 | #print "APW: ALLOWED: pre<$1>\n"; |
| 3232 | $allowed = 1; |
| 3233 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3234 | |
| 3235 | my ($level, $endln, @chunks) = |
| 3236 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 3237 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3238 | # Check the condition. |
| 3239 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3240 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3241 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3242 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3243 | } |
| 3244 | if (statement_lines($cond) > 1) { |
| 3245 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 3246 | $allowed = 1; |
| 3247 | } |
| 3248 | if ($block =~/\b(?:if|for|while)\b/) { |
| 3249 | #print "APW: ALLOWED: block<$block>\n"; |
| 3250 | $allowed = 1; |
| 3251 | } |
| 3252 | if (statement_block_size($block) > 1) { |
| 3253 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 3254 | $allowed = 1; |
| 3255 | } |
| 3256 | # Check the post-context. |
| 3257 | if (defined $chunks[1]) { |
| 3258 | my ($cond, $block) = @{$chunks[1]}; |
| 3259 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3260 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3261 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3262 | if ($block =~ /^\s*\{/) { |
| 3263 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 3264 | $allowed = 1; |
| 3265 | } |
| 3266 | } |
| 3267 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3268 | my $herectx = $here . "\n"; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3269 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3270 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3271 | for (my $n = 0; $n < $cnt; $n++) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3272 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3273 | } |
| 3274 | |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3275 | WARN("BRACES", |
| 3276 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3280 | # don't include deprecated include files (uses RAW line) |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3281 | for my $inc (@dep_includes) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3282 | if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3283 | ERROR("DEPRECATED_INCLUDE", |
| 3284 | "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3285 | } |
| 3286 | } |
| 3287 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3288 | # don't use deprecated functions |
| 3289 | for my $func (@dep_functions) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3290 | if ($line =~ /\b$func\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3291 | ERROR("DEPRECATED_FUNCTION", |
| 3292 | "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3297 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 3298 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3299 | WARN("VOLATILE", |
| 3300 | "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] | 3301 | } |
| 3302 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3303 | # sys_open/read/write/close are not allowed in the kernel |
| 3304 | if ($line =~ /\b(sys_(?:open|read|write|close))\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3305 | ERROR("FILE_OPS", "$1 is inappropriate in kernel code.\n" . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3306 | $herecurr); |
| 3307 | } |
| 3308 | |
| 3309 | # filp_open is a backdoor for sys_open |
| 3310 | if ($line =~ /\b(filp_open)\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3311 | ERROR("FILE_OPS", "$1 is inappropriate in kernel code.\n" . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3312 | $herecurr); |
| 3313 | } |
| 3314 | |
| 3315 | # read[bwl] & write[bwl] use too many barriers, use the _relaxed variants |
| 3316 | if ($line =~ /\b((?:read|write)[bwl])\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3317 | ERROR("NON_RELAXED_IO", |
| 3318 | "Use of $1 is deprecated: use $1_relaxed\n\t" . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3319 | "with appropriate memory barriers instead.\n" . |
| 3320 | $herecurr); |
| 3321 | } |
| 3322 | |
| 3323 | # likewise, in/out[bwl] should be __raw_read/write[bwl]... |
| 3324 | if ($line =~ /\b((in|out)([bwl]))\b/) { |
| 3325 | my ($all, $pref, $suf) = ($1, $2, $3); |
| 3326 | $pref =~ s/in/read/; |
| 3327 | $pref =~ s/out/write/; |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3328 | ERROR("NON_RELAXED_IO", |
| 3329 | "Use of $all is deprecated: use " . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3330 | "__raw_$pref$suf\n\t" . |
| 3331 | "with appropriate memory barriers instead.\n" . |
| 3332 | $herecurr); |
| 3333 | } |
| 3334 | |
| 3335 | # dsb is too ARMish, and should usually be mb. |
| 3336 | if ($line =~ /\bdsb\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3337 | WARN("ARM_BARRIER", |
| 3338 | "Use of dsb is discouranged: prefer mb.\n" . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3339 | $herecurr); |
| 3340 | } |
| 3341 | |
Rohit Vaswani | 60d78bb | 2011-12-06 20:03:07 -0800 | [diff] [blame] | 3342 | # MSM - check if a non board-gpiomux file has any gpiomux declarations |
| 3343 | if ($realfile =~ /\/mach-msm\/board-[0-9]+/ && |
| 3344 | $realfile !~ /camera/ && $realfile !~ /gpiomux/ && |
| 3345 | $line =~ /\s*struct msm_gpiomux_config\s*/ ) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3346 | WARN("GPIOMUX_IN_BOARD", |
| 3347 | "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] | 3348 | } |
| 3349 | |
Pankaj Kumar | adfb933 | 2012-01-06 15:02:19 +0530 | [diff] [blame] | 3350 | # MSM - check if vreg_xxx function are used |
| 3351 | if ($line =~ /\b(vreg_(get|put|set_level|enable|disable))\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3352 | WARN("DEPRECATED_VREG_APIS", "Use of $1 API is deprecated: " . |
Pankaj Kumar | adfb933 | 2012-01-06 15:02:19 +0530 | [diff] [blame] | 3353 | "use regulator APIs\n" . $herecurr); |
| 3354 | } |
| 3355 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3356 | # unbounded string functions are overflow risks |
| 3357 | my %str_fns = ( |
| 3358 | "sprintf" => "snprintf", |
Olav Haugan | 665be0d | 2012-02-06 09:42:18 -0800 | [diff] [blame] | 3359 | "strcpy" => "strlcpy", |
| 3360 | "strncpy" => "strlcpy", |
| 3361 | "strcat" => "strlcat", |
| 3362 | "strncat" => "strlcat", |
| 3363 | "vsprintf" => "vsnprintf", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3364 | "strchr" => "strnchr", |
| 3365 | "strstr" => "strnstr", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3366 | ); |
| 3367 | foreach my $k (keys %str_fns) { |
| 3368 | if ($line =~ /\b$k\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3369 | ERROR("UNBOUNDED_STRING_FNS", |
| 3370 | "Use of $k is deprecated: " . |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3371 | "use $str_fns{$k} instead.\n" . |
| 3372 | $herecurr); |
| 3373 | } |
| 3374 | } |
| 3375 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3376 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3377 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3378 | WARN("IF_0", |
| 3379 | "if this code is redundant consider removing it\n" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3380 | . $herecurr); |
| 3381 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 3382 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3383 | # warn about #if 1 |
| 3384 | if ($line =~ /^.\s*\#\s*if\s+1\b/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3385 | WARN("IF_1", |
| 3386 | "if this code is required consider removing" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3387 | . " #if 1\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3388 | } |
| 3389 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3390 | # check for needless kfree() checks |
| 3391 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 3392 | my $expr = $1; |
| 3393 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3394 | WARN("NEEDLESS_KFREE", |
| 3395 | "kfree(NULL) is safe this check is probably not required\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3396 | } |
| 3397 | } |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 3398 | # check for needless usb_free_urb() checks |
| 3399 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 3400 | my $expr = $1; |
| 3401 | if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3402 | WARN("NEEDLESS_USB_FREE_URB", |
| 3403 | "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] | 3404 | } |
| 3405 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3406 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3407 | # prefer usleep_range over udelay |
| 3408 | if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { |
| 3409 | # ignore udelay's < 10, however |
| 3410 | if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3411 | CHK("USLEEP_RANGE", |
| 3412 | "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] | 3413 | } |
| 3414 | } |
| 3415 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3416 | # warn about unexpectedly long msleep's |
| 3417 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 3418 | if ($1 < 20) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3419 | WARN("MSLEEP", |
| 3420 | "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] | 3421 | } |
| 3422 | } |
| 3423 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3424 | # check the patch for use of mdelay |
| 3425 | if ($line =~ /\bmdelay\s*\(/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3426 | WARN("MDELAY", |
| 3427 | "use of mdelay() found: msleep() is the preferred API.\n" . $line ); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3428 | } |
| 3429 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3430 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3431 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3432 | # print "#ifdef in C files should be avoided\n"; |
| 3433 | # print "$herecurr"; |
| 3434 | # $clean = 0; |
| 3435 | # } |
| 3436 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3437 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3438 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3439 | ERROR("SPACING", |
| 3440 | "exactly one space required after that #$1\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3441 | } |
| 3442 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3443 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3444 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 3445 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3446 | my $which = $1; |
| 3447 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3448 | CHK("UNCOMMENTED_DEFINITION", |
| 3449 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3450 | } |
| 3451 | } |
| 3452 | # check for memory barriers without a comment. |
| 3453 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 3454 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3455 | CHK("MEMORY_BARRIER", |
| 3456 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3457 | } |
| 3458 | } |
| 3459 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3460 | 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] | 3461 | CHK("ARCH_DEFINES", |
| 3462 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3463 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3464 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3465 | # Check that the storage class is at the beginning of a declaration |
| 3466 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3467 | WARN("STORAGE_CLASS", |
| 3468 | "storage class should be at the beginning of the declaration\n" . $herecurr) |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3469 | } |
| 3470 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3471 | # check the location of the inline attribute, that it is between |
| 3472 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3473 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 3474 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3475 | ERROR("INLINE_LOCATION", |
| 3476 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3479 | # Check for __inline__ and __inline, prefer inline |
| 3480 | if ($line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3481 | WARN("INLINE", |
| 3482 | "plain inline is preferred over $1\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3483 | } |
| 3484 | |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3485 | # Check for __attribute__ packed, prefer __packed |
| 3486 | if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3487 | WARN("PREFER_PACKED", |
| 3488 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3489 | } |
| 3490 | |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3491 | # Check for __attribute__ aligned, prefer __aligned |
| 3492 | if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3493 | WARN("PREFER_ALIGNED", |
| 3494 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 3497 | # Check for __attribute__ format(printf, prefer __printf |
| 3498 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
| 3499 | WARN("PREFER_PRINTF", |
| 3500 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); |
| 3501 | } |
| 3502 | |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3503 | # Check for __attribute__ format(scanf, prefer __scanf |
| 3504 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
| 3505 | WARN("PREFER_SCANF", |
| 3506 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr); |
| 3507 | } |
| 3508 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3509 | # check for sizeof(&) |
| 3510 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3511 | WARN("SIZEOF_ADDRESS", |
| 3512 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3513 | } |
| 3514 | |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3515 | # check for line continuations in quoted strings with odd counts of " |
| 3516 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3517 | WARN("LINE_CONTINUATIONS", |
| 3518 | "Avoid line continuations in quoted strings\n" . $herecurr); |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3521 | # Check for misused memsets |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3522 | if ($^V && $^V ge 5.10.0 && |
| 3523 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3524 | $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] | 3525 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3526 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3527 | my $ms_val = $7; |
| 3528 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3529 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3530 | if ($ms_size =~ /^(0x|)0$/i) { |
| 3531 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3532 | "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] | 3533 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 3534 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3535 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | # typecasts on min/max could be min_t/max_t |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3540 | if ($^V && $^V ge 5.10.0 && |
| 3541 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3542 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3543 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3544 | my $call = $1; |
| 3545 | my $cast1 = deparenthesize($2); |
| 3546 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3547 | my $cast2 = deparenthesize($7); |
| 3548 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3549 | my $cast; |
| 3550 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3551 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3552 | $cast = "$cast1 or $cast2"; |
| 3553 | } elsif ($cast1 ne "") { |
| 3554 | $cast = $cast1; |
| 3555 | } else { |
| 3556 | $cast = $cast2; |
| 3557 | } |
| 3558 | WARN("MINMAX", |
| 3559 | "$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] | 3560 | } |
| 3561 | } |
| 3562 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3563 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3564 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3565 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3566 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3567 | my $function_name = $1; |
| 3568 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3569 | |
| 3570 | my $s = $stat; |
| 3571 | if (defined $cond) { |
| 3572 | substr($s, 0, length($cond), ''); |
| 3573 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3574 | if ($s =~ /^\s*;/ && |
| 3575 | $function_name ne 'uninitialized_var') |
| 3576 | { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3577 | WARN("AVOID_EXTERNS", |
| 3578 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3582 | WARN("FUNCTION_ARGUMENTS", |
| 3583 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3584 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3585 | |
| 3586 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 3587 | $stat =~ /^.\s*extern\s+/) |
| 3588 | { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3589 | WARN("AVOID_EXTERNS", |
| 3590 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | # checks for new __setup's |
| 3594 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 3595 | my $name = $1; |
| 3596 | |
| 3597 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3598 | CHK("UNDOCUMENTED_SETUP", |
| 3599 | "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3600 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3601 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3602 | |
| 3603 | # check for pointless casting of kmalloc return |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 3604 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3605 | WARN("UNNECESSARY_CASTS", |
| 3606 | "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] | 3607 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3608 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 3609 | # check for multiple semicolons |
| 3610 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3611 | WARN("ONE_SEMICOLON", |
| 3612 | "Statements terminations use 1 semicolon\n" . $herecurr); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 3613 | } |
| 3614 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3615 | # check for return codes on error paths |
| 3616 | if ($line =~ /\breturn\s+-\d+/) { |
Steve Muckle | 40f0c3c | 2012-06-13 15:31:18 -0700 | [diff] [blame] | 3617 | ERROR("NO_ERROR_CODE", |
| 3618 | "illegal return value, please use an error code\n" . $herecurr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3621 | # check for gcc specific __FUNCTION__ |
| 3622 | if ($line =~ /__FUNCTION__/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3623 | WARN("USE_FUNC", |
| 3624 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3625 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3626 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3627 | # check for use of yield() |
| 3628 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 3629 | WARN("YIELD", |
| 3630 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 3631 | } |
| 3632 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 3633 | # check for semaphores initialized locked |
| 3634 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3635 | WARN("CONSIDER_COMPLETION", |
| 3636 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3637 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3638 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 3639 | # recommend kstrto* over simple_strto* and strict_strto* |
| 3640 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3641 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 3642 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3643 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3644 | |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 3645 | # check for __initcall(), use device_initcall() explicitly please |
| 3646 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3647 | WARN("USE_DEVICE_INITCALL", |
| 3648 | "please use device_initcall() instead of __initcall()\n" . $herecurr); |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 3649 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3650 | |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 3651 | # check for various ops structs, ensure they are const. |
| 3652 | my $struct_ops = qr{acpi_dock_ops| |
| 3653 | address_space_operations| |
| 3654 | backlight_ops| |
| 3655 | block_device_operations| |
| 3656 | dentry_operations| |
| 3657 | dev_pm_ops| |
| 3658 | dma_map_ops| |
| 3659 | extent_io_ops| |
| 3660 | file_lock_operations| |
| 3661 | file_operations| |
| 3662 | hv_ops| |
| 3663 | ide_dma_ops| |
| 3664 | intel_dvo_dev_ops| |
| 3665 | item_operations| |
| 3666 | iwl_ops| |
| 3667 | kgdb_arch| |
| 3668 | kgdb_io| |
| 3669 | kset_uevent_ops| |
| 3670 | lock_manager_operations| |
| 3671 | microcode_ops| |
| 3672 | mtrr_ops| |
| 3673 | neigh_ops| |
| 3674 | nlmsvc_binding| |
| 3675 | pci_raw_ops| |
| 3676 | pipe_buf_operations| |
| 3677 | platform_hibernation_ops| |
| 3678 | platform_suspend_ops| |
| 3679 | proto_ops| |
| 3680 | rpc_pipe_ops| |
| 3681 | seq_operations| |
| 3682 | snd_ac97_build_ops| |
| 3683 | soc_pcmcia_socket_ops| |
| 3684 | stacktrace_ops| |
| 3685 | sysfs_ops| |
| 3686 | tty_operations| |
| 3687 | usb_mon_operations| |
| 3688 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 3689 | if ($line !~ /\bconst\b/ && |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 3690 | $line =~ /\bstruct\s+($struct_ops)\b/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3691 | WARN("CONST_STRUCT", |
| 3692 | "struct $1 should normally be const\n" . |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 3693 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 3694 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3695 | |
| 3696 | # use of NR_CPUS is usually wrong |
| 3697 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 3698 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3699 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 3700 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3701 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 3702 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 3703 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3704 | { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3705 | WARN("NR_CPUS", |
| 3706 | "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] | 3707 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3708 | |
| 3709 | # check for %L{u,d,i} in strings |
| 3710 | my $string; |
| 3711 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 3712 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Andy Whitcroft | 2a1bc5d | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3713 | $string =~ s/%%/__/g; |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3714 | if ($string =~ /(?<!%)%L[udi]/) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3715 | WARN("PRINTF_L", |
| 3716 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3717 | last; |
| 3718 | } |
| 3719 | } |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 3720 | |
| 3721 | # whine mightly about in_atomic |
| 3722 | if ($line =~ /\bin_atomic\s*\(/) { |
| 3723 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3724 | ERROR("IN_ATOMIC", |
| 3725 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 3726 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3727 | WARN("IN_ATOMIC", |
| 3728 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 3729 | } |
| 3730 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 3731 | |
| 3732 | # check for lockdep_set_novalidate_class |
| 3733 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 3734 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 3735 | if ($realfile !~ m@^kernel/lockdep@ && |
| 3736 | $realfile !~ m@^include/linux/lockdep@ && |
| 3737 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3738 | ERROR("LOCKDEP", |
| 3739 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 3740 | } |
| 3741 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 3742 | |
| 3743 | if ($line =~ /debugfs_create_file.*S_IWUGO/ || |
| 3744 | $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3745 | WARN("EXPORTED_WORLD_WRITABLE", |
| 3746 | "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] | 3747 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | # If we have no input at all, then there is nothing to report on |
| 3751 | # so just keep quiet. |
| 3752 | if ($#rawlines == -1) { |
| 3753 | exit(0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3754 | } |
| 3755 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3756 | # In mailback mode only produce a report in the negative, for |
| 3757 | # things that appear to be patches. |
| 3758 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 3759 | exit(0); |
| 3760 | } |
| 3761 | |
| 3762 | # This is not a patch, and we are are in 'no-patch' mode so |
| 3763 | # just keep quiet. |
| 3764 | if (!$chk_patch && !$is_patch) { |
| 3765 | exit(0); |
| 3766 | } |
| 3767 | |
| 3768 | if (!$is_patch) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3769 | ERROR("NOT_UNIFIED_DIFF", |
| 3770 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3771 | } |
| 3772 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3773 | ERROR("MISSING_SIGN_OFF", |
| 3774 | "Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3775 | } |
| 3776 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3777 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3778 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 3779 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3780 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 3781 | (($check)? "$cnt_chk checks, " : "") . |
| 3782 | "$cnt_lines lines checked\n"; |
| 3783 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3784 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3785 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3786 | if ($quiet == 0) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3787 | |
| 3788 | if ($^V lt 5.10.0) { |
| 3789 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); |
| 3790 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); |
| 3791 | } |
| 3792 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3793 | # If there were whitespace errors which cleanpatch can fix |
| 3794 | # then suggest that. |
| 3795 | if ($rpt_cleaners) { |
| 3796 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; |
| 3797 | print " scripts/cleanfile\n\n"; |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 3798 | $rpt_cleaners = 0; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3799 | } |
| 3800 | } |
| 3801 | |
Artem Bityutskiy | 1123268 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3802 | if ($quiet == 0 && keys %ignore_type) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3803 | print "NOTE: Ignored message types:"; |
| 3804 | foreach my $ignore (sort keys %ignore_type) { |
| 3805 | print " $ignore"; |
| 3806 | } |
Artem Bityutskiy | 1123268 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3807 | print "\n\n"; |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3810 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3811 | 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] | 3812 | } |
| 3813 | if ($clean == 0 && $quiet == 0) { |
Joe Perches | 000d1cc | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3814 | print << "EOM"; |
| 3815 | $vname has style problems, please review. |
| 3816 | |
| 3817 | If any of these errors are false positives, please report |
| 3818 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 3819 | EOM |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3820 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3821 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3822 | return $clean; |
| 3823 | } |