checkpatch.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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.03';
  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 @dep_includes = ();
  33. my @dep_functions = ();
  34. my $removal = 'Documentation/feature-removal-schedule.txt';
  35. if ($tree && -f $removal) {
  36. open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
  37. while (<REMOVE>) {
  38. if (/^Files:\s+(.*\S)/) {
  39. for my $file (split(/[, ]+/, $1)) {
  40. if ($file =~ m@include/(.*)@) {
  41. push(@dep_includes, $1);
  42. }
  43. }
  44. } elsif (/^Funcs:\s+(.*\S)/) {
  45. for my $func (split(/[, ]+/, $1)) {
  46. push(@dep_functions, $func);
  47. }
  48. }
  49. }
  50. }
  51. my @lines = ();
  52. while (<>) {
  53. chomp;
  54. push(@lines, $_);
  55. if (eof(ARGV)) {
  56. if (!process($ARGV, @lines)) {
  57. $exit = 1;
  58. }
  59. @lines = ();
  60. }
  61. }
  62. exit($exit);
  63. sub top_of_kernel_tree {
  64. if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
  65. (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
  66. (-d "Documentation") && (-d "arch") && (-d "include") &&
  67. (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
  68. (-d "kernel") && (-d "lib") && (-d "scripts")) {
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. sub expand_tabs {
  74. my ($str) = @_;
  75. my $res = '';
  76. my $n = 0;
  77. for my $c (split(//, $str)) {
  78. if ($c eq "\t") {
  79. $res .= ' ';
  80. $n++;
  81. for (; ($n % 8) != 0; $n++) {
  82. $res .= ' ';
  83. }
  84. next;
  85. }
  86. $res .= $c;
  87. $n++;
  88. }
  89. return $res;
  90. }
  91. sub line_stats {
  92. my ($line) = @_;
  93. # Drop the diff line leader and expand tabs
  94. $line =~ s/^.//;
  95. $line = expand_tabs($line);
  96. # Pick the indent from the front of the line.
  97. my ($white) = ($line =~ /^(\s*)/);
  98. return (length($line), length($white));
  99. }
  100. sub ctx_block_get {
  101. my ($linenr, $remain, $outer) = @_;
  102. my $line;
  103. my $start = $linenr - 1;
  104. my $end = $linenr - 1 + $remain;
  105. my $blk = '';
  106. my @o;
  107. my @c;
  108. my @res = ();
  109. for ($line = $start; $line < $end; $line++) {
  110. $blk .= $lines[$line];
  111. @o = ($blk =~ /\{/g);
  112. @c = ($blk =~ /\}/g);
  113. if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
  114. push(@res, $lines[$line]);
  115. }
  116. last if (scalar(@o) == scalar(@c));
  117. }
  118. return @res;
  119. }
  120. sub ctx_block_outer {
  121. my ($linenr, $remain) = @_;
  122. return ctx_block_get($linenr, $remain, 1);
  123. }
  124. sub ctx_block {
  125. my ($linenr, $remain) = @_;
  126. return ctx_block_get($linenr, $remain, 0);
  127. }
  128. sub ctx_locate_comment {
  129. my ($first_line, $end_line) = @_;
  130. # Catch a comment on the end of the line itself.
  131. my ($current_comment) = ($lines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
  132. return $current_comment if (defined $current_comment);
  133. # Look through the context and try and figure out if there is a
  134. # comment.
  135. my $in_comment = 0;
  136. $current_comment = '';
  137. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  138. my $line = $lines[$linenr - 1];
  139. ##warn " $line\n";
  140. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  141. $in_comment = 1;
  142. }
  143. if ($line =~ m@/\*@) {
  144. $in_comment = 1;
  145. }
  146. if (!$in_comment && $current_comment ne '') {
  147. $current_comment = '';
  148. }
  149. $current_comment .= $line . "\n" if ($in_comment);
  150. if ($line =~ m@\*/@) {
  151. $in_comment = 0;
  152. }
  153. }
  154. chomp($current_comment);
  155. return($current_comment);
  156. }
  157. sub ctx_has_comment {
  158. my ($first_line, $end_line) = @_;
  159. my $cmt = ctx_locate_comment($first_line, $end_line);
  160. ##print "LINE: $lines[$end_line - 1 ]\n";
  161. ##print "CMMT: $cmt\n";
  162. return ($cmt ne '');
  163. }
  164. sub cat_vet {
  165. my ($vet) = @_;
  166. $vet =~ s/\t/^I/;
  167. $vet =~ s/$/\$/;
  168. return $vet;
  169. }
  170. sub has_non_quoted {
  171. return ($_[0] =~ m{$_[1]} and $_[0] !~ m{\".*$_[1].*\"});
  172. }
  173. sub process {
  174. my $filename = shift;
  175. my @lines = @_;
  176. my $linenr=0;
  177. my $prevline="";
  178. my $stashline="";
  179. my $length;
  180. my $indent;
  181. my $previndent=0;
  182. my $stashindent=0;
  183. my $clean = 1;
  184. my $signoff = 0;
  185. my $is_patch = 0;
  186. # Trace the real file/line as we go.
  187. my $realfile = '';
  188. my $realline = 0;
  189. my $realcnt = 0;
  190. my $here = '';
  191. my $in_comment = 0;
  192. my $first_line = 0;
  193. foreach my $line (@lines) {
  194. $linenr++;
  195. #extract the filename as it passes
  196. if ($line=~/^\+\+\+\s+(\S+)/) {
  197. $realfile=$1;
  198. $in_comment = 0;
  199. next;
  200. }
  201. #extract the line range in the file after the patch is applied
  202. if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
  203. $is_patch = 1;
  204. $first_line = $linenr + 1;
  205. $in_comment = 0;
  206. $realline=$1-1;
  207. if (defined $2) {
  208. $realcnt=$3+1;
  209. } else {
  210. $realcnt=1+1;
  211. }
  212. next;
  213. }
  214. # track the line number as we move through the hunk, note that
  215. # new versions of GNU diff omit the leading space on completely
  216. # blank context lines so we need to count that too.
  217. if ($line =~ /^( |\+|$)/) {
  218. $realline++;
  219. $realcnt-- if ($realcnt != 0);
  220. # track any sort of multi-line comment. Obviously if
  221. # the added text or context do not include the whole
  222. # comment we will not see it. Such is life.
  223. #
  224. # Guestimate if this is a continuing comment. If this
  225. # is the start of a diff block and this line starts
  226. # ' *' then it is very likely a comment.
  227. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  228. $in_comment = 1;
  229. }
  230. if ($line =~ m@/\*@) {
  231. $in_comment = 1;
  232. }
  233. if ($line =~ m@\*/@) {
  234. $in_comment = 0;
  235. }
  236. # Measure the line length and indent.
  237. ($length, $indent) = line_stats($line);
  238. # Track the previous line.
  239. ($prevline, $stashline) = ($stashline, $line);
  240. ($previndent, $stashindent) = ($stashindent, $indent);
  241. }
  242. #make up the handle for any error we report on this line
  243. $here = "PATCH: $ARGV:$linenr:";
  244. $here .= "\nFILE: $realfile:$realline:" if ($realcnt != 0);
  245. my $herecurr = "$here\n$line\n\n";
  246. my $hereprev = "$here\n$prevline\n$line\n\n";
  247. #check the patch for a signoff:
  248. if ($line =~ /^\s*Signed-off-by:\s/) {
  249. $signoff++;
  250. } elsif ($line =~ /^\s*signed-off-by:/i) {
  251. # This is a signoff, if ugly, so do not double report.
  252. $signoff++;
  253. if (!($line =~ /^\s*Signed-off-by:/)) {
  254. print "use Signed-off-by:\n";
  255. print "$herecurr";
  256. $clean = 0;
  257. }
  258. if ($line =~ /^\s*signed-off-by:\S/i) {
  259. print "need space after Signed-off-by:\n";
  260. print "$herecurr";
  261. $clean = 0;
  262. }
  263. }
  264. #ignore lines not being added
  265. if ($line=~/^[^\+]/) {next;}
  266. # check we are in a valid source file *.[hcsS] if not then ignore this hunk
  267. next if ($realfile !~ /\.[hcsS]$/);
  268. #trailing whitespace
  269. if ($line=~/\S\s+$/) {
  270. my $herevet = "$here\n" . cat_vet($line) . "\n\n";
  271. print "trailing whitespace\n";
  272. print "$herevet";
  273. $clean = 0;
  274. }
  275. #80 column limit
  276. if (!($prevline=~/\/\*\*/) && $length > 80) {
  277. print "line over 80 characters\n";
  278. print "$herecurr";
  279. $clean = 0;
  280. }
  281. # check we are in a valid source file *.[hc] if not then ignore this hunk
  282. next if ($realfile !~ /\.[hc]$/);
  283. # at the beginning of a line any tabs must come first and anything
  284. # more than 8 must use tabs.
  285. if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
  286. my $herevet = "$here\n" . cat_vet($line) . "\n\n";
  287. print "use tabs not spaces\n";
  288. print "$herevet";
  289. $clean = 0;
  290. }
  291. #
  292. # The rest of our checks refer specifically to C style
  293. # only apply those _outside_ comments.
  294. #
  295. next if ($in_comment);
  296. # no C99 // comments
  297. if (has_non_quoted($line, '//')) {
  298. print "do not use C99 // comments\n";
  299. print "$herecurr";
  300. $clean = 0;
  301. }
  302. # Remove comments from the line before processing.
  303. $line =~ s@/\*.*\*/@@g;
  304. $line =~ s@/\*.*@@;
  305. $line =~ s@.*\*/@@;
  306. $line =~ s@//.*@@;
  307. #EXPORT_SYMBOL should immediately follow its function closing }.
  308. if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) ||
  309. ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) {
  310. if (($prevline !~ /^}/) &&
  311. ($prevline !~ /^\+}/) &&
  312. ($prevline !~ /^ }/)) {
  313. print "EXPORT_SYMBOL(func); should immediately follow its function\n";
  314. print "$herecurr";
  315. $clean = 0;
  316. }
  317. }
  318. # check for static initialisers.
  319. if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
  320. print "do not initialise statics to 0 or NULL\n";
  321. print "$herecurr";
  322. $clean = 0;
  323. }
  324. # check for new typedefs.
  325. if ($line=~/\s*typedef\s/) {
  326. print "do not add new typedefs\n";
  327. print "$herecurr";
  328. $clean = 0;
  329. }
  330. # * goes on variable not on type
  331. if ($line=~/[A-Za-z\d_]+\* [A-Za-z\d_]+/) {
  332. print "\"foo* bar\" should be \"foo *bar\"\n";
  333. print "$herecurr";
  334. $clean = 0;
  335. }
  336. # # no BUG() or BUG_ON()
  337. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  338. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  339. # print "$herecurr";
  340. # $clean = 0;
  341. # }
  342. # printk should use KERN_* levels
  343. if ($line =~ /\bprintk\((?!KERN_)/) {
  344. print "printk() should include KERN_ facility level\n";
  345. print "$herecurr";
  346. $clean = 0;
  347. }
  348. #function brace can't be on same line, except for #defines of do while, or if closed on same line
  349. if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
  350. !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
  351. print "braces following function declarations go on the next line\n";
  352. print "$herecurr";
  353. $clean = 0;
  354. }
  355. # Note we expand the line with the leading + as the real
  356. # line will be displayed with the leading + and the tabs
  357. # will therefore also expand that way.
  358. my $opline = $line;
  359. $opline = expand_tabs($opline);
  360. $opline =~ s/^.//;
  361. if (!($line=~/\#\s*include/)) {
  362. # Check operator spacing.
  363. my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
  364. my $off = 1;
  365. for (my $n = 0; $n < $#elements; $n += 2) {
  366. $off += length($elements[$n]);
  367. my $a = '';
  368. $a = 'V' if ($elements[$n] ne '');
  369. $a = 'W' if ($elements[$n] =~ /\s$/);
  370. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  371. $a = 'O' if ($elements[$n] eq '');
  372. $a = 'E' if ($elements[$n] eq '' && $n == 0);
  373. my $op = $elements[$n + 1];
  374. my $c = '';
  375. if (defined $elements[$n + 2]) {
  376. $c = 'V' if ($elements[$n + 2] ne '');
  377. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  378. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  379. $c = 'O' if ($elements[$n + 2] eq '');
  380. } else {
  381. $c = 'E';
  382. }
  383. my $ctx = "${a}x${c}";
  384. my $at = "(ctx:$ctx)";
  385. my $ptr = (" " x $off) . "^";
  386. my $hereptr = "$here\n$line\n$ptr\n\n";
  387. ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
  388. # Skip things apparently in quotes.
  389. next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
  390. # We need ; as an operator. // is a comment.
  391. if ($op eq ';' or $op eq '//') {
  392. # -> should have no spaces
  393. } elsif ($op eq '->') {
  394. if ($ctx =~ /Wx.|.xW/) {
  395. print "no spaces around that '$op' $at\n";
  396. print "$hereptr";
  397. $clean = 0;
  398. }
  399. # , must have a space on the right.
  400. } elsif ($op eq ',') {
  401. if ($ctx !~ /.xW|.xE/) {
  402. print "need space after that '$op' $at\n";
  403. print "$hereptr";
  404. $clean = 0;
  405. }
  406. # unary ! and unary ~ are allowed no space on the right
  407. } elsif ($op eq '!' or $op eq '~') {
  408. if ($ctx !~ /[WOEB]x./) {
  409. print "need space before that '$op' $at\n";
  410. print "$hereptr";
  411. $clean = 0;
  412. }
  413. if ($ctx =~ /.xW/) {
  414. print "no space after that '$op' $at\n";
  415. print "$hereptr";
  416. $clean = 0;
  417. }
  418. # unary ++ and unary -- are allowed no space on one side.
  419. } elsif ($op eq '++' or $op eq '--') {
  420. if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) {
  421. print "need space one side of that '$op' $at\n";
  422. print "$hereptr";
  423. $clean = 0;
  424. }
  425. # & is both unary and binary
  426. # unary:
  427. # a &b
  428. # binary (consistent spacing):
  429. # a&b OK
  430. # a & b OK
  431. #
  432. # boiling down to: if there is a space on the right then there
  433. # should be one on the left.
  434. #
  435. # - is the same
  436. #
  437. # * is the same only adding:
  438. # type:
  439. # (foo *)
  440. # (foo **)
  441. #
  442. } elsif ($op eq '&' or $op eq '-') {
  443. if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]/) {
  444. print "need space before that '$op' $at\n";
  445. print "$hereptr";
  446. $clean = 0;
  447. }
  448. } elsif ($op eq '*') {
  449. if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]|[EWO]x[OBV]/) {
  450. print "need space before that '$op' $at\n";
  451. print "$hereptr";
  452. $clean = 0;
  453. }
  454. # << and >> may either have or not have spaces both sides
  455. } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
  456. $op eq '^' or $op eq '|')
  457. {
  458. if ($ctx !~ /VxV|WxW|VxE|WxE/) {
  459. print "need consistent spacing around '$op' $at\n";
  460. print "$hereptr";
  461. $clean = 0;
  462. }
  463. # All the others need spaces both sides.
  464. } elsif ($ctx !~ /[EW]x[WE]/) {
  465. print "need spaces around that '$op' $at\n";
  466. print "$hereptr";
  467. $clean = 0;
  468. }
  469. $off += length($elements[$n + 1]);
  470. }
  471. }
  472. #need space before brace following if, while, etc
  473. if ($line=~/\(.*\){/) {
  474. print "need a space before the brace\n";
  475. print "$herecurr";
  476. $clean = 0;
  477. }
  478. #goto labels aren't indented, allow a single space however
  479. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  480. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  481. print "labels should not be indented\n";
  482. print "$herecurr";
  483. $clean = 0;
  484. }
  485. # Need a space before open parenthesis after if, while etc
  486. if ($line=~/\b(if|while|for|switch)\(/) {
  487. print "need a space before the open parenthesis\n";
  488. print "$herecurr";
  489. $clean = 0;
  490. }
  491. # Check for illegal assignment in if conditional.
  492. if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) {
  493. print "do not use assignment in condition\n";
  494. print "$herecurr";
  495. $clean = 0;
  496. }
  497. # Check for }<nl>else {, these must be at the same
  498. # indent level to be relevant to each other.
  499. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  500. $previndent == $indent) {
  501. print "else should follow close brace\n";
  502. print "$hereprev";
  503. $clean = 0;
  504. }
  505. # Check for switch () and associated case and default
  506. # statements should be at the same indent.
  507. if ($line=~/\bswitch\s*\(.*\)/) {
  508. my $err = '';
  509. my $sep = '';
  510. my @ctx = ctx_block_outer($linenr, $realcnt);
  511. shift(@ctx);
  512. for my $ctx (@ctx) {
  513. my ($clen, $cindent) = line_stats($ctx);
  514. if ($ctx =~ /\s*(case\s+|default:)/ &&
  515. $indent != $cindent) {
  516. $err .= "$sep$ctx\n";
  517. $sep = '';
  518. } else {
  519. $sep = "[...]\n";
  520. }
  521. }
  522. if ($err ne '') {
  523. print "switch and case should be at the same indent\n";
  524. print "$here\n$line\n$err\n";
  525. $clean = 0;
  526. }
  527. }
  528. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  529. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  530. # print "No studly caps, use _\n";
  531. # print "$herecurr";
  532. # $clean = 0;
  533. # }
  534. #no spaces allowed after \ in define
  535. if ($line=~/\#define.*\\\s$/) {
  536. print("Whitepspace after \\ makes next lines useless\n");
  537. print "$herecurr";
  538. $clean = 0;
  539. }
  540. #warn if <asm/foo.h> is #included and <linux/foo.h> is available.
  541. if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) {
  542. my $checkfile = "include/linux/$1.h";
  543. if (-f $checkfile) {
  544. print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
  545. print $herecurr;
  546. $clean = 0;
  547. }
  548. }
  549. #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
  550. if ($prevline=~/\b(if|while|for|switch)\s*\(/) {
  551. my @opened = $prevline=~/\(/g;
  552. my @closed = $prevline=~/\)/g;
  553. my $nr_line = $linenr;
  554. my $remaining = $realcnt;
  555. my $next_line = $line;
  556. my $extra_lines = 0;
  557. my $display_segment = $prevline;
  558. while ($remaining > 1 && scalar @opened > scalar @closed) {
  559. $prevline .= $next_line;
  560. $display_segment .= "\n" . $next_line;
  561. $next_line = $lines[$nr_line];
  562. $nr_line++;
  563. $remaining--;
  564. @opened = $prevline=~/\(/g;
  565. @closed = $prevline=~/\)/g;
  566. }
  567. if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
  568. !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) {
  569. print "That { should be on the previous line\n";
  570. print "$here\n$display_segment\n$next_line\n\n";
  571. $clean = 0;
  572. }
  573. }
  574. #multiline macros should be enclosed in a do while loop
  575. if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and
  576. !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and
  577. !($line=~/do.*{/) and !($line=~/\(\{/)) {
  578. print "Macros with multiple statements should be enclosed in a do - while loop\n";
  579. print "$hereprev";
  580. $clean = 0;
  581. }
  582. # don't include deprecated include files
  583. for my $inc (@dep_includes) {
  584. if ($line =~ m@\#\s*include\s*\<$inc>@) {
  585. print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
  586. print "$herecurr";
  587. $clean = 0;
  588. }
  589. }
  590. # don't use deprecated functions
  591. for my $func (@dep_functions) {
  592. if (has_non_quoted($line, '\b' . $func . '\b')) {
  593. print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
  594. print "$herecurr";
  595. $clean = 0;
  596. }
  597. }
  598. # no volatiles please
  599. if (has_non_quoted($line, '\bvolatile\b')) {
  600. print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
  601. print "$herecurr";
  602. $clean = 0;
  603. }
  604. # warn about #ifdefs in C files
  605. if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  606. print "#ifdef in C files should be avoided\n";
  607. print "$herecurr";
  608. $clean = 0;
  609. }
  610. # check for spinlock_t definitions without a comment.
  611. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
  612. my $which = $1;
  613. if (!ctx_has_comment($first_line, $linenr)) {
  614. print "$1 definition without comment\n";
  615. print "$herecurr";
  616. $clean = 0;
  617. }
  618. }
  619. # check for memory barriers without a comment.
  620. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  621. if (!ctx_has_comment($first_line, $linenr)) {
  622. print "memory barrier without comment\n";
  623. print "$herecurr";
  624. $clean = 0;
  625. }
  626. }
  627. # check of hardware specific defines
  628. if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
  629. print "architecture specific defines should be avoided\n";
  630. print "$herecurr";
  631. $clean = 0;
  632. }
  633. }
  634. if ($chk_patch && !$is_patch) {
  635. $clean = 0;
  636. print "Does not appear to be a unified-diff format patch\n";
  637. }
  638. if ($is_patch && $chk_signoff && $signoff == 0) {
  639. $clean = 0;
  640. print "Missing Signed-off-by: line(s)\n";
  641. }
  642. if ($clean == 1 && $quiet == 0) {
  643. print "Your patch has no obvious style problems and is ready for submission.\n"
  644. }
  645. if ($clean == 0 && $quiet == 0) {
  646. print "Your patch has style problems, please review. If any of these errors\n";
  647. print "are false positives report them to the maintainer, see\n";
  648. print "CHECKPATCH in MAINTAINERS.\n";
  649. }
  650. return $clean;
  651. }