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