checkpatch.pl 29 KB

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