diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/checkpatch.pl | 388 |
1 files changed, 284 insertions, 104 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index bc6779398229..e30bac141b21 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -9,7 +9,7 @@ use strict; | |||
9 | my $P = $0; | 9 | my $P = $0; |
10 | $P =~ s@.*/@@g; | 10 | $P =~ s@.*/@@g; |
11 | 11 | ||
12 | my $V = '0.21'; | 12 | my $V = '0.24'; |
13 | 13 | ||
14 | use Getopt::Long qw(:config no_auto_abbrev); | 14 | use Getopt::Long qw(:config no_auto_abbrev); |
15 | 15 | ||
@@ -66,6 +66,7 @@ if ($#ARGV < 0) { | |||
66 | my $dbg_values = 0; | 66 | my $dbg_values = 0; |
67 | my $dbg_possible = 0; | 67 | my $dbg_possible = 0; |
68 | my $dbg_type = 0; | 68 | my $dbg_type = 0; |
69 | my $dbg_attr = 0; | ||
69 | for my $key (keys %debug) { | 70 | for my $key (keys %debug) { |
70 | eval "\${dbg_$key} = '$debug{$key}';" | 71 | eval "\${dbg_$key} = '$debug{$key}';" |
71 | } | 72 | } |
@@ -112,7 +113,10 @@ our $Attribute = qr{ | |||
112 | const| | 113 | const| |
113 | __read_mostly| | 114 | __read_mostly| |
114 | __kprobes| | 115 | __kprobes| |
115 | __(?:mem|cpu|dev|)(?:initdata|init) | 116 | __(?:mem|cpu|dev|)(?:initdata|init)| |
117 | ____cacheline_aligned| | ||
118 | ____cacheline_aligned_in_smp| | ||
119 | ____cacheline_internodealigned_in_smp | ||
116 | }x; | 120 | }x; |
117 | our $Modifier; | 121 | our $Modifier; |
118 | our $Inline = qr{inline|__always_inline|noinline}; | 122 | our $Inline = qr{inline|__always_inline|noinline}; |
@@ -142,6 +146,11 @@ our $UTF8 = qr { | |||
142 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 | 146 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
143 | }x; | 147 | }x; |
144 | 148 | ||
149 | our $typeTypedefs = qr{(?x: | ||
150 | (?:__)?(?:u|s|be|le)(?:\d|\d\d)| | ||
151 | atomic_t | ||
152 | )}; | ||
153 | |||
145 | our @typeList = ( | 154 | our @typeList = ( |
146 | qr{void}, | 155 | qr{void}, |
147 | qr{(?:unsigned\s+)?char}, | 156 | qr{(?:unsigned\s+)?char}, |
@@ -155,7 +164,6 @@ our @typeList = ( | |||
155 | qr{float}, | 164 | qr{float}, |
156 | qr{double}, | 165 | qr{double}, |
157 | qr{bool}, | 166 | qr{bool}, |
158 | qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)}, | ||
159 | qr{struct\s+$Ident}, | 167 | qr{struct\s+$Ident}, |
160 | qr{union\s+$Ident}, | 168 | qr{union\s+$Ident}, |
161 | qr{enum\s+$Ident}, | 169 | qr{enum\s+$Ident}, |
@@ -175,6 +183,7 @@ sub build_types { | |||
175 | (?:$Modifier\s+|const\s+)* | 183 | (?:$Modifier\s+|const\s+)* |
176 | (?: | 184 | (?: |
177 | (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| | 185 | (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| |
186 | (?:$typeTypedefs\b)| | ||
178 | (?:${all}\b) | 187 | (?:${all}\b) |
179 | ) | 188 | ) |
180 | (?:\s+$Modifier|\s+const)* | 189 | (?:\s+$Modifier|\s+const)* |
@@ -331,7 +340,7 @@ sub sanitise_line { | |||
331 | $off++; | 340 | $off++; |
332 | next; | 341 | next; |
333 | } | 342 | } |
334 | if (substr($line, $off, 2) eq '*/') { | 343 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
335 | $sanitise_quote = ''; | 344 | $sanitise_quote = ''; |
336 | substr($res, $off, 2, "$;$;"); | 345 | substr($res, $off, 2, "$;$;"); |
337 | $off++; | 346 | $off++; |
@@ -404,6 +413,7 @@ sub ctx_statement_block { | |||
404 | # context. | 413 | # context. |
405 | if ($off >= $len) { | 414 | if ($off >= $len) { |
406 | for (; $remain > 0; $line++) { | 415 | for (; $remain > 0; $line++) { |
416 | last if (!defined $lines[$line]); | ||
407 | next if ($lines[$line] =~ /^-/); | 417 | next if ($lines[$line] =~ /^-/); |
408 | $remain--; | 418 | $remain--; |
409 | $loff = $len; | 419 | $loff = $len; |
@@ -669,6 +679,22 @@ sub ctx_has_comment { | |||
669 | return ($cmt ne ''); | 679 | return ($cmt ne ''); |
670 | } | 680 | } |
671 | 681 | ||
682 | sub raw_line { | ||
683 | my ($linenr, $cnt) = @_; | ||
684 | |||
685 | my $offset = $linenr - 1; | ||
686 | $cnt++; | ||
687 | |||
688 | my $line; | ||
689 | while ($cnt) { | ||
690 | $line = $rawlines[$offset++]; | ||
691 | next if (defined($line) && $line =~ /^-/); | ||
692 | $cnt--; | ||
693 | } | ||
694 | |||
695 | return $line; | ||
696 | } | ||
697 | |||
672 | sub cat_vet { | 698 | sub cat_vet { |
673 | my ($vet) = @_; | 699 | my ($vet) = @_; |
674 | my ($res, $coded); | 700 | my ($res, $coded); |
@@ -782,9 +808,9 @@ sub annotate_values { | |||
782 | } | 808 | } |
783 | $type = 'N'; | 809 | $type = 'N'; |
784 | 810 | ||
785 | } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) { | 811 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
786 | print "COND($1)\n" if ($dbg_values > 1); | 812 | print "COND($1)\n" if ($dbg_values > 1); |
787 | $av_pending = 'N'; | 813 | $av_pending = 'E'; |
788 | $type = 'N'; | 814 | $type = 'N'; |
789 | 815 | ||
790 | } elsif ($cur =~/^(case)/o) { | 816 | } elsif ($cur =~/^(case)/o) { |
@@ -792,7 +818,7 @@ sub annotate_values { | |||
792 | $av_pend_colon = 'C'; | 818 | $av_pend_colon = 'C'; |
793 | $type = 'N'; | 819 | $type = 'N'; |
794 | 820 | ||
795 | } elsif ($cur =~/^(return|else|goto)/o) { | 821 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
796 | print "KEYWORD($1)\n" if ($dbg_values > 1); | 822 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
797 | $type = 'N'; | 823 | $type = 'N'; |
798 | 824 | ||
@@ -858,7 +884,7 @@ sub annotate_values { | |||
858 | print "CLOSE($1)\n" if ($dbg_values > 1); | 884 | print "CLOSE($1)\n" if ($dbg_values > 1); |
859 | $type = 'N'; | 885 | $type = 'N'; |
860 | 886 | ||
861 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&(?!\&))/o) { | 887 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
862 | my $variant; | 888 | my $variant; |
863 | 889 | ||
864 | print "OPV($1)\n" if ($dbg_values > 1); | 890 | print "OPV($1)\n" if ($dbg_values > 1); |
@@ -892,12 +918,22 @@ sub annotate_values { | |||
892 | sub possible { | 918 | sub possible { |
893 | my ($possible, $line) = @_; | 919 | my ($possible, $line) = @_; |
894 | 920 | ||
895 | print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1); | 921 | print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
896 | if ($possible !~ /^(?:$Modifier|$Storage|$Type|DEFINE_\S+)$/ && | 922 | if ($possible !~ /(?: |
897 | $possible ne 'goto' && $possible ne 'return' && | 923 | ^(?: |
898 | $possible ne 'case' && $possible ne 'else' && | 924 | $Modifier| |
899 | $possible ne 'asm' && $possible ne '__asm__' && | 925 | $Storage| |
900 | $possible !~ /^(typedef|struct|enum)\b/) { | 926 | $Type| |
927 | DEFINE_\S+| | ||
928 | goto| | ||
929 | return| | ||
930 | case| | ||
931 | else| | ||
932 | asm|__asm__| | ||
933 | do | ||
934 | )$| | ||
935 | ^(?:typedef|struct|enum)\b | ||
936 | )/x) { | ||
901 | # Check for modifiers. | 937 | # Check for modifiers. |
902 | $possible =~ s/\s*$Storage\s*//g; | 938 | $possible =~ s/\s*$Storage\s*//g; |
903 | $possible =~ s/\s*$Sparse\s*//g; | 939 | $possible =~ s/\s*$Sparse\s*//g; |
@@ -915,6 +951,8 @@ sub possible { | |||
915 | push(@typeList, $possible); | 951 | push(@typeList, $possible); |
916 | } | 952 | } |
917 | build_types(); | 953 | build_types(); |
954 | } else { | ||
955 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); | ||
918 | } | 956 | } |
919 | } | 957 | } |
920 | 958 | ||
@@ -954,6 +992,33 @@ sub CHK { | |||
954 | } | 992 | } |
955 | } | 993 | } |
956 | 994 | ||
995 | sub check_absolute_file { | ||
996 | my ($absolute, $herecurr) = @_; | ||
997 | my $file = $absolute; | ||
998 | |||
999 | ##print "absolute<$absolute>\n"; | ||
1000 | |||
1001 | # See if any suffix of this path is a path within the tree. | ||
1002 | while ($file =~ s@^[^/]*/@@) { | ||
1003 | if (-f "$root/$file") { | ||
1004 | ##print "file<$file>\n"; | ||
1005 | last; | ||
1006 | } | ||
1007 | } | ||
1008 | if (! -f _) { | ||
1009 | return 0; | ||
1010 | } | ||
1011 | |||
1012 | # It is, so see if the prefix is acceptable. | ||
1013 | my $prefix = $absolute; | ||
1014 | substr($prefix, -length($file)) = ''; | ||
1015 | |||
1016 | ##print "prefix<$prefix>\n"; | ||
1017 | if ($prefix ne ".../") { | ||
1018 | WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr); | ||
1019 | } | ||
1020 | } | ||
1021 | |||
957 | sub process { | 1022 | sub process { |
958 | my $filename = shift; | 1023 | my $filename = shift; |
959 | 1024 | ||
@@ -991,6 +1056,7 @@ sub process { | |||
991 | 1056 | ||
992 | # suppression flags | 1057 | # suppression flags |
993 | my %suppress_ifbraces; | 1058 | my %suppress_ifbraces; |
1059 | my %suppress_whiletrailers; | ||
994 | 1060 | ||
995 | # Pre-scan the patch sanitizing the lines. | 1061 | # Pre-scan the patch sanitizing the lines. |
996 | # Pre-scan the patch looking for any __setup documentation. | 1062 | # Pre-scan the patch looking for any __setup documentation. |
@@ -1025,9 +1091,14 @@ sub process { | |||
1025 | # edge is a close comment then we must be in a comment | 1091 | # edge is a close comment then we must be in a comment |
1026 | # at context start. | 1092 | # at context start. |
1027 | my $edge; | 1093 | my $edge; |
1028 | for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) { | 1094 | my $cnt = $realcnt; |
1029 | next if ($line =~ /^-/); | 1095 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
1030 | ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@); | 1096 | next if (defined $rawlines[$ln - 1] && |
1097 | $rawlines[$ln - 1] =~ /^-/); | ||
1098 | $cnt--; | ||
1099 | #print "RAW<$rawlines[$ln - 1]>\n"; | ||
1100 | ($edge) = (defined $rawlines[$ln - 1] && | ||
1101 | $rawlines[$ln - 1] =~ m@(/\*|\*/)@); | ||
1031 | last if (defined $edge); | 1102 | last if (defined $edge); |
1032 | } | 1103 | } |
1033 | if (defined $edge && $edge eq '*/') { | 1104 | if (defined $edge && $edge eq '*/') { |
@@ -1075,6 +1146,7 @@ sub process { | |||
1075 | $linenr++; | 1146 | $linenr++; |
1076 | 1147 | ||
1077 | my $rawline = $rawlines[$linenr - 1]; | 1148 | my $rawline = $rawlines[$linenr - 1]; |
1149 | my $hunk_line = ($realcnt != 0); | ||
1078 | 1150 | ||
1079 | #extract the line range in the file after the patch is applied | 1151 | #extract the line range in the file after the patch is applied |
1080 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { | 1152 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
@@ -1090,6 +1162,7 @@ sub process { | |||
1090 | $prev_values = 'E'; | 1162 | $prev_values = 'E'; |
1091 | 1163 | ||
1092 | %suppress_ifbraces = (); | 1164 | %suppress_ifbraces = (); |
1165 | %suppress_whiletrailers = (); | ||
1093 | next; | 1166 | next; |
1094 | 1167 | ||
1095 | # track the line number as we move through the hunk, note that | 1168 | # track the line number as we move through the hunk, note that |
@@ -1125,7 +1198,7 @@ sub process { | |||
1125 | $realfile = $1; | 1198 | $realfile = $1; |
1126 | $realfile =~ s@^[^/]*/@@; | 1199 | $realfile =~ s@^[^/]*/@@; |
1127 | 1200 | ||
1128 | if ($realfile =~ m@include/asm/@) { | 1201 | if ($realfile =~ m@^include/asm/@) { |
1129 | ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); | 1202 | ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
1130 | } | 1203 | } |
1131 | next; | 1204 | next; |
@@ -1159,6 +1232,20 @@ sub process { | |||
1159 | $herecurr) if (!$emitted_corrupt++); | 1232 | $herecurr) if (!$emitted_corrupt++); |
1160 | } | 1233 | } |
1161 | 1234 | ||
1235 | # Check for absolute kernel paths. | ||
1236 | if ($tree) { | ||
1237 | while ($line =~ m{(?:^|\s)(/\S*)}g) { | ||
1238 | my $file = $1; | ||
1239 | |||
1240 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && | ||
1241 | check_absolute_file($1, $herecurr)) { | ||
1242 | # | ||
1243 | } else { | ||
1244 | check_absolute_file($file, $herecurr); | ||
1245 | } | ||
1246 | } | ||
1247 | } | ||
1248 | |||
1162 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php | 1249 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
1163 | if (($realfile =~ /^$/ || $line =~ /^\+/) && | 1250 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
1164 | $rawline !~ m/^$UTF8*$/) { | 1251 | $rawline !~ m/^$UTF8*$/) { |
@@ -1171,11 +1258,8 @@ sub process { | |||
1171 | ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); | 1258 | ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); |
1172 | } | 1259 | } |
1173 | 1260 | ||
1174 | #ignore lines being removed | 1261 | # ignore non-hunk lines and lines being removed |
1175 | if ($line=~/^-/) {next;} | 1262 | next if (!$hunk_line || $line =~ /^-/); |
1176 | |||
1177 | # check we are in a valid source file if not then ignore this hunk | ||
1178 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); | ||
1179 | 1263 | ||
1180 | #trailing whitespace | 1264 | #trailing whitespace |
1181 | if ($line =~ /^\+.*\015/) { | 1265 | if ($line =~ /^\+.*\015/) { |
@@ -1186,6 +1270,10 @@ sub process { | |||
1186 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | 1270 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
1187 | ERROR("trailing whitespace\n" . $herevet); | 1271 | ERROR("trailing whitespace\n" . $herevet); |
1188 | } | 1272 | } |
1273 | |||
1274 | # check we are in a valid source file if not then ignore this hunk | ||
1275 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); | ||
1276 | |||
1189 | #80 column limit | 1277 | #80 column limit |
1190 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && | 1278 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
1191 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && | 1279 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
@@ -1200,8 +1288,8 @@ sub process { | |||
1200 | WARN("adding a line without newline at end of file\n" . $herecurr); | 1288 | WARN("adding a line without newline at end of file\n" . $herecurr); |
1201 | } | 1289 | } |
1202 | 1290 | ||
1203 | # check we are in a valid source file *.[hc] if not then ignore this hunk | 1291 | # check we are in a valid source file C or perl if not then ignore this hunk |
1204 | next if ($realfile !~ /\.[hc]$/); | 1292 | next if ($realfile !~ /\.(h|c|pl)$/); |
1205 | 1293 | ||
1206 | # at the beginning of a line any tabs must come first and anything | 1294 | # at the beginning of a line any tabs must come first and anything |
1207 | # more than 8 must use tabs. | 1295 | # more than 8 must use tabs. |
@@ -1211,15 +1299,18 @@ sub process { | |||
1211 | ERROR("code indent should use tabs where possible\n" . $herevet); | 1299 | ERROR("code indent should use tabs where possible\n" . $herevet); |
1212 | } | 1300 | } |
1213 | 1301 | ||
1302 | # check we are in a valid C source file if not then ignore this hunk | ||
1303 | next if ($realfile !~ /\.(h|c)$/); | ||
1304 | |||
1214 | # check for RCS/CVS revision markers | 1305 | # check for RCS/CVS revision markers |
1215 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { | 1306 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
1216 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); | 1307 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
1217 | } | 1308 | } |
1218 | 1309 | ||
1219 | # Check for potential 'bare' types | 1310 | # Check for potential 'bare' types |
1220 | my ($stat, $cond, $line_nr_next, $remain_next); | 1311 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next); |
1221 | if ($realcnt && $line =~ /.\s*\S/) { | 1312 | if ($realcnt && $line =~ /.\s*\S/) { |
1222 | ($stat, $cond, $line_nr_next, $remain_next) = | 1313 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
1223 | ctx_statement_block($linenr, $realcnt, 0); | 1314 | ctx_statement_block($linenr, $realcnt, 0); |
1224 | $stat =~ s/\n./\n /g; | 1315 | $stat =~ s/\n./\n /g; |
1225 | $cond =~ s/\n./\n /g; | 1316 | $cond =~ s/\n./\n /g; |
@@ -1240,7 +1331,7 @@ sub process { | |||
1240 | possible($type, "A:" . $s); | 1331 | possible($type, "A:" . $s); |
1241 | 1332 | ||
1242 | # definitions in global scope can only start with types | 1333 | # definitions in global scope can only start with types |
1243 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) { | 1334 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
1244 | possible($1, "B:" . $s); | 1335 | possible($1, "B:" . $s); |
1245 | } | 1336 | } |
1246 | 1337 | ||
@@ -1294,10 +1385,6 @@ sub process { | |||
1294 | ERROR("switch and case should be at the same indent\n$hereline$err"); | 1385 | ERROR("switch and case should be at the same indent\n$hereline$err"); |
1295 | } | 1386 | } |
1296 | } | 1387 | } |
1297 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && | ||
1298 | $line !~ /\G(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$/g) { | ||
1299 | ERROR("trailing statements should be on next line\n" . $herecurr); | ||
1300 | } | ||
1301 | 1388 | ||
1302 | # if/while/etc brace do not go on next line, unless defining a do while loop, | 1389 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
1303 | # or if that brace on the next line is for something else | 1390 | # or if that brace on the next line is for something else |
@@ -1338,6 +1425,91 @@ sub process { | |||
1338 | } | 1425 | } |
1339 | } | 1426 | } |
1340 | 1427 | ||
1428 | # Check relative indent for conditionals and blocks. | ||
1429 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { | ||
1430 | my ($s, $c) = ($stat, $cond); | ||
1431 | |||
1432 | substr($s, 0, length($c), ''); | ||
1433 | |||
1434 | # Make sure we remove the line prefixes as we have | ||
1435 | # none on the first line, and are going to readd them | ||
1436 | # where necessary. | ||
1437 | $s =~ s/\n./\n/gs; | ||
1438 | |||
1439 | # Find out how long the conditional actually is. | ||
1440 | my @newlines = ($c =~ /\n/gs); | ||
1441 | my $cond_lines = 1 + $#newlines; | ||
1442 | |||
1443 | # We want to check the first line inside the block | ||
1444 | # starting at the end of the conditional, so remove: | ||
1445 | # 1) any blank line termination | ||
1446 | # 2) any opening brace { on end of the line | ||
1447 | # 3) any do (...) { | ||
1448 | my $continuation = 0; | ||
1449 | my $check = 0; | ||
1450 | $s =~ s/^.*\bdo\b//; | ||
1451 | $s =~ s/^\s*{//; | ||
1452 | if ($s =~ s/^\s*\\//) { | ||
1453 | $continuation = 1; | ||
1454 | } | ||
1455 | if ($s =~ s/^\s*?\n//) { | ||
1456 | $check = 1; | ||
1457 | $cond_lines++; | ||
1458 | } | ||
1459 | |||
1460 | # Also ignore a loop construct at the end of a | ||
1461 | # preprocessor statement. | ||
1462 | if (($prevline =~ /^.\s*#\s*define\s/ || | ||
1463 | $prevline =~ /\\\s*$/) && $continuation == 0) { | ||
1464 | $check = 0; | ||
1465 | } | ||
1466 | |||
1467 | my $cond_ptr = -1; | ||
1468 | $continuation = 0; | ||
1469 | while ($cond_ptr != $cond_lines) { | ||
1470 | $cond_ptr = $cond_lines; | ||
1471 | |||
1472 | # If we see an #else/#elif then the code | ||
1473 | # is not linear. | ||
1474 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { | ||
1475 | $check = 0; | ||
1476 | } | ||
1477 | |||
1478 | # Ignore: | ||
1479 | # 1) blank lines, they should be at 0, | ||
1480 | # 2) preprocessor lines, and | ||
1481 | # 3) labels. | ||
1482 | if ($continuation || | ||
1483 | $s =~ /^\s*?\n/ || | ||
1484 | $s =~ /^\s*#\s*?/ || | ||
1485 | $s =~ /^\s*$Ident\s*:/) { | ||
1486 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; | ||
1487 | $s =~ s/^.*?\n//; | ||
1488 | $cond_lines++; | ||
1489 | } | ||
1490 | } | ||
1491 | |||
1492 | my (undef, $sindent) = line_stats("+" . $s); | ||
1493 | my $stat_real = raw_line($linenr, $cond_lines); | ||
1494 | |||
1495 | # Check if either of these lines are modified, else | ||
1496 | # this is not this patch's fault. | ||
1497 | if (!defined($stat_real) || | ||
1498 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { | ||
1499 | $check = 0; | ||
1500 | } | ||
1501 | if (defined($stat_real) && $cond_lines > 1) { | ||
1502 | $stat_real = "[...]\n$stat_real"; | ||
1503 | } | ||
1504 | |||
1505 | #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"; | ||
1506 | |||
1507 | if ($check && (($sindent % 8) != 0 || | ||
1508 | ($sindent <= $indent && $s ne ''))) { | ||
1509 | WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); | ||
1510 | } | ||
1511 | } | ||
1512 | |||
1341 | # Track the 'values' across context and added lines. | 1513 | # Track the 'values' across context and added lines. |
1342 | my $opline = $line; $opline =~ s/^./ /; | 1514 | my $opline = $line; $opline =~ s/^./ /; |
1343 | my ($curr_values, $curr_vars) = | 1515 | my ($curr_values, $curr_vars) = |
@@ -1363,6 +1535,15 @@ sub process { | |||
1363 | } | 1535 | } |
1364 | next; | 1536 | next; |
1365 | } | 1537 | } |
1538 | # TEST: allow direct testing of the attribute matcher. | ||
1539 | if ($dbg_attr) { | ||
1540 | if ($line =~ /^.\s*$Attribute\s*$/) { | ||
1541 | ERROR("TEST: is attr\n" . $herecurr); | ||
1542 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) { | ||
1543 | ERROR("TEST: is not attr ($1 is)\n". $herecurr); | ||
1544 | } | ||
1545 | next; | ||
1546 | } | ||
1366 | 1547 | ||
1367 | # check for initialisation to aggregates open brace on the next line | 1548 | # check for initialisation to aggregates open brace on the next line |
1368 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && | 1549 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && |
@@ -1395,13 +1576,14 @@ sub process { | |||
1395 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || | 1576 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || |
1396 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { | 1577 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
1397 | my $name = $1; | 1578 | my $name = $1; |
1398 | if (($prevline !~ /^}/) && | 1579 | if ($prevline !~ /(?: |
1399 | ($prevline !~ /^\+}/) && | 1580 | ^.}| |
1400 | ($prevline !~ /^ }/) && | 1581 | ^.DEFINE_$Ident\(\Q$name\E\)| |
1401 | ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) && | 1582 | ^.DECLARE_$Ident\(\Q$name\E\)| |
1402 | ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) && | 1583 | ^.LIST_HEAD\(\Q$name\E\)| |
1403 | ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) && | 1584 | ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
1404 | ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) { | 1585 | \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[) |
1586 | )/x) { | ||
1405 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); | 1587 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
1406 | } | 1588 | } |
1407 | } | 1589 | } |
@@ -1422,6 +1604,7 @@ sub process { | |||
1422 | if ($line =~ /\btypedef\s/ && | 1604 | if ($line =~ /\btypedef\s/ && |
1423 | $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && | 1605 | $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && |
1424 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && | 1606 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
1607 | $line !~ /\b$typeTypedefs\b/ && | ||
1425 | $line !~ /\b__bitwise(?:__|)\b/) { | 1608 | $line !~ /\b__bitwise(?:__|)\b/) { |
1426 | WARN("do not add new typedefs\n" . $herecurr); | 1609 | WARN("do not add new typedefs\n" . $herecurr); |
1427 | } | 1610 | } |
@@ -1493,11 +1676,13 @@ sub process { | |||
1493 | 1676 | ||
1494 | # check for spacing round square brackets; allowed: | 1677 | # check for spacing round square brackets; allowed: |
1495 | # 1. with a type on the left -- int [] a; | 1678 | # 1. with a type on the left -- int [] a; |
1496 | # 2. at the beginning of a line for slice initialisers -- [0..10] = 5, | 1679 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
1680 | # 3. inside a curly brace -- = { [0...10] = 5 } | ||
1497 | while ($line =~ /(.*?\s)\[/g) { | 1681 | while ($line =~ /(.*?\s)\[/g) { |
1498 | my ($where, $prefix) = ($-[1], $1); | 1682 | my ($where, $prefix) = ($-[1], $1); |
1499 | if ($prefix !~ /$Type\s+$/ && | 1683 | if ($prefix !~ /$Type\s+$/ && |
1500 | ($where != 0 || $prefix !~ /^.\s+$/)) { | 1684 | ($where != 0 || $prefix !~ /^.\s+$/) && |
1685 | $prefix !~ /{\s+$/) { | ||
1501 | ERROR("space prohibited before open square bracket '['\n" . $herecurr); | 1686 | ERROR("space prohibited before open square bracket '['\n" . $herecurr); |
1502 | } | 1687 | } |
1503 | } | 1688 | } |
@@ -1632,7 +1817,7 @@ sub process { | |||
1632 | # unary operator, or a cast | 1817 | # unary operator, or a cast |
1633 | } elsif ($op eq '!' || $op eq '~' || | 1818 | } elsif ($op eq '!' || $op eq '~' || |
1634 | $opv eq '*U' || $opv eq '-U' || | 1819 | $opv eq '*U' || $opv eq '-U' || |
1635 | $opv eq '&U') { | 1820 | $opv eq '&U' || $opv eq '&&U') { |
1636 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { | 1821 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
1637 | ERROR("space required before that '$op' $at\n" . $hereptr); | 1822 | ERROR("space required before that '$op' $at\n" . $hereptr); |
1638 | } | 1823 | } |
@@ -1785,7 +1970,26 @@ sub process { | |||
1785 | 1970 | ||
1786 | # Check for illegal assignment in if conditional -- and check for trailing | 1971 | # Check for illegal assignment in if conditional -- and check for trailing |
1787 | # statements after the conditional. | 1972 | # statements after the conditional. |
1788 | if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { | 1973 | if ($line =~ /do\s*(?!{)/) { |
1974 | my ($stat_next) = ctx_statement_block($line_nr_next, | ||
1975 | $remain_next, $off_next); | ||
1976 | $stat_next =~ s/\n./\n /g; | ||
1977 | ##print "stat<$stat> stat_next<$stat_next>\n"; | ||
1978 | |||
1979 | if ($stat_next =~ /^\s*while\b/) { | ||
1980 | # If the statement carries leading newlines, | ||
1981 | # then count those as offsets. | ||
1982 | my ($whitespace) = | ||
1983 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); | ||
1984 | my $offset = | ||
1985 | statement_rawlines($whitespace) - 1; | ||
1986 | |||
1987 | $suppress_whiletrailers{$line_nr_next + | ||
1988 | $offset} = 1; | ||
1989 | } | ||
1990 | } | ||
1991 | if (!defined $suppress_whiletrailers{$linenr} && | ||
1992 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { | ||
1789 | my ($s, $c) = ($stat, $cond); | 1993 | my ($s, $c) = ($stat, $cond); |
1790 | 1994 | ||
1791 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { | 1995 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { |
@@ -1800,57 +2004,16 @@ sub process { | |||
1800 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && | 2004 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
1801 | $c !~ /}\s*while\s*/) | 2005 | $c !~ /}\s*while\s*/) |
1802 | { | 2006 | { |
1803 | ERROR("trailing statements should be on next line\n" . $herecurr); | 2007 | # Find out how long the conditional actually is. |
1804 | } | 2008 | my @newlines = ($c =~ /\n/gs); |
1805 | } | 2009 | my $cond_lines = 1 + $#newlines; |
1806 | |||
1807 | # Check relative indent for conditionals and blocks. | ||
1808 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { | ||
1809 | my ($s, $c) = ($stat, $cond); | ||
1810 | |||
1811 | substr($s, 0, length($c), ''); | ||
1812 | 2010 | ||
1813 | # Make sure we remove the line prefixes as we have | 2011 | my $stat_real = raw_line($linenr, $cond_lines); |
1814 | # none on the first line, and are going to readd them | 2012 | if (defined($stat_real) && $cond_lines > 1) { |
1815 | # where necessary. | 2013 | $stat_real = "[...]\n$stat_real"; |
1816 | $s =~ s/\n./\n/gs; | 2014 | } |
1817 | |||
1818 | # We want to check the first line inside the block | ||
1819 | # starting at the end of the conditional, so remove: | ||
1820 | # 1) any blank line termination | ||
1821 | # 2) any opening brace { on end of the line | ||
1822 | # 3) any do (...) { | ||
1823 | my $continuation = 0; | ||
1824 | my $check = 0; | ||
1825 | $s =~ s/^.*\bdo\b//; | ||
1826 | $s =~ s/^\s*{//; | ||
1827 | if ($s =~ s/^\s*\\//) { | ||
1828 | $continuation = 1; | ||
1829 | } | ||
1830 | if ($s =~ s/^\s*\n//) { | ||
1831 | $check = 1; | ||
1832 | } | ||
1833 | |||
1834 | # Also ignore a loop construct at the end of a | ||
1835 | # preprocessor statement. | ||
1836 | if (($prevline =~ /^.\s*#\s*define\s/ || | ||
1837 | $prevline =~ /\\\s*$/) && $continuation == 0) { | ||
1838 | $check = 0; | ||
1839 | } | ||
1840 | |||
1841 | # Ignore the current line if its is a preprocessor | ||
1842 | # line. | ||
1843 | if ($s =~ /^\s*#\s*/) { | ||
1844 | $check = 0; | ||
1845 | } | ||
1846 | |||
1847 | my (undef, $sindent) = line_stats("+" . $s); | ||
1848 | |||
1849 | ##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n"; | ||
1850 | 2015 | ||
1851 | if ($check && (($sindent % 8) != 0 || | 2016 | ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); |
1852 | ($sindent <= $indent && $s ne ''))) { | ||
1853 | WARN("suspect code indent for conditional statements\n" . $herecurr); | ||
1854 | } | 2017 | } |
1855 | } | 2018 | } |
1856 | 2019 | ||
@@ -1877,6 +2040,15 @@ sub process { | |||
1877 | ERROR("trailing statements should be on next line\n" . $herecurr); | 2040 | ERROR("trailing statements should be on next line\n" . $herecurr); |
1878 | } | 2041 | } |
1879 | } | 2042 | } |
2043 | # case and default should not have general statements after them | ||
2044 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && | ||
2045 | $line !~ /\G(?: | ||
2046 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| | ||
2047 | \s*return\s+ | ||
2048 | )/xg) | ||
2049 | { | ||
2050 | ERROR("trailing statements should be on next line\n" . $herecurr); | ||
2051 | } | ||
1880 | 2052 | ||
1881 | # Check for }<nl>else {, these must be at the same | 2053 | # Check for }<nl>else {, these must be at the same |
1882 | # indent level to be relevant to each other. | 2054 | # indent level to be relevant to each other. |
@@ -1913,12 +2085,17 @@ sub process { | |||
1913 | 2085 | ||
1914 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) | 2086 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
1915 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { | 2087 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
1916 | my $checkfile = "include/linux/$1.h"; | 2088 | my $file = "$1.h"; |
1917 | if (-f "$root/$checkfile" && $realfile ne $checkfile && | 2089 | my $checkfile = "include/linux/$file"; |
2090 | if (-f "$root/$checkfile" && | ||
2091 | $realfile ne $checkfile && | ||
1918 | $1 ne 'irq') | 2092 | $1 ne 'irq') |
1919 | { | 2093 | { |
1920 | WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . | 2094 | if ($realfile =~ m{^arch/}) { |
1921 | $herecurr); | 2095 | CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
2096 | } else { | ||
2097 | WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); | ||
2098 | } | ||
1922 | } | 2099 | } |
1923 | } | 2100 | } |
1924 | 2101 | ||
@@ -1953,8 +2130,8 @@ sub process { | |||
1953 | # Extract the remainder of the define (if any) and | 2130 | # Extract the remainder of the define (if any) and |
1954 | # rip off surrounding spaces, and trailing \'s. | 2131 | # rip off surrounding spaces, and trailing \'s. |
1955 | $rest = ''; | 2132 | $rest = ''; |
1956 | while ($off != 0 || ($cnt > 0 && $rest =~ /(?:^|\\)\s*$/)) { | 2133 | while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { |
1957 | #print "ADDING $off <" . substr($lines[$ln - 1], $off) . ">\n"; | 2134 | #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; |
1958 | if ($off != 0 || $lines[$ln - 1] !~ /^-/) { | 2135 | if ($off != 0 || $lines[$ln - 1] !~ /^-/) { |
1959 | $rest .= substr($lines[$ln - 1], $off) . "\n"; | 2136 | $rest .= substr($lines[$ln - 1], $off) . "\n"; |
1960 | $cnt--; | 2137 | $cnt--; |
@@ -1978,9 +2155,10 @@ sub process { | |||
1978 | $dstat =~ s/\s*$//s; | 2155 | $dstat =~ s/\s*$//s; |
1979 | 2156 | ||
1980 | # Flatten any parentheses and braces | 2157 | # Flatten any parentheses and braces |
1981 | while ($dstat =~ s/\([^\(\)]*\)/1/) { | 2158 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
1982 | } | 2159 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
1983 | while ($dstat =~ s/\{[^\{\}]*\}/1/) { | 2160 | $dstat =~ s/\[[^\{\}]*\]/1/) |
2161 | { | ||
1984 | } | 2162 | } |
1985 | 2163 | ||
1986 | my $exceptions = qr{ | 2164 | my $exceptions = qr{ |
@@ -2003,6 +2181,7 @@ sub process { | |||
2003 | if ($dstat ne '' && | 2181 | if ($dstat ne '' && |
2004 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && | 2182 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && |
2005 | $dstat !~ /$exceptions/ && | 2183 | $dstat !~ /$exceptions/ && |
2184 | $dstat !~ /^\.$Ident\s*=/ && | ||
2006 | $dstat =~ /$Operators/) | 2185 | $dstat =~ /$Operators/) |
2007 | { | 2186 | { |
2008 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); | 2187 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); |
@@ -2103,10 +2282,10 @@ sub process { | |||
2103 | } | 2282 | } |
2104 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { | 2283 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
2105 | my $herectx = $here . "\n";; | 2284 | my $herectx = $here . "\n";; |
2106 | my $end = $linenr + statement_rawlines($block) - 1; | 2285 | my $cnt = statement_rawlines($block); |
2107 | 2286 | ||
2108 | for (my $ln = $linenr - 1; $ln < $end; $ln++) { | 2287 | for (my $n = 0; $n < $cnt; $n++) { |
2109 | $herectx .= $rawlines[$ln] . "\n";; | 2288 | $herectx .= raw_line($linenr, $n) . "\n";; |
2110 | } | 2289 | } |
2111 | 2290 | ||
2112 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); | 2291 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); |
@@ -2281,6 +2460,7 @@ sub process { | |||
2281 | my $string; | 2460 | my $string; |
2282 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { | 2461 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
2283 | $string = substr($rawline, $-[1], $+[1] - $-[1]); | 2462 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
2463 | $string =~ s/%%/__/g; | ||
2284 | if ($string =~ /(?<!%)%L[udi]/) { | 2464 | if ($string =~ /(?<!%)%L[udi]/) { |
2285 | WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); | 2465 | WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
2286 | last; | 2466 | last; |