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