checkpatch.pl 25 KB

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