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