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