checkpatch.pl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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.05';
  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 = '[A-Za-z\d_]+';
  224. my $storage = '(?:extern|static)';
  225. my $sparse = '(?:__user|__kernel|__force|__iomem)';
  226. my $type = '(?:unsigned\s+)?' .
  227. '(?:void|char|short|int|long|unsigned|float|double|' .
  228. 'long\s+long|' .
  229. "struct\\s+${ident}|" .
  230. "union\\s+${ident}|" .
  231. "${ident}_t)" .
  232. "(?:\\s+$sparse)*" .
  233. '(?:\s*\*+)?';
  234. my $attribute = '(?:__read_mostly|__init|__initdata)';
  235. my $Ident = $ident;
  236. my $Type = $type;
  237. my $Storage = $storage;
  238. my $Declare = "(?:$storage\\s+)?$type";
  239. my $Attribute = $attribute;
  240. foreach my $line (@lines) {
  241. $linenr++;
  242. my $rawline = $line;
  243. #extract the filename as it passes
  244. if ($line=~/^\+\+\+\s+(\S+)/) {
  245. $realfile=$1;
  246. $realfile =~ s@^[^/]*/@@;
  247. $in_comment = 0;
  248. next;
  249. }
  250. #extract the line range in the file after the patch is applied
  251. if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
  252. $is_patch = 1;
  253. $first_line = $linenr + 1;
  254. $in_comment = 0;
  255. $realline=$1-1;
  256. if (defined $2) {
  257. $realcnt=$3+1;
  258. } else {
  259. $realcnt=1+1;
  260. }
  261. next;
  262. }
  263. # track the line number as we move through the hunk, note that
  264. # new versions of GNU diff omit the leading space on completely
  265. # blank context lines so we need to count that too.
  266. if ($line =~ /^( |\+|$)/) {
  267. $realline++;
  268. # track any sort of multi-line comment. Obviously if
  269. # the added text or context do not include the whole
  270. # comment we will not see it. Such is life.
  271. #
  272. # Guestimate if this is a continuing comment. If this
  273. # is the start of a diff block and this line starts
  274. # ' *' then it is very likely a comment.
  275. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  276. $in_comment = 1;
  277. }
  278. if ($line =~ m@/\*@) {
  279. $in_comment = 1;
  280. }
  281. if ($line =~ m@\*/@) {
  282. $in_comment = 0;
  283. }
  284. # Measure the line length and indent.
  285. ($length, $indent) = line_stats($line);
  286. # Track the previous line.
  287. ($prevline, $stashline) = ($stashline, $line);
  288. ($previndent, $stashindent) = ($stashindent, $indent);
  289. }
  290. $realcnt-- if ($realcnt != 0);
  291. #make up the handle for any error we report on this line
  292. $here = "#$linenr: ";
  293. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  294. my $hereline = "$here\n$line\n";
  295. my $herecurr = "$here\n$line\n\n";
  296. my $hereprev = "$here\n$prevline\n$line\n\n";
  297. #check the patch for a signoff:
  298. if ($line =~ /^\s*Signed-off-by:\s/) {
  299. $signoff++;
  300. } elsif ($line =~ /^\s*signed-off-by:/i) {
  301. # This is a signoff, if ugly, so do not double report.
  302. $signoff++;
  303. if (!($line =~ /^\s*Signed-off-by:/)) {
  304. print "use Signed-off-by:\n";
  305. print "$herecurr";
  306. $clean = 0;
  307. }
  308. if ($line =~ /^\s*signed-off-by:\S/i) {
  309. print "need space after Signed-off-by:\n";
  310. print "$herecurr";
  311. $clean = 0;
  312. }
  313. }
  314. # Check for wrappage within a valid hunk of the file
  315. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
  316. print "patch seems to be corrupt (line wrapped?) [$realcnt]\n";
  317. print "$herecurr";
  318. $clean = 0;
  319. }
  320. #ignore lines being removed
  321. if ($line=~/^-/) {next;}
  322. # check we are in a valid source file if not then ignore this hunk
  323. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  324. #trailing whitespace
  325. if ($line=~/^\+.*\S\s+$/) {
  326. my $herevet = "$here\n" . cat_vet($line) . "\n\n";
  327. print "trailing whitespace\n";
  328. print "$herevet";
  329. $clean = 0;
  330. }
  331. #80 column limit
  332. if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
  333. print "line over 80 characters\n";
  334. print "$herecurr";
  335. $clean = 0;
  336. }
  337. # check we are in a valid source file *.[hc] if not then ignore this hunk
  338. next if ($realfile !~ /\.[hc]$/);
  339. # at the beginning of a line any tabs must come first and anything
  340. # more than 8 must use tabs.
  341. if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
  342. my $herevet = "$here\n" . cat_vet($line) . "\n\n";
  343. print "use tabs not spaces\n";
  344. print "$herevet";
  345. $clean = 0;
  346. }
  347. #
  348. # The rest of our checks refer specifically to C style
  349. # only apply those _outside_ comments.
  350. #
  351. next if ($in_comment);
  352. # Remove comments from the line before processing.
  353. $line =~ s@/\*.*\*/@@g;
  354. $line =~ s@/\*.*@@;
  355. $line =~ s@.*\*/@@;
  356. # Standardise the strings and chars within the input to simplify matching.
  357. $line = sanitise_line($line);
  358. #
  359. # Checks which may be anchored in the context.
  360. #
  361. # Check for switch () and associated case and default
  362. # statements should be at the same indent.
  363. if ($line=~/\bswitch\s*\(.*\)/) {
  364. my $err = '';
  365. my $sep = '';
  366. my @ctx = ctx_block_outer($linenr, $realcnt);
  367. shift(@ctx);
  368. for my $ctx (@ctx) {
  369. my ($clen, $cindent) = line_stats($ctx);
  370. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  371. $indent != $cindent) {
  372. $err .= "$sep$ctx\n";
  373. $sep = '';
  374. } else {
  375. $sep = "[...]\n";
  376. }
  377. }
  378. if ($err ne '') {
  379. print "switch and case should be at the same indent\n";
  380. print "$here\n$line\n$err\n";
  381. $clean = 0;
  382. }
  383. }
  384. #ignore lines not being added
  385. if ($line=~/^[^\+]/) {next;}
  386. # TEST: allow direct testing of the type matcher.
  387. if ($tst_type && $line =~ /^.$Declare$/) {
  388. print "TEST: is type $Declare\n";
  389. print "$herecurr";
  390. $clean = 0;
  391. next;
  392. }
  393. #
  394. # Checks which are anchored on the added line.
  395. #
  396. # check for malformed paths in #include statements (uses RAW line)
  397. if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
  398. my $path = $1;
  399. if ($path =~ m{//}) {
  400. print "malformed #include filename\n";
  401. print "$herecurr";
  402. $clean = 0;
  403. }
  404. # Sanitise this special form of string.
  405. $path = 'X' x length($path);
  406. $line =~ s{\<.*\>}{<$path>};
  407. }
  408. # no C99 // comments
  409. if ($line =~ m{//}) {
  410. print "do not use C99 // comments\n";
  411. print "$herecurr";
  412. $clean = 0;
  413. }
  414. # Remove C99 comments.
  415. $line =~ s@//.*@@;
  416. #EXPORT_SYMBOL should immediately follow its function closing }.
  417. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  418. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  419. my $name = $1;
  420. if (($prevline !~ /^}/) &&
  421. ($prevline !~ /^\+}/) &&
  422. ($prevline !~ /^ }/) &&
  423. ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
  424. print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n";
  425. print "$herecurr";
  426. $clean = 0;
  427. }
  428. }
  429. # check for static initialisers.
  430. if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
  431. print "do not initialise statics to 0 or NULL\n";
  432. print "$herecurr";
  433. $clean = 0;
  434. }
  435. # check for new typedefs, only function parameters and sparse annotations
  436. # make sense.
  437. if ($line =~ /\btypedef\s/ &&
  438. $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ &&
  439. $line !~ /\b__bitwise(?:__|)\b/) {
  440. print "do not add new typedefs\n";
  441. print "$herecurr";
  442. $clean = 0;
  443. }
  444. # * goes on variable not on type
  445. if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) {
  446. print "\"foo$1 bar\" should be \"foo $1bar\"\n";
  447. print "$herecurr";
  448. $clean = 0;
  449. }
  450. if ($line =~ m{$Type (\*) [A-Za-z\d_]+} ||
  451. $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) {
  452. print "\"foo $1 bar\" should be \"foo $1bar\"\n";
  453. print "$herecurr";
  454. $clean = 0;
  455. }
  456. if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) {
  457. print "\"(foo$1)\" should be \"(foo $1)\"\n";
  458. print "$herecurr";
  459. $clean = 0;
  460. }
  461. if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) {
  462. print "\"(foo $1 )\" should be \"(foo $1)\"\n";
  463. print "$herecurr";
  464. $clean = 0;
  465. }
  466. # # no BUG() or BUG_ON()
  467. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  468. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  469. # print "$herecurr";
  470. # $clean = 0;
  471. # }
  472. # printk should use KERN_* levels. Note that follow on printk's on the
  473. # same line do not need a level, so we use the current block context
  474. # to try and find and validate the current printk. In summary the current
  475. # printk includes all preceeding printk's which have no newline on the end.
  476. # we assume the first bad printk is the one to report.
  477. if ($line =~ /\bprintk\((?!KERN_)/) {
  478. my $ok = 0;
  479. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  480. #print "CHECK<$lines[$ln - 1]\n";
  481. # we have a preceeding printk if it ends
  482. # with "\n" ignore it, else it is to blame
  483. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  484. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  485. $ok = 1;
  486. }
  487. last;
  488. }
  489. }
  490. if ($ok == 0) {
  491. print "printk() should include KERN_ facility level\n";
  492. print "$herecurr";
  493. $clean = 0;
  494. }
  495. }
  496. # function brace can't be on same line, except for #defines of do while,
  497. # or if closed on same line
  498. if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
  499. !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
  500. print "braces following function declarations go on the next line\n";
  501. print "$herecurr";
  502. $clean = 0;
  503. }
  504. # Check operator spacing.
  505. # Note we expand the line with the leading + as the real
  506. # line will be displayed with the leading + and the tabs
  507. # will therefore also expand that way.
  508. my $opline = $line;
  509. $opline = expand_tabs($opline);
  510. $opline =~ s/^./ /;
  511. if (!($line=~/\#\s*include/)) {
  512. my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
  513. my $off = 0;
  514. for (my $n = 0; $n < $#elements; $n += 2) {
  515. $off += length($elements[$n]);
  516. my $a = '';
  517. $a = 'V' if ($elements[$n] ne '');
  518. $a = 'W' if ($elements[$n] =~ /\s$/);
  519. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  520. $a = 'O' if ($elements[$n] eq '');
  521. $a = 'E' if ($elements[$n] eq '' && $n == 0);
  522. my $op = $elements[$n + 1];
  523. my $c = '';
  524. if (defined $elements[$n + 2]) {
  525. $c = 'V' if ($elements[$n + 2] ne '');
  526. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  527. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  528. $c = 'O' if ($elements[$n + 2] eq '');
  529. } else {
  530. $c = 'E';
  531. }
  532. # Pick up the preceeding and succeeding characters.
  533. my $ca = substr($opline, $off - 1, 1);
  534. my $cc = '';
  535. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  536. $cc = substr($opline, $off + length($elements[$n + 1]), 1);
  537. }
  538. my $ctx = "${a}x${c}";
  539. my $at = "(ctx:$ctx)";
  540. my $ptr = (" " x $off) . "^";
  541. my $hereptr = "$hereline$ptr\n\n";
  542. ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
  543. # We need ; as an operator. // is a comment.
  544. if ($op eq ';' or $op eq '//') {
  545. # -> should have no spaces
  546. } elsif ($op eq '->') {
  547. if ($ctx =~ /Wx.|.xW/) {
  548. print "no spaces around that '$op' $at\n";
  549. print "$hereptr";
  550. $clean = 0;
  551. }
  552. # , must have a space on the right.
  553. } elsif ($op eq ',') {
  554. if ($ctx !~ /.xW|.xE/ && $cc ne '}') {
  555. print "need space after that '$op' $at\n";
  556. print "$hereptr";
  557. $clean = 0;
  558. }
  559. # unary ! and unary ~ are allowed no space on the right
  560. } elsif ($op eq '!' or $op eq '~') {
  561. if ($ctx !~ /[WOEB]x./) {
  562. print "need space before that '$op' $at\n";
  563. print "$hereptr";
  564. $clean = 0;
  565. }
  566. if ($ctx =~ /.xW/) {
  567. print "no space after that '$op' $at\n";
  568. print "$hereptr";
  569. $clean = 0;
  570. }
  571. # unary ++ and unary -- are allowed no space on one side.
  572. } elsif ($op eq '++' or $op eq '--') {
  573. if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOB]/) {
  574. print "need space one side of that '$op' $at\n";
  575. print "$hereptr";
  576. $clean = 0;
  577. }
  578. if ($ctx =~ /Wx./ && $cc eq ';') {
  579. print "no space before that '$op' $at\n";
  580. print "$hereptr";
  581. $clean = 0;
  582. }
  583. # & is both unary and binary
  584. # unary:
  585. # a &b
  586. # binary (consistent spacing):
  587. # a&b OK
  588. # a & b OK
  589. #
  590. # boiling down to: if there is a space on the right then there
  591. # should be one on the left.
  592. #
  593. # - is the same
  594. #
  595. } elsif ($op eq '&' or $op eq '-') {
  596. if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
  597. print "need space before that '$op' $at\n";
  598. print "$hereptr";
  599. $clean = 0;
  600. }
  601. # * is the same as & only adding:
  602. # type:
  603. # (foo *)
  604. # (foo **)
  605. #
  606. } elsif ($op eq '*') {
  607. if ($ca eq '*') {
  608. if ($cc =~ /\s/) {
  609. print "no space after that '$op' $at\n";
  610. print "$hereptr";
  611. $clean = 0;
  612. }
  613. } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
  614. print "need space before that '$op' $at\n";
  615. print "$hereptr";
  616. $clean = 0;
  617. }
  618. # << and >> may either have or not have spaces both sides
  619. } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
  620. $op eq '^' or $op eq '|')
  621. {
  622. if ($ctx !~ /VxV|WxW|VxE|WxE/) {
  623. print "need consistent spacing around '$op' $at\n";
  624. print "$hereptr";
  625. $clean = 0;
  626. }
  627. # All the others need spaces both sides.
  628. } elsif ($ctx !~ /[EW]x[WE]/) {
  629. print "need spaces around that '$op' $at\n";
  630. print "$hereptr";
  631. $clean = 0;
  632. }
  633. $off += length($elements[$n + 1]);
  634. }
  635. }
  636. #need space before brace following if, while, etc
  637. if ($line=~/\(.*\){/) {
  638. print "need a space before the brace\n";
  639. print "$herecurr";
  640. $clean = 0;
  641. }
  642. #goto labels aren't indented, allow a single space however
  643. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  644. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  645. print "labels should not be indented\n";
  646. print "$herecurr";
  647. $clean = 0;
  648. }
  649. # Need a space before open parenthesis after if, while etc
  650. if ($line=~/\b(if|while|for|switch)\(/) {
  651. print "need a space before the open parenthesis\n";
  652. print "$herecurr";
  653. $clean = 0;
  654. }
  655. # Check for illegal assignment in if conditional.
  656. if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
  657. #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
  658. print "do not use assignment in if condition\n";
  659. print "$herecurr";
  660. $clean = 0;
  661. }
  662. # Check for }<nl>else {, these must be at the same
  663. # indent level to be relevant to each other.
  664. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  665. $previndent == $indent) {
  666. print "else should follow close brace\n";
  667. print "$hereprev";
  668. $clean = 0;
  669. }
  670. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  671. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  672. # print "No studly caps, use _\n";
  673. # print "$herecurr";
  674. # $clean = 0;
  675. # }
  676. #no spaces allowed after \ in define
  677. if ($line=~/\#define.*\\\s$/) {
  678. print("Whitepspace after \\ makes next lines useless\n");
  679. print "$herecurr";
  680. $clean = 0;
  681. }
  682. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  683. if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
  684. my $checkfile = "include/linux/$1.h";
  685. if (-f $checkfile) {
  686. print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
  687. print $herecurr;
  688. $clean = 0;
  689. }
  690. }
  691. # if/while/etc brace do not go on next line, unless defining a do while loop,
  692. # or if that brace on the next line is for something else
  693. if ($prevline=~/\b(if|while|for|switch)\s*\(/) {
  694. my @opened = $prevline=~/\(/g;
  695. my @closed = $prevline=~/\)/g;
  696. my $nr_line = $linenr;
  697. my $remaining = $realcnt - 1;
  698. my $next_line = $line;
  699. my $extra_lines = 0;
  700. my $display_segment = $prevline;
  701. while ($remaining > 0 && scalar @opened > scalar @closed) {
  702. $prevline .= $next_line;
  703. $display_segment .= "\n" . $next_line;
  704. $next_line = $lines[$nr_line];
  705. $nr_line++;
  706. $remaining--;
  707. @opened = $prevline=~/\(/g;
  708. @closed = $prevline=~/\)/g;
  709. }
  710. if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
  711. !($next_line=~/\b(if|while|for|switch)/) and !($next_line=~/\#define.*do.*while/)) {
  712. print "That { should be on the previous line\n";
  713. print "$here\n$display_segment\n$next_line\n\n";
  714. $clean = 0;
  715. }
  716. }
  717. # multi-statement macros should be enclosed in a do while loop, grab the
  718. # first statement and ensure its the whole macro if its not enclosed
  719. # in a known goot container
  720. if (($prevline=~/\#define.*\\/) and
  721. !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
  722. !($line=~/do.*{/) and !($line=~/\(\{/) and
  723. !($line=~/^.\s*$Declare\s/)) {
  724. # Grab the first statement, if that is the entire macro
  725. # its ok. This may start either on the #define line
  726. # or the one below.
  727. my $ctx1 = join('', ctx_statement($linenr - 1, $realcnt + 1));
  728. my $ctx2 = join('', ctx_statement($linenr, $realcnt));
  729. if ($ctx1 =~ /\\$/ && $ctx2 =~ /\\$/) {
  730. print "Macros with multiple statements should be enclosed in a do - while loop\n";
  731. print "$hereprev";
  732. $clean = 0;
  733. }
  734. }
  735. # don't include deprecated include files (uses RAW line)
  736. for my $inc (@dep_includes) {
  737. if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
  738. print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
  739. print "$herecurr";
  740. $clean = 0;
  741. }
  742. }
  743. # don't use deprecated functions
  744. for my $func (@dep_functions) {
  745. if ($line =~ /\b$func\b/) {
  746. print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
  747. print "$herecurr";
  748. $clean = 0;
  749. }
  750. }
  751. # no volatiles please
  752. if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
  753. print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
  754. print "$herecurr";
  755. $clean = 0;
  756. }
  757. # warn about #if 0
  758. if ($line =~ /^.#\s*if\s+0\b/) {
  759. print "#if 0 -- if this code redundant remove it\n";
  760. print "$herecurr";
  761. $clean = 0;
  762. }
  763. # warn about #ifdefs in C files
  764. # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  765. # print "#ifdef in C files should be avoided\n";
  766. # print "$herecurr";
  767. # $clean = 0;
  768. # }
  769. # check for spinlock_t definitions without a comment.
  770. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
  771. my $which = $1;
  772. if (!ctx_has_comment($first_line, $linenr)) {
  773. print "$1 definition without comment\n";
  774. print "$herecurr";
  775. $clean = 0;
  776. }
  777. }
  778. # check for memory barriers without a comment.
  779. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  780. if (!ctx_has_comment($first_line, $linenr)) {
  781. print "memory barrier without comment\n";
  782. print "$herecurr";
  783. $clean = 0;
  784. }
  785. }
  786. # check of hardware specific defines
  787. if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
  788. print "architecture specific defines should be avoided\n";
  789. print "$herecurr";
  790. $clean = 0;
  791. }
  792. if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
  793. $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
  794. print "inline keyword should sit between storage class and type\n";
  795. print "$herecurr";
  796. $clean = 0;
  797. }
  798. }
  799. if ($chk_patch && !$is_patch) {
  800. $clean = 0;
  801. print "Does not appear to be a unified-diff format patch\n";
  802. }
  803. if ($is_patch && $chk_signoff && $signoff == 0) {
  804. $clean = 0;
  805. print "Missing Signed-off-by: line(s)\n";
  806. }
  807. if ($clean == 1 && $quiet == 0) {
  808. print "Your patch has no obvious style problems and is ready for submission.\n"
  809. }
  810. if ($clean == 0 && $quiet == 0) {
  811. print "Your patch has style problems, please review. If any of these errors\n";
  812. print "are false positives report them to the maintainer, see\n";
  813. print "CHECKPATCH in MAINTAINERS.\n";
  814. }
  815. return $clean;
  816. }