aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2008-06-16 05:24:43 -0400
committerIngo Molnar <mingo@elte.hu>2008-06-16 05:24:43 -0400
commit8bbd54d69e9c66adbf544e21d8dcfb15fb9198f7 (patch)
tree95f30814fc759c2cb523dbea95bc531c7f8f3231 /scripts/checkpatch.pl
parent8c2238eaaf0f774ca0f8d9daad7a616429bbb7f1 (diff)
parent066519068ad2fbe98c7f45552b1f592903a9c8c8 (diff)
Merge branch 'linus' into core/softlockup
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl284
1 files changed, 182 insertions, 102 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b6bbbcdc557e..6971bf078d13 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -9,7 +9,7 @@ use strict;
9my $P = $0; 9my $P = $0;
10$P =~ s@.*/@@g; 10$P =~ s@.*/@@g;
11 11
12my $V = '0.18'; 12my $V = '0.19';
13 13
14use Getopt::Long qw(:config no_auto_abbrev); 14use Getopt::Long qw(:config no_auto_abbrev);
15 15
@@ -115,6 +115,7 @@ our $Attribute = qr{
115 __kprobes| 115 __kprobes|
116 __(?:mem|cpu|dev|)(?:initdata|init) 116 __(?:mem|cpu|dev|)(?:initdata|init)
117 }x; 117 }x;
118our $Modifier;
118our $Inline = qr{inline|__always_inline|noinline}; 119our $Inline = qr{inline|__always_inline|noinline};
119our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 120our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
120our $Lval = qr{$Ident(?:$Member)*}; 121our $Lval = qr{$Ident(?:$Member)*};
@@ -144,17 +145,17 @@ our $UTF8 = qr {
144 145
145our @typeList = ( 146our @typeList = (
146 qr{void}, 147 qr{void},
147 qr{char}, 148 qr{(?:unsigned\s+)?char},
148 qr{short}, 149 qr{(?:unsigned\s+)?short},
149 qr{int}, 150 qr{(?:unsigned\s+)?int},
150 qr{long}, 151 qr{(?:unsigned\s+)?long},
152 qr{(?:unsigned\s+)?long\s+int},
153 qr{(?:unsigned\s+)?long\s+long},
154 qr{(?:unsigned\s+)?long\s+long\s+int},
151 qr{unsigned}, 155 qr{unsigned},
152 qr{float}, 156 qr{float},
153 qr{double}, 157 qr{double},
154 qr{bool}, 158 qr{bool},
155 qr{long\s+int},
156 qr{long\s+long},
157 qr{long\s+long\s+int},
158 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)}, 159 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
159 qr{struct\s+$Ident}, 160 qr{struct\s+$Ident},
160 qr{union\s+$Ident}, 161 qr{union\s+$Ident},
@@ -163,26 +164,29 @@ our @typeList = (
163 qr{${Ident}_handler}, 164 qr{${Ident}_handler},
164 qr{${Ident}_handler_fn}, 165 qr{${Ident}_handler_fn},
165); 166);
167our @modifierList = (
168 qr{fastcall},
169);
166 170
167sub build_types { 171sub build_types {
172 my $mods = "(?: \n" . join("|\n ", @modifierList) . "\n)";
168 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)"; 173 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
169 $NonptrType = qr{ 174 $NonptrType = qr{
170 \b
171 (?:const\s+)? 175 (?:const\s+)?
172 (?:unsigned\s+)? 176 (?:$mods\s+)?
173 (?: 177 (?:
174 $all| 178 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
175 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\) 179 (?:${all}\b)
176 ) 180 )
177 (?:\s+$Sparse|\s+const)* 181 (?:\s+$Sparse|\s+const)*
178 \b
179 }x; 182 }x;
180 $Type = qr{ 183 $Type = qr{
181 \b$NonptrType\b 184 $NonptrType
182 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? 185 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
183 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)* 186 (?:\s+$Inline|\s+$Sparse|\s+$Attribute|\s+$mods)*
184 }x; 187 }x;
185 $Declare = qr{(?:$Storage\s+)?$Type}; 188 $Declare = qr{(?:$Storage\s+)?$Type};
189 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
186} 190}
187build_types(); 191build_types();
188 192
@@ -329,7 +333,7 @@ sub sanitise_line {
329 $off++; 333 $off++;
330 next; 334 next;
331 } 335 }
332 if (substr($line, $off, 2) eq $sanitise_quote) { 336 if (substr($line, $off, 2) eq '*/') {
333 $sanitise_quote = ''; 337 $sanitise_quote = '';
334 substr($res, $off, 2, "$;$;"); 338 substr($res, $off, 2, "$;$;");
335 $off++; 339 $off++;
@@ -366,14 +370,14 @@ sub sanitise_line {
366 } 370 }
367 371
368 # The pathname on a #include may be surrounded by '<' and '>'. 372 # The pathname on a #include may be surrounded by '<' and '>'.
369 if ($res =~ /^.#\s*include\s+\<(.*)\>/) { 373 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
370 my $clean = 'X' x length($1); 374 my $clean = 'X' x length($1);
371 $res =~ s@\<.*\>@<$clean>@; 375 $res =~ s@\<.*\>@<$clean>@;
372 376
373 # The whole of a #error is a string. 377 # The whole of a #error is a string.
374 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) { 378 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
375 my $clean = 'X' x length($1); 379 my $clean = 'X' x length($1);
376 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@; 380 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
377 } 381 }
378 382
379 return $res; 383 return $res;
@@ -715,7 +719,7 @@ sub annotate_values {
715 print "DECLARE($1)\n" if ($dbg_values > 1); 719 print "DECLARE($1)\n" if ($dbg_values > 1);
716 $type = 'T'; 720 $type = 'T';
717 721
718 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { 722 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
719 print "DEFINE($1,$2)\n" if ($dbg_values > 1); 723 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
720 $av_preprocessor = 1; 724 $av_preprocessor = 1;
721 push(@av_paren_type, $type); 725 push(@av_paren_type, $type);
@@ -724,12 +728,12 @@ sub annotate_values {
724 } 728 }
725 $type = 'E'; 729 $type = 'E';
726 730
727 } elsif ($cur =~ /^(#\s*undef\s*$Ident)/o) { 731 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
728 print "UNDEF($1)\n" if ($dbg_values > 1); 732 print "UNDEF($1)\n" if ($dbg_values > 1);
729 $av_preprocessor = 1; 733 $av_preprocessor = 1;
730 push(@av_paren_type, $type); 734 push(@av_paren_type, $type);
731 735
732 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) { 736 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
733 print "PRE_START($1)\n" if ($dbg_values > 1); 737 print "PRE_START($1)\n" if ($dbg_values > 1);
734 $av_preprocessor = 1; 738 $av_preprocessor = 1;
735 739
@@ -737,7 +741,7 @@ sub annotate_values {
737 push(@av_paren_type, $type); 741 push(@av_paren_type, $type);
738 $type = 'E'; 742 $type = 'E';
739 743
740 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) { 744 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
741 print "PRE_RESTART($1)\n" if ($dbg_values > 1); 745 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
742 $av_preprocessor = 1; 746 $av_preprocessor = 1;
743 747
@@ -745,7 +749,7 @@ sub annotate_values {
745 749
746 $type = 'E'; 750 $type = 'E';
747 751
748 } elsif ($cur =~ /^(#\s*(?:endif))/o) { 752 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
749 print "PRE_END($1)\n" if ($dbg_values > 1); 753 print "PRE_END($1)\n" if ($dbg_values > 1);
750 754
751 $av_preprocessor = 1; 755 $av_preprocessor = 1;
@@ -837,14 +841,26 @@ sub annotate_values {
837sub possible { 841sub possible {
838 my ($possible, $line) = @_; 842 my ($possible, $line) = @_;
839 843
840 #print "CHECK<$possible>\n"; 844 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
841 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && 845 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
842 $possible ne 'goto' && $possible ne 'return' && 846 $possible ne 'goto' && $possible ne 'return' &&
843 $possible ne 'struct' && $possible ne 'enum' &&
844 $possible ne 'case' && $possible ne 'else' && 847 $possible ne 'case' && $possible ne 'else' &&
845 $possible ne 'typedef') { 848 $possible ne 'asm' &&
846 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 849 $possible !~ /^(typedef|struct|enum)\b/) {
847 push(@typeList, $possible); 850 # Check for modifiers.
851 $possible =~ s/\s*$Storage\s*//g;
852 $possible =~ s/\s*$Sparse\s*//g;
853 if ($possible =~ /^\s*$/) {
854
855 } elsif ($possible =~ /\s/) {
856 $possible =~ s/\s*$Type\s*//g;
857 warn "MODIFIER: $possible ($line)\n" if ($dbg_possible);
858 push(@modifierList, $possible);
859
860 } else {
861 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
862 push(@typeList, $possible);
863 }
848 build_types(); 864 build_types();
849 } 865 }
850} 866}
@@ -949,6 +965,7 @@ sub process {
949 } else { 965 } else {
950 $realcnt=1+1; 966 $realcnt=1+1;
951 } 967 }
968 $in_comment = 0;
952 969
953 # Guestimate if this is a continuing comment. Run 970 # Guestimate if this is a continuing comment. Run
954 # the context looking for a comment "edge". If this 971 # the context looking for a comment "edge". If this
@@ -1117,7 +1134,9 @@ sub process {
1117 ERROR("trailing whitespace\n" . $herevet); 1134 ERROR("trailing whitespace\n" . $herevet);
1118 } 1135 }
1119#80 column limit 1136#80 column limit
1120 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) { 1137 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1138 $rawline !~ /^.\s*\*\s*\@$Ident\s/ && $length > 80)
1139 {
1121 WARN("line over 80 characters\n" . $herecurr); 1140 WARN("line over 80 characters\n" . $herecurr);
1122 } 1141 }
1123 1142
@@ -1159,18 +1178,20 @@ sub process {
1159 # Ignore functions being called 1178 # Ignore functions being called
1160 } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1179 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1161 1180
1181 # declarations always start with types
1182 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))\s*(?:;|=|,|\()/s) {
1183 my $type = $1;
1184 $type =~ s/\s+/ /g;
1185 possible($type, "A:" . $s);
1186
1162 # definitions in global scope can only start with types 1187 # definitions in global scope can only start with types
1163 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) { 1188 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1164 possible($1, $s); 1189 possible($1, "B:" . $s);
1165
1166 # declarations always start with types
1167 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/s) {
1168 possible($1, $s);
1169 } 1190 }
1170 1191
1171 # any (foo ... *) is a pointer cast, and foo is a type 1192 # any (foo ... *) is a pointer cast, and foo is a type
1172 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) { 1193 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1173 possible($1, $s); 1194 possible($1, "C:" . $s);
1174 } 1195 }
1175 1196
1176 # Check for any sort of function declaration. 1197 # Check for any sort of function declaration.
@@ -1184,9 +1205,9 @@ sub process {
1184 $ctx =~ s/\)[^\)]*$//; 1205 $ctx =~ s/\)[^\)]*$//;
1185 1206
1186 for my $arg (split(/\s*,\s*/, $ctx)) { 1207 for my $arg (split(/\s*,\s*/, $ctx)) {
1187 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) { 1208 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1188 1209
1189 possible($1, $s); 1210 possible($1, "D:" . $s);
1190 } 1211 }
1191 } 1212 }
1192 } 1213 }
@@ -1221,7 +1242,7 @@ sub process {
1221 1242
1222# if/while/etc brace do not go on next line, unless defining a do while loop, 1243# if/while/etc brace do not go on next line, unless defining a do while loop,
1223# or if that brace on the next line is for something else 1244# or if that brace on the next line is for something else
1224 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { 1245 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1225 my $pre_ctx = "$1$2"; 1246 my $pre_ctx = "$1$2";
1226 1247
1227 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1248 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
@@ -1239,7 +1260,7 @@ sub process {
1239 1260
1240 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1261 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1241 ERROR("that open brace { should be on the previous line\n" . 1262 ERROR("that open brace { should be on the previous line\n" .
1242 "$here\n$ctx\n$lines[$ctx_ln - 1]"); 1263 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1243 } 1264 }
1244 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1265 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1245 $ctx =~ /\)\s*\;\s*$/ && 1266 $ctx =~ /\)\s*\;\s*$/ &&
@@ -1248,7 +1269,7 @@ sub process {
1248 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 1269 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1249 if ($nindent > $indent) { 1270 if ($nindent > $indent) {
1250 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . 1271 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1251 "$here\n$ctx\n$lines[$ctx_ln - 1]"); 1272 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1252 } 1273 }
1253 } 1274 }
1254 } 1275 }
@@ -1284,7 +1305,7 @@ sub process {
1284# 1305#
1285 1306
1286# check for malformed paths in #include statements (uses RAW line) 1307# check for malformed paths in #include statements (uses RAW line)
1287 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { 1308 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1288 my $path = $1; 1309 my $path = $1;
1289 if ($path =~ m{//}) { 1310 if ($path =~ m{//}) {
1290 ERROR("malformed #include filename\n" . 1311 ERROR("malformed #include filename\n" .
@@ -1316,7 +1337,7 @@ sub process {
1316 } 1337 }
1317 1338
1318# check for external initialisers. 1339# check for external initialisers.
1319 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL|false)\s*;/) { 1340 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1320 ERROR("do not initialise externals to 0 or NULL\n" . 1341 ERROR("do not initialise externals to 0 or NULL\n" .
1321 $herecurr); 1342 $herecurr);
1322 } 1343 }
@@ -1330,6 +1351,7 @@ sub process {
1330# make sense. 1351# make sense.
1331 if ($line =~ /\btypedef\s/ && 1352 if ($line =~ /\btypedef\s/ &&
1332 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && 1353 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1354 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1333 $line !~ /\b__bitwise(?:__|)\b/) { 1355 $line !~ /\b__bitwise(?:__|)\b/) {
1334 WARN("do not add new typedefs\n" . $herecurr); 1356 WARN("do not add new typedefs\n" . $herecurr);
1335 } 1357 }
@@ -1388,8 +1410,8 @@ sub process {
1388 1410
1389# function brace can't be on same line, except for #defines of do while, 1411# function brace can't be on same line, except for #defines of do while,
1390# or if closed on same line 1412# or if closed on same line
1391 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and 1413 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1392 !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 1414 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1393 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 1415 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1394 } 1416 }
1395 1417
@@ -1416,10 +1438,10 @@ sub process {
1416 # cpp #define statements have non-optional spaces, ie 1438 # cpp #define statements have non-optional spaces, ie
1417 # if there is a space between the name and the open 1439 # if there is a space between the name and the open
1418 # parenthesis it is simply not a parameter group. 1440 # parenthesis it is simply not a parameter group.
1419 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) { 1441 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1420 1442
1421 # cpp #elif statement condition may start with a ( 1443 # cpp #elif statement condition may start with a (
1422 } elsif ($ctx =~ /^.\#\s*elif\s*$/) { 1444 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1423 1445
1424 # If this whole things ends with a type its most 1446 # If this whole things ends with a type its most
1425 # likely a typedef for a function. 1447 # likely a typedef for a function.
@@ -1625,13 +1647,14 @@ sub process {
1625 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 1647 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1626 } 1648 }
1627 1649
1628# check spacing on paretheses 1650# check spacing on parentheses
1629 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 1651 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1630 $line !~ /for\s*\(\s+;/) { 1652 $line !~ /for\s*\(\s+;/) {
1631 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 1653 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1632 } 1654 }
1633 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 1655 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1634 $line !~ /for\s*\(.*;\s+\)/) { 1656 $line !~ /for\s*\(.*;\s+\)/ &&
1657 $line !~ /:\s+\)/) {
1635 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 1658 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
1636 } 1659 }
1637 1660
@@ -1641,6 +1664,23 @@ sub process {
1641 WARN("labels should not be indented\n" . $herecurr); 1664 WARN("labels should not be indented\n" . $herecurr);
1642 } 1665 }
1643 1666
1667# Return is not a function.
1668 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1669 my $spacing = $1;
1670 my $value = $2;
1671
1672 # Flatten any parentheses and braces
1673 while ($value =~ s/\([^\(\)]*\)/1/) {
1674 }
1675
1676 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1677 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1678
1679 } elsif ($spacing !~ /\s+/) {
1680 ERROR("space required before the open parenthesis '('\n" . $herecurr);
1681 }
1682 }
1683
1644# Need a space before open parenthesis after if, while etc 1684# Need a space before open parenthesis after if, while etc
1645 if ($line=~/\b(if|while|for|switch)\(/) { 1685 if ($line=~/\b(if|while|for|switch)\(/) {
1646 ERROR("space required before the open parenthesis '('\n" . $herecurr); 1686 ERROR("space required before the open parenthesis '('\n" . $herecurr);
@@ -1660,7 +1700,7 @@ sub process {
1660 $s =~ s/\n.*//g; 1700 $s =~ s/\n.*//g;
1661 $s =~ s/$;//g; # Remove any comments 1701 $s =~ s/$;//g; # Remove any comments
1662 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ && 1702 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1663 $c !~ /^.\#\s*if/) 1703 $c !~ /^.\s*\#\s*if/)
1664 { 1704 {
1665 ERROR("trailing statements should be on next line\n" . $herecurr); 1705 ERROR("trailing statements should be on next line\n" . $herecurr);
1666 } 1706 }
@@ -1719,14 +1759,16 @@ sub process {
1719# } 1759# }
1720 1760
1721#no spaces allowed after \ in define 1761#no spaces allowed after \ in define
1722 if ($line=~/\#define.*\\\s$/) { 1762 if ($line=~/\#\s*define.*\\\s$/) {
1723 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 1763 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1724 } 1764 }
1725 1765
1726#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 1766#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1727 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { 1767 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
1728 my $checkfile = "$root/include/linux/$1.h"; 1768 my $checkfile = "include/linux/$1.h";
1729 if (-f $checkfile && $1 ne 'irq') { 1769 if (-f "$root/$checkfile" && $realfile ne $checkfile &&
1770 $1 ne 'irq')
1771 {
1730 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . 1772 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1731 $herecurr); 1773 $herecurr);
1732 } 1774 }
@@ -1735,45 +1777,87 @@ sub process {
1735# multi-statement macros should be enclosed in a do while loop, grab the 1777# multi-statement macros should be enclosed in a do while loop, grab the
1736# first statement and ensure its the whole macro if its not enclosed 1778# first statement and ensure its the whole macro if its not enclosed
1737# in a known good container 1779# in a known good container
1738 if ($prevline =~ /\#define.*\\/ && 1780 if ($line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
1739 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1740 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1741 $line !~ /^.\s*$Declare\s/) {
1742 # Grab the first statement, if that is the entire macro
1743 # its ok. This may start either on the #define line
1744 # or the one below.
1745 my $ln = $linenr; 1781 my $ln = $linenr;
1746 my $cnt = $realcnt; 1782 my $cnt = $realcnt;
1747 my $off = 0; 1783 my ($off, $dstat, $dcond, $rest);
1784 my $ctx = '';
1748 1785
1749 # If the macro starts on the define line start 1786 my $args = defined($1);
1750 # grabbing the statement after the identifier 1787
1751 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; 1788 # Find the end of the macro and limit our statement
1752 ##print "1<$1> 2<$2>\n"; 1789 # search to that.
1753 if (defined $2 && $2 ne '') { 1790 while ($cnt > 0 && defined $lines[$ln - 1] &&
1754 $off = length($1); 1791 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
1755 $ln--; 1792 {
1756 $cnt++; 1793 $ctx .= $rawlines[$ln - 1] . "\n";
1757 while ($lines[$ln - 1] =~ /^-/) { 1794 $ln++;
1758 $ln--; 1795 $cnt--;
1759 $cnt++; 1796 }
1760 } 1797 $ctx .= $rawlines[$ln - 1];
1798
1799 ($dstat, $dcond, $ln, $cnt, $off) =
1800 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
1801 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
1802 #print "LINE<$lines[$ln]> len<" . length($lines[$ln]) . "\n";
1803
1804 # Extract the remainder of the define (if any) and
1805 # rip off surrounding spaces, and trailing \'s.
1806 $rest = '';
1807 if (defined $lines[$ln - 1] &&
1808 $off > length($lines[$ln - 1]))
1809 {
1810 $ln++;
1811 $cnt--;
1812 $off = 0;
1813 }
1814 while ($cnt > 0) {
1815 $rest .= substr($lines[$ln - 1], $off) . "\n";
1816 $ln++;
1817 $cnt--;
1818 $off = 0;
1819 }
1820 $rest =~ s/\\\n.//g;
1821 $rest =~ s/^\s*//s;
1822 $rest =~ s/\s*$//s;
1823
1824 # Clean up the original statement.
1825 if ($args) {
1826 substr($dstat, 0, length($dcond), '');
1827 } else {
1828 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
1761 } 1829 }
1762 my @ctx = ctx_statement($ln, $cnt, $off); 1830 $dstat =~ s/\\\n.//g;
1763 my $ctx_ln = $ln + $#ctx + 1; 1831 $dstat =~ s/^\s*//s;
1764 my $ctx = join("\n", @ctx); 1832 $dstat =~ s/\s*$//s;
1765 1833
1766 # Pull in any empty extension lines. 1834 # Flatten any parentheses and braces
1767 while ($ctx =~ /\\$/ && 1835 while ($dstat =~ s/\([^\(\)]*\)/1/) {
1768 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) { 1836 }
1769 $ctx .= $lines[$ctx_ln - 1]; 1837 while ($dstat =~ s/\{[^\{\}]*\}/1/) {
1770 $ctx_ln++;
1771 } 1838 }
1772 1839
1773 if ($ctx =~ /\\$/) { 1840 my $exceptions = qr{
1774 if ($ctx =~ /;/) { 1841 $Declare|
1842 module_param_named|
1843 MODULE_PARAM_DESC|
1844 DECLARE_PER_CPU|
1845 DEFINE_PER_CPU|
1846 __typeof__\(
1847 }x;
1848 if ($rest ne '') {
1849 if ($rest !~ /while\s*\(/ &&
1850 $dstat !~ /$exceptions/)
1851 {
1775 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 1852 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1776 } else { 1853 }
1854
1855 } elsif ($ctx !~ /;/) {
1856 if ($dstat ne '' &&
1857 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
1858 $dstat !~ /$exceptions/ &&
1859 $dstat =~ /$Operators/)
1860 {
1777 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 1861 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1778 } 1862 }
1779 } 1863 }
@@ -1884,7 +1968,7 @@ sub process {
1884 1968
1885# don't include deprecated include files (uses RAW line) 1969# don't include deprecated include files (uses RAW line)
1886 for my $inc (@dep_includes) { 1970 for my $inc (@dep_includes) {
1887 if ($rawline =~ m@\#\s*include\s*\<$inc>@) { 1971 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
1888 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 1972 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1889 } 1973 }
1890 } 1974 }
@@ -1908,7 +1992,7 @@ sub process {
1908 } 1992 }
1909 1993
1910# warn about #if 0 1994# warn about #if 0
1911 if ($line =~ /^.#\s*if\s+0\b/) { 1995 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
1912 CHK("if this code is redundant consider removing it\n" . 1996 CHK("if this code is redundant consider removing it\n" .
1913 $herecurr); 1997 $herecurr);
1914 } 1998 }
@@ -1920,23 +2004,16 @@ sub process {
1920 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); 2004 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1921 } 2005 }
1922 } 2006 }
1923# check for needless usb_free_urb() checks
1924 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1925 my $expr = $1;
1926 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1927 WARN("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1928 }
1929 }
1930 2007
1931# warn about #ifdefs in C files 2008# warn about #ifdefs in C files
1932# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 2009# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1933# print "#ifdef in C files should be avoided\n"; 2010# print "#ifdef in C files should be avoided\n";
1934# print "$herecurr"; 2011# print "$herecurr";
1935# $clean = 0; 2012# $clean = 0;
1936# } 2013# }
1937 2014
1938# warn about spacing in #ifdefs 2015# warn about spacing in #ifdefs
1939 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) { 2016 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
1940 ERROR("exactly one space required after that #$1\n" . $herecurr); 2017 ERROR("exactly one space required after that #$1\n" . $herecurr);
1941 } 2018 }
1942 2019
@@ -1955,7 +2032,7 @@ sub process {
1955 } 2032 }
1956 } 2033 }
1957# check of hardware specific defines 2034# check of hardware specific defines
1958 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 2035 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1959 CHK("architecture specific defines should be avoided\n" . $herecurr); 2036 CHK("architecture specific defines should be avoided\n" . $herecurr);
1960 } 2037 }
1961 2038
@@ -1973,15 +2050,18 @@ sub process {
1973 2050
1974# check for new externs in .c files. 2051# check for new externs in .c files.
1975 if ($realfile =~ /\.c$/ && defined $stat && 2052 if ($realfile =~ /\.c$/ && defined $stat &&
1976 $stat =~ /^.\s*(?:extern\s+)?$Type\s+$Ident(\s*)\(/s) 2053 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
1977 { 2054 {
1978 my $paren_space = $1; 2055 my $function_name = $1;
2056 my $paren_space = $2;
1979 2057
1980 my $s = $stat; 2058 my $s = $stat;
1981 if (defined $cond) { 2059 if (defined $cond) {
1982 substr($s, 0, length($cond), ''); 2060 substr($s, 0, length($cond), '');
1983 } 2061 }
1984 if ($s =~ /^\s*;/) { 2062 if ($s =~ /^\s*;/ &&
2063 $function_name ne 'uninitialized_var')
2064 {
1985 WARN("externs should be avoided in .c files\n" . $herecurr); 2065 WARN("externs should be avoided in .c files\n" . $herecurr);
1986 } 2066 }
1987 2067
@@ -2030,8 +2110,8 @@ sub process {
2030# use of NR_CPUS is usually wrong 2110# use of NR_CPUS is usually wrong
2031# ignore definitions of NR_CPUS and usage to define arrays as likely right 2111# ignore definitions of NR_CPUS and usage to define arrays as likely right
2032 if ($line =~ /\bNR_CPUS\b/ && 2112 if ($line =~ /\bNR_CPUS\b/ &&
2033 $line !~ /^.#\s*if\b.*\bNR_CPUS\b/ && 2113 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2034 $line !~ /^.#\s*define\b.*\bNR_CPUS\b/ && 2114 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2035 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 2115 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2036 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 2116 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2037 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 2117 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)