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