Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Dave Jones | f4432c5 | 2008-10-20 13:31:45 -0400 | [diff] [blame] | 2 | # (c) 2001, Dave Jones. <davej@redhat.com> (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) |
| 5 | # (c) 2008, Andy Whitcroft <apw@canonical.com> |
Andy Whitcroft | 0a920b5b | 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 | |
| 10 | my $P = $0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 11 | $P =~ s@.*/@@g; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 12 | |
Andy Whitcroft | 50a7dcf | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 13 | my $V = '0.25'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 14 | |
| 15 | use Getopt::Long qw(:config no_auto_abbrev); |
| 16 | |
| 17 | my $quiet = 0; |
| 18 | my $tree = 1; |
| 19 | my $chk_signoff = 1; |
| 20 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 21 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 22 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 23 | my $terse = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 24 | my $file = 0; |
| 25 | my $check = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 26 | my $summary = 1; |
| 27 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 28 | my $summary_file = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 29 | my $root; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 30 | my %debug; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 31 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 32 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 33 | 'tree!' => \$tree, |
| 34 | 'signoff!' => \$chk_signoff, |
| 35 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 36 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 37 | 'terse!' => \$terse, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 38 | 'file!' => \$file, |
| 39 | 'subjective!' => \$check, |
| 40 | 'strict!' => \$check, |
| 41 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 42 | 'summary!' => \$summary, |
| 43 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 44 | 'summary-file!' => \$summary_file, |
| 45 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 46 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 47 | 'test-only=s' => \$tst_only, |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 48 | ) or exit; |
| 49 | |
| 50 | my $exit = 0; |
| 51 | |
| 52 | if ($#ARGV < 0) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 53 | print "usage: $P [options] patchfile\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 54 | print "version: $V\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 55 | print "options: -q => quiet\n"; |
| 56 | print " --no-tree => run without a kernel tree\n"; |
| 57 | print " --terse => one line per report\n"; |
| 58 | print " --emacs => emacs compile window format\n"; |
| 59 | print " --file => check a source file\n"; |
| 60 | print " --strict => enable more subjective tests\n"; |
| 61 | print " --root => path to the kernel tree root\n"; |
| 62 | print " --no-summary => suppress the per-file summary\n"; |
| 63 | print " --summary-file => include the filename in summary\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 64 | exit(1); |
| 65 | } |
| 66 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 67 | my $dbg_values = 0; |
| 68 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 69 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 70 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 71 | for my $key (keys %debug) { |
| 72 | eval "\${dbg_$key} = '$debug{$key}';" |
| 73 | } |
| 74 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 75 | if ($terse) { |
| 76 | $emacs = 1; |
| 77 | $quiet++; |
| 78 | } |
| 79 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 80 | if ($tree) { |
| 81 | if (defined $root) { |
| 82 | if (!top_of_kernel_tree($root)) { |
| 83 | die "$P: $root: --root does not point at a valid tree\n"; |
| 84 | } |
| 85 | } else { |
| 86 | if (top_of_kernel_tree('.')) { |
| 87 | $root = '.'; |
| 88 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 89 | top_of_kernel_tree($1)) { |
| 90 | $root = $1; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (!defined $root) { |
| 95 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 96 | exit(2); |
| 97 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 100 | my $emitted_corrupt = 0; |
| 101 | |
| 102 | our $Ident = qr{[A-Za-z_][A-Za-z\d_]*}; |
| 103 | our $Storage = qr{extern|static|asmlinkage}; |
| 104 | our $Sparse = qr{ |
| 105 | __user| |
| 106 | __kernel| |
| 107 | __force| |
| 108 | __iomem| |
| 109 | __must_check| |
| 110 | __init_refok| |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 111 | __kprobes |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 112 | }x; |
| 113 | our $Attribute = qr{ |
| 114 | const| |
| 115 | __read_mostly| |
| 116 | __kprobes| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 117 | __(?:mem|cpu|dev|)(?:initdata|init)| |
| 118 | ____cacheline_aligned| |
| 119 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 120 | ____cacheline_internodealigned_in_smp| |
| 121 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 122 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 123 | our $Modifier; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 124 | our $Inline = qr{inline|__always_inline|noinline}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 125 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 126 | our $Lval = qr{$Ident(?:$Member)*}; |
| 127 | |
| 128 | our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; |
| 129 | our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 130 | our $Compare = qr{<=|>=|==|!=|<|>}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 131 | our $Operators = qr{ |
| 132 | <=|>=|==|!=| |
| 133 | =>|->|<<|>>|<|>|!|~| |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 134 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 135 | }x; |
| 136 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 137 | our $NonptrType; |
| 138 | our $Type; |
| 139 | our $Declare; |
| 140 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 141 | our $UTF8 = qr { |
| 142 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 143 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
| 144 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 145 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 146 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 147 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 148 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 149 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 150 | }x; |
| 151 | |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 152 | our $typeTypedefs = qr{(?x: |
| 153 | (?:__)?(?:u|s|be|le)(?:\d|\d\d)| |
| 154 | atomic_t |
| 155 | )}; |
| 156 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 157 | our @typeList = ( |
| 158 | qr{void}, |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 159 | qr{(?:unsigned\s+)?char}, |
| 160 | qr{(?:unsigned\s+)?short}, |
| 161 | qr{(?:unsigned\s+)?int}, |
| 162 | qr{(?:unsigned\s+)?long}, |
| 163 | qr{(?:unsigned\s+)?long\s+int}, |
| 164 | qr{(?:unsigned\s+)?long\s+long}, |
| 165 | qr{(?:unsigned\s+)?long\s+long\s+int}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 166 | qr{unsigned}, |
| 167 | qr{float}, |
| 168 | qr{double}, |
| 169 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 170 | qr{struct\s+$Ident}, |
| 171 | qr{union\s+$Ident}, |
| 172 | qr{enum\s+$Ident}, |
| 173 | qr{${Ident}_t}, |
| 174 | qr{${Ident}_handler}, |
| 175 | qr{${Ident}_handler_fn}, |
| 176 | ); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 177 | our @modifierList = ( |
| 178 | qr{fastcall}, |
| 179 | ); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 180 | |
| 181 | sub build_types { |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 182 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
| 183 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 184 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 185 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 186 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 187 | (?: |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 188 | (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 189 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 190 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 191 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 192 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 193 | }x; |
| 194 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 195 | $NonptrType |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 196 | (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 197 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 198 | }x; |
| 199 | $Declare = qr{(?:$Storage\s+)?$Type}; |
| 200 | } |
| 201 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 202 | |
| 203 | $chk_signoff = 0 if ($file); |
| 204 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 205 | my @dep_includes = (); |
| 206 | my @dep_functions = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 207 | my $removal = "Documentation/feature-removal-schedule.txt"; |
| 208 | if ($tree && -f "$root/$removal") { |
| 209 | open(REMOVE, "<$root/$removal") || |
| 210 | die "$P: $removal: open failed - $!\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 211 | while (<REMOVE>) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 212 | if (/^Check:\s+(.*\S)/) { |
| 213 | for my $entry (split(/[, ]+/, $1)) { |
| 214 | if ($entry =~ m@include/(.*)@) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 215 | push(@dep_includes, $1); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 216 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 217 | } elsif ($entry !~ m@/@) { |
| 218 | push(@dep_functions, $entry); |
| 219 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 220 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 225 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 226 | my @lines = (); |
| 227 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 228 | for my $filename (@ARGV) { |
| 229 | if ($file) { |
| 230 | open(FILE, "diff -u /dev/null $filename|") || |
| 231 | die "$P: $filename: diff failed - $!\n"; |
| 232 | } else { |
| 233 | open(FILE, "<$filename") || |
| 234 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 235 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 236 | if ($filename eq '-') { |
| 237 | $vname = 'Your patch'; |
| 238 | } else { |
| 239 | $vname = $filename; |
| 240 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 241 | while (<FILE>) { |
| 242 | chomp; |
| 243 | push(@rawlines, $_); |
| 244 | } |
| 245 | close(FILE); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 246 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 247 | $exit = 1; |
| 248 | } |
| 249 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 250 | @lines = (); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | exit($exit); |
| 254 | |
| 255 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 256 | my ($root) = @_; |
| 257 | |
| 258 | my @tree_check = ( |
| 259 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 260 | "README", "Documentation", "arch", "include", "drivers", |
| 261 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 262 | ); |
| 263 | |
| 264 | foreach my $check (@tree_check) { |
| 265 | if (! -e $root . '/' . $check) { |
| 266 | return 0; |
| 267 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 268 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 269 | return 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | sub expand_tabs { |
| 273 | my ($str) = @_; |
| 274 | |
| 275 | my $res = ''; |
| 276 | my $n = 0; |
| 277 | for my $c (split(//, $str)) { |
| 278 | if ($c eq "\t") { |
| 279 | $res .= ' '; |
| 280 | $n++; |
| 281 | for (; ($n % 8) != 0; $n++) { |
| 282 | $res .= ' '; |
| 283 | } |
| 284 | next; |
| 285 | } |
| 286 | $res .= $c; |
| 287 | $n++; |
| 288 | } |
| 289 | |
| 290 | return $res; |
| 291 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 292 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 293 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 294 | return $res; |
| 295 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 296 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 297 | sub line_stats { |
| 298 | my ($line) = @_; |
| 299 | |
| 300 | # Drop the diff line leader and expand tabs |
| 301 | $line =~ s/^.//; |
| 302 | $line = expand_tabs($line); |
| 303 | |
| 304 | # Pick the indent from the front of the line. |
| 305 | my ($white) = ($line =~ /^(\s*)/); |
| 306 | |
| 307 | return (length($line), length($white)); |
| 308 | } |
| 309 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 310 | my $sanitise_quote = ''; |
| 311 | |
| 312 | sub sanitise_line_reset { |
| 313 | my ($in_comment) = @_; |
| 314 | |
| 315 | if ($in_comment) { |
| 316 | $sanitise_quote = '*/'; |
| 317 | } else { |
| 318 | $sanitise_quote = ''; |
| 319 | } |
| 320 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 321 | sub sanitise_line { |
| 322 | my ($line) = @_; |
| 323 | |
| 324 | my $res = ''; |
| 325 | my $l = ''; |
| 326 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 327 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 328 | my $off = 0; |
| 329 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 330 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 331 | # Always copy over the diff marker. |
| 332 | $res = substr($line, 0, 1); |
| 333 | |
| 334 | for ($off = 1; $off < length($line); $off++) { |
| 335 | $c = substr($line, $off, 1); |
| 336 | |
| 337 | # Comments we are wacking completly including the begin |
| 338 | # and end, all to $;. |
| 339 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 340 | $sanitise_quote = '*/'; |
| 341 | |
| 342 | substr($res, $off, 2, "$;$;"); |
| 343 | $off++; |
| 344 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 345 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 346 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 347 | $sanitise_quote = ''; |
| 348 | substr($res, $off, 2, "$;$;"); |
| 349 | $off++; |
| 350 | next; |
| 351 | } |
| 352 | |
| 353 | # A \ in a string means ignore the next character. |
| 354 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 355 | $c eq "\\") { |
| 356 | substr($res, $off, 2, 'XX'); |
| 357 | $off++; |
| 358 | next; |
| 359 | } |
| 360 | # Regular quotes. |
| 361 | if ($c eq "'" || $c eq '"') { |
| 362 | if ($sanitise_quote eq '') { |
| 363 | $sanitise_quote = $c; |
| 364 | |
| 365 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 366 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 367 | } elsif ($sanitise_quote eq $c) { |
| 368 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 371 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 372 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 373 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 374 | substr($res, $off, 1, $;); |
| 375 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 376 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 377 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 378 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 379 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 383 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 384 | my $clean = 'X' x length($1); |
| 385 | $res =~ s@\<.*\>@<$clean>@; |
| 386 | |
| 387 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 388 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 389 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 390 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 393 | return $res; |
| 394 | } |
| 395 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 396 | sub ctx_statement_block { |
| 397 | my ($linenr, $remain, $off) = @_; |
| 398 | my $line = $linenr - 1; |
| 399 | my $blk = ''; |
| 400 | my $soff = $off; |
| 401 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 402 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 403 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 404 | my $loff = 0; |
| 405 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 406 | my $type = ''; |
| 407 | my $level = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 408 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 409 | my $c; |
| 410 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 411 | |
| 412 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 413 | while (1) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 414 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 415 | # If we are about to drop off the end, pull in more |
| 416 | # context. |
| 417 | if ($off >= $len) { |
| 418 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 419 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 420 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 421 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 422 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 423 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 424 | $len = length($blk); |
| 425 | $line++; |
| 426 | last; |
| 427 | } |
| 428 | # Bail if there is no further context. |
| 429 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 430 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 431 | last; |
| 432 | } |
| 433 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 434 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 435 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 436 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 437 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 438 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 439 | # Statement ends at the ';' or a close '}' at the |
| 440 | # outermost level. |
| 441 | if ($level == 0 && $c eq ';') { |
| 442 | last; |
| 443 | } |
| 444 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 445 | # 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] | 446 | if ($level == 0 && $coff_set == 0 && |
| 447 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 448 | $remainder =~ /^(else)(?:\s|{)/ && |
| 449 | $remainder !~ /^else\s+if\b/) { |
| 450 | $coff = $off + length($1) - 1; |
| 451 | $coff_set = 1; |
| 452 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 453 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 456 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 457 | $level++; |
| 458 | $type = '('; |
| 459 | } |
| 460 | if ($type eq '(' && $c eq ')') { |
| 461 | $level--; |
| 462 | $type = ($level != 0)? '(' : ''; |
| 463 | |
| 464 | if ($level == 0 && $coff < $soff) { |
| 465 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 466 | $coff_set = 1; |
| 467 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 471 | $level++; |
| 472 | $type = '{'; |
| 473 | } |
| 474 | if ($type eq '{' && $c eq '}') { |
| 475 | $level--; |
| 476 | $type = ($level != 0)? '{' : ''; |
| 477 | |
| 478 | if ($level == 0) { |
| 479 | last; |
| 480 | } |
| 481 | } |
| 482 | $off++; |
| 483 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 484 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 485 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 486 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 487 | $line++; |
| 488 | $remain--; |
| 489 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 490 | |
| 491 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 492 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 493 | |
| 494 | #warn "STATEMENT<$statement>\n"; |
| 495 | #warn "CONDITION<$condition>\n"; |
| 496 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 497 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 498 | |
| 499 | return ($statement, $condition, |
| 500 | $line, $remain + 1, $off - $loff + 1, $level); |
| 501 | } |
| 502 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 503 | sub statement_lines { |
| 504 | my ($stmt) = @_; |
| 505 | |
| 506 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 507 | $stmt =~ s/(^|\n)./$1/g; |
| 508 | $stmt =~ s/^\s*//; |
| 509 | $stmt =~ s/\s*$//; |
| 510 | |
| 511 | my @stmt_lines = ($stmt =~ /\n/g); |
| 512 | |
| 513 | return $#stmt_lines + 2; |
| 514 | } |
| 515 | |
| 516 | sub statement_rawlines { |
| 517 | my ($stmt) = @_; |
| 518 | |
| 519 | my @stmt_lines = ($stmt =~ /\n/g); |
| 520 | |
| 521 | return $#stmt_lines + 2; |
| 522 | } |
| 523 | |
| 524 | sub statement_block_size { |
| 525 | my ($stmt) = @_; |
| 526 | |
| 527 | $stmt =~ s/(^|\n)./$1/g; |
| 528 | $stmt =~ s/^\s*{//; |
| 529 | $stmt =~ s/}\s*$//; |
| 530 | $stmt =~ s/^\s*//; |
| 531 | $stmt =~ s/\s*$//; |
| 532 | |
| 533 | my @stmt_lines = ($stmt =~ /\n/g); |
| 534 | my @stmt_statements = ($stmt =~ /;/g); |
| 535 | |
| 536 | my $stmt_lines = $#stmt_lines + 2; |
| 537 | my $stmt_statements = $#stmt_statements + 1; |
| 538 | |
| 539 | if ($stmt_lines > $stmt_statements) { |
| 540 | return $stmt_lines; |
| 541 | } else { |
| 542 | return $stmt_statements; |
| 543 | } |
| 544 | } |
| 545 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 546 | sub ctx_statement_full { |
| 547 | my ($linenr, $remain, $off) = @_; |
| 548 | my ($statement, $condition, $level); |
| 549 | |
| 550 | my (@chunks); |
| 551 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 552 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 553 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 554 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 555 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 556 | push(@chunks, [ $condition, $statement ]); |
| 557 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 558 | return ($level, $linenr, @chunks); |
| 559 | } |
| 560 | |
| 561 | # Pull in the following conditional/block pairs and see if they |
| 562 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 563 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 564 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 565 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 566 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 567 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 568 | #print "C: push\n"; |
| 569 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 575 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 576 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 577 | my $line; |
| 578 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 579 | my $blk = ''; |
| 580 | my @o; |
| 581 | my @c; |
| 582 | my @res = (); |
| 583 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 584 | my $level = 0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 585 | for ($line = $start; $remain > 0; $line++) { |
| 586 | next if ($rawlines[$line] =~ /^-/); |
| 587 | $remain--; |
| 588 | |
| 589 | $blk .= $rawlines[$line]; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 590 | foreach my $c (split(//, $rawlines[$line])) { |
| 591 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 592 | if ($off > 0) { |
| 593 | $off--; |
| 594 | next; |
| 595 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 596 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 597 | if ($c eq $close && $level > 0) { |
| 598 | $level--; |
| 599 | last if ($level == 0); |
| 600 | } elsif ($c eq $open) { |
| 601 | $level++; |
| 602 | } |
| 603 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 604 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 605 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 606 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 609 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 612 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 613 | } |
| 614 | sub ctx_block_outer { |
| 615 | my ($linenr, $remain) = @_; |
| 616 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 617 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 618 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 619 | } |
| 620 | sub ctx_block { |
| 621 | my ($linenr, $remain) = @_; |
| 622 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 623 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 624 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 625 | } |
| 626 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 627 | my ($linenr, $remain, $off) = @_; |
| 628 | |
| 629 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 630 | return @r; |
| 631 | } |
| 632 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 633 | my ($linenr, $remain) = @_; |
| 634 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 635 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 636 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 637 | sub ctx_statement_level { |
| 638 | my ($linenr, $remain, $off) = @_; |
| 639 | |
| 640 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 641 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 642 | |
| 643 | sub ctx_locate_comment { |
| 644 | my ($first_line, $end_line) = @_; |
| 645 | |
| 646 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 647 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 648 | return $current_comment if (defined $current_comment); |
| 649 | |
| 650 | # Look through the context and try and figure out if there is a |
| 651 | # comment. |
| 652 | my $in_comment = 0; |
| 653 | $current_comment = ''; |
| 654 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 655 | my $line = $rawlines[$linenr - 1]; |
| 656 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 657 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 658 | $in_comment = 1; |
| 659 | } |
| 660 | if ($line =~ m@/\*@) { |
| 661 | $in_comment = 1; |
| 662 | } |
| 663 | if (!$in_comment && $current_comment ne '') { |
| 664 | $current_comment = ''; |
| 665 | } |
| 666 | $current_comment .= $line . "\n" if ($in_comment); |
| 667 | if ($line =~ m@\*/@) { |
| 668 | $in_comment = 0; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | chomp($current_comment); |
| 673 | return($current_comment); |
| 674 | } |
| 675 | sub ctx_has_comment { |
| 676 | my ($first_line, $end_line) = @_; |
| 677 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 678 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 679 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 680 | ##print "CMMT: $cmt\n"; |
| 681 | |
| 682 | return ($cmt ne ''); |
| 683 | } |
| 684 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 685 | sub raw_line { |
| 686 | my ($linenr, $cnt) = @_; |
| 687 | |
| 688 | my $offset = $linenr - 1; |
| 689 | $cnt++; |
| 690 | |
| 691 | my $line; |
| 692 | while ($cnt) { |
| 693 | $line = $rawlines[$offset++]; |
| 694 | next if (defined($line) && $line =~ /^-/); |
| 695 | $cnt--; |
| 696 | } |
| 697 | |
| 698 | return $line; |
| 699 | } |
| 700 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 701 | sub cat_vet { |
| 702 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 703 | my ($res, $coded); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 704 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 705 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 706 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 707 | $res .= $1; |
| 708 | if ($2 ne '') { |
| 709 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 710 | $res .= $coded; |
| 711 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 712 | } |
| 713 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 714 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 715 | return $res; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 718 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 719 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 720 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 721 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 722 | |
| 723 | sub annotate_reset { |
| 724 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 725 | $av_pending = '_'; |
| 726 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 727 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 730 | sub annotate_values { |
| 731 | my ($stream, $type) = @_; |
| 732 | |
| 733 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 734 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 735 | my $cur = $stream; |
| 736 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 737 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 738 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 739 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 740 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 741 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 742 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 743 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 744 | print "WS($1)\n" if ($dbg_values > 1); |
| 745 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 746 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 747 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Andy Whitcroft | 8ea3eb9 | 2008-07-23 21:29:08 -0700 | [diff] [blame] | 750 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 751 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 752 | $type = 'T'; |
| 753 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 754 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 755 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 756 | $type = 'T'; |
| 757 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 758 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 759 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 760 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 761 | push(@av_paren_type, $type); |
| 762 | if ($2 ne '') { |
| 763 | $av_pending = 'N'; |
| 764 | } |
| 765 | $type = 'E'; |
| 766 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 767 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 768 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 769 | $av_preprocessor = 1; |
| 770 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 771 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 772 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 773 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 774 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 775 | |
| 776 | push(@av_paren_type, $type); |
| 777 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 778 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 779 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 780 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 781 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 782 | $av_preprocessor = 1; |
| 783 | |
| 784 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 785 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 786 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 787 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 788 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 789 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 790 | |
| 791 | $av_preprocessor = 1; |
| 792 | |
| 793 | # Assume all arms of the conditional end as this |
| 794 | # one does, and continue as if the #endif was not here. |
| 795 | pop(@av_paren_type); |
| 796 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 797 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 798 | |
| 799 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 800 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 801 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 802 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 803 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 804 | $av_pending = $type; |
| 805 | $type = 'N'; |
| 806 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 807 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 808 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 809 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 810 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 811 | } |
| 812 | $type = 'N'; |
| 813 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 814 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 815 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 816 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 817 | $type = 'N'; |
| 818 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 819 | } elsif ($cur =~/^(case)/o) { |
| 820 | print "CASE($1)\n" if ($dbg_values > 1); |
| 821 | $av_pend_colon = 'C'; |
| 822 | $type = 'N'; |
| 823 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 824 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 825 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 826 | $type = 'N'; |
| 827 | |
| 828 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 829 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 830 | push(@av_paren_type, $av_pending); |
| 831 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 832 | $type = 'N'; |
| 833 | |
| 834 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 835 | my $new_type = pop(@av_paren_type); |
| 836 | if ($new_type ne '_') { |
| 837 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 838 | print "PAREN('$1') -> $type\n" |
| 839 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 840 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 841 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 844 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 845 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 846 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 847 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 848 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 849 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 850 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 851 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 852 | } elsif ($type eq 'E') { |
| 853 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 854 | } |
| 855 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 856 | $type = 'V'; |
| 857 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 858 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 859 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 860 | $type = 'V'; |
| 861 | |
| 862 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 863 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 864 | $type = 'N'; |
| 865 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 866 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 867 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 868 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 869 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 870 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 871 | } elsif ($cur =~/^(,)/) { |
| 872 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 873 | $type = 'C'; |
| 874 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 875 | } elsif ($cur =~ /^(\?)/o) { |
| 876 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 877 | $type = 'N'; |
| 878 | |
| 879 | } elsif ($cur =~ /^(:)/o) { |
| 880 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 881 | |
| 882 | substr($var, length($res), 1, $av_pend_colon); |
| 883 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 884 | $type = 'E'; |
| 885 | } else { |
| 886 | $type = 'N'; |
| 887 | } |
| 888 | $av_pend_colon = 'O'; |
| 889 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 890 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 891 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 892 | $type = 'N'; |
| 893 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 894 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 895 | my $variant; |
| 896 | |
| 897 | print "OPV($1)\n" if ($dbg_values > 1); |
| 898 | if ($type eq 'V') { |
| 899 | $variant = 'B'; |
| 900 | } else { |
| 901 | $variant = 'U'; |
| 902 | } |
| 903 | |
| 904 | substr($var, length($res), 1, $variant); |
| 905 | $type = 'N'; |
| 906 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 907 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 908 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 909 | if ($1 ne '++' && $1 ne '--') { |
| 910 | $type = 'N'; |
| 911 | } |
| 912 | |
| 913 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 914 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 915 | } |
| 916 | if (defined $1) { |
| 917 | $cur = substr($cur, length($1)); |
| 918 | $res .= $type x length($1); |
| 919 | } |
| 920 | } |
| 921 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 922 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 925 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 926 | my ($possible, $line) = @_; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 927 | |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 928 | print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 929 | if ($possible !~ /(?: |
| 930 | ^(?: |
| 931 | $Modifier| |
| 932 | $Storage| |
| 933 | $Type| |
| 934 | DEFINE_\S+| |
| 935 | goto| |
| 936 | return| |
| 937 | case| |
| 938 | else| |
| 939 | asm|__asm__| |
| 940 | do |
| 941 | )$| |
| 942 | ^(?:typedef|struct|enum)\b |
| 943 | )/x) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 944 | # Check for modifiers. |
| 945 | $possible =~ s/\s*$Storage\s*//g; |
| 946 | $possible =~ s/\s*$Sparse\s*//g; |
| 947 | if ($possible =~ /^\s*$/) { |
| 948 | |
| 949 | } elsif ($possible =~ /\s/) { |
| 950 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 951 | for my $modifier (split(' ', $possible)) { |
| 952 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
| 953 | push(@modifierList, $modifier); |
| 954 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 955 | |
| 956 | } else { |
| 957 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
| 958 | push(@typeList, $possible); |
| 959 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 960 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 961 | } else { |
| 962 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 966 | my $prefix = ''; |
| 967 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 968 | sub report { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 969 | if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { |
| 970 | return 0; |
| 971 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 972 | my $line = $prefix . $_[0]; |
| 973 | |
| 974 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
| 975 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 976 | push(our @report, $line); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 977 | |
| 978 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 979 | } |
| 980 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 981 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 982 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 983 | sub ERROR { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 984 | if (report("ERROR: $_[0]\n")) { |
| 985 | our $clean = 0; |
| 986 | our $cnt_error++; |
| 987 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 988 | } |
| 989 | sub WARN { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 990 | if (report("WARNING: $_[0]\n")) { |
| 991 | our $clean = 0; |
| 992 | our $cnt_warn++; |
| 993 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 994 | } |
| 995 | sub CHK { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 996 | if ($check && report("CHECK: $_[0]\n")) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 997 | our $clean = 0; |
| 998 | our $cnt_chk++; |
| 999 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1002 | sub check_absolute_file { |
| 1003 | my ($absolute, $herecurr) = @_; |
| 1004 | my $file = $absolute; |
| 1005 | |
| 1006 | ##print "absolute<$absolute>\n"; |
| 1007 | |
| 1008 | # See if any suffix of this path is a path within the tree. |
| 1009 | while ($file =~ s@^[^/]*/@@) { |
| 1010 | if (-f "$root/$file") { |
| 1011 | ##print "file<$file>\n"; |
| 1012 | last; |
| 1013 | } |
| 1014 | } |
| 1015 | if (! -f _) { |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | # It is, so see if the prefix is acceptable. |
| 1020 | my $prefix = $absolute; |
| 1021 | substr($prefix, -length($file)) = ''; |
| 1022 | |
| 1023 | ##print "prefix<$prefix>\n"; |
| 1024 | if ($prefix ne ".../") { |
| 1025 | WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr); |
| 1026 | } |
| 1027 | } |
| 1028 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1029 | sub process { |
| 1030 | my $filename = shift; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1031 | |
| 1032 | my $linenr=0; |
| 1033 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1034 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1035 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1036 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1037 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1038 | my $length; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1039 | my $indent; |
| 1040 | my $previndent=0; |
| 1041 | my $stashindent=0; |
| 1042 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1043 | our $clean = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1044 | my $signoff = 0; |
| 1045 | my $is_patch = 0; |
| 1046 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1047 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1048 | our $cnt_lines = 0; |
| 1049 | our $cnt_error = 0; |
| 1050 | our $cnt_warn = 0; |
| 1051 | our $cnt_chk = 0; |
| 1052 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1053 | # Trace the real file/line as we go. |
| 1054 | my $realfile = ''; |
| 1055 | my $realline = 0; |
| 1056 | my $realcnt = 0; |
| 1057 | my $here = ''; |
| 1058 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1059 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1060 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1061 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1062 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1063 | my $prev_values = 'E'; |
| 1064 | |
| 1065 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1066 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1067 | my %suppress_whiletrailers; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1068 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1069 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1070 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1071 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1072 | my @setup_docs = (); |
| 1073 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1074 | |
| 1075 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1076 | my $line; |
| 1077 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1078 | $linenr++; |
| 1079 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1080 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1081 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1082 | $setup_docs = 0; |
| 1083 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 1084 | $setup_docs = 1; |
| 1085 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1086 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1087 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1088 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 1089 | $realline=$1-1; |
| 1090 | if (defined $2) { |
| 1091 | $realcnt=$3+1; |
| 1092 | } else { |
| 1093 | $realcnt=1+1; |
| 1094 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1095 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1096 | |
| 1097 | # Guestimate if this is a continuing comment. Run |
| 1098 | # the context looking for a comment "edge". If this |
| 1099 | # edge is a close comment then we must be in a comment |
| 1100 | # at context start. |
| 1101 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 1102 | my $cnt = $realcnt; |
| 1103 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 1104 | next if (defined $rawlines[$ln - 1] && |
| 1105 | $rawlines[$ln - 1] =~ /^-/); |
| 1106 | $cnt--; |
| 1107 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 1108 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1109 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 1110 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 1111 | ($edge) = $1; |
| 1112 | last; |
| 1113 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1114 | } |
| 1115 | if (defined $edge && $edge eq '*/') { |
| 1116 | $in_comment = 1; |
| 1117 | } |
| 1118 | |
| 1119 | # Guestimate if this is a continuing comment. If this |
| 1120 | # is the start of a diff block and this line starts |
| 1121 | # ' *' then it is very likely a comment. |
| 1122 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 1123 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1124 | { |
| 1125 | $in_comment = 1; |
| 1126 | } |
| 1127 | |
| 1128 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 1129 | sanitise_line_reset($in_comment); |
| 1130 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1131 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1132 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1133 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1134 | $line = sanitise_line($rawline); |
| 1135 | } |
| 1136 | push(@lines, $line); |
| 1137 | |
| 1138 | if ($realcnt > 1) { |
| 1139 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 1140 | } else { |
| 1141 | $realcnt = 0; |
| 1142 | } |
| 1143 | |
| 1144 | #print "==>$rawline\n"; |
| 1145 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1146 | |
| 1147 | if ($setup_docs && $line =~ /^\+/) { |
| 1148 | push(@setup_docs, $line); |
| 1149 | } |
| 1150 | } |
| 1151 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1152 | $prefix = ''; |
| 1153 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1154 | $realcnt = 0; |
| 1155 | $linenr = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1156 | foreach my $line (@lines) { |
| 1157 | $linenr++; |
| 1158 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1159 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1160 | my $hunk_line = ($realcnt != 0); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1161 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1162 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1163 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1164 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1165 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1166 | $realline=$1-1; |
| 1167 | if (defined $2) { |
| 1168 | $realcnt=$3+1; |
| 1169 | } else { |
| 1170 | $realcnt=1+1; |
| 1171 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1172 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1173 | $prev_values = 'E'; |
| 1174 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1175 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1176 | %suppress_whiletrailers = (); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1177 | next; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1178 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1179 | # track the line number as we move through the hunk, note that |
| 1180 | # new versions of GNU diff omit the leading space on completely |
| 1181 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1182 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1183 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1184 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1185 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1186 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1187 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1188 | |
| 1189 | # Track the previous line. |
| 1190 | ($prevline, $stashline) = ($stashline, $line); |
| 1191 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1192 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 1193 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1194 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1195 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1196 | } elsif ($realcnt == 1) { |
| 1197 | $realcnt--; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | #make up the handle for any error we report on this line |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1201 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 1202 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 1203 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1204 | $here = "#$linenr: " if (!$file); |
| 1205 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1206 | |
| 1207 | # extract the filename as it passes |
| 1208 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 1209 | $realfile = $1; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1210 | $realfile =~ s@^([^/]*)/@@; |
| 1211 | |
| 1212 | $p1_prefix = $1; |
| 1213 | if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") { |
| 1214 | WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
| 1215 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1216 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 1217 | if ($realfile =~ m@^include/asm/@) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1218 | ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
| 1219 | } |
| 1220 | next; |
| 1221 | } |
| 1222 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 1223 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1224 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1225 | my $hereline = "$here\n$rawline\n"; |
| 1226 | my $herecurr = "$here\n$rawline\n"; |
| 1227 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1228 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1229 | $cnt_lines++ if ($realcnt != 0); |
| 1230 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1231 | #check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1232 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1233 | # This is a signoff, if ugly, so do not double report. |
| 1234 | $signoff++; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1235 | if (!($line =~ /^\s*Signed-off-by:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1236 | WARN("Signed-off-by: is the preferred form\n" . |
| 1237 | $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1238 | } |
| 1239 | if ($line =~ /^\s*signed-off-by:\S/i) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1240 | WARN("space required after Signed-off-by:\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1241 | $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1245 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1246 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1247 | ERROR("patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1248 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1251 | # Check for absolute kernel paths. |
| 1252 | if ($tree) { |
| 1253 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 1254 | my $file = $1; |
| 1255 | |
| 1256 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 1257 | check_absolute_file($1, $herecurr)) { |
| 1258 | # |
| 1259 | } else { |
| 1260 | check_absolute_file($file, $herecurr); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1265 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 1266 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1267 | $rawline !~ m/^$UTF8*$/) { |
| 1268 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 1269 | |
| 1270 | my $blank = copy_spacing($rawline); |
| 1271 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 1272 | my $hereptr = "$hereline$ptr\n"; |
| 1273 | |
| 1274 | 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] | 1275 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1276 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1277 | # ignore non-hunk lines and lines being removed |
| 1278 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1279 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1280 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1281 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1282 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1283 | ERROR("DOS line endings\n" . $herevet); |
| 1284 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1285 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1286 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1287 | ERROR("trailing whitespace\n" . $herevet); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1288 | } |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1289 | |
| 1290 | # check we are in a valid source file if not then ignore this hunk |
| 1291 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
| 1292 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1293 | #80 column limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1294 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1295 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
| 1296 | $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ && |
| 1297 | $length > 80) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1298 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1299 | WARN("line over 80 characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1302 | # check for adding lines without a newline. |
| 1303 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
| 1304 | WARN("adding a line without newline at end of file\n" . $herecurr); |
| 1305 | } |
| 1306 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 1307 | # check we are in a valid source file C or perl if not then ignore this hunk |
| 1308 | next if ($realfile !~ /\.(h|c|pl)$/); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1309 | |
| 1310 | # at the beginning of a line any tabs must come first and anything |
| 1311 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1312 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 1313 | $rawline =~ /^\+\s* \s*/) { |
| 1314 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1315 | ERROR("code indent should use tabs where possible\n" . $herevet); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 1318 | # check we are in a valid C source file if not then ignore this hunk |
| 1319 | next if ($realfile !~ /\.(h|c)$/); |
| 1320 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1321 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1322 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1323 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
| 1324 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1325 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1326 | # Check for potential 'bare' types |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1327 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next); |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 1328 | if ($realcnt && $line =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1329 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 1330 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1331 | $stat =~ s/\n./\n /g; |
| 1332 | $cond =~ s/\n./\n /g; |
| 1333 | |
| 1334 | my $s = $stat; |
| 1335 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1336 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1337 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1338 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1339 | |
| 1340 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1341 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1342 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1343 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1344 | } 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] | 1345 | my $type = $1; |
| 1346 | $type =~ s/\s+/ /g; |
| 1347 | possible($type, "A:" . $s); |
| 1348 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1349 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1350 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1351 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1352 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1353 | |
| 1354 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1355 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1356 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1357 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1358 | |
| 1359 | # Check for any sort of function declaration. |
| 1360 | # int foo(something bar, other baz); |
| 1361 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1362 | 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] | 1363 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1364 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1365 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1366 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1367 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1368 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1369 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1370 | 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] | 1371 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1372 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1379 | # |
| 1380 | # Checks which may be anchored in the context. |
| 1381 | # |
| 1382 | |
| 1383 | # Check for switch () and associated case and default |
| 1384 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1385 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 1386 | my $err = ''; |
| 1387 | my $sep = ''; |
| 1388 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 1389 | shift(@ctx); |
| 1390 | for my $ctx (@ctx) { |
| 1391 | my ($clen, $cindent) = line_stats($ctx); |
| 1392 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 1393 | $indent != $cindent) { |
| 1394 | $err .= "$sep$ctx\n"; |
| 1395 | $sep = ''; |
| 1396 | } else { |
| 1397 | $sep = "[...]\n"; |
| 1398 | } |
| 1399 | } |
| 1400 | if ($err ne '') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1401 | 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] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 1406 | # or if that brace on the next line is for something else |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1407 | 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] | 1408 | my $pre_ctx = "$1$2"; |
| 1409 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1410 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1411 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 1412 | my $ctx = join("\n", @ctx); |
| 1413 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1414 | my $ctx_ln = $linenr; |
| 1415 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1416 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1417 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 1418 | defined $lines[$ctx_ln - 1] && |
| 1419 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 1420 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 1421 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1422 | $ctx_ln++; |
| 1423 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1424 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 1425 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 1426 | #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] | 1427 | |
| 1428 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
| 1429 | ERROR("that open brace { should be on the previous line\n" . |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1430 | "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1431 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1432 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 1433 | $ctx =~ /\)\s*\;\s*$/ && |
| 1434 | defined $lines[$ctx_ln - 1]) |
| 1435 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1436 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 1437 | if ($nindent > $indent) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1438 | WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1439 | "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1440 | } |
| 1441 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1444 | # Check relative indent for conditionals and blocks. |
| 1445 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
| 1446 | my ($s, $c) = ($stat, $cond); |
| 1447 | |
| 1448 | substr($s, 0, length($c), ''); |
| 1449 | |
| 1450 | # Make sure we remove the line prefixes as we have |
| 1451 | # none on the first line, and are going to readd them |
| 1452 | # where necessary. |
| 1453 | $s =~ s/\n./\n/gs; |
| 1454 | |
| 1455 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1456 | my @newlines = ($c =~ /\n/gs); |
| 1457 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1458 | |
| 1459 | # We want to check the first line inside the block |
| 1460 | # starting at the end of the conditional, so remove: |
| 1461 | # 1) any blank line termination |
| 1462 | # 2) any opening brace { on end of the line |
| 1463 | # 3) any do (...) { |
| 1464 | my $continuation = 0; |
| 1465 | my $check = 0; |
| 1466 | $s =~ s/^.*\bdo\b//; |
| 1467 | $s =~ s/^\s*{//; |
| 1468 | if ($s =~ s/^\s*\\//) { |
| 1469 | $continuation = 1; |
| 1470 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1471 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1472 | $check = 1; |
| 1473 | $cond_lines++; |
| 1474 | } |
| 1475 | |
| 1476 | # Also ignore a loop construct at the end of a |
| 1477 | # preprocessor statement. |
| 1478 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 1479 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 1480 | $check = 0; |
| 1481 | } |
| 1482 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1483 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 1484 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1485 | while ($cond_ptr != $cond_lines) { |
| 1486 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1487 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 1488 | # If we see an #else/#elif then the code |
| 1489 | # is not linear. |
| 1490 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 1491 | $check = 0; |
| 1492 | } |
| 1493 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1494 | # Ignore: |
| 1495 | # 1) blank lines, they should be at 0, |
| 1496 | # 2) preprocessor lines, and |
| 1497 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 1498 | if ($continuation || |
| 1499 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1500 | $s =~ /^\s*#\s*?/ || |
| 1501 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 1502 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1503 | $s =~ s/^.*?\n//; |
| 1504 | $cond_lines++; |
| 1505 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | my (undef, $sindent) = line_stats("+" . $s); |
| 1509 | my $stat_real = raw_line($linenr, $cond_lines); |
| 1510 | |
| 1511 | # Check if either of these lines are modified, else |
| 1512 | # this is not this patch's fault. |
| 1513 | if (!defined($stat_real) || |
| 1514 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 1515 | $check = 0; |
| 1516 | } |
| 1517 | if (defined($stat_real) && $cond_lines > 1) { |
| 1518 | $stat_real = "[...]\n$stat_real"; |
| 1519 | } |
| 1520 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1521 | #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] | 1522 | |
| 1523 | if ($check && (($sindent % 8) != 0 || |
| 1524 | ($sindent <= $indent && $s ne ''))) { |
| 1525 | WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
| 1526 | } |
| 1527 | } |
| 1528 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1529 | # Track the 'values' across context and added lines. |
| 1530 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1531 | my ($curr_values, $curr_vars) = |
| 1532 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1533 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1534 | if ($dbg_values) { |
| 1535 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1536 | print "$linenr > .$outline\n"; |
| 1537 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1538 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1539 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1540 | $prev_values = substr($curr_values, -1); |
| 1541 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1542 | #ignore lines not being added |
| 1543 | if ($line=~/^[^\+]/) {next;} |
| 1544 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1545 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 1546 | if ($dbg_type) { |
| 1547 | if ($line =~ /^.\s*$Declare\s*$/) { |
| 1548 | ERROR("TEST: is type\n" . $herecurr); |
| 1549 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
| 1550 | ERROR("TEST: is not type ($1 is)\n". $herecurr); |
| 1551 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1552 | next; |
| 1553 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 1554 | # TEST: allow direct testing of the attribute matcher. |
| 1555 | if ($dbg_attr) { |
| 1556 | if ($line =~ /^.\s*$Attribute\s*$/) { |
| 1557 | ERROR("TEST: is attr\n" . $herecurr); |
| 1558 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) { |
| 1559 | ERROR("TEST: is not attr ($1 is)\n". $herecurr); |
| 1560 | } |
| 1561 | next; |
| 1562 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1563 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1564 | # check for initialisation to aggregates open brace on the next line |
| 1565 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && |
| 1566 | $line =~ /^.\s*{/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1567 | ERROR("that open brace { should be on the previous line\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1570 | # |
| 1571 | # Checks which are anchored on the added line. |
| 1572 | # |
| 1573 | |
| 1574 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1575 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1576 | my $path = $1; |
| 1577 | if ($path =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1578 | ERROR("malformed #include filename\n" . |
| 1579 | $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1580 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1581 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1582 | |
| 1583 | # no C99 // comments |
| 1584 | if ($line =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1585 | ERROR("do not use C99 // comments\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1586 | } |
| 1587 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1588 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1589 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1590 | |
| 1591 | #EXPORT_SYMBOL should immediately follow its function closing }. |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1592 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || |
| 1593 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 1594 | my $name = $1; |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 1595 | if ($prevline !~ /(?: |
| 1596 | ^.}| |
| 1597 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 1598 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 1599 | ^.LIST_HEAD\(\Q$name\E\)| |
| 1600 | ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 1601 | \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[) |
| 1602 | )/x) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1603 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1607 | # check for external initialisers. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1608 | if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1609 | ERROR("do not initialise externals to 0 or NULL\n" . |
| 1610 | $herecurr); |
| 1611 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1612 | # check for static initialisers. |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 1613 | if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1614 | ERROR("do not initialise statics to 0 or NULL\n" . |
| 1615 | $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1618 | # check for new typedefs, only function parameters and sparse annotations |
| 1619 | # make sense. |
| 1620 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1621 | $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1622 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 1623 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1624 | $line !~ /\b__bitwise(?:__|)\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1625 | WARN("do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1629 | # (char*[ const]) |
| 1630 | if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) { |
| 1631 | my ($from, $to) = ($1, $1); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1632 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1633 | # Should start with a space. |
| 1634 | $to =~ s/^(\S)/ $1/; |
| 1635 | # Should not end with a space. |
| 1636 | $to =~ s/\s+$//; |
| 1637 | # '*'s should not have spaces between. |
| 1638 | while ($to =~ s/(.)\s\*/$1\*/) { |
| 1639 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1640 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1641 | #print "from<$from> to<$to>\n"; |
| 1642 | if ($from ne $to) { |
| 1643 | ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); |
| 1644 | } |
| 1645 | } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) { |
| 1646 | my ($from, $to, $ident) = ($1, $1, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1647 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1648 | # Should start with a space. |
| 1649 | $to =~ s/^(\S)/ $1/; |
| 1650 | # Should not end with a space. |
| 1651 | $to =~ s/\s+$//; |
| 1652 | # '*'s should not have spaces between. |
| 1653 | while ($to =~ s/(.)\s\*/$1\*/) { |
| 1654 | } |
| 1655 | # Modifiers should have spaces. |
| 1656 | $to =~ s/(\b$Modifier$)/$1 /; |
| 1657 | |
| 1658 | #print "from<$from> to<$to>\n"; |
| 1659 | if ($from ne $to) { |
| 1660 | ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); |
| 1661 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | # # no BUG() or BUG_ON() |
| 1665 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 1666 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 1667 | # print "$herecurr"; |
| 1668 | # $clean = 0; |
| 1669 | # } |
| 1670 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1671 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1672 | 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] | 1673 | } |
| 1674 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1675 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 1676 | # same line do not need a level, so we use the current block context |
| 1677 | # to try and find and validate the current printk. In summary the current |
| 1678 | # printk includes all preceeding printk's which have no newline on the end. |
| 1679 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1680 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1681 | my $ok = 0; |
| 1682 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 1683 | #print "CHECK<$lines[$ln - 1]\n"; |
| 1684 | # we have a preceeding printk if it ends |
| 1685 | # with "\n" ignore it, else it is to blame |
| 1686 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 1687 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 1688 | $ok = 1; |
| 1689 | } |
| 1690 | last; |
| 1691 | } |
| 1692 | } |
| 1693 | if ($ok == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1694 | WARN("printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1695 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1698 | # function brace can't be on same line, except for #defines of do while, |
| 1699 | # or if closed on same line |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1700 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
| 1701 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1702 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1703 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1704 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1705 | # open braces for enum, union and struct go on the same line. |
| 1706 | if ($line =~ /^.\s*{/ && |
| 1707 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
| 1708 | ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); |
| 1709 | } |
| 1710 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 1711 | # check for spacing round square brackets; allowed: |
| 1712 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 1713 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 1714 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 1715 | while ($line =~ /(.*?\s)\[/g) { |
| 1716 | my ($where, $prefix) = ($-[1], $1); |
| 1717 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 1718 | ($where != 0 || $prefix !~ /^.\s+$/) && |
| 1719 | $prefix !~ /{\s+$/) { |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 1720 | ERROR("space prohibited before open square bracket '['\n" . $herecurr); |
| 1721 | } |
| 1722 | } |
| 1723 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1724 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1725 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1726 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1727 | my $ctx_before = substr($line, 0, $-[1]); |
| 1728 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1729 | |
| 1730 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1731 | if ($name =~ /^(?: |
| 1732 | if|for|while|switch|return|case| |
| 1733 | volatile|__volatile__| |
| 1734 | __attribute__|format|__extension__| |
| 1735 | asm|__asm__)$/x) |
| 1736 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1737 | |
| 1738 | # cpp #define statements have non-optional spaces, ie |
| 1739 | # if there is a space between the name and the open |
| 1740 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1741 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1742 | |
| 1743 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1744 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1745 | |
| 1746 | # If this whole things ends with a type its most |
| 1747 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1748 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1749 | |
| 1750 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1751 | WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1752 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1753 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1754 | # Check operator spacing. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1755 | if (!($line=~/\#\s*include/)) { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1756 | my $ops = qr{ |
| 1757 | <<=|>>=|<=|>=|==|!=| |
| 1758 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 1759 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1760 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
| 1761 | \?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1762 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1763 | my @elements = split(/($ops|;)/, $opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1764 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1765 | |
| 1766 | my $blank = copy_spacing($opline); |
| 1767 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1768 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1769 | $off += length($elements[$n]); |
| 1770 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1771 | # Pick up the preceeding and succeeding characters. |
| 1772 | my $ca = substr($opline, 0, $off); |
| 1773 | my $cc = ''; |
| 1774 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 1775 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 1776 | } |
| 1777 | my $cb = "$ca$;$cc"; |
| 1778 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1779 | my $a = ''; |
| 1780 | $a = 'V' if ($elements[$n] ne ''); |
| 1781 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1782 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1783 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 1784 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1785 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1786 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1787 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1788 | |
| 1789 | my $c = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1790 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1791 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 1792 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1793 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1794 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 1795 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1796 | $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1797 | } else { |
| 1798 | $c = 'E'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1801 | my $ctx = "${a}x${c}"; |
| 1802 | |
| 1803 | my $at = "(ctx:$ctx)"; |
| 1804 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1805 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1806 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1807 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1808 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1809 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1810 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1811 | # Get the full operator variant. |
| 1812 | my $opv = $op . substr($curr_vars, $off, 1); |
| 1813 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1814 | # Ignore operators passed as parameters. |
| 1815 | if ($op_type ne 'V' && |
| 1816 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { |
| 1817 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1818 | # # Ignore comments |
| 1819 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1820 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1821 | # ; 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] | 1822 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1823 | if ($ctx !~ /.x[WEBC]/ && |
| 1824 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1825 | ERROR("space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | # // is a comment |
| 1829 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1830 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1831 | # No spaces for: |
| 1832 | # -> |
| 1833 | # : when part of a bitfield |
| 1834 | } elsif ($op eq '->' || $opv eq ':B') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1835 | if ($ctx =~ /Wx.|.xW/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1836 | ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | # , must have a space on the right. |
| 1840 | } elsif ($op eq ',') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1841 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1842 | ERROR("space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1845 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1846 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1847 | #warn "'*' is part of type\n"; |
| 1848 | |
| 1849 | # unary operators should have a space before and |
| 1850 | # none after. May be left adjacent to another |
| 1851 | # unary operator, or a cast |
| 1852 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1853 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1854 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1855 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1856 | ERROR("space required before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1857 | } |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1858 | if ($op eq '*' && $cc =~/\s*const\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1859 | # A unary '*' may be const |
| 1860 | |
| 1861 | } elsif ($ctx =~ /.xW/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1862 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | # unary ++ and unary -- are allowed no space on one side. |
| 1866 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1867 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
| 1868 | ERROR("space required one side of that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1869 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1870 | if ($ctx =~ /Wx[BE]/ || |
| 1871 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
| 1872 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1873 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1874 | if ($ctx =~ /ExW/) { |
| 1875 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); |
| 1876 | } |
| 1877 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1878 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1879 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1880 | } elsif ($op eq '<<' or $op eq '>>' or |
| 1881 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 1882 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1883 | $op eq '*' or $op eq '/' or |
| 1884 | $op eq '%') |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1885 | { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1886 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1887 | ERROR("need consistent spacing around '$op' $at\n" . |
| 1888 | $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1891 | # A colon needs no spaces before when it is |
| 1892 | # terminating a case value or a label. |
| 1893 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 1894 | if ($ctx =~ /Wx./) { |
| 1895 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); |
| 1896 | } |
| 1897 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1898 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1899 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1900 | my $ok = 0; |
| 1901 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1902 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1903 | if (($op eq '<' && |
| 1904 | $cc =~ /^\S+\@\S+>/) || |
| 1905 | ($op eq '>' && |
| 1906 | $ca =~ /<\S+\@\S+$/)) |
| 1907 | { |
| 1908 | $ok = 1; |
| 1909 | } |
| 1910 | |
| 1911 | # Ignore ?: |
| 1912 | if (($opv eq ':O' && $ca =~ /\?$/) || |
| 1913 | ($op eq '?' && $cc =~ /^:/)) { |
| 1914 | $ok = 1; |
| 1915 | } |
| 1916 | |
| 1917 | if ($ok == 0) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1918 | ERROR("spaces required around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1919 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1920 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1921 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1925 | # check for multiple assignments |
| 1926 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1927 | CHK("multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1928 | } |
| 1929 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1930 | ## # check for multiple declarations, allowing for a function declaration |
| 1931 | ## # continuation. |
| 1932 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 1933 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 1934 | ## |
| 1935 | ## # Remove any bracketed sections to ensure we do not |
| 1936 | ## # falsly report the parameters of functions. |
| 1937 | ## my $ln = $line; |
| 1938 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 1939 | ## } |
| 1940 | ## if ($ln =~ /,/) { |
| 1941 | ## WARN("declaring multiple variables together should be avoided\n" . $herecurr); |
| 1942 | ## } |
| 1943 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1944 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1945 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1946 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 1947 | $line =~ /do{/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1948 | ERROR("space required before the open brace '{'\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | # closing brace should have a space following it when it has anything |
| 1952 | # on the line |
| 1953 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1954 | ERROR("space required after that close brace '}'\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1957 | # check spacing on square brackets |
| 1958 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1959 | ERROR("space prohibited after that open square bracket '['\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1960 | } |
| 1961 | if ($line =~ /\s\]/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1962 | ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1965 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1966 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 1967 | $line !~ /for\s*\(\s+;/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1968 | ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1969 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1970 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1971 | $line !~ /for\s*\(.*;\s+\)/ && |
| 1972 | $line !~ /:\s+\)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1973 | ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1976 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1977 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1978 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1979 | WARN("labels should not be indented\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1982 | # Return is not a function. |
| 1983 | if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { |
| 1984 | my $spacing = $1; |
| 1985 | my $value = $2; |
| 1986 | |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1987 | # Flatten any parentheses |
Andy Whitcroft | fee61c4 | 2008-07-23 21:28:56 -0700 | [diff] [blame] | 1988 | $value =~ s/\)\(/\) \(/g; |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1989 | while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | if ($value =~ /^(?:$Ident|-?$Constant)$/) { |
| 1993 | ERROR("return is not a function, parentheses are not required\n" . $herecurr); |
| 1994 | |
| 1995 | } elsif ($spacing !~ /\s+/) { |
| 1996 | ERROR("space required before the open parenthesis '('\n" . $herecurr); |
| 1997 | } |
| 1998 | } |
| 1999 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2000 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2001 | if ($line=~/\b(if|while|for|switch)\(/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2002 | ERROR("space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2005 | # Check for illegal assignment in if conditional -- and check for trailing |
| 2006 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2007 | if ($line =~ /do\s*(?!{)/) { |
| 2008 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 2009 | $remain_next, $off_next); |
| 2010 | $stat_next =~ s/\n./\n /g; |
| 2011 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 2012 | |
| 2013 | if ($stat_next =~ /^\s*while\b/) { |
| 2014 | # If the statement carries leading newlines, |
| 2015 | # then count those as offsets. |
| 2016 | my ($whitespace) = |
| 2017 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 2018 | my $offset = |
| 2019 | statement_rawlines($whitespace) - 1; |
| 2020 | |
| 2021 | $suppress_whiletrailers{$line_nr_next + |
| 2022 | $offset} = 1; |
| 2023 | } |
| 2024 | } |
| 2025 | if (!defined $suppress_whiletrailers{$linenr} && |
| 2026 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2027 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2028 | |
| 2029 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2030 | ERROR("do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | # Find out what is on the end of the line after the |
| 2034 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2035 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2036 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2037 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2038 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 2039 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2040 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2041 | # Find out how long the conditional actually is. |
| 2042 | my @newlines = ($c =~ /\n/gs); |
| 2043 | my $cond_lines = 1 + $#newlines; |
| 2044 | |
| 2045 | my $stat_real = raw_line($linenr, $cond_lines); |
| 2046 | if (defined($stat_real) && $cond_lines > 1) { |
| 2047 | $stat_real = "[...]\n$stat_real"; |
| 2048 | } |
| 2049 | |
| 2050 | ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2054 | # Check for bitwise tests written as boolean |
| 2055 | if ($line =~ / |
| 2056 | (?: |
| 2057 | (?:\[|\(|\&\&|\|\|) |
| 2058 | \s*0[xX][0-9]+\s* |
| 2059 | (?:\&\&|\|\|) |
| 2060 | | |
| 2061 | (?:\&\&|\|\|) |
| 2062 | \s*0[xX][0-9]+\s* |
| 2063 | (?:\&\&|\|\||\)|\]) |
| 2064 | )/x) |
| 2065 | { |
| 2066 | WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
| 2067 | } |
| 2068 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2069 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2070 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 2071 | my $s = $1; |
| 2072 | $s =~ s/$;//g; # Remove any comments |
| 2073 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
| 2074 | ERROR("trailing statements should be on next line\n" . $herecurr); |
| 2075 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2076 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 2077 | # case and default should not have general statements after them |
| 2078 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 2079 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 2080 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 2081 | \s*return\s+ |
| 2082 | )/xg) |
| 2083 | { |
| 2084 | ERROR("trailing statements should be on next line\n" . $herecurr); |
| 2085 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2086 | |
| 2087 | # Check for }<nl>else {, these must be at the same |
| 2088 | # indent level to be relevant to each other. |
| 2089 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 2090 | $previndent == $indent) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2091 | ERROR("else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2094 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
| 2095 | $previndent == $indent) { |
| 2096 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 2097 | |
| 2098 | # Find out what is on the end of the line after the |
| 2099 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2100 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2101 | $s =~ s/\n.*//g; |
| 2102 | |
| 2103 | if ($s =~ /^\s*;/) { |
| 2104 | ERROR("while should follow close brace '}'\n" . $hereprev); |
| 2105 | } |
| 2106 | } |
| 2107 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2108 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
| 2109 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
| 2110 | # print "No studly caps, use _\n"; |
| 2111 | # print "$herecurr"; |
| 2112 | # $clean = 0; |
| 2113 | # } |
| 2114 | |
| 2115 | #no spaces allowed after \ in define |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2116 | if ($line=~/\#\s*define.*\\\s$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2117 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2120 | #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] | 2121 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2122 | my $file = "$1.h"; |
| 2123 | my $checkfile = "include/linux/$file"; |
| 2124 | if (-f "$root/$checkfile" && |
| 2125 | $realfile ne $checkfile && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2126 | $1 ne 'irq') |
| 2127 | { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2128 | if ($realfile =~ m{^arch/}) { |
| 2129 | CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 2130 | } else { |
| 2131 | WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 2132 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2136 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 2137 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2138 | # in a known good container |
Andy Whitcroft | b8f96a31 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 2139 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 2140 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2141 | my $ln = $linenr; |
| 2142 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2143 | my ($off, $dstat, $dcond, $rest); |
| 2144 | my $ctx = ''; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2145 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2146 | my $args = defined($1); |
| 2147 | |
| 2148 | # Find the end of the macro and limit our statement |
| 2149 | # search to that. |
| 2150 | while ($cnt > 0 && defined $lines[$ln - 1] && |
| 2151 | $lines[$ln - 1] =~ /^(?:-|..*\\$)/) |
| 2152 | { |
| 2153 | $ctx .= $rawlines[$ln - 1] . "\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 2154 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2155 | $ln++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2156 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2157 | $ctx .= $rawlines[$ln - 1]; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2158 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2159 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 2160 | ctx_statement_block($linenr, $ln - $linenr + 1, 0); |
| 2161 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 2162 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2163 | |
| 2164 | # Extract the remainder of the define (if any) and |
| 2165 | # rip off surrounding spaces, and trailing \'s. |
| 2166 | $rest = ''; |
Andy Whitcroft | 636d140 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 2167 | while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { |
| 2168 | #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] | 2169 | if ($off != 0 || $lines[$ln - 1] !~ /^-/) { |
| 2170 | $rest .= substr($lines[$ln - 1], $off) . "\n"; |
| 2171 | $cnt--; |
| 2172 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2173 | $ln++; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2174 | $off = 0; |
| 2175 | } |
| 2176 | $rest =~ s/\\\n.//g; |
| 2177 | $rest =~ s/^\s*//s; |
| 2178 | $rest =~ s/\s*$//s; |
| 2179 | |
| 2180 | # Clean up the original statement. |
| 2181 | if ($args) { |
| 2182 | substr($dstat, 0, length($dcond), ''); |
| 2183 | } else { |
| 2184 | $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; |
| 2185 | } |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 2186 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2187 | $dstat =~ s/\\\n.//g; |
| 2188 | $dstat =~ s/^\s*//s; |
| 2189 | $dstat =~ s/\s*$//s; |
| 2190 | |
| 2191 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 2192 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 2193 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
| 2194 | $dstat =~ s/\[[^\{\}]*\]/1/) |
| 2195 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2196 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2197 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2198 | my $exceptions = qr{ |
| 2199 | $Declare| |
| 2200 | module_param_named| |
| 2201 | MODULE_PARAM_DESC| |
| 2202 | DECLARE_PER_CPU| |
| 2203 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 2204 | __typeof__\(| |
| 2205 | \.$Ident\s*=\s* |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2206 | }x; |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 2207 | #print "REST<$rest> dstat<$dstat>\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2208 | if ($rest ne '') { |
| 2209 | if ($rest !~ /while\s*\(/ && |
| 2210 | $dstat !~ /$exceptions/) |
| 2211 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2212 | 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] | 2213 | } |
| 2214 | |
| 2215 | } elsif ($ctx !~ /;/) { |
| 2216 | if ($dstat ne '' && |
| 2217 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && |
| 2218 | $dstat !~ /$exceptions/ && |
Andy Whitcroft | b132e5d | 2008-10-15 22:02:31 -0700 | [diff] [blame] | 2219 | $dstat !~ /^\.$Ident\s*=/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2220 | $dstat =~ /$Operators/) |
| 2221 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2222 | 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] | 2223 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2224 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame^] | 2227 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 2228 | # all assignments may have only one of the following with an assignment: |
| 2229 | # . |
| 2230 | # ALIGN(...) |
| 2231 | # VMLINUX_SYMBOL(...) |
| 2232 | if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { |
| 2233 | WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
| 2234 | } |
| 2235 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2236 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2237 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 2238 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2239 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2240 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2241 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 2242 | if ($#chunks > 0 && $level == 0) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2243 | my $allowed = 0; |
| 2244 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2245 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2246 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2247 | for my $chunk (@chunks) { |
| 2248 | my ($cond, $block) = @{$chunk}; |
| 2249 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2250 | # If the condition carries leading newlines, then count those as offsets. |
| 2251 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 2252 | my $offset = statement_rawlines($whitespace) - 1; |
| 2253 | |
| 2254 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 2255 | |
| 2256 | # We have looked at and allowed this specific line. |
| 2257 | $suppress_ifbraces{$ln + $offset} = 1; |
| 2258 | |
| 2259 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2260 | $ln += statement_rawlines($block) - 1; |
| 2261 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2262 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2263 | |
| 2264 | $seen++ if ($block =~ /^\s*{/); |
| 2265 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2266 | #print "cond<$cond> block<$block> allowed<$allowed>\n"; |
| 2267 | if (statement_lines($cond) > 1) { |
| 2268 | #print "APW: ALLOWED: cond<$cond>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2269 | $allowed = 1; |
| 2270 | } |
| 2271 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2272 | #print "APW: ALLOWED: block<$block>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2273 | $allowed = 1; |
| 2274 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2275 | if (statement_block_size($block) > 1) { |
| 2276 | #print "APW: ALLOWED: lines block<$block>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2277 | $allowed = 1; |
| 2278 | } |
| 2279 | } |
| 2280 | if ($seen && !$allowed) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2281 | 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] | 2282 | } |
| 2283 | } |
| 2284 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2285 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2286 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2287 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2288 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2289 | # Check the pre-context. |
| 2290 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 2291 | #print "APW: ALLOWED: pre<$1>\n"; |
| 2292 | $allowed = 1; |
| 2293 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2294 | |
| 2295 | my ($level, $endln, @chunks) = |
| 2296 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 2297 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2298 | # Check the condition. |
| 2299 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2300 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2301 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2302 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2303 | } |
| 2304 | if (statement_lines($cond) > 1) { |
| 2305 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 2306 | $allowed = 1; |
| 2307 | } |
| 2308 | if ($block =~/\b(?:if|for|while)\b/) { |
| 2309 | #print "APW: ALLOWED: block<$block>\n"; |
| 2310 | $allowed = 1; |
| 2311 | } |
| 2312 | if (statement_block_size($block) > 1) { |
| 2313 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 2314 | $allowed = 1; |
| 2315 | } |
| 2316 | # Check the post-context. |
| 2317 | if (defined $chunks[1]) { |
| 2318 | my ($cond, $block) = @{$chunks[1]}; |
| 2319 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2320 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2321 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2322 | if ($block =~ /^\s*\{/) { |
| 2323 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 2324 | $allowed = 1; |
| 2325 | } |
| 2326 | } |
| 2327 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
| 2328 | my $herectx = $here . "\n";; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 2329 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2330 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 2331 | for (my $n = 0; $n < $cnt; $n++) { |
| 2332 | $herectx .= raw_line($linenr, $n) . "\n";; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2336 | } |
| 2337 | } |
| 2338 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2339 | # don't include deprecated include files (uses RAW line) |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2340 | for my $inc (@dep_includes) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2341 | if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2342 | ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2346 | # don't use deprecated functions |
| 2347 | for my $func (@dep_functions) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2348 | if ($line =~ /\b$func\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2349 | 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] | 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2354 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 2355 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2356 | 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] | 2357 | } |
| 2358 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2359 | # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated |
| 2360 | if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { |
| 2361 | ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); |
| 2362 | } |
| 2363 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2364 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2365 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2366 | CHK("if this code is redundant consider removing it\n" . |
| 2367 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2368 | } |
| 2369 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2370 | # check for needless kfree() checks |
| 2371 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 2372 | my $expr = $1; |
| 2373 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { |
Wolfram Sang | 3c23214 | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 2374 | 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] | 2375 | } |
| 2376 | } |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 2377 | # check for needless usb_free_urb() checks |
| 2378 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 2379 | my $expr = $1; |
| 2380 | if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { |
| 2381 | WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); |
| 2382 | } |
| 2383 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2384 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2385 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2386 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2387 | # print "#ifdef in C files should be avoided\n"; |
| 2388 | # print "$herecurr"; |
| 2389 | # $clean = 0; |
| 2390 | # } |
| 2391 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2392 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2393 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2394 | ERROR("exactly one space required after that #$1\n" . $herecurr); |
| 2395 | } |
| 2396 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2397 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2398 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 2399 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2400 | my $which = $1; |
| 2401 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2402 | CHK("$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2403 | } |
| 2404 | } |
| 2405 | # check for memory barriers without a comment. |
| 2406 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 2407 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2408 | CHK("memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2409 | } |
| 2410 | } |
| 2411 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2412 | 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] | 2413 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2414 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2415 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2416 | # check the location of the inline attribute, that it is between |
| 2417 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2418 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 2419 | $line =~ /\b$Inline\s+$Storage\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2420 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
| 2421 | } |
| 2422 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2423 | # Check for __inline__ and __inline, prefer inline |
| 2424 | if ($line =~ /\b(__inline__|__inline)\b/) { |
| 2425 | WARN("plain inline is preferred over $1\n" . $herecurr); |
| 2426 | } |
| 2427 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2428 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2429 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2430 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2431 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2432 | my $function_name = $1; |
| 2433 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2434 | |
| 2435 | my $s = $stat; |
| 2436 | if (defined $cond) { |
| 2437 | substr($s, 0, length($cond), ''); |
| 2438 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2439 | if ($s =~ /^\s*;/ && |
| 2440 | $function_name ne 'uninitialized_var') |
| 2441 | { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2442 | WARN("externs should be avoided in .c files\n" . $herecurr); |
| 2443 | } |
| 2444 | |
| 2445 | if ($paren_space =~ /\n/) { |
| 2446 | WARN("arguments for function declarations should follow identifier\n" . $herecurr); |
| 2447 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 2448 | |
| 2449 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 2450 | $stat =~ /^.\s*extern\s+/) |
| 2451 | { |
| 2452 | WARN("externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | # checks for new __setup's |
| 2456 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 2457 | my $name = $1; |
| 2458 | |
| 2459 | if (!grep(/$name/, @setup_docs)) { |
| 2460 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
| 2461 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2462 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2463 | |
| 2464 | # check for pointless casting of kmalloc return |
| 2465 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { |
| 2466 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
| 2467 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2468 | |
| 2469 | # check for gcc specific __FUNCTION__ |
| 2470 | if ($line =~ /__FUNCTION__/) { |
| 2471 | WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); |
| 2472 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2473 | |
| 2474 | # check for semaphores used as mutexes |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2475 | if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2476 | WARN("mutexes are preferred for single holder semaphores\n" . $herecurr); |
| 2477 | } |
| 2478 | # check for semaphores used as mutexes |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2479 | if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2480 | WARN("consider using a completion\n" . $herecurr); |
| 2481 | } |
| 2482 | # recommend strict_strto* over simple_strto* |
| 2483 | if ($line =~ /\bsimple_(strto.*?)\s*\(/) { |
| 2484 | WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); |
| 2485 | } |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 2486 | # check for __initcall(), use device_initcall() explicitly please |
| 2487 | if ($line =~ /^.\s*__initcall\s*\(/) { |
| 2488 | WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); |
| 2489 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2490 | |
| 2491 | # use of NR_CPUS is usually wrong |
| 2492 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 2493 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2494 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 2495 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2496 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 2497 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 2498 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2499 | { |
| 2500 | WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
| 2501 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 2502 | |
| 2503 | # check for %L{u,d,i} in strings |
| 2504 | my $string; |
| 2505 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 2506 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Andy Whitcroft | 2a1bc5d | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 2507 | $string =~ s/%%/__/g; |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 2508 | if ($string =~ /(?<!%)%L[udi]/) { |
| 2509 | WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
| 2510 | last; |
| 2511 | } |
| 2512 | } |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 2513 | |
| 2514 | # whine mightly about in_atomic |
| 2515 | if ($line =~ /\bin_atomic\s*\(/) { |
| 2516 | if ($realfile =~ m@^drivers/@) { |
| 2517 | ERROR("do not use in_atomic in drivers\n" . $herecurr); |
| 2518 | } else { |
| 2519 | WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
| 2520 | } |
| 2521 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | # If we have no input at all, then there is nothing to report on |
| 2525 | # so just keep quiet. |
| 2526 | if ($#rawlines == -1) { |
| 2527 | exit(0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2530 | # In mailback mode only produce a report in the negative, for |
| 2531 | # things that appear to be patches. |
| 2532 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 2533 | exit(0); |
| 2534 | } |
| 2535 | |
| 2536 | # This is not a patch, and we are are in 'no-patch' mode so |
| 2537 | # just keep quiet. |
| 2538 | if (!$chk_patch && !$is_patch) { |
| 2539 | exit(0); |
| 2540 | } |
| 2541 | |
| 2542 | if (!$is_patch) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2543 | ERROR("Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2544 | } |
| 2545 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2546 | ERROR("Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2549 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2550 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 2551 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2552 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 2553 | (($check)? "$cnt_chk checks, " : "") . |
| 2554 | "$cnt_lines lines checked\n"; |
| 2555 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2556 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2557 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2558 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2559 | print "$vname has no obvious style problems and is ready for submission.\n" |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2560 | } |
| 2561 | if ($clean == 0 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2562 | print "$vname has style problems, please review. If any of these errors\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2563 | print "are false positives report them to the maintainer, see\n"; |
| 2564 | print "CHECKPATCH in MAINTAINERS.\n"; |
| 2565 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2566 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2567 | return $clean; |
| 2568 | } |