checkpatch.pl 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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.10';
  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_statement_level {
  179. my ($linenr, $remain, $off) = @_;
  180. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  181. }
  182. sub ctx_locate_comment {
  183. my ($first_line, $end_line) = @_;
  184. # Catch a comment on the end of the line itself.
  185. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
  186. return $current_comment if (defined $current_comment);
  187. # Look through the context and try and figure out if there is a
  188. # comment.
  189. my $in_comment = 0;
  190. $current_comment = '';
  191. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  192. my $line = $rawlines[$linenr - 1];
  193. #warn " $line\n";
  194. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  195. $in_comment = 1;
  196. }
  197. if ($line =~ m@/\*@) {
  198. $in_comment = 1;
  199. }
  200. if (!$in_comment && $current_comment ne '') {
  201. $current_comment = '';
  202. }
  203. $current_comment .= $line . "\n" if ($in_comment);
  204. if ($line =~ m@\*/@) {
  205. $in_comment = 0;
  206. }
  207. }
  208. chomp($current_comment);
  209. return($current_comment);
  210. }
  211. sub ctx_has_comment {
  212. my ($first_line, $end_line) = @_;
  213. my $cmt = ctx_locate_comment($first_line, $end_line);
  214. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  215. ##print "CMMT: $cmt\n";
  216. return ($cmt ne '');
  217. }
  218. sub ctx_expr_before {
  219. my ($line) = @_;
  220. ##print "CHECK<$line>\n";
  221. my $pos = length($line) - 1;
  222. my $count = 0;
  223. my $c;
  224. for (; $pos >= 0; $pos--) {
  225. $c = substr($line, $pos, 1);
  226. ##print "CHECK: c<$c> count<$count>\n";
  227. if ($c eq ')') {
  228. $count++;
  229. } elsif ($c eq '(') {
  230. last if (--$count == 0);
  231. }
  232. }
  233. ##print "CHECK: result<" . substr($line, 0, $pos) . ">\n";
  234. return substr($line, 0, $pos);
  235. }
  236. sub cat_vet {
  237. my ($vet) = @_;
  238. my ($res, $coded);
  239. $res = '';
  240. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]])/g) {
  241. $coded = sprintf("^%c", unpack('C', $2) + 64);
  242. $res .= $1 . $coded;
  243. }
  244. $res =~ s/$/\$/;
  245. return $res;
  246. }
  247. my @report = ();
  248. sub report {
  249. push(@report, $_[0]);
  250. }
  251. sub report_dump {
  252. @report;
  253. }
  254. sub ERROR {
  255. report("ERROR: $_[0]\n");
  256. our $clean = 0;
  257. }
  258. sub WARN {
  259. report("WARNING: $_[0]\n");
  260. our $clean = 0;
  261. }
  262. sub CHK {
  263. report("CHECK: $_[0]\n");
  264. our $clean = 0;
  265. }
  266. sub process {
  267. my $filename = shift;
  268. my @lines = @_;
  269. my $linenr=0;
  270. my $prevline="";
  271. my $stashline="";
  272. my $length;
  273. my $indent;
  274. my $previndent=0;
  275. my $stashindent=0;
  276. our $clean = 1;
  277. my $signoff = 0;
  278. my $is_patch = 0;
  279. # Trace the real file/line as we go.
  280. my $realfile = '';
  281. my $realline = 0;
  282. my $realcnt = 0;
  283. my $here = '';
  284. my $in_comment = 0;
  285. my $first_line = 0;
  286. my $Ident = qr{[A-Za-z\d_]+};
  287. my $Storage = qr{extern|static|asmlinkage};
  288. my $Sparse = qr{
  289. __user|
  290. __kernel|
  291. __force|
  292. __iomem|
  293. __must_check|
  294. __init_refok|
  295. fastcall
  296. }x;
  297. my $Inline = qr{inline|__always_inline|noinline};
  298. my $NonptrType = qr{
  299. \b
  300. (?:const\s+)?
  301. (?:unsigned\s+)?
  302. (?:
  303. void|
  304. char|
  305. short|
  306. int|
  307. long|
  308. unsigned|
  309. float|
  310. double|
  311. bool|
  312. long\s+int|
  313. long\s+long|
  314. long\s+long\s+int|
  315. u8|u16|u32|u64|
  316. s8|s16|s32|s64|
  317. struct\s+$Ident|
  318. union\s+$Ident|
  319. enum\s+$Ident|
  320. ${Ident}_t
  321. )
  322. (?:\s+$Sparse)*
  323. \b
  324. }x;
  325. my $Type = qr{
  326. \b$NonptrType\b
  327. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  328. (?:\s+$Sparse)*
  329. }x;
  330. my $Declare = qr{(?:$Storage\s+)?$Type};
  331. my $Attribute = qr{
  332. const|
  333. __read_mostly|
  334. __(?:mem|cpu|dev|)(?:initdata|init)
  335. }x;
  336. my $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  337. my $Lval = qr{$Ident(?:$Member)*};
  338. # Possible bare types.
  339. my @bare = ();
  340. my $Bare = $NonptrType;
  341. # Pre-scan the patch looking for any __setup documentation.
  342. my @setup_docs = ();
  343. my $setup_docs = 0;
  344. foreach my $line (@lines) {
  345. if ($line=~/^\+\+\+\s+(\S+)/) {
  346. $setup_docs = 0;
  347. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  348. $setup_docs = 1;
  349. }
  350. next;
  351. }
  352. if ($setup_docs && $line =~ /^\+/) {
  353. push(@setup_docs, $line);
  354. }
  355. }
  356. foreach my $line (@lines) {
  357. $linenr++;
  358. my $rawline = $line;
  359. #extract the filename as it passes
  360. if ($line=~/^\+\+\+\s+(\S+)/) {
  361. $realfile=$1;
  362. $realfile =~ s@^[^/]*/@@;
  363. $in_comment = 0;
  364. next;
  365. }
  366. #extract the line range in the file after the patch is applied
  367. if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
  368. $is_patch = 1;
  369. $first_line = $linenr + 1;
  370. $in_comment = 0;
  371. $realline=$1-1;
  372. if (defined $2) {
  373. $realcnt=$3+1;
  374. } else {
  375. $realcnt=1+1;
  376. }
  377. next;
  378. }
  379. # track the line number as we move through the hunk, note that
  380. # new versions of GNU diff omit the leading space on completely
  381. # blank context lines so we need to count that too.
  382. if ($line =~ /^( |\+|$)/) {
  383. $realline++;
  384. $realcnt-- if ($realcnt != 0);
  385. # track any sort of multi-line comment. Obviously if
  386. # the added text or context do not include the whole
  387. # comment we will not see it. Such is life.
  388. #
  389. # Guestimate if this is a continuing comment. If this
  390. # is the start of a diff block and this line starts
  391. # ' *' then it is very likely a comment.
  392. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  393. $in_comment = 1;
  394. }
  395. if ($line =~ m@/\*@) {
  396. $in_comment = 1;
  397. }
  398. if ($line =~ m@\*/@) {
  399. $in_comment = 0;
  400. }
  401. # Measure the line length and indent.
  402. ($length, $indent) = line_stats($line);
  403. # Track the previous line.
  404. ($prevline, $stashline) = ($stashline, $line);
  405. ($previndent, $stashindent) = ($stashindent, $indent);
  406. } elsif ($realcnt == 1) {
  407. $realcnt--;
  408. }
  409. #make up the handle for any error we report on this line
  410. $here = "#$linenr: ";
  411. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  412. my $hereline = "$here\n$line\n";
  413. my $herecurr = "$here\n$line\n";
  414. my $hereprev = "$here\n$prevline\n$line\n";
  415. #check the patch for a signoff:
  416. if ($line =~ /^\s*signed-off-by:/i) {
  417. # This is a signoff, if ugly, so do not double report.
  418. $signoff++;
  419. if (!($line =~ /^\s*Signed-off-by:/)) {
  420. WARN("Signed-off-by: is the preferred form\n" .
  421. $herecurr);
  422. }
  423. if ($line =~ /^\s*signed-off-by:\S/i) {
  424. WARN("need space after Signed-off-by:\n" .
  425. $herecurr);
  426. }
  427. }
  428. # Check for wrappage within a valid hunk of the file
  429. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
  430. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  431. $herecurr);
  432. }
  433. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  434. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  435. !($line =~ m/^(
  436. [\x09\x0A\x0D\x20-\x7E] # ASCII
  437. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  438. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  439. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  440. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  441. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  442. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  443. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  444. )*$/x )) {
  445. ERROR("Invalid UTF-8\n" . $herecurr);
  446. }
  447. #ignore lines being removed
  448. if ($line=~/^-/) {next;}
  449. # check we are in a valid source file if not then ignore this hunk
  450. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  451. #trailing whitespace
  452. if ($line =~ /^\+.*\015/) {
  453. my $herevet = "$here\n" . cat_vet($line) . "\n";
  454. ERROR("DOS line endings\n" . $herevet);
  455. } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
  456. my $herevet = "$here\n" . cat_vet($line) . "\n";
  457. ERROR("trailing whitespace\n" . $herevet);
  458. }
  459. #80 column limit
  460. if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
  461. WARN("line over 80 characters\n" . $herecurr);
  462. }
  463. # check we are in a valid source file *.[hc] if not then ignore this hunk
  464. next if ($realfile !~ /\.[hc]$/);
  465. # at the beginning of a line any tabs must come first and anything
  466. # more than 8 must use tabs.
  467. if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
  468. my $herevet = "$here\n" . cat_vet($line) . "\n";
  469. ERROR("use tabs not spaces\n" . $herevet);
  470. }
  471. # Remove comments from the line before processing.
  472. my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
  473. ($line =~ s@/\*.*@@) +
  474. ($line =~ s@^(.).*\*/@$1@);
  475. # The rest of our checks refer specifically to C style
  476. # only apply those _outside_ comments. Only skip
  477. # lines in the middle of comments.
  478. next if (!$comment_edge && $in_comment);
  479. # Standardise the strings and chars within the input to simplify matching.
  480. $line = sanitise_line($line);
  481. # Check for potential 'bare' types
  482. if ($realcnt &&
  483. $line !~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?$Type\b/ &&
  484. $line !~ /$Ident:\s*$/ &&
  485. $line !~ /^.\s*$Ident\s*\(/ &&
  486. ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?($Ident)\b/ ||
  487. $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/)) {
  488. my $possible = $1;
  489. if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
  490. $possible ne 'goto' && $possible ne 'return' &&
  491. $possible ne 'struct' && $possible ne 'enum' &&
  492. $possible ne 'case' && $possible ne 'else' &&
  493. $possible ne 'typedef') {
  494. #print "POSSIBLE<$possible>\n";
  495. push(@bare, $possible);
  496. my $bare = join("|", @bare);
  497. $Bare = qr{
  498. \b(?:$bare)\b
  499. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  500. (?:\s+$Sparse)*
  501. }x;
  502. }
  503. }
  504. #
  505. # Checks which may be anchored in the context.
  506. #
  507. # Check for switch () and associated case and default
  508. # statements should be at the same indent.
  509. if ($line=~/\bswitch\s*\(.*\)/) {
  510. my $err = '';
  511. my $sep = '';
  512. my @ctx = ctx_block_outer($linenr, $realcnt);
  513. shift(@ctx);
  514. for my $ctx (@ctx) {
  515. my ($clen, $cindent) = line_stats($ctx);
  516. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  517. $indent != $cindent) {
  518. $err .= "$sep$ctx\n";
  519. $sep = '';
  520. } else {
  521. $sep = "[...]\n";
  522. }
  523. }
  524. if ($err ne '') {
  525. ERROR("switch and case should be at the same indent\n$hereline$err");
  526. }
  527. }
  528. # if/while/etc brace do not go on next line, unless defining a do while loop,
  529. # or if that brace on the next line is for something else
  530. if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
  531. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  532. my $ctx_ln = $linenr + $#ctx + 1;
  533. my $ctx_cnt = $realcnt - $#ctx - 1;
  534. my $ctx = join("\n", @ctx);
  535. # Skip over any removed lines in the context following statement.
  536. while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
  537. $ctx_ln++;
  538. $ctx_cnt--;
  539. }
  540. ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
  541. if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  542. ERROR("That open brace { should be on the previous line\n" .
  543. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  544. }
  545. if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
  546. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  547. if ($nindent > $indent) {
  548. WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
  549. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  550. }
  551. }
  552. }
  553. #ignore lines not being added
  554. if ($line=~/^[^\+]/) {next;}
  555. # TEST: allow direct testing of the type matcher.
  556. if ($tst_type && $line =~ /^.$Declare$/) {
  557. ERROR("TEST: is type $Declare\n" . $herecurr);
  558. next;
  559. }
  560. # check for initialisation to aggregates open brace on the next line
  561. if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
  562. $line =~ /^.\s*{/) {
  563. ERROR("That open brace { should be on the previous line\n" . $hereprev);
  564. }
  565. #
  566. # Checks which are anchored on the added line.
  567. #
  568. # check for malformed paths in #include statements (uses RAW line)
  569. if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
  570. my $path = $1;
  571. if ($path =~ m{//}) {
  572. ERROR("malformed #include filename\n" .
  573. $herecurr);
  574. }
  575. # Sanitise this special form of string.
  576. $path = 'X' x length($path);
  577. $line =~ s{\<.*\>}{<$path>};
  578. }
  579. # no C99 // comments
  580. if ($line =~ m{//}) {
  581. ERROR("do not use C99 // comments\n" . $herecurr);
  582. }
  583. # Remove C99 comments.
  584. $line =~ s@//.*@@;
  585. #EXPORT_SYMBOL should immediately follow its function closing }.
  586. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  587. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  588. my $name = $1;
  589. if (($prevline !~ /^}/) &&
  590. ($prevline !~ /^\+}/) &&
  591. ($prevline !~ /^ }/) &&
  592. ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
  593. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  594. }
  595. }
  596. # check for external initialisers.
  597. if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
  598. ERROR("do not initialise externals to 0 or NULL\n" .
  599. $herecurr);
  600. }
  601. # check for static initialisers.
  602. if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
  603. ERROR("do not initialise statics to 0 or NULL\n" .
  604. $herecurr);
  605. }
  606. # check for new typedefs, only function parameters and sparse annotations
  607. # make sense.
  608. if ($line =~ /\btypedef\s/ &&
  609. $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
  610. $line !~ /\b__bitwise(?:__|)\b/) {
  611. WARN("do not add new typedefs\n" . $herecurr);
  612. }
  613. # * goes on variable not on type
  614. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  615. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  616. $herecurr);
  617. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  618. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  619. $herecurr);
  620. } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  621. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  622. $herecurr);
  623. } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  624. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  625. $herecurr);
  626. }
  627. # # no BUG() or BUG_ON()
  628. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  629. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  630. # print "$herecurr";
  631. # $clean = 0;
  632. # }
  633. # printk should use KERN_* levels. Note that follow on printk's on the
  634. # same line do not need a level, so we use the current block context
  635. # to try and find and validate the current printk. In summary the current
  636. # printk includes all preceeding printk's which have no newline on the end.
  637. # we assume the first bad printk is the one to report.
  638. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  639. my $ok = 0;
  640. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  641. #print "CHECK<$lines[$ln - 1]\n";
  642. # we have a preceeding printk if it ends
  643. # with "\n" ignore it, else it is to blame
  644. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  645. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  646. $ok = 1;
  647. }
  648. last;
  649. }
  650. }
  651. if ($ok == 0) {
  652. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  653. }
  654. }
  655. # function brace can't be on same line, except for #defines of do while,
  656. # or if closed on same line
  657. if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
  658. !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
  659. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  660. }
  661. # check for spaces between functions and their parentheses.
  662. if ($line =~ /($Ident)\s+\(/ &&
  663. $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ &&
  664. $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
  665. WARN("no space between function name and open parenthesis '('\n" . $herecurr);
  666. }
  667. # Check operator spacing.
  668. # Note we expand the line with the leading + as the real
  669. # line will be displayed with the leading + and the tabs
  670. # will therefore also expand that way.
  671. my $opline = $line;
  672. $opline = expand_tabs($opline);
  673. $opline =~ s/^./ /;
  674. if (!($line=~/\#\s*include/)) {
  675. my $ops = qr{
  676. <<=|>>=|<=|>=|==|!=|
  677. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  678. =>|->|<<|>>|<|>|=|!|~|
  679. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/
  680. }x;
  681. my @elements = split(/($ops|;)/, $opline);
  682. my $off = 0;
  683. for (my $n = 0; $n < $#elements; $n += 2) {
  684. $off += length($elements[$n]);
  685. my $a = '';
  686. $a = 'V' if ($elements[$n] ne '');
  687. $a = 'W' if ($elements[$n] =~ /\s$/);
  688. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  689. $a = 'O' if ($elements[$n] eq '');
  690. $a = 'E' if ($elements[$n] eq '' && $n == 0);
  691. my $op = $elements[$n + 1];
  692. my $c = '';
  693. if (defined $elements[$n + 2]) {
  694. $c = 'V' if ($elements[$n + 2] ne '');
  695. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  696. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  697. $c = 'O' if ($elements[$n + 2] eq '');
  698. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  699. } else {
  700. $c = 'E';
  701. }
  702. # Pick up the preceeding and succeeding characters.
  703. my $ca = substr($opline, 0, $off);
  704. my $cc = '';
  705. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  706. $cc = substr($opline, $off + length($elements[$n + 1]));
  707. }
  708. my $cb = "$ca$;$cc";
  709. my $ctx = "${a}x${c}";
  710. my $at = "(ctx:$ctx)";
  711. my $ptr = (" " x $off) . "^";
  712. my $hereptr = "$hereline$ptr\n";
  713. # Classify operators into binary, unary, or
  714. # definitions (* only) where they have more
  715. # than one mode.
  716. my $unary_ctx = $prevline . $ca;
  717. $unary_ctx =~ s/^./ /;
  718. my $is_unary = 0;
  719. my $Unary = qr{
  720. (?:
  721. ^|;|,|$ops|\(|\?|:|
  722. \(\s*$Type\s*\)|
  723. $Type|
  724. return|case|else|
  725. \{|\}|
  726. \[|
  727. ^.\#\s*define\s+$Ident\s*(?:\([^\)]*\))?|
  728. ^.\#\s*else|
  729. ^.\#\s*endif|
  730. ^.\#\s*(?:if|ifndef|ifdef)\b.*
  731. )\s*(?:|\\)\s*$
  732. }x;
  733. my $UnaryFalse = qr{
  734. sizeof\s*\(\s*$Type\s*\)\s*$
  735. }x;
  736. my $UnaryDefine = qr{
  737. (?:$Type|$Bare)\s*|
  738. (?:$Type|$Bare).*,\s*\**
  739. }x;
  740. if ($op eq '-' || $op eq '&' || $op eq '*') {
  741. # An operator is binary if the left hand
  742. # side is a value. Pick out the known
  743. # non-values.
  744. if ($unary_ctx =~ /$Unary$/s &&
  745. $unary_ctx !~ /$UnaryFalse$/s) {
  746. $is_unary = 1;
  747. # Special handling for ')' check if this
  748. # brace represents a conditional, if so
  749. # we are unary.
  750. } elsif ($unary_ctx =~ /\)\s*$/) {
  751. my $before = ctx_expr_before($unary_ctx);
  752. if ($before =~ /(?:for|if|while)\s*$/) {
  753. $is_unary = 1;
  754. }
  755. }
  756. # Check for type definition for of '*'.
  757. if ($op eq '*' && $unary_ctx =~ /$UnaryDefine$/) {
  758. $is_unary = 2;
  759. }
  760. }
  761. #if ($op eq '-' || $op eq '&' || $op eq '*') {
  762. # print "UNARY: <$is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
  763. #}
  764. # ; should have either the end of line or a space or \ after it
  765. if ($op eq ';') {
  766. if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
  767. $cc !~ /^;/) {
  768. ERROR("need space after that '$op' $at\n" . $hereptr);
  769. }
  770. # // is a comment
  771. } elsif ($op eq '//') {
  772. # -> should have no spaces
  773. } elsif ($op eq '->') {
  774. if ($ctx =~ /Wx.|.xW/) {
  775. ERROR("no spaces around that '$op' $at\n" . $hereptr);
  776. }
  777. # , must have a space on the right.
  778. } elsif ($op eq ',') {
  779. if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
  780. ERROR("need space after that '$op' $at\n" . $hereptr);
  781. }
  782. # '*' as part of a type definition -- reported already.
  783. } elsif ($op eq '*' && $is_unary == 2) {
  784. #warn "'*' is part of type\n";
  785. # unary operators should have a space before and
  786. # none after. May be left adjacent to another
  787. # unary operator, or a cast
  788. } elsif ($op eq '!' || $op eq '~' ||
  789. ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
  790. if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  791. ERROR("need space before that '$op' $at\n" . $hereptr);
  792. }
  793. if ($ctx =~ /.xW/) {
  794. ERROR("no space after that '$op' $at\n" . $hereptr);
  795. }
  796. # unary ++ and unary -- are allowed no space on one side.
  797. } elsif ($op eq '++' or $op eq '--') {
  798. if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
  799. ERROR("need space one side of that '$op' $at\n" . $hereptr);
  800. }
  801. if ($ctx =~ /Wx./ && $cc =~ /^;/) {
  802. ERROR("no space before that '$op' $at\n" . $hereptr);
  803. }
  804. # << and >> may either have or not have spaces both sides
  805. } elsif ($op eq '<<' or $op eq '>>' or
  806. $op eq '&' or $op eq '^' or $op eq '|' or
  807. $op eq '+' or $op eq '-' or
  808. $op eq '*' or $op eq '/')
  809. {
  810. if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
  811. ERROR("need consistent spacing around '$op' $at\n" .
  812. $hereptr);
  813. }
  814. # All the others need spaces both sides.
  815. } elsif ($ctx !~ /[EW]x[WE]/) {
  816. # Ignore email addresses <foo@bar>
  817. if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
  818. !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
  819. ERROR("need spaces around that '$op' $at\n" . $hereptr);
  820. }
  821. }
  822. $off += length($elements[$n + 1]);
  823. }
  824. }
  825. # check for multiple assignments
  826. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  827. WARN("multiple assignments should be avoided\n" . $herecurr);
  828. }
  829. ## # check for multiple declarations, allowing for a function declaration
  830. ## # continuation.
  831. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  832. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  833. ##
  834. ## # Remove any bracketed sections to ensure we do not
  835. ## # falsly report the parameters of functions.
  836. ## my $ln = $line;
  837. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  838. ## }
  839. ## if ($ln =~ /,/) {
  840. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  841. ## }
  842. ## }
  843. #need space before brace following if, while, etc
  844. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  845. $line =~ /do{/) {
  846. ERROR("need a space before the open brace '{'\n" . $herecurr);
  847. }
  848. # closing brace should have a space following it when it has anything
  849. # on the line
  850. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  851. ERROR("need a space after that close brace '}'\n" . $herecurr);
  852. }
  853. # check spacing on square brackets
  854. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  855. ERROR("no space after that open square bracket '['\n" . $herecurr);
  856. }
  857. if ($line =~ /\s\]/) {
  858. ERROR("no space before that close square bracket ']'\n" . $herecurr);
  859. }
  860. # check spacing on paretheses
  861. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  862. $line !~ /for\s*\(\s+;/) {
  863. ERROR("no space after that open parenthesis '('\n" . $herecurr);
  864. }
  865. if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ &&
  866. $line !~ /for\s*\(.*;\s+\)/) {
  867. ERROR("no space before that close parenthesis ')'\n" . $herecurr);
  868. }
  869. #goto labels aren't indented, allow a single space however
  870. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  871. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  872. WARN("labels should not be indented\n" . $herecurr);
  873. }
  874. # Need a space before open parenthesis after if, while etc
  875. if ($line=~/\b(if|while|for|switch)\(/) {
  876. ERROR("need a space before the open parenthesis '('\n" . $herecurr);
  877. }
  878. # Check for illegal assignment in if conditional.
  879. if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
  880. #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
  881. ERROR("do not use assignment in if condition\n" . $herecurr);
  882. }
  883. # Check for }<nl>else {, these must be at the same
  884. # indent level to be relevant to each other.
  885. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  886. $previndent == $indent) {
  887. ERROR("else should follow close brace '}'\n" . $hereprev);
  888. }
  889. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  890. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  891. # print "No studly caps, use _\n";
  892. # print "$herecurr";
  893. # $clean = 0;
  894. # }
  895. #no spaces allowed after \ in define
  896. if ($line=~/\#define.*\\\s$/) {
  897. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  898. }
  899. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  900. if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
  901. my $checkfile = "include/linux/$1.h";
  902. if (-f $checkfile) {
  903. CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
  904. $herecurr);
  905. }
  906. }
  907. # if and else should not have general statements after it
  908. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
  909. $1 !~ /^\s*(?:\sif|{|\\|$)/) {
  910. ERROR("trailing statements should be on next line\n" . $herecurr);
  911. }
  912. # multi-statement macros should be enclosed in a do while loop, grab the
  913. # first statement and ensure its the whole macro if its not enclosed
  914. # in a known goot container
  915. if ($prevline =~ /\#define.*\\/ &&
  916. $prevline !~/(?:do\s+{|\(\{|\{)/ &&
  917. $line !~ /(?:do\s+{|\(\{|\{)/ &&
  918. $line !~ /^.\s*$Declare\s/) {
  919. # Grab the first statement, if that is the entire macro
  920. # its ok. This may start either on the #define line
  921. # or the one below.
  922. my $ln = $linenr;
  923. my $cnt = $realcnt;
  924. my $off = 0;
  925. # If the macro starts on the define line start
  926. # grabbing the statement after the identifier
  927. $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
  928. ##print "1<$1> 2<$2>\n";
  929. if (defined $2 && $2 ne '') {
  930. $off = length($1);
  931. $ln--;
  932. $cnt++;
  933. }
  934. my @ctx = ctx_statement($ln, $cnt, $off);
  935. my $ctx_ln = $ln + $#ctx + 1;
  936. my $ctx = join("\n", @ctx);
  937. # Pull in any empty extension lines.
  938. while ($ctx =~ /\\$/ &&
  939. $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
  940. $ctx .= $lines[$ctx_ln - 1];
  941. $ctx_ln++;
  942. }
  943. if ($ctx =~ /\\$/) {
  944. if ($ctx =~ /;/) {
  945. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  946. } else {
  947. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  948. }
  949. }
  950. }
  951. # check for redundant bracing round if etc
  952. if ($line =~ /\b(if|while|for|else)\b/) {
  953. # Locate the end of the opening statement.
  954. my @control = ctx_statement($linenr, $realcnt, 0);
  955. my $nr = $linenr + (scalar(@control) - 1);
  956. my $cnt = $realcnt - (scalar(@control) - 1);
  957. my $off = $realcnt - $cnt;
  958. #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
  959. # If this is is a braced statement group check it
  960. if ($lines[$nr - 1] =~ /{\s*$/) {
  961. my ($lvl, @block) = ctx_block_level($nr, $cnt);
  962. my $stmt = join(' ', @block);
  963. $stmt =~ s/(^[^{]*){//;
  964. my $before = $1;
  965. $stmt =~ s/}([^}]*$)//;
  966. my $after = $1;
  967. #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
  968. #print "stmt<$stmt>\n\n";
  969. # Count the ;'s if there is fewer than two
  970. # then there can only be one statement,
  971. # if there is a brace inside we cannot
  972. # trivially detect if its one statement.
  973. # Also nested if's often require braces to
  974. # disambiguate the else binding so shhh there.
  975. my @semi = ($stmt =~ /;/g);
  976. push(@semi, "/**/") if ($stmt =~ m@/\*@);
  977. ##print "semi<" . scalar(@semi) . ">\n";
  978. if ($lvl == 0 && scalar(@semi) < 2 &&
  979. $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
  980. $before !~ /}/ && $after !~ /{/) {
  981. my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
  982. shift(@block);
  983. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  984. }
  985. }
  986. }
  987. # don't include deprecated include files (uses RAW line)
  988. for my $inc (@dep_includes) {
  989. if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
  990. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  991. }
  992. }
  993. # don't use deprecated functions
  994. for my $func (@dep_functions) {
  995. if ($line =~ /\b$func\b/) {
  996. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  997. }
  998. }
  999. # no volatiles please
  1000. if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
  1001. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1002. }
  1003. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1004. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1005. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1006. }
  1007. # warn about #if 0
  1008. if ($line =~ /^.#\s*if\s+0\b/) {
  1009. CHK("if this code is redundant consider removing it\n" .
  1010. $herecurr);
  1011. }
  1012. # check for needless kfree() checks
  1013. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1014. my $expr = $1;
  1015. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1016. WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
  1017. }
  1018. }
  1019. # warn about #ifdefs in C files
  1020. # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1021. # print "#ifdef in C files should be avoided\n";
  1022. # print "$herecurr";
  1023. # $clean = 0;
  1024. # }
  1025. # warn about spacing in #ifdefs
  1026. if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
  1027. ERROR("exactly one space required after that #$1\n" . $herecurr);
  1028. }
  1029. # check for spinlock_t definitions without a comment.
  1030. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
  1031. my $which = $1;
  1032. if (!ctx_has_comment($first_line, $linenr)) {
  1033. CHK("$1 definition without comment\n" . $herecurr);
  1034. }
  1035. }
  1036. # check for memory barriers without a comment.
  1037. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  1038. if (!ctx_has_comment($first_line, $linenr)) {
  1039. CHK("memory barrier without comment\n" . $herecurr);
  1040. }
  1041. }
  1042. # check of hardware specific defines
  1043. if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  1044. CHK("architecture specific defines should be avoided\n" . $herecurr);
  1045. }
  1046. # check the location of the inline attribute, that it is between
  1047. # storage class and type.
  1048. if ($line =~ /\b$Type\s+$Inline\b/ ||
  1049. $line =~ /\b$Inline\s+$Storage\b/) {
  1050. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  1051. }
  1052. # check for new externs in .c files.
  1053. if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
  1054. WARN("externs should be avoided in .c files\n" . $herecurr);
  1055. }
  1056. # checks for new __setup's
  1057. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  1058. my $name = $1;
  1059. if (!grep(/$name/, @setup_docs)) {
  1060. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  1061. }
  1062. }
  1063. # check for pointless casting of kmalloc return
  1064. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  1065. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  1066. }
  1067. }
  1068. if ($chk_patch && !$is_patch) {
  1069. ERROR("Does not appear to be a unified-diff format patch\n");
  1070. }
  1071. if ($is_patch && $chk_signoff && $signoff == 0) {
  1072. ERROR("Missing Signed-off-by: line(s)\n");
  1073. }
  1074. if ($clean == 0 && ($chk_patch || $is_patch)) {
  1075. print report_dump();
  1076. }
  1077. if ($clean == 1 && $quiet == 0) {
  1078. print "Your patch has no obvious style problems and is ready for submission.\n"
  1079. }
  1080. if ($clean == 0 && $quiet == 0) {
  1081. print "Your patch has style problems, please review. If any of these errors\n";
  1082. print "are false positives report them to the maintainer, see\n";
  1083. print "CHECKPATCH in MAINTAINERS.\n";
  1084. }
  1085. return $clean;
  1086. }