checkpatch.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #!/usr/bin/perl -w
  2. # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
  3. # (c) 2005, Joel Scohpp <jschopp@austin.ibm.com> (the ugly bit)
  4. # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
  5. # Licensed under the terms of the GNU GPL License version 2
  6. use strict;
  7. my $P = $0;
  8. my $V = '0.01';
  9. use Getopt::Long qw(:config no_auto_abbrev);
  10. my $quiet = 0;
  11. my $tree = 1;
  12. my $chk_signoff = 1;
  13. my $chk_patch = 1;
  14. GetOptions(
  15. 'q|quiet' => \$quiet,
  16. 'tree!' => \$tree,
  17. 'signoff!' => \$chk_signoff,
  18. 'patch!' => \$chk_patch,
  19. ) or exit;
  20. my $exit = 0;
  21. if ($#ARGV < 0) {
  22. print "usage: patchstylecheckemail.pl [options] patchfile\n";
  23. print "version: $V\n";
  24. print "options: -q => quiet\n";
  25. print " --no-tree => run without a kernel tree\n";
  26. exit(1);
  27. }
  28. if ($tree && !top_of_kernel_tree()) {
  29. print "Must be run from the top-level dir. of a kernel tree\n";
  30. exit(2);
  31. }
  32. my @deprecated = ();
  33. my $removal = 'Documentation/feature-removal-schedule.txt';
  34. if ($tree && -f $removal) {
  35. open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
  36. while (<REMOVE>) {
  37. if (/^Files:\s+(.*\S)/) {
  38. for my $file (split(/[, ]+/, $1)) {
  39. if ($file =~ m@include/(.*)@) {
  40. push(@deprecated, $1);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. my @lines = ();
  47. while (<>) {
  48. chomp;
  49. push(@lines, $_);
  50. if (eof(ARGV)) {
  51. if (!process($ARGV, @lines)) {
  52. $exit = 1;
  53. }
  54. @lines = ();
  55. }
  56. }
  57. exit($exit);
  58. sub top_of_kernel_tree {
  59. if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
  60. (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
  61. (-d "Documentation") && (-d "arch") && (-d "include") &&
  62. (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
  63. (-d "kernel") && (-d "lib") && (-d "scripts")) {
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. sub expand_tabs {
  69. my ($str) = @_;
  70. my $res = '';
  71. my $n = 0;
  72. for my $c (split(//, $str)) {
  73. if ($c eq "\t") {
  74. $res .= ' ';
  75. $n++;
  76. for (; ($n % 8) != 0; $n++) {
  77. $res .= ' ';
  78. }
  79. next;
  80. }
  81. $res .= $c;
  82. $n++;
  83. }
  84. return $res;
  85. }
  86. sub cat_vet {
  87. my ($vet) = @_;
  88. $vet =~ s/\t/^I/;
  89. $vet =~ s/$/\$/;
  90. return $vet;
  91. }
  92. sub process {
  93. my $filename = shift;
  94. my @lines = @_;
  95. my $linenr=0;
  96. my $prevline="";
  97. my $stashline="";
  98. my $lineforcounting='';
  99. my $indent;
  100. my $previndent=0;
  101. my $stashindent=0;
  102. my $clean = 1;
  103. my $signoff = 0;
  104. my $is_patch = 0;
  105. # Trace the real file/line as we go.
  106. my $realfile = '';
  107. my $realline = 0;
  108. my $realcnt = 0;
  109. my $here = '';
  110. my $in_comment = 0;
  111. my $first_line = 0;
  112. foreach my $line (@lines) {
  113. $linenr++;
  114. #extract the filename as it passes
  115. if ($line=~/^\+\+\+\s+(\S+)/) {
  116. $realfile=$1;
  117. $in_comment = 0;
  118. next;
  119. }
  120. #extract the line range in the file after the patch is applied
  121. if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
  122. $is_patch = 1;
  123. $first_line = 1;
  124. $in_comment = 0;
  125. $realline=$1-1;
  126. if (defined $2) {
  127. $realcnt=$3+1;
  128. } else {
  129. $realcnt=1+1;
  130. }
  131. next;
  132. }
  133. #track the line number as we move through the hunk
  134. if ($line=~/^[ \+]/) {
  135. $realline++;
  136. $realcnt-- if ($realcnt != 0);
  137. # track any sort of multi-line comment. Obviously if
  138. # the added text or context do not include the whole
  139. # comment we will not see it. Such is life.
  140. #
  141. # Guestimate if this is a continuing comment. If this
  142. # is the start of a diff block and this line starts
  143. # ' *' then it is very likely a comment.
  144. if ($first_line and $line =~ m@^.\s*\*@) {
  145. $in_comment = 1;
  146. }
  147. if ($line =~ m@/\*@) {
  148. $in_comment = 1;
  149. }
  150. if ($line =~ m@\*/@) {
  151. $in_comment = 0;
  152. }
  153. $lineforcounting = $line;
  154. $lineforcounting =~ s/^\+//;
  155. $lineforcounting = expand_tabs($lineforcounting);
  156. my ($white) = ($lineforcounting =~ /^(\s*)/);
  157. $indent = length($white);
  158. # Track the previous line.
  159. ($prevline, $stashline) = ($stashline, $line);
  160. ($previndent, $stashindent) = ($stashindent, $indent);
  161. $first_line = 0;
  162. }
  163. #make up the handle for any error we report on this line
  164. $here = "PATCH: $ARGV:$linenr:";
  165. $here .= "\nFILE: $realfile:$realline:" if ($realcnt != 0);
  166. my $herecurr = "$here\n$line\n\n";
  167. my $hereprev = "$here\n$prevline\n$line\n\n";
  168. #check the patch for a signoff:
  169. if ($line =~ /^\s*Signed-off-by:\s/) {
  170. $signoff++;
  171. } elsif ($line =~ /^\s*signed-off-by:/i) {
  172. if (!($line =~ /^\s*Signed-off-by:/)) {
  173. print "use Signed-off-by:\n";
  174. print "$herecurr";
  175. $clean = 0;
  176. }
  177. if ($line =~ /^\s*signed-off-by:\S/i) {
  178. print "need space after Signed-off-by:\n";
  179. print "$herecurr";
  180. $clean = 0;
  181. }
  182. }
  183. #ignore lines not being added
  184. if ($line=~/^[^\+]/) {next;}
  185. # check we are in a valid source file *.[hcsS] if not then ignore this hunk
  186. next if ($realfile !~ /\.[hcsS]$/);
  187. #trailing whitespace
  188. if ($line=~/\S\s+$/) {
  189. my $herevet = "$here\n" . cat_vet($line) . "\n\n";
  190. print "trailing whitespace\n";
  191. print "$herevet";
  192. $clean = 0;
  193. }
  194. #80 column limit
  195. if (!($prevline=~/\/\*\*/) && length($lineforcounting) > 80) {
  196. print "line over 80 characters\n";
  197. print "$herecurr";
  198. $clean = 0;
  199. }
  200. # check we are in a valid source file *.[hc] if not then ignore this hunk
  201. next if ($realfile !~ /\.[hc]$/);
  202. # at the beginning of a line any tabs must come first and anything
  203. # more than 8 must use tabs.
  204. if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
  205. my $herevet = "$here\n" . cat_vet($line) . "\n\n";
  206. print "use tabs not spaces\n";
  207. print "$herevet";
  208. $clean = 0;
  209. }
  210. #
  211. # The rest of our checks refer specifically to C style
  212. # only apply those _outside_ comments.
  213. #
  214. next if ($in_comment);
  215. # no C99 // comments
  216. if ($line =~ m@//@ and !($line =~ m@\".*//.*\"@)) {
  217. print "do not use C99 // comments\n";
  218. print "$herecurr";
  219. $clean = 0;
  220. }
  221. # Remove comments from the line before processing.
  222. $line =~ s@/\*.*\*/@@g;
  223. $line =~ s@/\*.*@@;
  224. $line =~ s@.*\*/@@;
  225. $line =~ s@//.*@@;
  226. #EXPORT_SYMBOL should immediately follow its function closing }.
  227. if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) ||
  228. ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) {
  229. if (($prevline !~ /^}/) &&
  230. ($prevline !~ /^\+}/) &&
  231. ($prevline !~ /^ }/)) {
  232. print "EXPORT_SYMBOL(func); should immediately follow its function\n";
  233. print "$herecurr";
  234. $clean = 0;
  235. }
  236. }
  237. # check for static initialisers.
  238. if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
  239. print "do not initialise statics to 0 or NULL\n";
  240. print "$herecurr";
  241. $clean = 0;
  242. }
  243. # check for new typedefs.
  244. if ($line=~/\s*typedef\s/) {
  245. print "do not add new typedefs\n";
  246. print "$herecurr";
  247. $clean = 0;
  248. }
  249. # * goes on variable not on type
  250. if ($line=~/[A-Za-z\d_]+\* [A-Za-z\d_]+/) {
  251. print "\"foo* bar\" should be \"foo *bar\"\n";
  252. print "$herecurr";
  253. $clean = 0;
  254. }
  255. # # no BUG() or BUG_ON()
  256. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  257. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  258. # print "$herecurr";
  259. # $clean = 0;
  260. # }
  261. # printk should use KERN_* levels
  262. if ($line =~ /\bprintk\((?!KERN_)/) {
  263. print "printk() should include KERN_ facility level\n";
  264. print "$herecurr";
  265. $clean = 0;
  266. }
  267. #function brace can't be on same line, except for #defines of do while, or if closed on same line
  268. if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
  269. !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
  270. print "braces following function declarations go on the next line\n";
  271. print "$herecurr";
  272. $clean = 0;
  273. }
  274. my $opline = $line;
  275. $opline =~ s/^.//;
  276. if (!($line=~/\#\s*include/)) {
  277. # Check operator spacing.
  278. my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
  279. for (my $n = 0; $n < $#elements; $n += 2) {
  280. # $wN says we have white-space before or after
  281. # $sN says we have a separator before or after
  282. # $oN says we have another operator before or after
  283. my $w1 = $elements[$n] =~ /\s$/;
  284. my $s1 = $elements[$n] =~ /(\[|\(|\s)$/;
  285. my $o1 = $elements[$n] eq '';
  286. my $op = $elements[$n + 1];
  287. my $w2 = 1;
  288. my $s2 = 1;
  289. my $o2 = 0;
  290. # If we have something after the operator handle it.
  291. if (defined $elements[$n + 2]) {
  292. $w2 = $elements[$n + 2] =~ /^\s/;
  293. $s2 = $elements[$n + 2] =~ /^(\s|\)|\]|;)/;
  294. $o2 = $elements[$n + 2] eq '';
  295. }
  296. # Generate the context.
  297. my $at = "here: ";
  298. for (my $m = $n; $m >= 0; $m--) {
  299. if ($elements[$m] ne '') {
  300. $at .= $elements[$m];
  301. last;
  302. }
  303. }
  304. $at .= $op;
  305. for (my $m = $n + 2; defined $elements[$m]; $m++) {
  306. if ($elements[$m] ne '') {
  307. $at .= $elements[$m];
  308. last;
  309. }
  310. }
  311. ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
  312. # Skip things apparently in quotes.
  313. next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
  314. # We need ; as an operator. // is a comment.
  315. if ($op eq ';' or $op eq '//') {
  316. # -> should have no spaces
  317. } elsif ($op eq '->') {
  318. if ($s1 or $s2) {
  319. print "no spaces around that '$op' $at\n";
  320. print "$herecurr";
  321. $clean = 0;
  322. }
  323. # , must have a space on the right.
  324. } elsif ($op eq ',') {
  325. if (!$s2) {
  326. print "need space after that '$op' $at\n";
  327. print "$herecurr";
  328. $clean = 0;
  329. }
  330. # unary ! and unary ~ are allowed no space on the right
  331. } elsif ($op eq '!' or $op eq '~') {
  332. if (!$s1 && !$o1) {
  333. print "need space before that '$op' $at\n";
  334. print "$herecurr";
  335. $clean = 0;
  336. }
  337. if ($s2) {
  338. print "no space after that '$op' $at\n";
  339. print "$herecurr";
  340. $clean = 0;
  341. }
  342. # unary ++ and unary -- are allowed no space on one side.
  343. } elsif ($op eq '++' or $op eq '--') {
  344. if (($s1 && $s2) || ((!$s1 && !$o1) && (!$s2 && !$o2))) {
  345. print "need space one side of that '$op' $at\n";
  346. print "$herecurr";
  347. $clean = 0;
  348. }
  349. # & is both unary and binary
  350. # unary:
  351. # a &b
  352. # binary (consistent spacing):
  353. # a&b OK
  354. # a & b OK
  355. #
  356. # boiling down to: if there is a space on the right then there
  357. # should be one on the left.
  358. #
  359. # - is the same
  360. #
  361. # * is the same only adding:
  362. # type:
  363. # (foo *)
  364. # (foo **)
  365. #
  366. } elsif ($op eq '&' or $op eq '-' or $op eq '*') {
  367. if ($w2 and !$w1) {
  368. print "need space before that '$op' $at\n";
  369. print "$herecurr";
  370. $clean = 0;
  371. }
  372. # << and >> may either have or not have spaces both sides
  373. } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
  374. $op eq '^' or $op eq '|')
  375. {
  376. if ($s1 != $s2) {
  377. print "need consistent spacing around '$op' $at\n";
  378. print "$herecurr";
  379. $clean = 0;
  380. }
  381. # All the others need spaces both sides.
  382. } elsif (!$s1 or !$s2) {
  383. print "need spaces around that '$op' $at\n";
  384. print "$herecurr";
  385. $clean = 0;
  386. }
  387. }
  388. }
  389. #need space before brace following if, while, etc
  390. if ($line=~/\(.*\){/) {
  391. print "need a space before the brace\n";
  392. print "$herecurr";
  393. $clean = 0;
  394. }
  395. #goto labels aren't indented, allow a single space however
  396. if ($line=~/^.\s+[A-Za-z\d_]+:/ and
  397. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  398. print "labels should not be indented\n";
  399. print "$herecurr";
  400. $clean = 0;
  401. }
  402. # Need a space before open parenthesis after if, while etc
  403. if ($line=~/(if|while|for|switch)\(/) {
  404. print "need a space before the open parenthesis\n";
  405. print "$herecurr";
  406. $clean = 0;
  407. }
  408. # Check for illegal assignment in if conditional.
  409. if ($line=~/(if|while)\s*\(.*[^<>!=]=[^=].*\)/) {
  410. print "do not use assignment in if condition\n";
  411. print "$herecurr";
  412. $clean = 0;
  413. }
  414. # Check for }<nl>else {, these must be at the same
  415. # indent level to be relevant to each other.
  416. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  417. $previndent == $indent) {
  418. print "else should follow close brace\n";
  419. print "$hereprev";
  420. $clean = 0;
  421. }
  422. # Check for switch () {<nl>case, these must be at the
  423. # same indent. We will only catch the first one, as our
  424. # context is very small but people tend to be consistent
  425. # so we will catch them out more often than not.
  426. if ($prevline=~/\s*switch\s*\(.*\)/ and $line=~/\s*case\s+/
  427. and $previndent != $indent) {
  428. print "switch and case should be at the same indent\n";
  429. print "$hereprev";
  430. $clean = 0;
  431. }
  432. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  433. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  434. # print "No studly caps, use _\n";
  435. # print "$herecurr";
  436. # $clean = 0;
  437. # }
  438. #no spaces allowed after \ in define
  439. if ($line=~/\#define.*\\\s$/) {
  440. print("Whitepspace after \\ makes next lines useless\n");
  441. print "$herecurr";
  442. $clean = 0;
  443. }
  444. #warn if <asm/foo.h> is #included and <linux/foo.h> is available.
  445. if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) {
  446. my $checkfile = "include/linux/$1.h";
  447. if (-f $checkfile) {
  448. print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
  449. print $herecurr;
  450. $clean = 0;
  451. }
  452. }
  453. #if/while/etc brace do not go on next line, unless #defining a do while loop, or if that brace on the next line is for something else
  454. if ($prevline=~/(if|while|for|switch)\s*\(/) {
  455. my @opened = $prevline=~/\(/g;
  456. my @closed = $prevline=~/\)/g;
  457. my $nr_line = $linenr;
  458. my $remaining = $realcnt;
  459. my $next_line = $line;
  460. my $extra_lines = 0;
  461. my $display_segment = $prevline;
  462. while ($remaining > 0 && scalar @opened > scalar @closed) {
  463. $prevline .= $next_line;
  464. $display_segment .= "\n" . $next_line;
  465. $next_line = $lines[$nr_line];
  466. $nr_line++;
  467. $remaining--;
  468. @opened = $prevline=~/\(/g;
  469. @closed = $prevline=~/\)/g;
  470. }
  471. if (($prevline=~/(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
  472. !($next_line=~/(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) {
  473. print "That { should be on the previous line\n";
  474. print "$display_segment\n$next_line\n\n";
  475. $clean = 0;
  476. }
  477. }
  478. #multiline macros should be enclosed in a do while loop
  479. if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and
  480. !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and
  481. !($line=~/do.*{/) and !($line=~/\(\{/)) {
  482. print "Macros with multiple statements should be enclosed in a do - while loop\n";
  483. print "$hereprev";
  484. $clean = 0;
  485. }
  486. # don't include deprecated include files
  487. for my $inc (@deprecated) {
  488. if ($line =~ m@\#\s*include\s*\<$inc>@) {
  489. print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
  490. print "$herecurr";
  491. $clean = 0;
  492. }
  493. }
  494. # don't use kernel_thread()
  495. if ($line =~ /\bkernel_thread\b/) {
  496. print "Don't use kernel_thread(), use kthread(): see Documentation/feature-removal-schedule.txt\n";
  497. print "$herecurr";
  498. $clean = 0;
  499. }
  500. }
  501. if ($chk_patch && !$is_patch) {
  502. $clean = 0;
  503. print "Does not appear to be a unified-diff format patch\n";
  504. }
  505. if ($is_patch && $chk_signoff && $signoff == 0) {
  506. $clean = 0;
  507. print "Missing Signed-off-by: line(s)\n";
  508. }
  509. if ($clean == 1 && $quiet == 0) {
  510. print "Your patch has no obvious style problems and is ready for submission.\n"
  511. }
  512. if ($clean == 0 && $quiet == 0) {
  513. print "Your patch has style problems, please review. If any of these errors\n";
  514. print "are false positives report them to the maintainer, see\n";
  515. print "CHECKPATCH in MAINTAINERS.\n";
  516. }
  517. return $clean;
  518. }