checkpatch.pl 30 KB

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