Andy Whitcroft | 0a920b5b | 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 | 015830be | 2010-10-26 14:23:17 -0700 | [diff] [blame] | 5 | # (c) 2008-2010 Andy Whitcroft <apw@canonical.com> |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 6 | # Licensed under the terms of the GNU GPL License version 2 |
| 7 | |
| 8 | use strict; |
| 9 | |
| 10 | my $P = $0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 11 | $P =~ s@.*/@@g; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 12 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 13 | my $V = '0.32'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 14 | |
| 15 | use Getopt::Long qw(:config no_auto_abbrev); |
| 16 | |
| 17 | my $quiet = 0; |
| 18 | my $tree = 1; |
| 19 | my $chk_signoff = 1; |
| 20 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 21 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 22 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 23 | my $terse = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 24 | my $file = 0; |
| 25 | my $check = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 26 | my $summary = 1; |
| 27 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 28 | my $summary_file = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 29 | my $show_types = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 30 | my $root; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 31 | my %debug; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 32 | my %ignore_type = (); |
| 33 | my @ignore = (); |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 34 | my $help = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 35 | my $configuration_file = ".checkpatch.conf"; |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 36 | my $max_line_length = 80; |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 37 | |
| 38 | sub help { |
| 39 | my ($exitcode) = @_; |
| 40 | |
| 41 | print << "EOM"; |
| 42 | Usage: $P [OPTION]... [FILE]... |
| 43 | Version: $V |
| 44 | |
| 45 | Options: |
| 46 | -q, --quiet quiet |
| 47 | --no-tree run without a kernel tree |
| 48 | --no-signoff do not check for 'Signed-off-by' line |
| 49 | --patch treat FILE as patchfile (default) |
| 50 | --emacs emacs compile window format |
| 51 | --terse one line per report |
| 52 | -f, --file treat FILE as regular source file |
| 53 | --subjective, --strict enable more subjective tests |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 54 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 55 | --max-line-length=n set the maximum line length, if exceeded, warn |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 56 | --show-types show the message "types" in the output |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 57 | --root=PATH PATH to the kernel tree root |
| 58 | --no-summary suppress the per-file summary |
| 59 | --mailback only produce a report in case of warnings/errors |
| 60 | --summary-file include the filename in summary |
| 61 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 62 | 'values', 'possible', 'type', and 'attr' (default |
| 63 | is all off) |
| 64 | --test-only=WORD report only warnings/errors containing WORD |
| 65 | literally |
| 66 | -h, --help, --version display this help and exit |
| 67 | |
| 68 | When FILE is - read standard input. |
| 69 | EOM |
| 70 | |
| 71 | exit($exitcode); |
| 72 | } |
| 73 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 74 | my $conf = which_conf($configuration_file); |
| 75 | if (-f $conf) { |
| 76 | my @conf_args; |
| 77 | open(my $conffile, '<', "$conf") |
| 78 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 79 | |
| 80 | while (<$conffile>) { |
| 81 | my $line = $_; |
| 82 | |
| 83 | $line =~ s/\s*\n?$//g; |
| 84 | $line =~ s/^\s*//g; |
| 85 | $line =~ s/\s+/ /g; |
| 86 | |
| 87 | next if ($line =~ m/^\s*#/); |
| 88 | next if ($line =~ m/^\s*$/); |
| 89 | |
| 90 | my @words = split(" ", $line); |
| 91 | foreach my $word (@words) { |
| 92 | last if ($word =~ m/^#/); |
| 93 | push (@conf_args, $word); |
| 94 | } |
| 95 | } |
| 96 | close($conffile); |
| 97 | unshift(@ARGV, @conf_args) if @conf_args; |
| 98 | } |
| 99 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 100 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 101 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 102 | 'tree!' => \$tree, |
| 103 | 'signoff!' => \$chk_signoff, |
| 104 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 105 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 106 | 'terse!' => \$terse, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 107 | 'f|file!' => \$file, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 108 | 'subjective!' => \$check, |
| 109 | 'strict!' => \$check, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 110 | 'ignore=s' => \@ignore, |
| 111 | 'show-types!' => \$show_types, |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 112 | 'max-line-length=i' => \$max_line_length, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 113 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 114 | 'summary!' => \$summary, |
| 115 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 116 | 'summary-file!' => \$summary_file, |
| 117 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 118 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 119 | 'test-only=s' => \$tst_only, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 120 | 'h|help' => \$help, |
| 121 | 'version' => \$help |
| 122 | ) or help(1); |
| 123 | |
| 124 | help(0) if ($help); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 125 | |
| 126 | my $exit = 0; |
| 127 | |
| 128 | if ($#ARGV < 0) { |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 129 | print "$P: no input files\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 130 | exit(1); |
| 131 | } |
| 132 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 133 | @ignore = split(/,/, join(',',@ignore)); |
| 134 | foreach my $word (@ignore) { |
| 135 | $word =~ s/\s*\n?$//g; |
| 136 | $word =~ s/^\s*//g; |
| 137 | $word =~ s/\s+/ /g; |
| 138 | $word =~ tr/[a-z]/[A-Z]/; |
| 139 | |
| 140 | next if ($word =~ m/^\s*#/); |
| 141 | next if ($word =~ m/^\s*$/); |
| 142 | |
| 143 | $ignore_type{$word}++; |
| 144 | } |
| 145 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 146 | my $dbg_values = 0; |
| 147 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 148 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 149 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 150 | for my $key (keys %debug) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 151 | ## no critic |
| 152 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 153 | die "$@" if ($@); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 156 | my $rpt_cleaners = 0; |
| 157 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 158 | if ($terse) { |
| 159 | $emacs = 1; |
| 160 | $quiet++; |
| 161 | } |
| 162 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 163 | if ($tree) { |
| 164 | if (defined $root) { |
| 165 | if (!top_of_kernel_tree($root)) { |
| 166 | die "$P: $root: --root does not point at a valid tree\n"; |
| 167 | } |
| 168 | } else { |
| 169 | if (top_of_kernel_tree('.')) { |
| 170 | $root = '.'; |
| 171 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 172 | top_of_kernel_tree($1)) { |
| 173 | $root = $1; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!defined $root) { |
| 178 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 179 | exit(2); |
| 180 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 183 | my $emitted_corrupt = 0; |
| 184 | |
Andy Whitcroft | 2ceb532 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 185 | our $Ident = qr{ |
| 186 | [A-Za-z_][A-Za-z\d_]* |
| 187 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 188 | }x; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 189 | our $Storage = qr{extern|static|asmlinkage}; |
| 190 | our $Sparse = qr{ |
| 191 | __user| |
| 192 | __kernel| |
| 193 | __force| |
| 194 | __iomem| |
| 195 | __must_check| |
| 196 | __init_refok| |
Andy Whitcroft | 417495e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 197 | __kprobes| |
Sven Eckelmann | 165e72a | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 198 | __ref| |
| 199 | __rcu |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 200 | }x; |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 201 | |
| 202 | # Notes to $Attribute: |
| 203 | # 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] | 204 | our $Attribute = qr{ |
| 205 | const| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 206 | __percpu| |
| 207 | __nocast| |
| 208 | __safe| |
| 209 | __bitwise__| |
| 210 | __packed__| |
| 211 | __packed2__| |
| 212 | __naked| |
| 213 | __maybe_unused| |
| 214 | __always_unused| |
| 215 | __noreturn| |
| 216 | __used| |
| 217 | __cold| |
| 218 | __noclone| |
| 219 | __deprecated| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 220 | __read_mostly| |
| 221 | __kprobes| |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 222 | __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 223 | ____cacheline_aligned| |
| 224 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 225 | ____cacheline_internodealigned_in_smp| |
| 226 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 227 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 228 | our $Modifier; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 229 | our $Inline = qr{inline|__always_inline|noinline}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 230 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 231 | our $Lval = qr{$Ident(?:$Member)*}; |
| 232 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 233 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 234 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 235 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 236 | our $Int = qr{[0-9]+$Int_type?}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 237 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 238 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 239 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
Joe Perches | 74349bc | 2012-12-17 16:02:05 -0800 | [diff] [blame] | 240 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 241 | our $Constant = qr{$Float|$Binary|$Hex|$Int}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 242 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 243 | our $Compare = qr{<=|>=|==|!=|<|>}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 244 | our $Operators = qr{ |
| 245 | <=|>=|==|!=| |
| 246 | =>|->|<<|>>|<|>|!|~| |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 247 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 248 | }x; |
| 249 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 250 | our $NonptrType; |
| 251 | our $Type; |
| 252 | our $Declare; |
| 253 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 254 | our $NON_ASCII_UTF8 = qr{ |
| 255 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 256 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 257 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 258 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 259 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 260 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 261 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 262 | }x; |
| 263 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 264 | our $UTF8 = qr{ |
| 265 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 266 | | $NON_ASCII_UTF8 |
| 267 | }x; |
| 268 | |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 269 | our $typeTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 270 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 271 | atomic_t |
| 272 | )}; |
| 273 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 274 | our $logFunctions = qr{(?x: |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 275 | printk(?:_ratelimited|_once|)| |
| 276 | [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| |
| 277 | WARN(?:_RATELIMIT|_ONCE|)| |
Joe Perches | b053172 | 2011-05-24 17:13:40 -0700 | [diff] [blame] | 278 | panic| |
| 279 | MODULE_[A-Z_]+ |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 280 | )}; |
| 281 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 282 | our $signature_tags = qr{(?xi: |
| 283 | Signed-off-by:| |
| 284 | Acked-by:| |
| 285 | Tested-by:| |
| 286 | Reviewed-by:| |
| 287 | Reported-by:| |
Mugunthan V N | 8543ae1 | 2013-04-29 16:18:17 -0700 | [diff] [blame] | 288 | Suggested-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 289 | To:| |
| 290 | Cc: |
| 291 | )}; |
| 292 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 293 | our @typeList = ( |
| 294 | qr{void}, |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 295 | qr{(?:unsigned\s+)?char}, |
| 296 | qr{(?:unsigned\s+)?short}, |
| 297 | qr{(?:unsigned\s+)?int}, |
| 298 | qr{(?:unsigned\s+)?long}, |
| 299 | qr{(?:unsigned\s+)?long\s+int}, |
| 300 | qr{(?:unsigned\s+)?long\s+long}, |
| 301 | qr{(?:unsigned\s+)?long\s+long\s+int}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 302 | qr{unsigned}, |
| 303 | qr{float}, |
| 304 | qr{double}, |
| 305 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 306 | qr{struct\s+$Ident}, |
| 307 | qr{union\s+$Ident}, |
| 308 | qr{enum\s+$Ident}, |
| 309 | qr{${Ident}_t}, |
| 310 | qr{${Ident}_handler}, |
| 311 | qr{${Ident}_handler_fn}, |
| 312 | ); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 313 | our @modifierList = ( |
| 314 | qr{fastcall}, |
| 315 | ); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 316 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 317 | our $allowed_asm_includes = qr{(?x: |
| 318 | irq| |
| 319 | memory |
| 320 | )}; |
| 321 | # memory.h: ARM has a custom one |
| 322 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 323 | sub build_types { |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 324 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
| 325 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 326 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 327 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 328 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 329 | (?: |
Andy Whitcroft | 6b48db2 | 2012-01-10 15:10:13 -0800 | [diff] [blame] | 330 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 331 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 332 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 333 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 334 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 335 | }x; |
| 336 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 337 | $NonptrType |
Andy Whitcroft | b337d8b | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 338 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 339 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 340 | }x; |
| 341 | $Declare = qr{(?:$Storage\s+)?$Type}; |
| 342 | } |
| 343 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 344 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 345 | |
| 346 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 347 | |
| 348 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 349 | # requires at least perl version v5.10.0 |
| 350 | # Any use must be runtime checked with $^V |
| 351 | |
| 352 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
| 353 | our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 354 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 355 | |
| 356 | sub deparenthesize { |
| 357 | my ($string) = @_; |
| 358 | return "" if (!defined($string)); |
| 359 | $string =~ s@^\s*\(\s*@@g; |
| 360 | $string =~ s@\s*\)\s*$@@g; |
| 361 | $string =~ s@\s+@ @g; |
| 362 | return $string; |
| 363 | } |
| 364 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 365 | $chk_signoff = 0 if ($file); |
| 366 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 367 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 368 | my @lines = (); |
| 369 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 370 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 371 | my $FILE; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 372 | if ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 373 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 374 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 375 | } elsif ($filename eq '-') { |
| 376 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 377 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 378 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 379 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 380 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 381 | if ($filename eq '-') { |
| 382 | $vname = 'Your patch'; |
| 383 | } else { |
| 384 | $vname = $filename; |
| 385 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 386 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 387 | chomp; |
| 388 | push(@rawlines, $_); |
| 389 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 390 | close($FILE); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 391 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 392 | $exit = 1; |
| 393 | } |
| 394 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 395 | @lines = (); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | exit($exit); |
| 399 | |
| 400 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 401 | my ($root) = @_; |
| 402 | |
| 403 | my @tree_check = ( |
| 404 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 405 | "README", "Documentation", "arch", "include", "drivers", |
| 406 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 407 | ); |
| 408 | |
| 409 | foreach my $check (@tree_check) { |
| 410 | if (! -e $root . '/' . $check) { |
| 411 | return 0; |
| 412 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 413 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 414 | return 1; |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 415 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 416 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 417 | sub parse_email { |
| 418 | my ($formatted_email) = @_; |
| 419 | |
| 420 | my $name = ""; |
| 421 | my $address = ""; |
| 422 | my $comment = ""; |
| 423 | |
| 424 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 425 | $name = $1; |
| 426 | $address = $2; |
| 427 | $comment = $3 if defined $3; |
| 428 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 429 | $address = $1; |
| 430 | $comment = $2 if defined $2; |
| 431 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 432 | $address = $1; |
| 433 | $comment = $2 if defined $2; |
| 434 | $formatted_email =~ s/$address.*$//; |
| 435 | $name = $formatted_email; |
| 436 | $name =~ s/^\s+|\s+$//g; |
| 437 | $name =~ s/^\"|\"$//g; |
| 438 | # If there's a name left after stripping spaces and |
| 439 | # leading quotes, and the address doesn't have both |
| 440 | # leading and trailing angle brackets, the address |
| 441 | # is invalid. ie: |
| 442 | # "joe smith joe@smith.com" bad |
| 443 | # "joe smith <joe@smith.com" bad |
| 444 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 445 | $name = ""; |
| 446 | $address = ""; |
| 447 | $comment = ""; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | $name =~ s/^\s+|\s+$//g; |
| 452 | $name =~ s/^\"|\"$//g; |
| 453 | $address =~ s/^\s+|\s+$//g; |
| 454 | $address =~ s/^\<|\>$//g; |
| 455 | |
| 456 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 457 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 458 | $name = "\"$name\""; |
| 459 | } |
| 460 | |
| 461 | return ($name, $address, $comment); |
| 462 | } |
| 463 | |
| 464 | sub format_email { |
| 465 | my ($name, $address) = @_; |
| 466 | |
| 467 | my $formatted_email; |
| 468 | |
| 469 | $name =~ s/^\s+|\s+$//g; |
| 470 | $name =~ s/^\"|\"$//g; |
| 471 | $address =~ s/^\s+|\s+$//g; |
| 472 | |
| 473 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 474 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 475 | $name = "\"$name\""; |
| 476 | } |
| 477 | |
| 478 | if ("$name" eq "") { |
| 479 | $formatted_email = "$address"; |
| 480 | } else { |
| 481 | $formatted_email = "$name <$address>"; |
| 482 | } |
| 483 | |
| 484 | return $formatted_email; |
| 485 | } |
| 486 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 487 | sub which_conf { |
| 488 | my ($conf) = @_; |
| 489 | |
| 490 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 491 | if (-e "$path/$conf") { |
| 492 | return "$path/$conf"; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return ""; |
| 497 | } |
| 498 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 499 | sub expand_tabs { |
| 500 | my ($str) = @_; |
| 501 | |
| 502 | my $res = ''; |
| 503 | my $n = 0; |
| 504 | for my $c (split(//, $str)) { |
| 505 | if ($c eq "\t") { |
| 506 | $res .= ' '; |
| 507 | $n++; |
| 508 | for (; ($n % 8) != 0; $n++) { |
| 509 | $res .= ' '; |
| 510 | } |
| 511 | next; |
| 512 | } |
| 513 | $res .= $c; |
| 514 | $n++; |
| 515 | } |
| 516 | |
| 517 | return $res; |
| 518 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 519 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 520 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 521 | return $res; |
| 522 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 523 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 524 | sub line_stats { |
| 525 | my ($line) = @_; |
| 526 | |
| 527 | # Drop the diff line leader and expand tabs |
| 528 | $line =~ s/^.//; |
| 529 | $line = expand_tabs($line); |
| 530 | |
| 531 | # Pick the indent from the front of the line. |
| 532 | my ($white) = ($line =~ /^(\s*)/); |
| 533 | |
| 534 | return (length($line), length($white)); |
| 535 | } |
| 536 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 537 | my $sanitise_quote = ''; |
| 538 | |
| 539 | sub sanitise_line_reset { |
| 540 | my ($in_comment) = @_; |
| 541 | |
| 542 | if ($in_comment) { |
| 543 | $sanitise_quote = '*/'; |
| 544 | } else { |
| 545 | $sanitise_quote = ''; |
| 546 | } |
| 547 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 548 | sub sanitise_line { |
| 549 | my ($line) = @_; |
| 550 | |
| 551 | my $res = ''; |
| 552 | my $l = ''; |
| 553 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 554 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 555 | my $off = 0; |
| 556 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 557 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 558 | # Always copy over the diff marker. |
| 559 | $res = substr($line, 0, 1); |
| 560 | |
| 561 | for ($off = 1; $off < length($line); $off++) { |
| 562 | $c = substr($line, $off, 1); |
| 563 | |
| 564 | # Comments we are wacking completly including the begin |
| 565 | # and end, all to $;. |
| 566 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 567 | $sanitise_quote = '*/'; |
| 568 | |
| 569 | substr($res, $off, 2, "$;$;"); |
| 570 | $off++; |
| 571 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 572 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 573 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 574 | $sanitise_quote = ''; |
| 575 | substr($res, $off, 2, "$;$;"); |
| 576 | $off++; |
| 577 | next; |
| 578 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 579 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 580 | $sanitise_quote = '//'; |
| 581 | |
| 582 | substr($res, $off, 2, $sanitise_quote); |
| 583 | $off++; |
| 584 | next; |
| 585 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 586 | |
| 587 | # A \ in a string means ignore the next character. |
| 588 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 589 | $c eq "\\") { |
| 590 | substr($res, $off, 2, 'XX'); |
| 591 | $off++; |
| 592 | next; |
| 593 | } |
| 594 | # Regular quotes. |
| 595 | if ($c eq "'" || $c eq '"') { |
| 596 | if ($sanitise_quote eq '') { |
| 597 | $sanitise_quote = $c; |
| 598 | |
| 599 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 600 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 601 | } elsif ($sanitise_quote eq $c) { |
| 602 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 605 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 606 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 607 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 608 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 609 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 610 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 611 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 612 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 613 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 614 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 615 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 618 | if ($sanitise_quote eq '//') { |
| 619 | $sanitise_quote = ''; |
| 620 | } |
| 621 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 622 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 623 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 624 | my $clean = 'X' x length($1); |
| 625 | $res =~ s@\<.*\>@<$clean>@; |
| 626 | |
| 627 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 628 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 629 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 630 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 633 | return $res; |
| 634 | } |
| 635 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 636 | sub get_quoted_string { |
| 637 | my ($line, $rawline) = @_; |
| 638 | |
| 639 | return "" if ($line !~ m/(\"[X]+\")/g); |
| 640 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 641 | } |
| 642 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 643 | sub ctx_statement_block { |
| 644 | my ($linenr, $remain, $off) = @_; |
| 645 | my $line = $linenr - 1; |
| 646 | my $blk = ''; |
| 647 | my $soff = $off; |
| 648 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 649 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 650 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 651 | my $loff = 0; |
| 652 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 653 | my $type = ''; |
| 654 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 655 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 656 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 657 | my $c; |
| 658 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 659 | |
| 660 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 661 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 662 | @stack = (['', 0]) if ($#stack == -1); |
| 663 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 664 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 665 | # If we are about to drop off the end, pull in more |
| 666 | # context. |
| 667 | if ($off >= $len) { |
| 668 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 669 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 670 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 671 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 672 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 673 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 674 | $len = length($blk); |
| 675 | $line++; |
| 676 | last; |
| 677 | } |
| 678 | # Bail if there is no further context. |
| 679 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 680 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 681 | last; |
| 682 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 683 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 684 | $level++; |
| 685 | $type = '#'; |
| 686 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 687 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 688 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 689 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 690 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 691 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 692 | #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] | 693 | |
| 694 | # Handle nested #if/#else. |
| 695 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 696 | push(@stack, [ $type, $level ]); |
| 697 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 698 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 699 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 700 | ($type, $level) = @{pop(@stack)}; |
| 701 | } |
| 702 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 703 | # Statement ends at the ';' or a close '}' at the |
| 704 | # outermost level. |
| 705 | if ($level == 0 && $c eq ';') { |
| 706 | last; |
| 707 | } |
| 708 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 709 | # 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] | 710 | if ($level == 0 && $coff_set == 0 && |
| 711 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 712 | $remainder =~ /^(else)(?:\s|{)/ && |
| 713 | $remainder !~ /^else\s+if\b/) { |
| 714 | $coff = $off + length($1) - 1; |
| 715 | $coff_set = 1; |
| 716 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 717 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 720 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 721 | $level++; |
| 722 | $type = '('; |
| 723 | } |
| 724 | if ($type eq '(' && $c eq ')') { |
| 725 | $level--; |
| 726 | $type = ($level != 0)? '(' : ''; |
| 727 | |
| 728 | if ($level == 0 && $coff < $soff) { |
| 729 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 730 | $coff_set = 1; |
| 731 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 735 | $level++; |
| 736 | $type = '{'; |
| 737 | } |
| 738 | if ($type eq '{' && $c eq '}') { |
| 739 | $level--; |
| 740 | $type = ($level != 0)? '{' : ''; |
| 741 | |
| 742 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 743 | if (substr($blk, $off + 1, 1) eq ';') { |
| 744 | $off++; |
| 745 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 746 | last; |
| 747 | } |
| 748 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 749 | # Preprocessor commands end at the newline unless escaped. |
| 750 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 751 | $level--; |
| 752 | $type = ''; |
| 753 | $off++; |
| 754 | last; |
| 755 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 756 | $off++; |
| 757 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 758 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 759 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 760 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 761 | $line++; |
| 762 | $remain--; |
| 763 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 764 | |
| 765 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 766 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 767 | |
| 768 | #warn "STATEMENT<$statement>\n"; |
| 769 | #warn "CONDITION<$condition>\n"; |
| 770 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 771 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 772 | |
| 773 | return ($statement, $condition, |
| 774 | $line, $remain + 1, $off - $loff + 1, $level); |
| 775 | } |
| 776 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 777 | sub statement_lines { |
| 778 | my ($stmt) = @_; |
| 779 | |
| 780 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 781 | $stmt =~ s/(^|\n)./$1/g; |
| 782 | $stmt =~ s/^\s*//; |
| 783 | $stmt =~ s/\s*$//; |
| 784 | |
| 785 | my @stmt_lines = ($stmt =~ /\n/g); |
| 786 | |
| 787 | return $#stmt_lines + 2; |
| 788 | } |
| 789 | |
| 790 | sub statement_rawlines { |
| 791 | my ($stmt) = @_; |
| 792 | |
| 793 | my @stmt_lines = ($stmt =~ /\n/g); |
| 794 | |
| 795 | return $#stmt_lines + 2; |
| 796 | } |
| 797 | |
| 798 | sub statement_block_size { |
| 799 | my ($stmt) = @_; |
| 800 | |
| 801 | $stmt =~ s/(^|\n)./$1/g; |
| 802 | $stmt =~ s/^\s*{//; |
| 803 | $stmt =~ s/}\s*$//; |
| 804 | $stmt =~ s/^\s*//; |
| 805 | $stmt =~ s/\s*$//; |
| 806 | |
| 807 | my @stmt_lines = ($stmt =~ /\n/g); |
| 808 | my @stmt_statements = ($stmt =~ /;/g); |
| 809 | |
| 810 | my $stmt_lines = $#stmt_lines + 2; |
| 811 | my $stmt_statements = $#stmt_statements + 1; |
| 812 | |
| 813 | if ($stmt_lines > $stmt_statements) { |
| 814 | return $stmt_lines; |
| 815 | } else { |
| 816 | return $stmt_statements; |
| 817 | } |
| 818 | } |
| 819 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 820 | sub ctx_statement_full { |
| 821 | my ($linenr, $remain, $off) = @_; |
| 822 | my ($statement, $condition, $level); |
| 823 | |
| 824 | my (@chunks); |
| 825 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 826 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 827 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 828 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 829 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 830 | push(@chunks, [ $condition, $statement ]); |
| 831 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 832 | return ($level, $linenr, @chunks); |
| 833 | } |
| 834 | |
| 835 | # Pull in the following conditional/block pairs and see if they |
| 836 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 837 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 838 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 839 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 840 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 841 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 842 | #print "C: push\n"; |
| 843 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 849 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 850 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 851 | my $line; |
| 852 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 853 | my $blk = ''; |
| 854 | my @o; |
| 855 | my @c; |
| 856 | my @res = (); |
| 857 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 858 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 859 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 860 | for ($line = $start; $remain > 0; $line++) { |
| 861 | next if ($rawlines[$line] =~ /^-/); |
| 862 | $remain--; |
| 863 | |
| 864 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 865 | |
| 866 | # Handle nested #if/#else. |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 867 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 868 | push(@stack, $level); |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 869 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 870 | $level = $stack[$#stack - 1]; |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 871 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 872 | $level = pop(@stack); |
| 873 | } |
| 874 | |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 875 | foreach my $c (split(//, $lines[$line])) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 876 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 877 | if ($off > 0) { |
| 878 | $off--; |
| 879 | next; |
| 880 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 881 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 882 | if ($c eq $close && $level > 0) { |
| 883 | $level--; |
| 884 | last if ($level == 0); |
| 885 | } elsif ($c eq $open) { |
| 886 | $level++; |
| 887 | } |
| 888 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 889 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 890 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 891 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 894 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 897 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 898 | } |
| 899 | sub ctx_block_outer { |
| 900 | my ($linenr, $remain) = @_; |
| 901 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 902 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 903 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 904 | } |
| 905 | sub ctx_block { |
| 906 | my ($linenr, $remain) = @_; |
| 907 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 908 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 909 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 910 | } |
| 911 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 912 | my ($linenr, $remain, $off) = @_; |
| 913 | |
| 914 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 915 | return @r; |
| 916 | } |
| 917 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 918 | my ($linenr, $remain) = @_; |
| 919 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 920 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 921 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 922 | sub ctx_statement_level { |
| 923 | my ($linenr, $remain, $off) = @_; |
| 924 | |
| 925 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 926 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 927 | |
| 928 | sub ctx_locate_comment { |
| 929 | my ($first_line, $end_line) = @_; |
| 930 | |
| 931 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 932 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 933 | return $current_comment if (defined $current_comment); |
| 934 | |
| 935 | # Look through the context and try and figure out if there is a |
| 936 | # comment. |
| 937 | my $in_comment = 0; |
| 938 | $current_comment = ''; |
| 939 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 940 | my $line = $rawlines[$linenr - 1]; |
| 941 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 942 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 943 | $in_comment = 1; |
| 944 | } |
| 945 | if ($line =~ m@/\*@) { |
| 946 | $in_comment = 1; |
| 947 | } |
| 948 | if (!$in_comment && $current_comment ne '') { |
| 949 | $current_comment = ''; |
| 950 | } |
| 951 | $current_comment .= $line . "\n" if ($in_comment); |
| 952 | if ($line =~ m@\*/@) { |
| 953 | $in_comment = 0; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | chomp($current_comment); |
| 958 | return($current_comment); |
| 959 | } |
| 960 | sub ctx_has_comment { |
| 961 | my ($first_line, $end_line) = @_; |
| 962 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 963 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 964 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 965 | ##print "CMMT: $cmt\n"; |
| 966 | |
| 967 | return ($cmt ne ''); |
| 968 | } |
| 969 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 970 | sub raw_line { |
| 971 | my ($linenr, $cnt) = @_; |
| 972 | |
| 973 | my $offset = $linenr - 1; |
| 974 | $cnt++; |
| 975 | |
| 976 | my $line; |
| 977 | while ($cnt) { |
| 978 | $line = $rawlines[$offset++]; |
| 979 | next if (defined($line) && $line =~ /^-/); |
| 980 | $cnt--; |
| 981 | } |
| 982 | |
| 983 | return $line; |
| 984 | } |
| 985 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 986 | sub cat_vet { |
| 987 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 988 | my ($res, $coded); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 989 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 990 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 991 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 992 | $res .= $1; |
| 993 | if ($2 ne '') { |
| 994 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 995 | $res .= $coded; |
| 996 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 997 | } |
| 998 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 999 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1000 | return $res; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1003 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1004 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1005 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1006 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1007 | |
| 1008 | sub annotate_reset { |
| 1009 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1010 | $av_pending = '_'; |
| 1011 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1012 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1015 | sub annotate_values { |
| 1016 | my ($stream, $type) = @_; |
| 1017 | |
| 1018 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1019 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1020 | my $cur = $stream; |
| 1021 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1022 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1023 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1024 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1025 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1026 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1027 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1028 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1029 | print "WS($1)\n" if ($dbg_values > 1); |
| 1030 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1031 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1032 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Florian Mickler | c023e473 | 2011-01-12 16:59:58 -0800 | [diff] [blame] | 1035 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1036 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1037 | push(@av_paren_type, $type); |
Andy Whitcroft | addcdce | 2012-01-10 15:10:11 -0800 | [diff] [blame] | 1038 | $type = 'c'; |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1039 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1040 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1041 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1042 | $type = 'T'; |
| 1043 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 1044 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1045 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1046 | $type = 'T'; |
| 1047 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1048 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1049 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1050 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1051 | push(@av_paren_type, $type); |
| 1052 | if ($2 ne '') { |
| 1053 | $av_pending = 'N'; |
| 1054 | } |
| 1055 | $type = 'E'; |
| 1056 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1057 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1058 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1059 | $av_preprocessor = 1; |
| 1060 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1061 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1062 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1063 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1064 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1065 | |
| 1066 | push(@av_paren_type, $type); |
| 1067 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1068 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1069 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1070 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1071 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1072 | $av_preprocessor = 1; |
| 1073 | |
| 1074 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1075 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1076 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1077 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1078 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1079 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1080 | |
| 1081 | $av_preprocessor = 1; |
| 1082 | |
| 1083 | # Assume all arms of the conditional end as this |
| 1084 | # one does, and continue as if the #endif was not here. |
| 1085 | pop(@av_paren_type); |
| 1086 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1087 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1088 | |
| 1089 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1090 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1091 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1092 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1093 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1094 | $av_pending = $type; |
| 1095 | $type = 'N'; |
| 1096 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1097 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1098 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1099 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1100 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1101 | } |
| 1102 | $type = 'N'; |
| 1103 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1104 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1105 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1106 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1107 | $type = 'N'; |
| 1108 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1109 | } elsif ($cur =~/^(case)/o) { |
| 1110 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1111 | $av_pend_colon = 'C'; |
| 1112 | $type = 'N'; |
| 1113 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1114 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1115 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1116 | $type = 'N'; |
| 1117 | |
| 1118 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1119 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1120 | push(@av_paren_type, $av_pending); |
| 1121 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1122 | $type = 'N'; |
| 1123 | |
| 1124 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1125 | my $new_type = pop(@av_paren_type); |
| 1126 | if ($new_type ne '_') { |
| 1127 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1128 | print "PAREN('$1') -> $type\n" |
| 1129 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1130 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1131 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1134 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1135 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1136 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1137 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1138 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1139 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1140 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1141 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1142 | } elsif ($type eq 'E') { |
| 1143 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1144 | } |
| 1145 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1146 | $type = 'V'; |
| 1147 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1148 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1149 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1150 | $type = 'V'; |
| 1151 | |
| 1152 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1153 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1154 | $type = 'N'; |
| 1155 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1156 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1157 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1158 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1159 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1160 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1161 | } elsif ($cur =~/^(,)/) { |
| 1162 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1163 | $type = 'C'; |
| 1164 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1165 | } elsif ($cur =~ /^(\?)/o) { |
| 1166 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1167 | $type = 'N'; |
| 1168 | |
| 1169 | } elsif ($cur =~ /^(:)/o) { |
| 1170 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1171 | |
| 1172 | substr($var, length($res), 1, $av_pend_colon); |
| 1173 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1174 | $type = 'E'; |
| 1175 | } else { |
| 1176 | $type = 'N'; |
| 1177 | } |
| 1178 | $av_pend_colon = 'O'; |
| 1179 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1180 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1181 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1182 | $type = 'N'; |
| 1183 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1184 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1185 | my $variant; |
| 1186 | |
| 1187 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1188 | if ($type eq 'V') { |
| 1189 | $variant = 'B'; |
| 1190 | } else { |
| 1191 | $variant = 'U'; |
| 1192 | } |
| 1193 | |
| 1194 | substr($var, length($res), 1, $variant); |
| 1195 | $type = 'N'; |
| 1196 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1197 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1198 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1199 | if ($1 ne '++' && $1 ne '--') { |
| 1200 | $type = 'N'; |
| 1201 | } |
| 1202 | |
| 1203 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1204 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1205 | } |
| 1206 | if (defined $1) { |
| 1207 | $cur = substr($cur, length($1)); |
| 1208 | $res .= $type x length($1); |
| 1209 | } |
| 1210 | } |
| 1211 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1212 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1215 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1216 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1217 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1218 | ^(?: |
| 1219 | $Modifier| |
| 1220 | $Storage| |
| 1221 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1222 | DEFINE_\S+ |
| 1223 | )$| |
| 1224 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1225 | goto| |
| 1226 | return| |
| 1227 | case| |
| 1228 | else| |
| 1229 | asm|__asm__| |
Andy Whitcroft | 89a8835 | 2012-01-10 15:10:00 -0800 | [diff] [blame] | 1230 | do| |
| 1231 | \#| |
| 1232 | \#\#| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1233 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1234 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1235 | )}x; |
| 1236 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1237 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1238 | # Check for modifiers. |
| 1239 | $possible =~ s/\s*$Storage\s*//g; |
| 1240 | $possible =~ s/\s*$Sparse\s*//g; |
| 1241 | if ($possible =~ /^\s*$/) { |
| 1242 | |
| 1243 | } elsif ($possible =~ /\s/) { |
| 1244 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1245 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1246 | if ($modifier !~ $notPermitted) { |
| 1247 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
| 1248 | push(@modifierList, $modifier); |
| 1249 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1250 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1251 | |
| 1252 | } else { |
| 1253 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
| 1254 | push(@typeList, $possible); |
| 1255 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1256 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1257 | } else { |
| 1258 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1262 | my $prefix = ''; |
| 1263 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1264 | sub show_type { |
| 1265 | return !defined $ignore_type{$_[0]}; |
| 1266 | } |
| 1267 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1268 | sub report { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1269 | if (!show_type($_[1]) || |
| 1270 | (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1271 | return 0; |
| 1272 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1273 | my $line; |
| 1274 | if ($show_types) { |
| 1275 | $line = "$prefix$_[0]:$_[1]: $_[2]\n"; |
| 1276 | } else { |
| 1277 | $line = "$prefix$_[0]: $_[2]\n"; |
| 1278 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1279 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
| 1280 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1281 | push(our @report, $line); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1282 | |
| 1283 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1284 | } |
| 1285 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1286 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1287 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1288 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1289 | sub ERROR { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1290 | if (report("ERROR", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1291 | our $clean = 0; |
| 1292 | our $cnt_error++; |
| 1293 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1294 | } |
| 1295 | sub WARN { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1296 | if (report("WARNING", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1297 | our $clean = 0; |
| 1298 | our $cnt_warn++; |
| 1299 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1300 | } |
| 1301 | sub CHK { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1302 | if ($check && report("CHECK", $_[0], $_[1])) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1303 | our $clean = 0; |
| 1304 | our $cnt_chk++; |
| 1305 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1308 | sub check_absolute_file { |
| 1309 | my ($absolute, $herecurr) = @_; |
| 1310 | my $file = $absolute; |
| 1311 | |
| 1312 | ##print "absolute<$absolute>\n"; |
| 1313 | |
| 1314 | # See if any suffix of this path is a path within the tree. |
| 1315 | while ($file =~ s@^[^/]*/@@) { |
| 1316 | if (-f "$root/$file") { |
| 1317 | ##print "file<$file>\n"; |
| 1318 | last; |
| 1319 | } |
| 1320 | } |
| 1321 | if (! -f _) { |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | # It is, so see if the prefix is acceptable. |
| 1326 | my $prefix = $absolute; |
| 1327 | substr($prefix, -length($file)) = ''; |
| 1328 | |
| 1329 | ##print "prefix<$prefix>\n"; |
| 1330 | if ($prefix ne ".../") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1331 | WARN("USE_RELATIVE_PATH", |
| 1332 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1336 | sub pos_last_openparen { |
| 1337 | my ($line) = @_; |
| 1338 | |
| 1339 | my $pos = 0; |
| 1340 | |
| 1341 | my $opens = $line =~ tr/\(/\(/; |
| 1342 | my $closes = $line =~ tr/\)/\)/; |
| 1343 | |
| 1344 | my $last_openparen = 0; |
| 1345 | |
| 1346 | if (($opens == 0) || ($closes >= $opens)) { |
| 1347 | return -1; |
| 1348 | } |
| 1349 | |
| 1350 | my $len = length($line); |
| 1351 | |
| 1352 | for ($pos = 0; $pos < $len; $pos++) { |
| 1353 | my $string = substr($line, $pos); |
| 1354 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 1355 | $pos += length($1) - 1; |
| 1356 | } elsif (substr($line, $pos, 1) eq '(') { |
| 1357 | $last_openparen = $pos; |
| 1358 | } elsif (index($string, '(') == -1) { |
| 1359 | last; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | return $last_openparen + 1; |
| 1364 | } |
| 1365 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1366 | sub process { |
| 1367 | my $filename = shift; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1368 | |
| 1369 | my $linenr=0; |
| 1370 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1371 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1372 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1373 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1374 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1375 | my $length; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1376 | my $indent; |
| 1377 | my $previndent=0; |
| 1378 | my $stashindent=0; |
| 1379 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1380 | our $clean = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1381 | my $signoff = 0; |
| 1382 | my $is_patch = 0; |
| 1383 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1384 | my $in_header_lines = 1; |
| 1385 | my $in_commit_log = 0; #Scanning lines before patch |
| 1386 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1387 | my $non_utf8_charset = 0; |
| 1388 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1389 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1390 | our $cnt_lines = 0; |
| 1391 | our $cnt_error = 0; |
| 1392 | our $cnt_warn = 0; |
| 1393 | our $cnt_chk = 0; |
| 1394 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1395 | # Trace the real file/line as we go. |
| 1396 | my $realfile = ''; |
| 1397 | my $realline = 0; |
| 1398 | my $realcnt = 0; |
| 1399 | my $here = ''; |
| 1400 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1401 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1402 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1403 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1404 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1405 | my $prev_values = 'E'; |
| 1406 | |
| 1407 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1408 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1409 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1410 | my %suppress_export; |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1411 | my $suppress_statement = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1412 | |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 1413 | my %camelcase = (); |
| 1414 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1415 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1416 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1417 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1418 | my @setup_docs = (); |
| 1419 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1420 | |
| 1421 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1422 | my $line; |
| 1423 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1424 | $linenr++; |
| 1425 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1426 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1427 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1428 | $setup_docs = 0; |
| 1429 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 1430 | $setup_docs = 1; |
| 1431 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1432 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1433 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1434 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 1435 | $realline=$1-1; |
| 1436 | if (defined $2) { |
| 1437 | $realcnt=$3+1; |
| 1438 | } else { |
| 1439 | $realcnt=1+1; |
| 1440 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1441 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1442 | |
| 1443 | # Guestimate if this is a continuing comment. Run |
| 1444 | # the context looking for a comment "edge". If this |
| 1445 | # edge is a close comment then we must be in a comment |
| 1446 | # at context start. |
| 1447 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 1448 | my $cnt = $realcnt; |
| 1449 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 1450 | next if (defined $rawlines[$ln - 1] && |
| 1451 | $rawlines[$ln - 1] =~ /^-/); |
| 1452 | $cnt--; |
| 1453 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 1454 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1455 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 1456 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 1457 | ($edge) = $1; |
| 1458 | last; |
| 1459 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1460 | } |
| 1461 | if (defined $edge && $edge eq '*/') { |
| 1462 | $in_comment = 1; |
| 1463 | } |
| 1464 | |
| 1465 | # Guestimate if this is a continuing comment. If this |
| 1466 | # is the start of a diff block and this line starts |
| 1467 | # ' *' then it is very likely a comment. |
| 1468 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 1469 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1470 | { |
| 1471 | $in_comment = 1; |
| 1472 | } |
| 1473 | |
| 1474 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 1475 | sanitise_line_reset($in_comment); |
| 1476 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1477 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1478 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1479 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1480 | $line = sanitise_line($rawline); |
| 1481 | } |
| 1482 | push(@lines, $line); |
| 1483 | |
| 1484 | if ($realcnt > 1) { |
| 1485 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 1486 | } else { |
| 1487 | $realcnt = 0; |
| 1488 | } |
| 1489 | |
| 1490 | #print "==>$rawline\n"; |
| 1491 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1492 | |
| 1493 | if ($setup_docs && $line =~ /^\+/) { |
| 1494 | push(@setup_docs, $line); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1498 | $prefix = ''; |
| 1499 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1500 | $realcnt = 0; |
| 1501 | $linenr = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1502 | foreach my $line (@lines) { |
| 1503 | $linenr++; |
| 1504 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1505 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1506 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1507 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1508 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1509 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1510 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1511 | $realline=$1-1; |
| 1512 | if (defined $2) { |
| 1513 | $realcnt=$3+1; |
| 1514 | } else { |
| 1515 | $realcnt=1+1; |
| 1516 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1517 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1518 | $prev_values = 'E'; |
| 1519 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1520 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1521 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1522 | %suppress_export = (); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1523 | $suppress_statement = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1524 | next; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1525 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1526 | # track the line number as we move through the hunk, note that |
| 1527 | # new versions of GNU diff omit the leading space on completely |
| 1528 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1529 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1530 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1531 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1532 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1533 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1534 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1535 | |
| 1536 | # Track the previous line. |
| 1537 | ($prevline, $stashline) = ($stashline, $line); |
| 1538 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1539 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 1540 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1541 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1542 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1543 | } elsif ($realcnt == 1) { |
| 1544 | $realcnt--; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 1547 | my $hunk_line = ($realcnt != 0); |
| 1548 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1549 | #make up the handle for any error we report on this line |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1550 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 1551 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 1552 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1553 | $here = "#$linenr: " if (!$file); |
| 1554 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1555 | |
| 1556 | # extract the filename as it passes |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1557 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 1558 | $realfile = $1; |
| 1559 | $realfile =~ s@^([^/]*)/@@; |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1560 | $in_commit_log = 0; |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1561 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1562 | $realfile = $1; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1563 | $realfile =~ s@^([^/]*)/@@; |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1564 | $in_commit_log = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1565 | |
| 1566 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 1567 | if (!$file && $tree && $p1_prefix ne '' && |
| 1568 | -e "$root/$p1_prefix") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1569 | WARN("PATCH_PREFIX", |
| 1570 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1571 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1572 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 1573 | if ($realfile =~ m@^include/asm/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1574 | ERROR("MODIFIED_INCLUDE_ASM", |
| 1575 | "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1576 | } |
| 1577 | next; |
| 1578 | } |
| 1579 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 1580 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1581 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1582 | my $hereline = "$here\n$rawline\n"; |
| 1583 | my $herecurr = "$here\n$rawline\n"; |
| 1584 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1585 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1586 | $cnt_lines++ if ($realcnt != 0); |
| 1587 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1588 | # Check for incorrect file permissions |
| 1589 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 1590 | my $permhere = $here . "FILE: $realfile\n"; |
Joe Perches | 04db4d2 | 2013-04-29 16:18:14 -0700 | [diff] [blame] | 1591 | if ($realfile !~ m@scripts/@ && |
| 1592 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1593 | ERROR("EXECUTE_PERMISSIONS", |
| 1594 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1598 | # Check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1599 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1600 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1601 | $in_commit_log = 0; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1605 | if (!$in_header_lines && |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1606 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1607 | my $space_before = $1; |
| 1608 | my $sign_off = $2; |
| 1609 | my $space_after = $3; |
| 1610 | my $email = $4; |
| 1611 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 1612 | |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1613 | if ($sign_off !~ /$signature_tags/) { |
| 1614 | WARN("BAD_SIGN_OFF", |
| 1615 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 1616 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1617 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1618 | WARN("BAD_SIGN_OFF", |
| 1619 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1620 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1621 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1622 | WARN("BAD_SIGN_OFF", |
| 1623 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1624 | } |
| 1625 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1626 | WARN("BAD_SIGN_OFF", |
| 1627 | "Use a single space after $ucfirst_sign_off\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 1631 | my $suggested_email = format_email(($email_name, $email_address)); |
| 1632 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1633 | ERROR("BAD_SIGN_OFF", |
| 1634 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1635 | } else { |
| 1636 | my $dequoted = $suggested_email; |
| 1637 | $dequoted =~ s/^"//; |
| 1638 | $dequoted =~ s/" </ </; |
| 1639 | # Don't force email to have quotes |
| 1640 | # Allow just an angle bracketed address |
| 1641 | if ("$dequoted$comment" ne $email && |
| 1642 | "<$email_address>$comment" ne $email && |
| 1643 | "$suggested_email$comment" ne $email) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1644 | WARN("BAD_SIGN_OFF", |
| 1645 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1646 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1650 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1651 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1652 | ERROR("CORRUPTED_PATCH", |
| 1653 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1654 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1657 | # Check for absolute kernel paths. |
| 1658 | if ($tree) { |
| 1659 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 1660 | my $file = $1; |
| 1661 | |
| 1662 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 1663 | check_absolute_file($1, $herecurr)) { |
| 1664 | # |
| 1665 | } else { |
| 1666 | check_absolute_file($file, $herecurr); |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1671 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 1672 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1673 | $rawline !~ m/^$UTF8*$/) { |
| 1674 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 1675 | |
| 1676 | my $blank = copy_spacing($rawline); |
| 1677 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 1678 | my $hereptr = "$hereline$ptr\n"; |
| 1679 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 1680 | CHK("INVALID_UTF8", |
| 1681 | "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] | 1682 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1683 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1684 | # Check if it's the start of a commit log |
| 1685 | # (not a header line and we haven't seen the patch filename) |
| 1686 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1687 | $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1688 | $in_header_lines = 0; |
| 1689 | $in_commit_log = 1; |
| 1690 | } |
| 1691 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1692 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 1693 | # declined it, i.e defined some charset where it is missing. |
| 1694 | if ($in_header_lines && |
| 1695 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 1696 | $1 !~ /utf-8/i) { |
| 1697 | $non_utf8_charset = 1; |
| 1698 | } |
| 1699 | |
| 1700 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1701 | $rawline =~ /$NON_ASCII_UTF8/) { |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1702 | WARN("UTF8_BEFORE_PATCH", |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1703 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 1704 | } |
| 1705 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1706 | # ignore non-hunk lines and lines being removed |
| 1707 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1708 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1709 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1710 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1711 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1712 | ERROR("DOS_LINE_ENDINGS", |
| 1713 | "DOS line endings\n" . $herevet); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1714 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1715 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1716 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1717 | ERROR("TRAILING_WHITESPACE", |
| 1718 | "trailing whitespace\n" . $herevet); |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1719 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1720 | } |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1721 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1722 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1723 | # Only applies when adding the entry originally, after that we do not have |
| 1724 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1725 | if ($realfile =~ /Kconfig/ && |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1726 | $line =~ /.\s*config\s+/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1727 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1728 | my $cnt = $realcnt; |
| 1729 | my $ln = $linenr + 1; |
| 1730 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1731 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1732 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1733 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1734 | $f = $lines[$ln - 1]; |
| 1735 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 1736 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1737 | |
| 1738 | next if ($f =~ /^-/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1739 | |
| 1740 | if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { |
| 1741 | $is_start = 1; |
| 1742 | } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { |
| 1743 | $length = -1; |
| 1744 | } |
| 1745 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1746 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1747 | $f =~ s/#.*//; |
| 1748 | $f =~ s/^\s+//; |
| 1749 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1750 | if ($f =~ /^\s*config\s/) { |
| 1751 | $is_end = 1; |
| 1752 | last; |
| 1753 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1754 | $length++; |
| 1755 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1756 | WARN("CONFIG_DESCRIPTION", |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1757 | "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); |
| 1758 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 1761 | # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. |
| 1762 | if ($realfile =~ /Kconfig/ && |
| 1763 | $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { |
| 1764 | WARN("CONFIG_EXPERIMENTAL", |
| 1765 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 1766 | } |
| 1767 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 1768 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 1769 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 1770 | my $flag = $1; |
| 1771 | my $replacement = { |
| 1772 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 1773 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 1774 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 1775 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 1776 | }; |
| 1777 | |
| 1778 | WARN("DEPRECATED_VARIABLE", |
| 1779 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 1780 | } |
| 1781 | |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1782 | # check we are in a valid source file if not then ignore this hunk |
| 1783 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
| 1784 | |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 1785 | #line length limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1786 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1787 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
Joe Perches | 0fccc62 | 2011-05-24 17:13:41 -0700 | [diff] [blame] | 1788 | !($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] | 1789 | $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 1790 | $length > $max_line_length) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1791 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1792 | WARN("LONG_LINE", |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 1793 | "line over $max_line_length characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Josh Triplett | ca56dc0 | 2012-03-23 15:02:21 -0700 | [diff] [blame] | 1796 | # Check for user-visible strings broken across lines, which breaks the ability |
| 1797 | # to grep for the string. Limited to strings used as parameters (those |
| 1798 | # following an open parenthesis), which almost completely eliminates false |
| 1799 | # positives, as well as warning only once per parameter rather than once per |
| 1800 | # line of the string. Make an exception when the previous string ends in a |
| 1801 | # newline (multiple lines in one string constant) or \n\t (common in inline |
| 1802 | # assembly to indent the instruction on the following line). |
| 1803 | if ($line =~ /^\+\s*"/ && |
| 1804 | $prevline =~ /"\s*$/ && |
| 1805 | $prevline =~ /\(/ && |
| 1806 | $prevrawline !~ /\\n(?:\\t)*"\s*$/) { |
| 1807 | WARN("SPLIT_STRING", |
| 1808 | "quoted string split across lines\n" . $hereprev); |
| 1809 | } |
| 1810 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 1811 | # check for spaces before a quoted newline |
| 1812 | if ($rawline =~ /^.*\".*\s\\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1813 | WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 1814 | "unnecessary whitespace before a quoted newline\n" . $herecurr); |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 1815 | } |
| 1816 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1817 | # check for adding lines without a newline. |
| 1818 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1819 | WARN("MISSING_EOF_NEWLINE", |
| 1820 | "adding a line without newline at end of file\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1823 | # Blackfin: use hi/lo macros |
| 1824 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 1825 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 1826 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1827 | ERROR("LO_MACRO", |
| 1828 | "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1829 | } |
| 1830 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 1831 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1832 | ERROR("HI_MACRO", |
| 1833 | "use the HI() macro, not (... >> 16)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 1837 | # check we are in a valid source file C or perl if not then ignore this hunk |
| 1838 | next if ($realfile !~ /\.(h|c|pl)$/); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1839 | |
| 1840 | # at the beginning of a line any tabs must come first and anything |
| 1841 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1842 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 1843 | $rawline =~ /^\+\s* \s*/) { |
| 1844 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1845 | ERROR("CODE_INDENT", |
| 1846 | "code indent should use tabs where possible\n" . $herevet); |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1847 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 1850 | # check for space before tabs. |
| 1851 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 1852 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1853 | WARN("SPACE_BEFORE_TAB", |
| 1854 | "please, no space before tabs\n" . $herevet); |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1857 | # check for && or || at the start of a line |
| 1858 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 1859 | CHK("LOGICAL_CONTINUATIONS", |
| 1860 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 1861 | } |
| 1862 | |
| 1863 | # check multi-line statement indentation matches previous line |
| 1864 | if ($^V && $^V ge 5.10.0 && |
| 1865 | $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { |
| 1866 | $prevline =~ /^\+(\t*)(.*)$/; |
| 1867 | my $oldindent = $1; |
| 1868 | my $rest = $2; |
| 1869 | |
| 1870 | my $pos = pos_last_openparen($rest); |
| 1871 | if ($pos >= 0) { |
Joe Perches | b34a26f | 2012-07-30 14:41:16 -0700 | [diff] [blame] | 1872 | $line =~ /^(\+| )([ \t]*)/; |
| 1873 | my $newindent = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1874 | |
| 1875 | my $goodtabindent = $oldindent . |
| 1876 | "\t" x ($pos / 8) . |
| 1877 | " " x ($pos % 8); |
| 1878 | my $goodspaceindent = $oldindent . " " x $pos; |
| 1879 | |
| 1880 | if ($newindent ne $goodtabindent && |
| 1881 | $newindent ne $goodspaceindent) { |
| 1882 | CHK("PARENTHESIS_ALIGNMENT", |
| 1883 | "Alignment should match open parenthesis\n" . $hereprev); |
| 1884 | } |
| 1885 | } |
| 1886 | } |
| 1887 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 1888 | if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) { |
| 1889 | CHK("SPACING", |
| 1890 | "No space is necessary after a cast\n" . $hereprev); |
| 1891 | } |
| 1892 | |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 1893 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | fdb4bcd | 2013-07-03 15:05:23 -0700 | [diff] [blame] | 1894 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
| 1895 | $rawline =~ /^\+[ \t]*\*/) { |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 1896 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 1897 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 1898 | } |
| 1899 | |
| 1900 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame^] | 1901 | $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* |
| 1902 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
| 1903 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
| 1904 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 1905 | "networking block comments start with * on subsequent lines\n" . $hereprev); |
| 1906 | } |
| 1907 | |
| 1908 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | c24f9f1 | 2012-11-08 15:53:29 -0800 | [diff] [blame] | 1909 | $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
| 1910 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 1911 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 1912 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 1913 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 1914 | "networking block comments put the trailing */ on a separate line\n" . $herecurr); |
| 1915 | } |
| 1916 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 1917 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1918 | # Exceptions: |
| 1919 | # 1) within comments |
| 1920 | # 2) indented preprocessor commands |
| 1921 | # 3) hanging labels |
| 1922 | if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 1923 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1924 | WARN("LEADING_SPACE", |
| 1925 | "please, no spaces at the start of a line\n" . $herevet); |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 1928 | # check we are in a valid C source file if not then ignore this hunk |
| 1929 | next if ($realfile !~ /\.(h|c)$/); |
| 1930 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 1931 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). |
| 1932 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { |
| 1933 | WARN("CONFIG_EXPERIMENTAL", |
| 1934 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 1935 | } |
| 1936 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1937 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1938 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1939 | WARN("CVS_KEYWORD", |
| 1940 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1941 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1942 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1943 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 1944 | if ($line =~ /__builtin_bfin_csync/) { |
| 1945 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1946 | ERROR("CSYNC", |
| 1947 | "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1948 | } |
| 1949 | if ($line =~ /__builtin_bfin_ssync/) { |
| 1950 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1951 | ERROR("SSYNC", |
| 1952 | "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Joe Perches | 56e77d7 | 2013-02-21 16:44:14 -0800 | [diff] [blame] | 1955 | # check for old HOTPLUG __dev<foo> section markings |
| 1956 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 1957 | WARN("HOTPLUG_SECTION", |
| 1958 | "Using $1 is unnecessary\n" . $herecurr); |
| 1959 | } |
| 1960 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1961 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1962 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 1963 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1964 | #print "LINE<$line>\n"; |
| 1965 | if ($linenr >= $suppress_statement && |
| 1966 | $realcnt && $line =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1967 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 1968 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1969 | $stat =~ s/\n./\n /g; |
| 1970 | $cond =~ s/\n./\n /g; |
| 1971 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1972 | #print "linenr<$linenr> <$stat>\n"; |
| 1973 | # If this statement has no statement boundaries within |
| 1974 | # it there is no point in retrying a statement scan |
| 1975 | # until we hit end of it. |
| 1976 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 1977 | if ($frag !~ /(?:{|;)/) { |
| 1978 | #print "skip<$line_nr_next>\n"; |
| 1979 | $suppress_statement = $line_nr_next; |
| 1980 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 1981 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1982 | # Find the real next line. |
| 1983 | $realline_next = $line_nr_next; |
| 1984 | if (defined $realline_next && |
| 1985 | (!defined $lines[$realline_next - 1] || |
| 1986 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 1987 | $realline_next++; |
| 1988 | } |
| 1989 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1990 | my $s = $stat; |
| 1991 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1992 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1993 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1994 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1995 | |
| 1996 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1997 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1998 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 1999 | } elsif ($s =~ /^.\s*else\b/s) { |
| 2000 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2001 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2002 | } 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] | 2003 | my $type = $1; |
| 2004 | $type =~ s/\s+/ /g; |
| 2005 | possible($type, "A:" . $s); |
| 2006 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2007 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2008 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2009 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2010 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2011 | |
| 2012 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2013 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2014 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2015 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2016 | |
| 2017 | # Check for any sort of function declaration. |
| 2018 | # int foo(something bar, other baz); |
| 2019 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2020 | 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] | 2021 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2022 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2023 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2024 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2025 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2026 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2027 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2028 | 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] | 2029 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2030 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2037 | # |
| 2038 | # Checks which may be anchored in the context. |
| 2039 | # |
| 2040 | |
| 2041 | # Check for switch () and associated case and default |
| 2042 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2043 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 2044 | my $err = ''; |
| 2045 | my $sep = ''; |
| 2046 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 2047 | shift(@ctx); |
| 2048 | for my $ctx (@ctx) { |
| 2049 | my ($clen, $cindent) = line_stats($ctx); |
| 2050 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 2051 | $indent != $cindent) { |
| 2052 | $err .= "$sep$ctx\n"; |
| 2053 | $sep = ''; |
| 2054 | } else { |
| 2055 | $sep = "[...]\n"; |
| 2056 | } |
| 2057 | } |
| 2058 | if ($err ne '') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2059 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 2060 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 2065 | # or if that brace on the next line is for something else |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2066 | 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] | 2067 | my $pre_ctx = "$1$2"; |
| 2068 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2069 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 2070 | |
| 2071 | if ($line =~ /^\+\t{6,}/) { |
| 2072 | WARN("DEEP_INDENTATION", |
| 2073 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 2074 | } |
| 2075 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2076 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 2077 | my $ctx = join("\n", @ctx); |
| 2078 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2079 | my $ctx_ln = $linenr; |
| 2080 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2081 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2082 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 2083 | defined $lines[$ctx_ln - 1] && |
| 2084 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 2085 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 2086 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2087 | $ctx_ln++; |
| 2088 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2089 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2090 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 2091 | #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] | 2092 | |
| 2093 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2094 | ERROR("OPEN_BRACE", |
| 2095 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2096 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2097 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2098 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 2099 | $ctx =~ /\)\s*\;\s*$/ && |
| 2100 | defined $lines[$ctx_ln - 1]) |
| 2101 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2102 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 2103 | if ($nindent > $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2104 | WARN("TRAILING_SEMICOLON", |
| 2105 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2106 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2107 | } |
| 2108 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2111 | # Check relative indent for conditionals and blocks. |
| 2112 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2113 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2114 | ctx_statement_block($linenr, $realcnt, 0) |
| 2115 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2116 | my ($s, $c) = ($stat, $cond); |
| 2117 | |
| 2118 | substr($s, 0, length($c), ''); |
| 2119 | |
| 2120 | # Make sure we remove the line prefixes as we have |
| 2121 | # none on the first line, and are going to readd them |
| 2122 | # where necessary. |
| 2123 | $s =~ s/\n./\n/gs; |
| 2124 | |
| 2125 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2126 | my @newlines = ($c =~ /\n/gs); |
| 2127 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2128 | |
| 2129 | # We want to check the first line inside the block |
| 2130 | # starting at the end of the conditional, so remove: |
| 2131 | # 1) any blank line termination |
| 2132 | # 2) any opening brace { on end of the line |
| 2133 | # 3) any do (...) { |
| 2134 | my $continuation = 0; |
| 2135 | my $check = 0; |
| 2136 | $s =~ s/^.*\bdo\b//; |
| 2137 | $s =~ s/^\s*{//; |
| 2138 | if ($s =~ s/^\s*\\//) { |
| 2139 | $continuation = 1; |
| 2140 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2141 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2142 | $check = 1; |
| 2143 | $cond_lines++; |
| 2144 | } |
| 2145 | |
| 2146 | # Also ignore a loop construct at the end of a |
| 2147 | # preprocessor statement. |
| 2148 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 2149 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 2150 | $check = 0; |
| 2151 | } |
| 2152 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2153 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2154 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2155 | while ($cond_ptr != $cond_lines) { |
| 2156 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2157 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2158 | # If we see an #else/#elif then the code |
| 2159 | # is not linear. |
| 2160 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 2161 | $check = 0; |
| 2162 | } |
| 2163 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2164 | # Ignore: |
| 2165 | # 1) blank lines, they should be at 0, |
| 2166 | # 2) preprocessor lines, and |
| 2167 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2168 | if ($continuation || |
| 2169 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2170 | $s =~ /^\s*#\s*?/ || |
| 2171 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2172 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 2173 | if ($s =~ s/^.*?\n//) { |
| 2174 | $cond_lines++; |
| 2175 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2176 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | my (undef, $sindent) = line_stats("+" . $s); |
| 2180 | my $stat_real = raw_line($linenr, $cond_lines); |
| 2181 | |
| 2182 | # Check if either of these lines are modified, else |
| 2183 | # this is not this patch's fault. |
| 2184 | if (!defined($stat_real) || |
| 2185 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 2186 | $check = 0; |
| 2187 | } |
| 2188 | if (defined($stat_real) && $cond_lines > 1) { |
| 2189 | $stat_real = "[...]\n$stat_real"; |
| 2190 | } |
| 2191 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2192 | #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] | 2193 | |
| 2194 | if ($check && (($sindent % 8) != 0 || |
| 2195 | ($sindent <= $indent && $s ne ''))) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2196 | WARN("SUSPECT_CODE_INDENT", |
| 2197 | "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2201 | # Track the 'values' across context and added lines. |
| 2202 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2203 | my ($curr_values, $curr_vars) = |
| 2204 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2205 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2206 | if ($dbg_values) { |
| 2207 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2208 | print "$linenr > .$outline\n"; |
| 2209 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2210 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2211 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2212 | $prev_values = substr($curr_values, -1); |
| 2213 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2214 | #ignore lines not being added |
| 2215 | if ($line=~/^[^\+]/) {next;} |
| 2216 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2217 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2218 | if ($dbg_type) { |
| 2219 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2220 | ERROR("TEST_TYPE", |
| 2221 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2222 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2223 | ERROR("TEST_NOT_TYPE", |
| 2224 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2225 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2226 | next; |
| 2227 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2228 | # TEST: allow direct testing of the attribute matcher. |
| 2229 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2230 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2231 | ERROR("TEST_ATTR", |
| 2232 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2233 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2234 | ERROR("TEST_NOT_ATTR", |
| 2235 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2236 | } |
| 2237 | next; |
| 2238 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2239 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2240 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 2241 | if ($line =~ /^.\s*{/ && |
| 2242 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2243 | ERROR("OPEN_BRACE", |
| 2244 | "that open brace { should be on the previous line\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2247 | # |
| 2248 | # Checks which are anchored on the added line. |
| 2249 | # |
| 2250 | |
| 2251 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2252 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2253 | my $path = $1; |
| 2254 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2255 | ERROR("MALFORMED_INCLUDE", |
Joe Perches | 495e9d8 | 2012-12-20 15:05:37 -0800 | [diff] [blame] | 2256 | "malformed #include filename\n" . $herecurr); |
| 2257 | } |
| 2258 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 2259 | ERROR("UAPI_INCLUDE", |
| 2260 | "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2261 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2262 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2263 | |
| 2264 | # no C99 // comments |
| 2265 | if ($line =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2266 | ERROR("C99_COMMENTS", |
| 2267 | "do not use C99 // comments\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2268 | } |
| 2269 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2270 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2271 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2272 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2273 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 2274 | # the whole statement. |
| 2275 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 2276 | if (defined $realline_next && |
| 2277 | exists $lines[$realline_next - 1] && |
| 2278 | !defined $suppress_export{$realline_next} && |
| 2279 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2280 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2281 | # Handle definitions which produce identifiers with |
| 2282 | # a prefix: |
| 2283 | # XXX(foo); |
| 2284 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2285 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 2286 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2287 | $name =~ /^${Ident}_$2/) { |
| 2288 | #print "FOO C name<$name>\n"; |
| 2289 | $suppress_export{$realline_next} = 1; |
| 2290 | |
| 2291 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2292 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2293 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 2294 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 2295 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2296 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 2297 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2298 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2299 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 2300 | $suppress_export{$realline_next} = 2; |
| 2301 | } else { |
| 2302 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2303 | } |
| 2304 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2305 | if (!defined $suppress_export{$linenr} && |
| 2306 | $prevline =~ /^.\s*$/ && |
| 2307 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2308 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 2309 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 2310 | $suppress_export{$linenr} = 2; |
| 2311 | } |
| 2312 | if (defined $suppress_export{$linenr} && |
| 2313 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2314 | WARN("EXPORT_SYMBOL", |
| 2315 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2316 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2317 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 2318 | # check for global initialisers. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2319 | if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2320 | ERROR("GLOBAL_INITIALISERS", |
| 2321 | "do not initialise globals to 0 or NULL\n" . |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2322 | $herecurr); |
| 2323 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2324 | # check for static initialisers. |
Andy Whitcroft | 2d1bafd | 2009-01-06 14:41:28 -0800 | [diff] [blame] | 2325 | if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2326 | ERROR("INITIALISED_STATIC", |
| 2327 | "do not initialise statics to 0 or NULL\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2328 | $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2331 | # check for static const char * arrays. |
| 2332 | if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2333 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2334 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2335 | $herecurr); |
| 2336 | } |
| 2337 | |
| 2338 | # check for static char foo[] = "bar" declarations. |
| 2339 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2340 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2341 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2342 | $herecurr); |
| 2343 | } |
| 2344 | |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2345 | # check for declarations of struct pci_device_id |
| 2346 | if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2347 | WARN("DEFINE_PCI_DEVICE_TABLE", |
| 2348 | "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2351 | # check for new typedefs, only function parameters and sparse annotations |
| 2352 | # make sense. |
| 2353 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 2354 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2355 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2356 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2357 | $line !~ /\b__bitwise(?:__|)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2358 | WARN("NEW_TYPEDEFS", |
| 2359 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2363 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2364 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 2365 | #print "AA<$1>\n"; |
| 2366 | my ($from, $to) = ($2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2367 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2368 | # Should start with a space. |
| 2369 | $to =~ s/^(\S)/ $1/; |
| 2370 | # Should not end with a space. |
| 2371 | $to =~ s/\s+$//; |
| 2372 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2373 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2374 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2375 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2376 | #print "from<$from> to<$to>\n"; |
| 2377 | if ($from ne $to) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2378 | ERROR("POINTER_LOCATION", |
| 2379 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2380 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2381 | } |
| 2382 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 2383 | #print "BB<$1>\n"; |
| 2384 | my ($from, $to, $ident) = ($2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2385 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2386 | # Should start with a space. |
| 2387 | $to =~ s/^(\S)/ $1/; |
| 2388 | # Should not end with a space. |
| 2389 | $to =~ s/\s+$//; |
| 2390 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2391 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2392 | } |
| 2393 | # Modifiers should have spaces. |
| 2394 | $to =~ s/(\b$Modifier$)/$1 /; |
| 2395 | |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2396 | #print "from<$from> to<$to> ident<$ident>\n"; |
| 2397 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2398 | ERROR("POINTER_LOCATION", |
| 2399 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2400 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | # # no BUG() or BUG_ON() |
| 2404 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 2405 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 2406 | # print "$herecurr"; |
| 2407 | # $clean = 0; |
| 2408 | # } |
| 2409 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2410 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2411 | WARN("LINUX_VERSION_CODE", |
| 2412 | "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] | 2413 | } |
| 2414 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2415 | # check for uses of printk_ratelimit |
| 2416 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2417 | WARN("PRINTK_RATELIMITED", |
| 2418 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2419 | } |
| 2420 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2421 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 2422 | # same line do not need a level, so we use the current block context |
| 2423 | # 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] | 2424 | # 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] | 2425 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2426 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2427 | my $ok = 0; |
| 2428 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 2429 | #print "CHECK<$lines[$ln - 1]\n"; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2430 | # we have a preceding printk if it ends |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2431 | # with "\n" ignore it, else it is to blame |
| 2432 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 2433 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 2434 | $ok = 1; |
| 2435 | } |
| 2436 | last; |
| 2437 | } |
| 2438 | } |
| 2439 | if ($ok == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2440 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 2441 | "printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2442 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2443 | } |
| 2444 | |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2445 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 2446 | my $orig = $1; |
| 2447 | my $level = lc($orig); |
| 2448 | $level = "warn" if ($level eq "warning"); |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2449 | my $level2 = $level; |
| 2450 | $level2 = "dbg" if ($level eq "debug"); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2451 | WARN("PREFER_PR_LEVEL", |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2452 | "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | if ($line =~ /\bpr_warning\s*\(/) { |
| 2456 | WARN("PREFER_PR_LEVEL", |
| 2457 | "Prefer pr_warn(... to pr_warning(...\n" . $herecurr); |
| 2458 | } |
| 2459 | |
Joe Perches | dc13931 | 2013-02-21 16:44:13 -0800 | [diff] [blame] | 2460 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 2461 | my $orig = $1; |
| 2462 | my $level = lc($orig); |
| 2463 | $level = "warn" if ($level eq "warning"); |
| 2464 | $level = "dbg" if ($level eq "debug"); |
| 2465 | WARN("PREFER_DEV_LEVEL", |
| 2466 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 2467 | } |
| 2468 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2469 | # function brace can't be on same line, except for #defines of do while, |
| 2470 | # or if closed on same line |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2471 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
| 2472 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2473 | ERROR("OPEN_BRACE", |
| 2474 | "open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2475 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2476 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2477 | # open braces for enum, union and struct go on the same line. |
| 2478 | if ($line =~ /^.\s*{/ && |
| 2479 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2480 | ERROR("OPEN_BRACE", |
| 2481 | "open brace '{' following $1 go on the same line\n" . $hereprev); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2482 | } |
| 2483 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2484 | # missing space after union, struct or enum definition |
| 2485 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2486 | WARN("SPACING", |
| 2487 | "missing space after $1 definition\n" . $herecurr); |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2490 | # check for spacing round square brackets; allowed: |
| 2491 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2492 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 2493 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2494 | while ($line =~ /(.*?\s)\[/g) { |
| 2495 | my ($where, $prefix) = ($-[1], $1); |
| 2496 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2497 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Andy Whitcroft | daebc53 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 2498 | $prefix !~ /[{,]\s+$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2499 | ERROR("BRACKET_SPACE", |
| 2500 | "space prohibited before open square bracket '['\n" . $herecurr); |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2504 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2505 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2506 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2507 | my $ctx_before = substr($line, 0, $-[1]); |
| 2508 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2509 | |
| 2510 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2511 | if ($name =~ /^(?: |
| 2512 | if|for|while|switch|return|case| |
| 2513 | volatile|__volatile__| |
| 2514 | __attribute__|format|__extension__| |
| 2515 | asm|__asm__)$/x) |
| 2516 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2517 | |
| 2518 | # cpp #define statements have non-optional spaces, ie |
| 2519 | # if there is a space between the name and the open |
| 2520 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2521 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2522 | |
| 2523 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2524 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2525 | |
| 2526 | # If this whole things ends with a type its most |
| 2527 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2528 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2529 | |
| 2530 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2531 | WARN("SPACING", |
| 2532 | "space prohibited between function name and open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2533 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2534 | } |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2535 | |
| 2536 | # check for whitespace before a non-naked semicolon |
| 2537 | if ($line =~ /^\+.*\S\s+;/) { |
Joe Perches | 5646bc7 | 2013-04-29 16:18:15 -0700 | [diff] [blame] | 2538 | WARN("SPACING", |
| 2539 | "space prohibited before semicolon\n" . $herecurr); |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2542 | # Check operator spacing. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2543 | if (!($line=~/\#\s*include/)) { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2544 | my $ops = qr{ |
| 2545 | <<=|>>=|<=|>=|==|!=| |
| 2546 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 2547 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2548 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
| 2549 | \?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2550 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2551 | my @elements = split(/($ops|;)/, $opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2552 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2553 | |
| 2554 | my $blank = copy_spacing($opline); |
| 2555 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2556 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2557 | $off += length($elements[$n]); |
| 2558 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2559 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2560 | my $ca = substr($opline, 0, $off); |
| 2561 | my $cc = ''; |
| 2562 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 2563 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 2564 | } |
| 2565 | my $cb = "$ca$;$cc"; |
| 2566 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2567 | my $a = ''; |
| 2568 | $a = 'V' if ($elements[$n] ne ''); |
| 2569 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2570 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2571 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 2572 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2573 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2574 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2575 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2576 | |
| 2577 | my $c = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2578 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2579 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 2580 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2581 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2582 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 2583 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 2584 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2585 | } else { |
| 2586 | $c = 'E'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2589 | my $ctx = "${a}x${c}"; |
| 2590 | |
| 2591 | my $at = "(ctx:$ctx)"; |
| 2592 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2593 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2594 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2595 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2596 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2597 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2598 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2599 | # Get the full operator variant. |
| 2600 | my $opv = $op . substr($curr_vars, $off, 1); |
| 2601 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2602 | # Ignore operators passed as parameters. |
| 2603 | if ($op_type ne 'V' && |
| 2604 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { |
| 2605 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2606 | # # Ignore comments |
| 2607 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2608 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2609 | # ; 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] | 2610 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2611 | if ($ctx !~ /.x[WEBC]/ && |
| 2612 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2613 | ERROR("SPACING", |
| 2614 | "space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | # // is a comment |
| 2618 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2619 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2620 | # No spaces for: |
| 2621 | # -> |
| 2622 | # : when part of a bitfield |
| 2623 | } elsif ($op eq '->' || $opv eq ':B') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2624 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2625 | ERROR("SPACING", |
| 2626 | "spaces prohibited around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | # , must have a space on the right. |
| 2630 | } elsif ($op eq ',') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2631 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2632 | ERROR("SPACING", |
| 2633 | "space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2636 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2637 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2638 | #warn "'*' is part of type\n"; |
| 2639 | |
| 2640 | # unary operators should have a space before and |
| 2641 | # none after. May be left adjacent to another |
| 2642 | # unary operator, or a cast |
| 2643 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2644 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2645 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2646 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2647 | ERROR("SPACING", |
| 2648 | "space required before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2649 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 2650 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2651 | # A unary '*' may be const |
| 2652 | |
| 2653 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2654 | ERROR("SPACING", |
| 2655 | "space prohibited after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | # unary ++ and unary -- are allowed no space on one side. |
| 2659 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2660 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2661 | ERROR("SPACING", |
| 2662 | "space required one side of that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2663 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2664 | if ($ctx =~ /Wx[BE]/ || |
| 2665 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2666 | ERROR("SPACING", |
| 2667 | "space prohibited before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2668 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2669 | if ($ctx =~ /ExW/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2670 | ERROR("SPACING", |
| 2671 | "space prohibited after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2672 | } |
| 2673 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2674 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2675 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2676 | } elsif ($op eq '<<' or $op eq '>>' or |
| 2677 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 2678 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2679 | $op eq '*' or $op eq '/' or |
| 2680 | $op eq '%') |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2681 | { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2682 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2683 | ERROR("SPACING", |
| 2684 | "need consistent spacing around '$op' $at\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2685 | $hereptr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2688 | # A colon needs no spaces before when it is |
| 2689 | # terminating a case value or a label. |
| 2690 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 2691 | if ($ctx =~ /Wx./) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2692 | ERROR("SPACING", |
| 2693 | "space prohibited before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2696 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2697 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2698 | my $ok = 0; |
| 2699 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2700 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2701 | if (($op eq '<' && |
| 2702 | $cc =~ /^\S+\@\S+>/) || |
| 2703 | ($op eq '>' && |
| 2704 | $ca =~ /<\S+\@\S+$/)) |
| 2705 | { |
| 2706 | $ok = 1; |
| 2707 | } |
| 2708 | |
| 2709 | # Ignore ?: |
| 2710 | if (($opv eq ':O' && $ca =~ /\?$/) || |
| 2711 | ($op eq '?' && $cc =~ /^:/)) { |
| 2712 | $ok = 1; |
| 2713 | } |
| 2714 | |
| 2715 | if ($ok == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2716 | ERROR("SPACING", |
| 2717 | "spaces required around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2718 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2719 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2720 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2724 | # check for multiple assignments |
| 2725 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2726 | CHK("MULTIPLE_ASSIGNMENTS", |
| 2727 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2730 | ## # check for multiple declarations, allowing for a function declaration |
| 2731 | ## # continuation. |
| 2732 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 2733 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 2734 | ## |
| 2735 | ## # Remove any bracketed sections to ensure we do not |
| 2736 | ## # falsly report the parameters of functions. |
| 2737 | ## my $ln = $line; |
| 2738 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 2739 | ## } |
| 2740 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2741 | ## WARN("MULTIPLE_DECLARATION", |
| 2742 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2743 | ## } |
| 2744 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2745 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2746 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2747 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 2748 | $line =~ /do{/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2749 | ERROR("SPACING", |
| 2750 | "space required before the open brace '{'\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | # closing brace should have a space following it when it has anything |
| 2754 | # on the line |
| 2755 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2756 | ERROR("SPACING", |
| 2757 | "space required after that close brace '}'\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2760 | # check spacing on square brackets |
| 2761 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2762 | ERROR("SPACING", |
| 2763 | "space prohibited after that open square bracket '['\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2764 | } |
| 2765 | if ($line =~ /\s\]/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2766 | ERROR("SPACING", |
| 2767 | "space prohibited before that close square bracket ']'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2770 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2771 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 2772 | $line !~ /for\s*\(\s+;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2773 | ERROR("SPACING", |
| 2774 | "space prohibited after that open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2775 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2776 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2777 | $line !~ /for\s*\(.*;\s+\)/ && |
| 2778 | $line !~ /:\s+\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2779 | ERROR("SPACING", |
| 2780 | "space prohibited before that close parenthesis ')'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2783 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2784 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2785 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2786 | WARN("INDENTED_LABEL", |
| 2787 | "labels should not be indented\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2788 | } |
| 2789 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2790 | # Return is not a function. |
| 2791 | if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { |
| 2792 | my $spacing = $1; |
| 2793 | my $value = $2; |
| 2794 | |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2795 | # Flatten any parentheses |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2796 | $value =~ s/\(/ \(/g; |
| 2797 | $value =~ s/\)/\) /g; |
Andy Whitcroft | e01886a | 2012-01-10 15:10:08 -0800 | [diff] [blame] | 2798 | while ($value =~ s/\[[^\[\]]*\]/1/ || |
Andy Whitcroft | 63f17f8 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 2799 | $value !~ /(?:$Ident|-?$Constant)\s* |
| 2800 | $Compare\s* |
| 2801 | (?:$Ident|-?$Constant)/x && |
| 2802 | $value =~ s/\([^\(\)]*\)/1/) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2803 | } |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2804 | #print "value<$value>\n"; |
| 2805 | if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2806 | ERROR("RETURN_PARENTHESES", |
| 2807 | "return is not a function, parentheses are not required\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2808 | |
| 2809 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2810 | ERROR("SPACING", |
| 2811 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2812 | } |
| 2813 | } |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 2814 | # Return of what appears to be an errno should normally be -'ve |
| 2815 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
| 2816 | my $name = $1; |
| 2817 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2818 | WARN("USE_NEGATIVE_ERRNO", |
| 2819 | "return of an errno should typically be -ve (return -$1)\n" . $herecurr); |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 2820 | } |
| 2821 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2822 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2823 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2824 | if ($line=~/\b(if|while|for|switch)\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2825 | ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2828 | # Check for illegal assignment in if conditional -- and check for trailing |
| 2829 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2830 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2831 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2832 | ctx_statement_block($linenr, $realcnt, 0) |
| 2833 | if (!defined $stat); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2834 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 2835 | $remain_next, $off_next); |
| 2836 | $stat_next =~ s/\n./\n /g; |
| 2837 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 2838 | |
| 2839 | if ($stat_next =~ /^\s*while\b/) { |
| 2840 | # If the statement carries leading newlines, |
| 2841 | # then count those as offsets. |
| 2842 | my ($whitespace) = |
| 2843 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 2844 | my $offset = |
| 2845 | statement_rawlines($whitespace) - 1; |
| 2846 | |
| 2847 | $suppress_whiletrailers{$line_nr_next + |
| 2848 | $offset} = 1; |
| 2849 | } |
| 2850 | } |
| 2851 | if (!defined $suppress_whiletrailers{$linenr} && |
| 2852 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2853 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2854 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 2855 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2856 | ERROR("ASSIGN_IN_IF", |
| 2857 | "do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | # Find out what is on the end of the line after the |
| 2861 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2862 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2863 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2864 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2865 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 2866 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2867 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2868 | # Find out how long the conditional actually is. |
| 2869 | my @newlines = ($c =~ /\n/gs); |
| 2870 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 2871 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2872 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 2873 | $stat_real = raw_line($linenr, $cond_lines) |
| 2874 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2875 | if (defined($stat_real) && $cond_lines > 1) { |
| 2876 | $stat_real = "[...]\n$stat_real"; |
| 2877 | } |
| 2878 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2879 | ERROR("TRAILING_STATEMENTS", |
| 2880 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2884 | # Check for bitwise tests written as boolean |
| 2885 | if ($line =~ / |
| 2886 | (?: |
| 2887 | (?:\[|\(|\&\&|\|\|) |
| 2888 | \s*0[xX][0-9]+\s* |
| 2889 | (?:\&\&|\|\|) |
| 2890 | | |
| 2891 | (?:\&\&|\|\|) |
| 2892 | \s*0[xX][0-9]+\s* |
| 2893 | (?:\&\&|\|\||\)|\]) |
| 2894 | )/x) |
| 2895 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2896 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 2897 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2898 | } |
| 2899 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2900 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2901 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 2902 | my $s = $1; |
| 2903 | $s =~ s/$;//g; # Remove any comments |
| 2904 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2905 | ERROR("TRAILING_STATEMENTS", |
| 2906 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2907 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2908 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 2909 | # if should not continue a brace |
| 2910 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2911 | ERROR("TRAILING_STATEMENTS", |
| 2912 | "trailing statements should be on next line\n" . |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 2913 | $herecurr); |
| 2914 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 2915 | # case and default should not have general statements after them |
| 2916 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 2917 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 2918 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 2919 | \s*return\s+ |
| 2920 | )/xg) |
| 2921 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2922 | ERROR("TRAILING_STATEMENTS", |
| 2923 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 2924 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2925 | |
| 2926 | # Check for }<nl>else {, these must be at the same |
| 2927 | # indent level to be relevant to each other. |
| 2928 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 2929 | $previndent == $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2930 | ERROR("ELSE_AFTER_BRACE", |
| 2931 | "else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2932 | } |
| 2933 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2934 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
| 2935 | $previndent == $indent) { |
| 2936 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 2937 | |
| 2938 | # Find out what is on the end of the line after the |
| 2939 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2940 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2941 | $s =~ s/\n.*//g; |
| 2942 | |
| 2943 | if ($s =~ /^\s*;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2944 | ERROR("WHILE_AFTER_BRACE", |
| 2945 | "while should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2946 | } |
| 2947 | } |
| 2948 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 2949 | #Specific variable tests |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 2950 | while ($line =~ m{($Constant|$Lval)}g) { |
| 2951 | my $var = $1; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 2952 | |
| 2953 | #gcc binary extension |
| 2954 | if ($var =~ /^$Binary$/) { |
| 2955 | WARN("GCC_BINARY_CONSTANT", |
| 2956 | "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr); |
| 2957 | } |
| 2958 | |
| 2959 | #CamelCase |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 2960 | if ($var !~ /^$Constant$/ && |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 2961 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 2962 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 2963 | !defined $camelcase{$var}) { |
| 2964 | $camelcase{$var} = 1; |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 2965 | CHK("CAMELCASE", |
| 2966 | "Avoid CamelCase: <$var>\n" . $herecurr); |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 2967 | } |
| 2968 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2969 | |
| 2970 | #no spaces allowed after \ in define |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2971 | if ($line=~/\#\s*define.*\\\s$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2972 | WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 2973 | "Whitepspace after \\ makes next lines useless\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2976 | #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] | 2977 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2978 | my $file = "$1.h"; |
| 2979 | my $checkfile = "include/linux/$file"; |
| 2980 | if (-f "$root/$checkfile" && |
| 2981 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 2982 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2983 | { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2984 | if ($realfile =~ m{^arch/}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2985 | CHK("ARCH_INCLUDE_LINUX", |
| 2986 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2987 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2988 | WARN("INCLUDE_LINUX", |
| 2989 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2990 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2991 | } |
| 2992 | } |
| 2993 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2994 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 2995 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2996 | # in a known good container |
Andy Whitcroft | b8f96a31 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 2997 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 2998 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2999 | my $ln = $linenr; |
| 3000 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3001 | my ($off, $dstat, $dcond, $rest); |
| 3002 | my $ctx = ''; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3003 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3004 | ctx_statement_block($linenr, $realcnt, 0); |
| 3005 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3006 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 3007 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3008 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3009 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 3010 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3011 | $dstat =~ s/\\\n.//g; |
| 3012 | $dstat =~ s/^\s*//s; |
| 3013 | $dstat =~ s/\s*$//s; |
| 3014 | |
| 3015 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3016 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 3017 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Andy Whitcroft | c81769f | 2012-01-10 15:10:10 -0800 | [diff] [blame] | 3018 | $dstat =~ s/\[[^\[\]]*\]/1/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3019 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3020 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3021 | |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 3022 | # Flatten any obvious string concatentation. |
| 3023 | while ($dstat =~ s/("X*")\s*$Ident/$1/ || |
| 3024 | $dstat =~ s/$Ident\s*("X*")/$1/) |
| 3025 | { |
| 3026 | } |
| 3027 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3028 | my $exceptions = qr{ |
| 3029 | $Declare| |
| 3030 | module_param_named| |
Kees Cook | a0a0a7a | 2012-10-04 17:13:38 -0700 | [diff] [blame] | 3031 | MODULE_PARM_DESC| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3032 | DECLARE_PER_CPU| |
| 3033 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 3034 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 3035 | union| |
| 3036 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 3037 | \.$Ident\s*=\s*| |
| 3038 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3039 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3040 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3041 | if ($dstat ne '' && |
| 3042 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 3043 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Andy Whitcroft | fd1b57a | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 3044 | $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo |
Andy Whitcroft | b9df76a | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3045 | $dstat !~ /^'X'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3046 | $dstat !~ /$exceptions/ && |
| 3047 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Joe Perches | e942e2c | 2013-04-17 15:58:26 -0700 | [diff] [blame] | 3048 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 3049 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3050 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 3051 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 3052 | $dstat !~ /^do\s*{/ && # do {... |
| 3053 | $dstat !~ /^\({/) # ({... |
| 3054 | { |
| 3055 | $ctx =~ s/\n*$//; |
| 3056 | my $herectx = $here . "\n"; |
| 3057 | my $cnt = statement_rawlines($ctx); |
| 3058 | |
| 3059 | for (my $n = 0; $n < $cnt; $n++) { |
| 3060 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3063 | if ($dstat =~ /;/) { |
| 3064 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 3065 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 3066 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3067 | ERROR("COMPLEX_MACRO", |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3068 | "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3069 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3070 | } |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3071 | |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3072 | # check for line continuations outside of #defines, preprocessor #, and asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3073 | |
| 3074 | } else { |
| 3075 | if ($prevline !~ /^..*\\$/ && |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3076 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 3077 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3078 | $line =~ /^\+.*\\$/) { |
| 3079 | WARN("LINE_CONTINUATIONS", |
| 3080 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 3081 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3084 | # do {} while (0) macro tests: |
| 3085 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 3086 | # macro should not end with a semicolon |
| 3087 | if ($^V && $^V ge 5.10.0 && |
| 3088 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 3089 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 3090 | my $ln = $linenr; |
| 3091 | my $cnt = $realcnt; |
| 3092 | my ($off, $dstat, $dcond, $rest); |
| 3093 | my $ctx = ''; |
| 3094 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 3095 | ctx_statement_block($linenr, $realcnt, 0); |
| 3096 | $ctx = $dstat; |
| 3097 | |
| 3098 | $dstat =~ s/\\\n.//g; |
| 3099 | |
| 3100 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 3101 | my $stmts = $2; |
| 3102 | my $semis = $3; |
| 3103 | |
| 3104 | $ctx =~ s/\n*$//; |
| 3105 | my $cnt = statement_rawlines($ctx); |
| 3106 | my $herectx = $here . "\n"; |
| 3107 | |
| 3108 | for (my $n = 0; $n < $cnt; $n++) { |
| 3109 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 3110 | } |
| 3111 | |
Joe Perches | ac8e97f | 2012-08-21 16:15:53 -0700 | [diff] [blame] | 3112 | if (($stmts =~ tr/;/;/) == 1 && |
| 3113 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3114 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 3115 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 3116 | } |
| 3117 | if (defined $semis && $semis ne "") { |
| 3118 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 3119 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 3120 | } |
| 3121 | } |
| 3122 | } |
| 3123 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3124 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 3125 | # all assignments may have only one of the following with an assignment: |
| 3126 | # . |
| 3127 | # ALIGN(...) |
| 3128 | # VMLINUX_SYMBOL(...) |
| 3129 | if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3130 | WARN("MISSING_VMLINUX_SYMBOL", |
| 3131 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3132 | } |
| 3133 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3134 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3135 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 3136 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3137 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3138 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3139 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 3140 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3141 | my @allowed = (); |
| 3142 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3143 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3144 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3145 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3146 | for my $chunk (@chunks) { |
| 3147 | my ($cond, $block) = @{$chunk}; |
| 3148 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3149 | # If the condition carries leading newlines, then count those as offsets. |
| 3150 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3151 | my $offset = statement_rawlines($whitespace) - 1; |
| 3152 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3153 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3154 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 3155 | |
| 3156 | # We have looked at and allowed this specific line. |
| 3157 | $suppress_ifbraces{$ln + $offset} = 1; |
| 3158 | |
| 3159 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3160 | $ln += statement_rawlines($block) - 1; |
| 3161 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3162 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3163 | |
| 3164 | $seen++ if ($block =~ /^\s*{/); |
| 3165 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3166 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3167 | if (statement_lines($cond) > 1) { |
| 3168 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3169 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3170 | } |
| 3171 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3172 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3173 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3174 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3175 | if (statement_block_size($block) > 1) { |
| 3176 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3177 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3178 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3179 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3180 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3181 | if ($seen) { |
| 3182 | my $sum_allowed = 0; |
| 3183 | foreach (@allowed) { |
| 3184 | $sum_allowed += $_; |
| 3185 | } |
| 3186 | if ($sum_allowed == 0) { |
| 3187 | WARN("BRACES", |
| 3188 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 3189 | } elsif ($sum_allowed != $allow && |
| 3190 | $seen != $allow) { |
| 3191 | CHK("BRACES", |
| 3192 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 3193 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3197 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3198 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3199 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3200 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3201 | # Check the pre-context. |
| 3202 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 3203 | #print "APW: ALLOWED: pre<$1>\n"; |
| 3204 | $allowed = 1; |
| 3205 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3206 | |
| 3207 | my ($level, $endln, @chunks) = |
| 3208 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 3209 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3210 | # Check the condition. |
| 3211 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3212 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3213 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3214 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3215 | } |
| 3216 | if (statement_lines($cond) > 1) { |
| 3217 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 3218 | $allowed = 1; |
| 3219 | } |
| 3220 | if ($block =~/\b(?:if|for|while)\b/) { |
| 3221 | #print "APW: ALLOWED: block<$block>\n"; |
| 3222 | $allowed = 1; |
| 3223 | } |
| 3224 | if (statement_block_size($block) > 1) { |
| 3225 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 3226 | $allowed = 1; |
| 3227 | } |
| 3228 | # Check the post-context. |
| 3229 | if (defined $chunks[1]) { |
| 3230 | my ($cond, $block) = @{$chunks[1]}; |
| 3231 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3232 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3233 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3234 | if ($block =~ /^\s*\{/) { |
| 3235 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 3236 | $allowed = 1; |
| 3237 | } |
| 3238 | } |
| 3239 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3240 | my $herectx = $here . "\n"; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3241 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3242 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3243 | for (my $n = 0; $n < $cnt; $n++) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3244 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3245 | } |
| 3246 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3247 | WARN("BRACES", |
| 3248 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3252 | # check for unnecessary blank lines around braces |
Matthijs Kooijman | 74c8f43 | 2013-04-29 16:18:16 -0700 | [diff] [blame] | 3253 | if (($line =~ /^.\s*}\s*$/ && $prevline =~ /^.\s*$/)) { |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3254 | CHK("BRACES", |
| 3255 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); |
| 3256 | } |
| 3257 | if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
| 3258 | CHK("BRACES", |
| 3259 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); |
| 3260 | } |
| 3261 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3262 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3263 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 3264 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3265 | WARN("VOLATILE", |
| 3266 | "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] | 3267 | } |
| 3268 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3269 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3270 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3271 | CHK("REDUNDANT_CODE", |
| 3272 | "if this code is redundant consider removing it\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3273 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3274 | } |
| 3275 | |
Andy Whitcroft | 03df4b5 | 2012-12-17 16:01:52 -0800 | [diff] [blame] | 3276 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 3277 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
| 3278 | my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; |
| 3279 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { |
| 3280 | WARN('NEEDLESS_IF', |
| 3281 | "$1(NULL) is safe this check is probably not required\n" . $hereprev); |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 3282 | } |
| 3283 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3284 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3285 | # prefer usleep_range over udelay |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3286 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3287 | # ignore udelay's < 10, however |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3288 | if (! ($1 < 10) ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3289 | CHK("USLEEP_RANGE", |
| 3290 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3291 | } |
| 3292 | } |
| 3293 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3294 | # warn about unexpectedly long msleep's |
| 3295 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 3296 | if ($1 < 20) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3297 | WARN("MSLEEP", |
| 3298 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3299 | } |
| 3300 | } |
| 3301 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3302 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3303 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3304 | # print "#ifdef in C files should be avoided\n"; |
| 3305 | # print "$herecurr"; |
| 3306 | # $clean = 0; |
| 3307 | # } |
| 3308 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3309 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3310 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3311 | ERROR("SPACING", |
| 3312 | "exactly one space required after that #$1\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3315 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3316 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 3317 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3318 | my $which = $1; |
| 3319 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3320 | CHK("UNCOMMENTED_DEFINITION", |
| 3321 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3322 | } |
| 3323 | } |
| 3324 | # check for memory barriers without a comment. |
| 3325 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 3326 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3327 | CHK("MEMORY_BARRIER", |
| 3328 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3329 | } |
| 3330 | } |
| 3331 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3332 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3333 | CHK("ARCH_DEFINES", |
| 3334 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3335 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3336 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3337 | # Check that the storage class is at the beginning of a declaration |
| 3338 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3339 | WARN("STORAGE_CLASS", |
| 3340 | "storage class should be at the beginning of the declaration\n" . $herecurr) |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3341 | } |
| 3342 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3343 | # check the location of the inline attribute, that it is between |
| 3344 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3345 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 3346 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3347 | ERROR("INLINE_LOCATION", |
| 3348 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3351 | # Check for __inline__ and __inline, prefer inline |
| 3352 | if ($line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3353 | WARN("INLINE", |
| 3354 | "plain inline is preferred over $1\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3355 | } |
| 3356 | |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3357 | # Check for __attribute__ packed, prefer __packed |
| 3358 | if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3359 | WARN("PREFER_PACKED", |
| 3360 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 3361 | } |
| 3362 | |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3363 | # Check for __attribute__ aligned, prefer __aligned |
| 3364 | if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3365 | WARN("PREFER_ALIGNED", |
| 3366 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 3369 | # Check for __attribute__ format(printf, prefer __printf |
| 3370 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
| 3371 | WARN("PREFER_PRINTF", |
| 3372 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); |
| 3373 | } |
| 3374 | |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3375 | # Check for __attribute__ format(scanf, prefer __scanf |
| 3376 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
| 3377 | WARN("PREFER_SCANF", |
| 3378 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr); |
| 3379 | } |
| 3380 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3381 | # check for sizeof(&) |
| 3382 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3383 | WARN("SIZEOF_ADDRESS", |
| 3384 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 3385 | } |
| 3386 | |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 3387 | # check for sizeof without parenthesis |
| 3388 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
| 3389 | WARN("SIZEOF_PARENTHESIS", |
| 3390 | "sizeof $1 should be sizeof($1)\n" . $herecurr); |
| 3391 | } |
| 3392 | |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3393 | # check for line continuations in quoted strings with odd counts of " |
| 3394 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3395 | WARN("LINE_CONTINUATIONS", |
| 3396 | "Avoid line continuations in quoted strings\n" . $herecurr); |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 3397 | } |
| 3398 | |
Joe Perches | 88982fe | 2012-12-17 16:02:00 -0800 | [diff] [blame] | 3399 | # check for struct spinlock declarations |
| 3400 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 3401 | WARN("USE_SPINLOCK_T", |
| 3402 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 3403 | } |
| 3404 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 3405 | # check for seq_printf uses that could be seq_puts |
| 3406 | if ($line =~ /\bseq_printf\s*\(/) { |
| 3407 | my $fmt = get_quoted_string($line, $rawline); |
| 3408 | if ($fmt !~ /[^\\]\%/) { |
| 3409 | WARN("PREFER_SEQ_PUTS", |
| 3410 | "Prefer seq_puts to seq_printf\n" . $herecurr); |
| 3411 | } |
| 3412 | } |
| 3413 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3414 | # Check for misused memsets |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3415 | if ($^V && $^V ge 5.10.0 && |
| 3416 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3417 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3418 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3419 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3420 | my $ms_val = $7; |
| 3421 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3422 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3423 | if ($ms_size =~ /^(0x|)0$/i) { |
| 3424 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3425 | "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3426 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 3427 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3428 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 3429 | } |
| 3430 | } |
| 3431 | |
| 3432 | # typecasts on min/max could be min_t/max_t |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3433 | if ($^V && $^V ge 5.10.0 && |
| 3434 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3435 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3436 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3437 | my $call = $1; |
| 3438 | my $cast1 = deparenthesize($2); |
| 3439 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3440 | my $cast2 = deparenthesize($7); |
| 3441 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3442 | my $cast; |
| 3443 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3444 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 3445 | $cast = "$cast1 or $cast2"; |
| 3446 | } elsif ($cast1 ne "") { |
| 3447 | $cast = $cast1; |
| 3448 | } else { |
| 3449 | $cast = $cast2; |
| 3450 | } |
| 3451 | WARN("MINMAX", |
| 3452 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 3453 | } |
| 3454 | } |
| 3455 | |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 3456 | # check usleep_range arguments |
| 3457 | if ($^V && $^V ge 5.10.0 && |
| 3458 | defined $stat && |
| 3459 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 3460 | my $min = $1; |
| 3461 | my $max = $7; |
| 3462 | if ($min eq $max) { |
| 3463 | WARN("USLEEP_RANGE", |
| 3464 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 3465 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 3466 | $min > $max) { |
| 3467 | WARN("USLEEP_RANGE", |
| 3468 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 3469 | } |
| 3470 | } |
| 3471 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3472 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3473 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3474 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3475 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3476 | my $function_name = $1; |
| 3477 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3478 | |
| 3479 | my $s = $stat; |
| 3480 | if (defined $cond) { |
| 3481 | substr($s, 0, length($cond), ''); |
| 3482 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3483 | if ($s =~ /^\s*;/ && |
| 3484 | $function_name ne 'uninitialized_var') |
| 3485 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3486 | WARN("AVOID_EXTERNS", |
| 3487 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3491 | WARN("FUNCTION_ARGUMENTS", |
| 3492 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3493 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3494 | |
| 3495 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 3496 | $stat =~ /^.\s*extern\s+/) |
| 3497 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3498 | WARN("AVOID_EXTERNS", |
| 3499 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | # checks for new __setup's |
| 3503 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 3504 | my $name = $1; |
| 3505 | |
| 3506 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3507 | CHK("UNDOCUMENTED_SETUP", |
| 3508 | "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3509 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3510 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3511 | |
| 3512 | # check for pointless casting of kmalloc return |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 3513 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3514 | WARN("UNNECESSARY_CASTS", |
| 3515 | "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3516 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3517 | |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 3518 | # alloc style |
| 3519 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
| 3520 | if ($^V && $^V ge 5.10.0 && |
| 3521 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
| 3522 | CHK("ALLOC_SIZEOF_STRUCT", |
| 3523 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 3524 | } |
| 3525 | |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 3526 | # check for krealloc arg reuse |
| 3527 | if ($^V && $^V ge 5.10.0 && |
| 3528 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { |
| 3529 | WARN("KREALLOC_ARG_REUSE", |
| 3530 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 3531 | } |
| 3532 | |
Joe Perches | 5ce59ae | 2013-02-21 16:44:18 -0800 | [diff] [blame] | 3533 | # check for alloc argument mismatch |
| 3534 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 3535 | WARN("ALLOC_ARRAY_ARGS", |
| 3536 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 3537 | } |
| 3538 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 3539 | # check for multiple semicolons |
| 3540 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 3541 | WARN("ONE_SEMICOLON", |
| 3542 | "Statements terminations use 1 semicolon\n" . $herecurr); |
| 3543 | } |
| 3544 | |
| 3545 | # check for switch/default statements without a break; |
| 3546 | if ($^V && $^V ge 5.10.0 && |
| 3547 | defined $stat && |
| 3548 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
| 3549 | my $ctx = ''; |
| 3550 | my $herectx = $here . "\n"; |
| 3551 | my $cnt = statement_rawlines($stat); |
| 3552 | for (my $n = 0; $n < $cnt; $n++) { |
| 3553 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 3554 | } |
| 3555 | WARN("DEFAULT_NO_BREAK", |
| 3556 | "switch default: should use break\n" . $herectx); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 3557 | } |
| 3558 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3559 | # check for gcc specific __FUNCTION__ |
| 3560 | if ($line =~ /__FUNCTION__/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3561 | WARN("USE_FUNC", |
| 3562 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3563 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3564 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3565 | # check for use of yield() |
| 3566 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 3567 | WARN("YIELD", |
| 3568 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 3569 | } |
| 3570 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 3571 | # check for semaphores initialized locked |
| 3572 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3573 | WARN("CONSIDER_COMPLETION", |
| 3574 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3575 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3576 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 3577 | # recommend kstrto* over simple_strto* and strict_strto* |
| 3578 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3579 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 3580 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3581 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3582 | |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 3583 | # check for __initcall(), use device_initcall() explicitly please |
| 3584 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3585 | WARN("USE_DEVICE_INITCALL", |
| 3586 | "please use device_initcall() instead of __initcall()\n" . $herecurr); |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 3587 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 3588 | |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 3589 | # check for various ops structs, ensure they are const. |
| 3590 | my $struct_ops = qr{acpi_dock_ops| |
| 3591 | address_space_operations| |
| 3592 | backlight_ops| |
| 3593 | block_device_operations| |
| 3594 | dentry_operations| |
| 3595 | dev_pm_ops| |
| 3596 | dma_map_ops| |
| 3597 | extent_io_ops| |
| 3598 | file_lock_operations| |
| 3599 | file_operations| |
| 3600 | hv_ops| |
| 3601 | ide_dma_ops| |
| 3602 | intel_dvo_dev_ops| |
| 3603 | item_operations| |
| 3604 | iwl_ops| |
| 3605 | kgdb_arch| |
| 3606 | kgdb_io| |
| 3607 | kset_uevent_ops| |
| 3608 | lock_manager_operations| |
| 3609 | microcode_ops| |
| 3610 | mtrr_ops| |
| 3611 | neigh_ops| |
| 3612 | nlmsvc_binding| |
| 3613 | pci_raw_ops| |
| 3614 | pipe_buf_operations| |
| 3615 | platform_hibernation_ops| |
| 3616 | platform_suspend_ops| |
| 3617 | proto_ops| |
| 3618 | rpc_pipe_ops| |
| 3619 | seq_operations| |
| 3620 | snd_ac97_build_ops| |
| 3621 | soc_pcmcia_socket_ops| |
| 3622 | stacktrace_ops| |
| 3623 | sysfs_ops| |
| 3624 | tty_operations| |
| 3625 | usb_mon_operations| |
| 3626 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 3627 | if ($line !~ /\bconst\b/ && |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 3628 | $line =~ /\bstruct\s+($struct_ops)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3629 | WARN("CONST_STRUCT", |
| 3630 | "struct $1 should normally be const\n" . |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 3631 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 3632 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3633 | |
| 3634 | # use of NR_CPUS is usually wrong |
| 3635 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 3636 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3637 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 3638 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3639 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 3640 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 3641 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3642 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3643 | WARN("NR_CPUS", |
| 3644 | "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3645 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3646 | |
| 3647 | # check for %L{u,d,i} in strings |
| 3648 | my $string; |
| 3649 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 3650 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Andy Whitcroft | 2a1bc5d | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3651 | $string =~ s/%%/__/g; |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3652 | if ($string =~ /(?<!%)%L[udi]/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3653 | WARN("PRINTF_L", |
| 3654 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 3655 | last; |
| 3656 | } |
| 3657 | } |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 3658 | |
| 3659 | # whine mightly about in_atomic |
| 3660 | if ($line =~ /\bin_atomic\s*\(/) { |
| 3661 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3662 | ERROR("IN_ATOMIC", |
| 3663 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 3664 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3665 | WARN("IN_ATOMIC", |
| 3666 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 3667 | } |
| 3668 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 3669 | |
| 3670 | # check for lockdep_set_novalidate_class |
| 3671 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 3672 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 3673 | if ($realfile !~ m@^kernel/lockdep@ && |
| 3674 | $realfile !~ m@^include/linux/lockdep@ && |
| 3675 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3676 | ERROR("LOCKDEP", |
| 3677 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 3678 | } |
| 3679 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 3680 | |
| 3681 | if ($line =~ /debugfs_create_file.*S_IWUGO/ || |
| 3682 | $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3683 | WARN("EXPORTED_WORLD_WRITABLE", |
| 3684 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 3685 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | # If we have no input at all, then there is nothing to report on |
| 3689 | # so just keep quiet. |
| 3690 | if ($#rawlines == -1) { |
| 3691 | exit(0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3692 | } |
| 3693 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3694 | # In mailback mode only produce a report in the negative, for |
| 3695 | # things that appear to be patches. |
| 3696 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 3697 | exit(0); |
| 3698 | } |
| 3699 | |
| 3700 | # This is not a patch, and we are are in 'no-patch' mode so |
| 3701 | # just keep quiet. |
| 3702 | if (!$chk_patch && !$is_patch) { |
| 3703 | exit(0); |
| 3704 | } |
| 3705 | |
| 3706 | if (!$is_patch) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3707 | ERROR("NOT_UNIFIED_DIFF", |
| 3708 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3709 | } |
| 3710 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3711 | ERROR("MISSING_SIGN_OFF", |
| 3712 | "Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3713 | } |
| 3714 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3715 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3716 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 3717 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3718 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 3719 | (($check)? "$cnt_chk checks, " : "") . |
| 3720 | "$cnt_lines lines checked\n"; |
| 3721 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3722 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3723 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3724 | if ($quiet == 0) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3725 | |
| 3726 | if ($^V lt 5.10.0) { |
| 3727 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); |
| 3728 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); |
| 3729 | } |
| 3730 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3731 | # If there were whitespace errors which cleanpatch can fix |
| 3732 | # then suggest that. |
| 3733 | if ($rpt_cleaners) { |
| 3734 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; |
| 3735 | print " scripts/cleanfile\n\n"; |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 3736 | $rpt_cleaners = 0; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3737 | } |
| 3738 | } |
| 3739 | |
Artem Bityutskiy | 1123268 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3740 | if ($quiet == 0 && keys %ignore_type) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3741 | print "NOTE: Ignored message types:"; |
| 3742 | foreach my $ignore (sort keys %ignore_type) { |
| 3743 | print " $ignore"; |
| 3744 | } |
Artem Bityutskiy | 1123268 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3745 | print "\n\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3748 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3749 | print "$vname has no obvious style problems and is ready for submission.\n" |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3750 | } |
| 3751 | if ($clean == 0 && $quiet == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3752 | print << "EOM"; |
| 3753 | $vname has style problems, please review. |
| 3754 | |
| 3755 | If any of these errors are false positives, please report |
| 3756 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 3757 | EOM |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3758 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3759 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3760 | return $clean; |
| 3761 | } |