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