checkpatch.pl 22 KB

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