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