checkpatch.pl 25 KB

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