checkpatch.pl 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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.12';
  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. my $emacs = 0;
  17. my $terse = 0;
  18. my $file = 0;
  19. my $check = 0;
  20. my $summary = 1;
  21. my $mailback = 0;
  22. my $root;
  23. GetOptions(
  24. 'q|quiet+' => \$quiet,
  25. 'tree!' => \$tree,
  26. 'signoff!' => \$chk_signoff,
  27. 'patch!' => \$chk_patch,
  28. 'test-type!' => \$tst_type,
  29. 'emacs!' => \$emacs,
  30. 'terse!' => \$terse,
  31. 'file!' => \$file,
  32. 'subjective!' => \$check,
  33. 'strict!' => \$check,
  34. 'root=s' => \$root,
  35. 'summary!' => \$summary,
  36. 'mailback!' => \$mailback,
  37. ) or exit;
  38. my $exit = 0;
  39. if ($#ARGV < 0) {
  40. print "usage: $P [options] patchfile\n";
  41. print "version: $V\n";
  42. print "options: -q => quiet\n";
  43. print " --no-tree => run without a kernel tree\n";
  44. print " --terse => one line per report\n";
  45. print " --emacs => emacs compile window format\n";
  46. print " --file => check a source file\n";
  47. print " --strict => enable more subjective tests\n";
  48. print " --root => path to the kernel tree root\n";
  49. exit(1);
  50. }
  51. if ($terse) {
  52. $emacs = 1;
  53. $quiet++;
  54. }
  55. if ($tree) {
  56. if (defined $root) {
  57. if (!top_of_kernel_tree($root)) {
  58. die "$P: $root: --root does not point at a valid tree\n";
  59. }
  60. } else {
  61. if (top_of_kernel_tree('.')) {
  62. $root = '.';
  63. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  64. top_of_kernel_tree($1)) {
  65. $root = $1;
  66. }
  67. }
  68. if (!defined $root) {
  69. print "Must be run from the top-level dir. of a kernel tree\n";
  70. exit(2);
  71. }
  72. }
  73. my $emitted_corrupt = 0;
  74. our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
  75. our $Storage = qr{extern|static|asmlinkage};
  76. our $Sparse = qr{
  77. __user|
  78. __kernel|
  79. __force|
  80. __iomem|
  81. __must_check|
  82. __init_refok|
  83. __kprobes|
  84. fastcall
  85. }x;
  86. our $Attribute = qr{
  87. const|
  88. __read_mostly|
  89. __kprobes|
  90. __(?:mem|cpu|dev|)(?:initdata|init)
  91. }x;
  92. our $Inline = qr{inline|__always_inline|noinline};
  93. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  94. our $Lval = qr{$Ident(?:$Member)*};
  95. our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
  96. our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
  97. our $Operators = qr{
  98. <=|>=|==|!=|
  99. =>|->|<<|>>|<|>|!|~|
  100. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/
  101. }x;
  102. our $NonptrType;
  103. our $Type;
  104. our $Declare;
  105. our @typeList = (
  106. qr{void},
  107. qr{char},
  108. qr{short},
  109. qr{int},
  110. qr{long},
  111. qr{unsigned},
  112. qr{float},
  113. qr{double},
  114. qr{bool},
  115. qr{long\s+int},
  116. qr{long\s+long},
  117. qr{long\s+long\s+int},
  118. qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
  119. qr{struct\s+$Ident},
  120. qr{union\s+$Ident},
  121. qr{enum\s+$Ident},
  122. qr{${Ident}_t},
  123. qr{${Ident}_handler},
  124. qr{${Ident}_handler_fn},
  125. );
  126. sub build_types {
  127. my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
  128. $NonptrType = qr{
  129. \b
  130. (?:const\s+)?
  131. (?:unsigned\s+)?
  132. $all
  133. (?:\s+$Sparse|\s+const)*
  134. \b
  135. }x;
  136. $Type = qr{
  137. \b$NonptrType\b
  138. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  139. (?:\s+$Sparse|\s+$Attribute)*
  140. }x;
  141. $Declare = qr{(?:$Storage\s+)?$Type};
  142. }
  143. build_types();
  144. $chk_signoff = 0 if ($file);
  145. my @dep_includes = ();
  146. my @dep_functions = ();
  147. my $removal = "Documentation/feature-removal-schedule.txt";
  148. if ($tree && -f "$root/$removal") {
  149. open(REMOVE, "<$root/$removal") ||
  150. die "$P: $removal: open failed - $!\n";
  151. while (<REMOVE>) {
  152. if (/^Check:\s+(.*\S)/) {
  153. for my $entry (split(/[, ]+/, $1)) {
  154. if ($entry =~ m@include/(.*)@) {
  155. push(@dep_includes, $1);
  156. } elsif ($entry !~ m@/@) {
  157. push(@dep_functions, $entry);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. my @rawlines = ();
  164. for my $filename (@ARGV) {
  165. if ($file) {
  166. open(FILE, "diff -u /dev/null $filename|") ||
  167. die "$P: $filename: diff failed - $!\n";
  168. } else {
  169. open(FILE, "<$filename") ||
  170. die "$P: $filename: open failed - $!\n";
  171. }
  172. while (<FILE>) {
  173. chomp;
  174. push(@rawlines, $_);
  175. }
  176. close(FILE);
  177. if (!process($filename, @rawlines)) {
  178. $exit = 1;
  179. }
  180. @rawlines = ();
  181. }
  182. exit($exit);
  183. sub top_of_kernel_tree {
  184. my ($root) = @_;
  185. my @tree_check = (
  186. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  187. "README", "Documentation", "arch", "include", "drivers",
  188. "fs", "init", "ipc", "kernel", "lib", "scripts",
  189. );
  190. foreach my $check (@tree_check) {
  191. if (! -e $root . '/' . $check) {
  192. return 0;
  193. }
  194. }
  195. return 1;
  196. }
  197. sub expand_tabs {
  198. my ($str) = @_;
  199. my $res = '';
  200. my $n = 0;
  201. for my $c (split(//, $str)) {
  202. if ($c eq "\t") {
  203. $res .= ' ';
  204. $n++;
  205. for (; ($n % 8) != 0; $n++) {
  206. $res .= ' ';
  207. }
  208. next;
  209. }
  210. $res .= $c;
  211. $n++;
  212. }
  213. return $res;
  214. }
  215. sub copy_spacing {
  216. my ($str) = @_;
  217. my $res = '';
  218. for my $c (split(//, $str)) {
  219. if ($c eq "\t") {
  220. $res .= $c;
  221. } else {
  222. $res .= ' ';
  223. }
  224. }
  225. return $res;
  226. }
  227. sub line_stats {
  228. my ($line) = @_;
  229. # Drop the diff line leader and expand tabs
  230. $line =~ s/^.//;
  231. $line = expand_tabs($line);
  232. # Pick the indent from the front of the line.
  233. my ($white) = ($line =~ /^(\s*)/);
  234. return (length($line), length($white));
  235. }
  236. sub sanitise_line {
  237. my ($line) = @_;
  238. my $res = '';
  239. my $l = '';
  240. my $quote = '';
  241. foreach my $c (split(//, $line)) {
  242. if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
  243. if ($quote eq '') {
  244. $quote = $c;
  245. $res .= $c;
  246. $l = $c;
  247. next;
  248. } elsif ($quote eq $c) {
  249. $quote = '';
  250. }
  251. }
  252. if ($quote && $c ne "\t") {
  253. $res .= "X";
  254. } else {
  255. $res .= $c;
  256. }
  257. $l = $c;
  258. }
  259. return $res;
  260. }
  261. sub ctx_statement_block {
  262. my ($linenr, $remain, $off) = @_;
  263. my $line = $linenr - 1;
  264. my $blk = '';
  265. my $soff = $off;
  266. my $coff = $off - 1;
  267. my $type = '';
  268. my $level = 0;
  269. my $c;
  270. my $len = 0;
  271. while (1) {
  272. #warn "CSB: blk<$blk>\n";
  273. # If we are about to drop off the end, pull in more
  274. # context.
  275. if ($off >= $len) {
  276. for (; $remain > 0; $line++) {
  277. next if ($rawlines[$line] =~ /^-/);
  278. $remain--;
  279. $blk .= sanitise_line($rawlines[$line]) . "\n";
  280. $len = length($blk);
  281. $line++;
  282. last;
  283. }
  284. # Bail if there is no further context.
  285. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  286. if ($off == $len) {
  287. last;
  288. }
  289. }
  290. $c = substr($blk, $off, 1);
  291. #warn "CSB: c<$c> type<$type> level<$level>\n";
  292. # Statement ends at the ';' or a close '}' at the
  293. # outermost level.
  294. if ($level == 0 && $c eq ';') {
  295. last;
  296. }
  297. if (($type eq '' || $type eq '(') && $c eq '(') {
  298. $level++;
  299. $type = '(';
  300. }
  301. if ($type eq '(' && $c eq ')') {
  302. $level--;
  303. $type = ($level != 0)? '(' : '';
  304. if ($level == 0 && $coff < $soff) {
  305. $coff = $off;
  306. }
  307. }
  308. if (($type eq '' || $type eq '{') && $c eq '{') {
  309. $level++;
  310. $type = '{';
  311. }
  312. if ($type eq '{' && $c eq '}') {
  313. $level--;
  314. $type = ($level != 0)? '{' : '';
  315. if ($level == 0) {
  316. last;
  317. }
  318. }
  319. $off++;
  320. }
  321. my $statement = substr($blk, $soff, $off - $soff + 1);
  322. my $condition = substr($blk, $soff, $coff - $soff + 1);
  323. #warn "STATEMENT<$statement>\n";
  324. #warn "CONDITION<$condition>\n";
  325. return ($statement, $condition);
  326. }
  327. sub ctx_block_get {
  328. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  329. my $line;
  330. my $start = $linenr - 1;
  331. my $blk = '';
  332. my @o;
  333. my @c;
  334. my @res = ();
  335. my $level = 0;
  336. for ($line = $start; $remain > 0; $line++) {
  337. next if ($rawlines[$line] =~ /^-/);
  338. $remain--;
  339. $blk .= $rawlines[$line];
  340. foreach my $c (split(//, $rawlines[$line])) {
  341. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  342. if ($off > 0) {
  343. $off--;
  344. next;
  345. }
  346. if ($c eq $close && $level > 0) {
  347. $level--;
  348. last if ($level == 0);
  349. } elsif ($c eq $open) {
  350. $level++;
  351. }
  352. }
  353. if (!$outer || $level <= 1) {
  354. push(@res, $rawlines[$line]);
  355. }
  356. last if ($level == 0);
  357. }
  358. return ($level, @res);
  359. }
  360. sub ctx_block_outer {
  361. my ($linenr, $remain) = @_;
  362. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  363. return @r;
  364. }
  365. sub ctx_block {
  366. my ($linenr, $remain) = @_;
  367. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  368. return @r;
  369. }
  370. sub ctx_statement {
  371. my ($linenr, $remain, $off) = @_;
  372. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  373. return @r;
  374. }
  375. sub ctx_block_level {
  376. my ($linenr, $remain) = @_;
  377. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  378. }
  379. sub ctx_statement_level {
  380. my ($linenr, $remain, $off) = @_;
  381. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  382. }
  383. sub ctx_locate_comment {
  384. my ($first_line, $end_line) = @_;
  385. # Catch a comment on the end of the line itself.
  386. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
  387. return $current_comment if (defined $current_comment);
  388. # Look through the context and try and figure out if there is a
  389. # comment.
  390. my $in_comment = 0;
  391. $current_comment = '';
  392. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  393. my $line = $rawlines[$linenr - 1];
  394. #warn " $line\n";
  395. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  396. $in_comment = 1;
  397. }
  398. if ($line =~ m@/\*@) {
  399. $in_comment = 1;
  400. }
  401. if (!$in_comment && $current_comment ne '') {
  402. $current_comment = '';
  403. }
  404. $current_comment .= $line . "\n" if ($in_comment);
  405. if ($line =~ m@\*/@) {
  406. $in_comment = 0;
  407. }
  408. }
  409. chomp($current_comment);
  410. return($current_comment);
  411. }
  412. sub ctx_has_comment {
  413. my ($first_line, $end_line) = @_;
  414. my $cmt = ctx_locate_comment($first_line, $end_line);
  415. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  416. ##print "CMMT: $cmt\n";
  417. return ($cmt ne '');
  418. }
  419. sub cat_vet {
  420. my ($vet) = @_;
  421. my ($res, $coded);
  422. $res = '';
  423. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  424. $res .= $1;
  425. if ($2 ne '') {
  426. $coded = sprintf("^%c", unpack('C', $2) + 64);
  427. $res .= $coded;
  428. }
  429. }
  430. $res =~ s/$/\$/;
  431. return $res;
  432. }
  433. sub annotate_values {
  434. my ($stream, $type) = @_;
  435. my $res;
  436. my $cur = $stream;
  437. my $debug = 0;
  438. print "$stream\n" if ($debug);
  439. ##my $type = 'N';
  440. my $pos = 0;
  441. my $preprocessor = 0;
  442. my $paren = 0;
  443. my @paren_type;
  444. while (length($cur)) {
  445. print " <$type> " if ($debug);
  446. if ($cur =~ /^(\s+)/o) {
  447. print "WS($1)\n" if ($debug);
  448. if ($1 =~ /\n/ && $preprocessor) {
  449. $preprocessor = 0;
  450. $type = 'N';
  451. }
  452. } elsif ($cur =~ /^($Type)/) {
  453. print "DECLARE($1)\n" if ($debug);
  454. $type = 'T';
  455. } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
  456. print "DEFINE($1)\n" if ($debug);
  457. $preprocessor = 1;
  458. $paren_type[$paren] = 'N';
  459. } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|endif))/o) {
  460. print "PRE($1)\n" if ($debug);
  461. $preprocessor = 1;
  462. $type = 'N';
  463. } elsif ($cur =~ /^(\\\n)/o) {
  464. print "PRECONT($1)\n" if ($debug);
  465. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  466. print "SIZEOF($1)\n" if ($debug);
  467. if (defined $2) {
  468. $paren_type[$paren] = 'V';
  469. }
  470. $type = 'N';
  471. } elsif ($cur =~ /^(if|while|typeof|for)\b/o) {
  472. print "COND($1)\n" if ($debug);
  473. $paren_type[$paren] = 'N';
  474. $type = 'N';
  475. } elsif ($cur =~/^(return|case|else)/o) {
  476. print "KEYWORD($1)\n" if ($debug);
  477. $type = 'N';
  478. } elsif ($cur =~ /^(\()/o) {
  479. print "PAREN('$1')\n" if ($debug);
  480. $paren++;
  481. $type = 'N';
  482. } elsif ($cur =~ /^(\))/o) {
  483. $paren-- if ($paren > 0);
  484. if (defined $paren_type[$paren]) {
  485. $type = $paren_type[$paren];
  486. undef $paren_type[$paren];
  487. print "PAREN('$1') -> $type\n" if ($debug);
  488. } else {
  489. print "PAREN('$1')\n" if ($debug);
  490. }
  491. } elsif ($cur =~ /^($Ident)\(/o) {
  492. print "FUNC($1)\n" if ($debug);
  493. $paren_type[$paren] = 'V';
  494. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  495. print "IDENT($1)\n" if ($debug);
  496. $type = 'V';
  497. } elsif ($cur =~ /^($Assignment)/o) {
  498. print "ASSIGN($1)\n" if ($debug);
  499. $type = 'N';
  500. } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) {
  501. print "END($1)\n" if ($debug);
  502. $type = 'N';
  503. } elsif ($cur =~ /^($Operators)/o) {
  504. print "OP($1)\n" if ($debug);
  505. if ($1 ne '++' && $1 ne '--') {
  506. $type = 'N';
  507. }
  508. } elsif ($cur =~ /(^.)/o) {
  509. print "C($1)\n" if ($debug);
  510. }
  511. if (defined $1) {
  512. $cur = substr($cur, length($1));
  513. $res .= $type x length($1);
  514. }
  515. }
  516. return $res;
  517. }
  518. sub possible {
  519. my ($possible) = @_;
  520. #print "CHECK<$possible>\n";
  521. if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
  522. $possible ne 'goto' && $possible ne 'return' &&
  523. $possible ne 'struct' && $possible ne 'enum' &&
  524. $possible ne 'case' && $possible ne 'else' &&
  525. $possible ne 'typedef') {
  526. #print "POSSIBLE<$possible>\n";
  527. push(@typeList, $possible);
  528. build_types();
  529. }
  530. }
  531. my $prefix = '';
  532. my @report = ();
  533. sub report {
  534. my $line = $prefix . $_[0];
  535. $line = (split('\n', $line))[0] . "\n" if ($terse);
  536. push(@report, $line);
  537. }
  538. sub report_dump {
  539. @report;
  540. }
  541. sub ERROR {
  542. report("ERROR: $_[0]\n");
  543. our $clean = 0;
  544. our $cnt_error++;
  545. }
  546. sub WARN {
  547. report("WARNING: $_[0]\n");
  548. our $clean = 0;
  549. our $cnt_warn++;
  550. }
  551. sub CHK {
  552. if ($check) {
  553. report("CHECK: $_[0]\n");
  554. our $clean = 0;
  555. our $cnt_chk++;
  556. }
  557. }
  558. sub process {
  559. my $filename = shift;
  560. my @lines = @_;
  561. my $linenr=0;
  562. my $prevline="";
  563. my $stashline="";
  564. my $length;
  565. my $indent;
  566. my $previndent=0;
  567. my $stashindent=0;
  568. our $clean = 1;
  569. my $signoff = 0;
  570. my $is_patch = 0;
  571. our $cnt_lines = 0;
  572. our $cnt_error = 0;
  573. our $cnt_warn = 0;
  574. our $cnt_chk = 0;
  575. # Trace the real file/line as we go.
  576. my $realfile = '';
  577. my $realline = 0;
  578. my $realcnt = 0;
  579. my $here = '';
  580. my $in_comment = 0;
  581. my $first_line = 0;
  582. my $prev_values = 'N';
  583. # Pre-scan the patch looking for any __setup documentation.
  584. my @setup_docs = ();
  585. my $setup_docs = 0;
  586. foreach my $line (@lines) {
  587. if ($line=~/^\+\+\+\s+(\S+)/) {
  588. $setup_docs = 0;
  589. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  590. $setup_docs = 1;
  591. }
  592. next;
  593. }
  594. if ($setup_docs && $line =~ /^\+/) {
  595. push(@setup_docs, $line);
  596. }
  597. }
  598. $prefix = '';
  599. foreach my $line (@lines) {
  600. $linenr++;
  601. my $rawline = $line;
  602. #extract the filename as it passes
  603. if ($line=~/^\+\+\+\s+(\S+)/) {
  604. $realfile=$1;
  605. $realfile =~ s@^[^/]*/@@;
  606. $in_comment = 0;
  607. next;
  608. }
  609. #extract the line range in the file after the patch is applied
  610. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  611. $is_patch = 1;
  612. $first_line = $linenr + 1;
  613. $in_comment = 0;
  614. $realline=$1-1;
  615. if (defined $2) {
  616. $realcnt=$3+1;
  617. } else {
  618. $realcnt=1+1;
  619. }
  620. $prev_values = 'N';
  621. next;
  622. }
  623. # track the line number as we move through the hunk, note that
  624. # new versions of GNU diff omit the leading space on completely
  625. # blank context lines so we need to count that too.
  626. if ($line =~ /^( |\+|$)/) {
  627. $realline++;
  628. $realcnt-- if ($realcnt != 0);
  629. # Guestimate if this is a continuing comment. Run
  630. # the context looking for a comment "edge". If this
  631. # edge is a close comment then we must be in a comment
  632. # at context start.
  633. if ($linenr == $first_line) {
  634. my $edge;
  635. for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
  636. ($edge) = ($lines[$ln - 1] =~ m@(/\*|\*/)@);
  637. last if (defined $edge);
  638. }
  639. if (defined $edge && $edge eq '*/') {
  640. $in_comment = 1;
  641. }
  642. }
  643. # Guestimate if this is a continuing comment. If this
  644. # is the start of a diff block and this line starts
  645. # ' *' then it is very likely a comment.
  646. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  647. $in_comment = 1;
  648. }
  649. # Find the last comment edge on _this_ line.
  650. while (($line =~ m@(/\*|\*/)@g)) {
  651. if ($1 eq '/*') {
  652. $in_comment = 1;
  653. } else {
  654. $in_comment = 0;
  655. }
  656. }
  657. # Measure the line length and indent.
  658. ($length, $indent) = line_stats($line);
  659. # Track the previous line.
  660. ($prevline, $stashline) = ($stashline, $line);
  661. ($previndent, $stashindent) = ($stashindent, $indent);
  662. } elsif ($realcnt == 1) {
  663. $realcnt--;
  664. }
  665. #make up the handle for any error we report on this line
  666. $here = "#$linenr: " if (!$file);
  667. $here = "#$realline: " if ($file);
  668. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  669. my $hereline = "$here\n$line\n";
  670. my $herecurr = "$here\n$line\n";
  671. my $hereprev = "$here\n$prevline\n$line\n";
  672. $prefix = "$filename:$realline: " if ($emacs && $file);
  673. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  674. $cnt_lines++ if ($realcnt != 0);
  675. #check the patch for a signoff:
  676. if ($line =~ /^\s*signed-off-by:/i) {
  677. # This is a signoff, if ugly, so do not double report.
  678. $signoff++;
  679. if (!($line =~ /^\s*Signed-off-by:/)) {
  680. WARN("Signed-off-by: is the preferred form\n" .
  681. $herecurr);
  682. }
  683. if ($line =~ /^\s*signed-off-by:\S/i) {
  684. WARN("need space after Signed-off-by:\n" .
  685. $herecurr);
  686. }
  687. }
  688. # Check for wrappage within a valid hunk of the file
  689. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  690. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  691. $herecurr) if (!$emitted_corrupt++);
  692. }
  693. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  694. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  695. !($line =~ m/^(
  696. [\x09\x0A\x0D\x20-\x7E] # ASCII
  697. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  698. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  699. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  700. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  701. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  702. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  703. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  704. )*$/x )) {
  705. ERROR("Invalid UTF-8\n" . $herecurr);
  706. }
  707. #ignore lines being removed
  708. if ($line=~/^-/) {next;}
  709. # check we are in a valid source file if not then ignore this hunk
  710. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  711. #trailing whitespace
  712. if ($line =~ /^\+.*\015/) {
  713. my $herevet = "$here\n" . cat_vet($line) . "\n";
  714. ERROR("DOS line endings\n" . $herevet);
  715. } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
  716. my $herevet = "$here\n" . cat_vet($line) . "\n";
  717. ERROR("trailing whitespace\n" . $herevet);
  718. }
  719. #80 column limit
  720. if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
  721. WARN("line over 80 characters\n" . $herecurr);
  722. }
  723. # check for adding lines without a newline.
  724. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  725. WARN("adding a line without newline at end of file\n" . $herecurr);
  726. }
  727. # check we are in a valid source file *.[hc] if not then ignore this hunk
  728. next if ($realfile !~ /\.[hc]$/);
  729. # at the beginning of a line any tabs must come first and anything
  730. # more than 8 must use tabs.
  731. if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
  732. my $herevet = "$here\n" . cat_vet($line) . "\n";
  733. ERROR("use tabs not spaces\n" . $herevet);
  734. }
  735. # Remove comments from the line before processing.
  736. my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
  737. ($line =~ s@/\*.*@@) +
  738. ($line =~ s@^(.).*\*/@$1@);
  739. # The rest of our checks refer specifically to C style
  740. # only apply those _outside_ comments. Only skip
  741. # lines in the middle of comments.
  742. next if (!$comment_edge && $in_comment);
  743. # Standardise the strings and chars within the input to simplify matching.
  744. $line = sanitise_line($line);
  745. # Check for potential 'bare' types
  746. if ($realcnt &&
  747. $line !~ /$Ident:\s*$/ &&
  748. ($line =~ /^.\s*$Ident\s*\(\*+\s*$Ident\)\s*\(/ ||
  749. $line !~ /^.\s*$Ident\s*\(/)) {
  750. # definitions in global scope can only start with types
  751. if ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
  752. possible($1);
  753. # declarations always start with types
  754. } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/) {
  755. possible($1);
  756. # any (foo ... *) is a pointer cast, and foo is a type
  757. } elsif ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/) {
  758. possible($1);
  759. }
  760. # Check for any sort of function declaration.
  761. # int foo(something bar, other baz);
  762. # void (*store_gdt)(x86_descr_ptr *);
  763. if ($prev_values eq 'N' && $line =~ /^(.(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
  764. my ($name_len) = length($1);
  765. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len);
  766. my $ctx = join("\n", @ctx);
  767. $ctx =~ s/\n.//;
  768. substr($ctx, 0, $name_len + 1) = '';
  769. $ctx =~ s/\)[^\)]*$//;
  770. for my $arg (split(/\s*,\s*/, $ctx)) {
  771. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
  772. possible($1);
  773. }
  774. }
  775. }
  776. }
  777. #
  778. # Checks which may be anchored in the context.
  779. #
  780. # Check for switch () and associated case and default
  781. # statements should be at the same indent.
  782. if ($line=~/\bswitch\s*\(.*\)/) {
  783. my $err = '';
  784. my $sep = '';
  785. my @ctx = ctx_block_outer($linenr, $realcnt);
  786. shift(@ctx);
  787. for my $ctx (@ctx) {
  788. my ($clen, $cindent) = line_stats($ctx);
  789. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  790. $indent != $cindent) {
  791. $err .= "$sep$ctx\n";
  792. $sep = '';
  793. } else {
  794. $sep = "[...]\n";
  795. }
  796. }
  797. if ($err ne '') {
  798. ERROR("switch and case should be at the same indent\n$hereline$err");
  799. }
  800. }
  801. # if/while/etc brace do not go on next line, unless defining a do while loop,
  802. # or if that brace on the next line is for something else
  803. if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
  804. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  805. my $ctx_ln = $linenr + $#ctx + 1;
  806. my $ctx_cnt = $realcnt - $#ctx - 1;
  807. my $ctx = join("\n", @ctx);
  808. # Skip over any removed lines in the context following statement.
  809. while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
  810. $ctx_ln++;
  811. $ctx_cnt--;
  812. }
  813. ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
  814. if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  815. ERROR("That open brace { should be on the previous line\n" .
  816. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  817. }
  818. if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
  819. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  820. if ($nindent > $indent) {
  821. WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
  822. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  823. }
  824. }
  825. }
  826. # Track the 'values' across context and added lines.
  827. my $opline = $line; $opline =~ s/^./ /;
  828. my $curr_values = annotate_values($opline . "\n", $prev_values);
  829. $curr_values = $prev_values . $curr_values;
  830. #warn "--> $opline\n";
  831. #warn "--> $curr_values ($prev_values)\n";
  832. $prev_values = substr($curr_values, -1);
  833. #ignore lines not being added
  834. if ($line=~/^[^\+]/) {next;}
  835. # TEST: allow direct testing of the type matcher.
  836. if ($tst_type && $line =~ /^.$Declare$/) {
  837. ERROR("TEST: is type $Declare\n" . $herecurr);
  838. next;
  839. }
  840. # check for initialisation to aggregates open brace on the next line
  841. if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
  842. $line =~ /^.\s*{/) {
  843. ERROR("That open brace { should be on the previous line\n" . $hereprev);
  844. }
  845. #
  846. # Checks which are anchored on the added line.
  847. #
  848. # check for malformed paths in #include statements (uses RAW line)
  849. if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
  850. my $path = $1;
  851. if ($path =~ m{//}) {
  852. ERROR("malformed #include filename\n" .
  853. $herecurr);
  854. }
  855. # Sanitise this special form of string.
  856. $path = 'X' x length($path);
  857. $line =~ s{\<.*\>}{<$path>};
  858. }
  859. # no C99 // comments
  860. if ($line =~ m{//}) {
  861. ERROR("do not use C99 // comments\n" . $herecurr);
  862. }
  863. # Remove C99 comments.
  864. $line =~ s@//.*@@;
  865. $opline =~ s@//.*@@;
  866. #EXPORT_SYMBOL should immediately follow its function closing }.
  867. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  868. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  869. my $name = $1;
  870. if (($prevline !~ /^}/) &&
  871. ($prevline !~ /^\+}/) &&
  872. ($prevline !~ /^ }/) &&
  873. ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
  874. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  875. }
  876. }
  877. # check for external initialisers.
  878. if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
  879. ERROR("do not initialise externals to 0 or NULL\n" .
  880. $herecurr);
  881. }
  882. # check for static initialisers.
  883. if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
  884. ERROR("do not initialise statics to 0 or NULL\n" .
  885. $herecurr);
  886. }
  887. # check for new typedefs, only function parameters and sparse annotations
  888. # make sense.
  889. if ($line =~ /\btypedef\s/ &&
  890. $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
  891. $line !~ /\b__bitwise(?:__|)\b/) {
  892. WARN("do not add new typedefs\n" . $herecurr);
  893. }
  894. # * goes on variable not on type
  895. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  896. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  897. $herecurr);
  898. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  899. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  900. $herecurr);
  901. } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  902. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  903. $herecurr);
  904. } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  905. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  906. $herecurr);
  907. }
  908. # # no BUG() or BUG_ON()
  909. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  910. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  911. # print "$herecurr";
  912. # $clean = 0;
  913. # }
  914. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  915. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged" . $herecurr);
  916. }
  917. # printk should use KERN_* levels. Note that follow on printk's on the
  918. # same line do not need a level, so we use the current block context
  919. # to try and find and validate the current printk. In summary the current
  920. # printk includes all preceeding printk's which have no newline on the end.
  921. # we assume the first bad printk is the one to report.
  922. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  923. my $ok = 0;
  924. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  925. #print "CHECK<$lines[$ln - 1]\n";
  926. # we have a preceeding printk if it ends
  927. # with "\n" ignore it, else it is to blame
  928. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  929. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  930. $ok = 1;
  931. }
  932. last;
  933. }
  934. }
  935. if ($ok == 0) {
  936. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  937. }
  938. }
  939. # function brace can't be on same line, except for #defines of do while,
  940. # or if closed on same line
  941. if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
  942. !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
  943. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  944. }
  945. # open braces for enum, union and struct go on the same line.
  946. if ($line =~ /^.\s*{/ &&
  947. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  948. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  949. }
  950. # check for spaces between functions and their parentheses.
  951. while ($line =~ /($Ident)\s+\(/g) {
  952. if ($1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/ &&
  953. $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
  954. WARN("no space between function name and open parenthesis '('\n" . $herecurr);
  955. }
  956. }
  957. # Check operator spacing.
  958. if (!($line=~/\#\s*include/)) {
  959. my $ops = qr{
  960. <<=|>>=|<=|>=|==|!=|
  961. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  962. =>|->|<<|>>|<|>|=|!|~|
  963. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/
  964. }x;
  965. my @elements = split(/($ops|;)/, $opline);
  966. my $off = 0;
  967. my $blank = copy_spacing($opline);
  968. for (my $n = 0; $n < $#elements; $n += 2) {
  969. $off += length($elements[$n]);
  970. my $a = '';
  971. $a = 'V' if ($elements[$n] ne '');
  972. $a = 'W' if ($elements[$n] =~ /\s$/);
  973. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  974. $a = 'O' if ($elements[$n] eq '');
  975. $a = 'E' if ($elements[$n] eq '' && $n == 0);
  976. my $op = $elements[$n + 1];
  977. my $c = '';
  978. if (defined $elements[$n + 2]) {
  979. $c = 'V' if ($elements[$n + 2] ne '');
  980. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  981. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  982. $c = 'O' if ($elements[$n + 2] eq '');
  983. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  984. } else {
  985. $c = 'E';
  986. }
  987. # Pick up the preceeding and succeeding characters.
  988. my $ca = substr($opline, 0, $off);
  989. my $cc = '';
  990. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  991. $cc = substr($opline, $off + length($elements[$n + 1]));
  992. }
  993. my $cb = "$ca$;$cc";
  994. my $ctx = "${a}x${c}";
  995. my $at = "(ctx:$ctx)";
  996. my $ptr = substr($blank, 0, $off) . "^";
  997. my $hereptr = "$hereline$ptr\n";
  998. # Classify operators into binary, unary, or
  999. # definitions (* only) where they have more
  1000. # than one mode.
  1001. my $op_type = substr($curr_values, $off + 1, 1);
  1002. my $op_left = substr($curr_values, $off, 1);
  1003. my $is_unary;
  1004. if ($op_type eq 'T') {
  1005. $is_unary = 2;
  1006. } elsif ($op_left eq 'V') {
  1007. $is_unary = 0;
  1008. } else {
  1009. $is_unary = 1;
  1010. }
  1011. #if ($op eq '-' || $op eq '&' || $op eq '*') {
  1012. # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
  1013. #}
  1014. # ; should have either the end of line or a space or \ after it
  1015. if ($op eq ';') {
  1016. if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
  1017. $cc !~ /^;/) {
  1018. ERROR("need space after that '$op' $at\n" . $hereptr);
  1019. }
  1020. # // is a comment
  1021. } elsif ($op eq '//') {
  1022. # -> should have no spaces
  1023. } elsif ($op eq '->') {
  1024. if ($ctx =~ /Wx.|.xW/) {
  1025. ERROR("no spaces around that '$op' $at\n" . $hereptr);
  1026. }
  1027. # , must have a space on the right.
  1028. } elsif ($op eq ',') {
  1029. if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
  1030. ERROR("need space after that '$op' $at\n" . $hereptr);
  1031. }
  1032. # '*' as part of a type definition -- reported already.
  1033. } elsif ($op eq '*' && $is_unary == 2) {
  1034. #warn "'*' is part of type\n";
  1035. # unary operators should have a space before and
  1036. # none after. May be left adjacent to another
  1037. # unary operator, or a cast
  1038. } elsif ($op eq '!' || $op eq '~' ||
  1039. ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
  1040. if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1041. ERROR("need space before that '$op' $at\n" . $hereptr);
  1042. }
  1043. if ($ctx =~ /.xW/) {
  1044. ERROR("no space after that '$op' $at\n" . $hereptr);
  1045. }
  1046. # unary ++ and unary -- are allowed no space on one side.
  1047. } elsif ($op eq '++' or $op eq '--') {
  1048. if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
  1049. ERROR("need space one side of that '$op' $at\n" . $hereptr);
  1050. }
  1051. if ($ctx =~ /Wx./ && $cc =~ /^;/) {
  1052. ERROR("no space before that '$op' $at\n" . $hereptr);
  1053. }
  1054. # << and >> may either have or not have spaces both sides
  1055. } elsif ($op eq '<<' or $op eq '>>' or
  1056. $op eq '&' or $op eq '^' or $op eq '|' or
  1057. $op eq '+' or $op eq '-' or
  1058. $op eq '*' or $op eq '/')
  1059. {
  1060. if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
  1061. ERROR("need consistent spacing around '$op' $at\n" .
  1062. $hereptr);
  1063. }
  1064. # All the others need spaces both sides.
  1065. } elsif ($ctx !~ /[EW]x[WE]/) {
  1066. # Ignore email addresses <foo@bar>
  1067. if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
  1068. !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
  1069. ERROR("need spaces around that '$op' $at\n" . $hereptr);
  1070. }
  1071. }
  1072. $off += length($elements[$n + 1]);
  1073. }
  1074. }
  1075. # check for multiple assignments
  1076. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1077. CHK("multiple assignments should be avoided\n" . $herecurr);
  1078. }
  1079. ## # check for multiple declarations, allowing for a function declaration
  1080. ## # continuation.
  1081. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1082. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1083. ##
  1084. ## # Remove any bracketed sections to ensure we do not
  1085. ## # falsly report the parameters of functions.
  1086. ## my $ln = $line;
  1087. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1088. ## }
  1089. ## if ($ln =~ /,/) {
  1090. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1091. ## }
  1092. ## }
  1093. #need space before brace following if, while, etc
  1094. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1095. $line =~ /do{/) {
  1096. ERROR("need a space before the open brace '{'\n" . $herecurr);
  1097. }
  1098. # closing brace should have a space following it when it has anything
  1099. # on the line
  1100. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1101. ERROR("need a space after that close brace '}'\n" . $herecurr);
  1102. }
  1103. # check spacing on square brackets
  1104. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1105. ERROR("no space after that open square bracket '['\n" . $herecurr);
  1106. }
  1107. if ($line =~ /\s\]/) {
  1108. ERROR("no space before that close square bracket ']'\n" . $herecurr);
  1109. }
  1110. # check spacing on paretheses
  1111. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1112. $line !~ /for\s*\(\s+;/) {
  1113. ERROR("no space after that open parenthesis '('\n" . $herecurr);
  1114. }
  1115. if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ &&
  1116. $line !~ /for\s*\(.*;\s+\)/) {
  1117. ERROR("no space before that close parenthesis ')'\n" . $herecurr);
  1118. }
  1119. #goto labels aren't indented, allow a single space however
  1120. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1121. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1122. WARN("labels should not be indented\n" . $herecurr);
  1123. }
  1124. # Need a space before open parenthesis after if, while etc
  1125. if ($line=~/\b(if|while|for|switch)\(/) {
  1126. ERROR("need a space before the open parenthesis '('\n" . $herecurr);
  1127. }
  1128. # Check for illegal assignment in if conditional.
  1129. if ($line =~ /\bif\s*\(/) {
  1130. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1131. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
  1132. ERROR("do not use assignment in if condition ($c)\n" . $herecurr);
  1133. }
  1134. # Find out what is on the end of the line after the
  1135. # conditional.
  1136. substr($s, 0, length($c)) = '';
  1137. $s =~ s/\n.*//g;
  1138. if (length($c) && $s !~ /^\s*({|;|\/\*.*\*\/)?\s*\\*\s*$/) {
  1139. ERROR("trailing statements should be on next line\n" . $herecurr);
  1140. }
  1141. }
  1142. # if and else should not have general statements after it
  1143. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
  1144. $1 !~ /^\s*(?:\sif|{|\\|$)/) {
  1145. ERROR("trailing statements should be on next line\n" . $herecurr);
  1146. }
  1147. # Check for }<nl>else {, these must be at the same
  1148. # indent level to be relevant to each other.
  1149. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1150. $previndent == $indent) {
  1151. ERROR("else should follow close brace '}'\n" . $hereprev);
  1152. }
  1153. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1154. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1155. # print "No studly caps, use _\n";
  1156. # print "$herecurr";
  1157. # $clean = 0;
  1158. # }
  1159. #no spaces allowed after \ in define
  1160. if ($line=~/\#define.*\\\s$/) {
  1161. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1162. }
  1163. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1164. if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1165. my $checkfile = "$root/include/linux/$1.h";
  1166. if (-f $checkfile && $1 ne 'irq.h') {
  1167. CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
  1168. $herecurr);
  1169. }
  1170. }
  1171. # multi-statement macros should be enclosed in a do while loop, grab the
  1172. # first statement and ensure its the whole macro if its not enclosed
  1173. # in a known goot container
  1174. if ($prevline =~ /\#define.*\\/ &&
  1175. $prevline !~/(?:do\s+{|\(\{|\{)/ &&
  1176. $line !~ /(?:do\s+{|\(\{|\{)/ &&
  1177. $line !~ /^.\s*$Declare\s/) {
  1178. # Grab the first statement, if that is the entire macro
  1179. # its ok. This may start either on the #define line
  1180. # or the one below.
  1181. my $ln = $linenr;
  1182. my $cnt = $realcnt;
  1183. my $off = 0;
  1184. # If the macro starts on the define line start
  1185. # grabbing the statement after the identifier
  1186. $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
  1187. ##print "1<$1> 2<$2>\n";
  1188. if (defined $2 && $2 ne '') {
  1189. $off = length($1);
  1190. $ln--;
  1191. $cnt++;
  1192. while ($lines[$ln - 1] =~ /^-/) {
  1193. $ln--;
  1194. $cnt++;
  1195. }
  1196. }
  1197. my @ctx = ctx_statement($ln, $cnt, $off);
  1198. my $ctx_ln = $ln + $#ctx + 1;
  1199. my $ctx = join("\n", @ctx);
  1200. # Pull in any empty extension lines.
  1201. while ($ctx =~ /\\$/ &&
  1202. $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
  1203. $ctx .= $lines[$ctx_ln - 1];
  1204. $ctx_ln++;
  1205. }
  1206. if ($ctx =~ /\\$/) {
  1207. if ($ctx =~ /;/) {
  1208. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  1209. } else {
  1210. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  1211. }
  1212. }
  1213. }
  1214. # check for redundant bracing round if etc
  1215. if ($line =~ /\b(if|while|for|else)\b/) {
  1216. # Locate the end of the opening statement.
  1217. my @control = ctx_statement($linenr, $realcnt, 0);
  1218. my $nr = $linenr + (scalar(@control) - 1);
  1219. my $cnt = $realcnt - (scalar(@control) - 1);
  1220. my $off = $realcnt - $cnt;
  1221. #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
  1222. # If this is is a braced statement group check it
  1223. if ($lines[$nr - 1] =~ /{\s*$/) {
  1224. my ($lvl, @block) = ctx_block_level($nr, $cnt);
  1225. my $stmt = join("\n", @block);
  1226. # Drop the diff line leader.
  1227. $stmt =~ s/\n./\n/g;
  1228. # Drop the code outside the block.
  1229. $stmt =~ s/(^[^{]*){\s*//;
  1230. my $before = $1;
  1231. $stmt =~ s/\s*}([^}]*$)//;
  1232. my $after = $1;
  1233. #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
  1234. #print "stmt<$stmt>\n\n";
  1235. # Count the newlines, if there is only one
  1236. # then the block should not have {}'s.
  1237. my @lines = ($stmt =~ /\n/g);
  1238. #print "lines<" . scalar(@lines) . ">\n";
  1239. if ($lvl == 0 && scalar(@lines) == 0 &&
  1240. $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
  1241. $before !~ /}/ && $after !~ /{/) {
  1242. my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
  1243. shift(@block);
  1244. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  1245. }
  1246. }
  1247. }
  1248. # don't include deprecated include files (uses RAW line)
  1249. for my $inc (@dep_includes) {
  1250. if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
  1251. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1252. }
  1253. }
  1254. # don't use deprecated functions
  1255. for my $func (@dep_functions) {
  1256. if ($line =~ /\b$func\b/) {
  1257. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1258. }
  1259. }
  1260. # no volatiles please
  1261. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  1262. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  1263. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1264. }
  1265. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1266. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1267. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1268. }
  1269. # warn about #if 0
  1270. if ($line =~ /^.#\s*if\s+0\b/) {
  1271. CHK("if this code is redundant consider removing it\n" .
  1272. $herecurr);
  1273. }
  1274. # check for needless kfree() checks
  1275. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1276. my $expr = $1;
  1277. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1278. WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
  1279. }
  1280. }
  1281. # warn about #ifdefs in C files
  1282. # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1283. # print "#ifdef in C files should be avoided\n";
  1284. # print "$herecurr";
  1285. # $clean = 0;
  1286. # }
  1287. # warn about spacing in #ifdefs
  1288. if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
  1289. ERROR("exactly one space required after that #$1\n" . $herecurr);
  1290. }
  1291. # check for spinlock_t definitions without a comment.
  1292. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
  1293. my $which = $1;
  1294. if (!ctx_has_comment($first_line, $linenr)) {
  1295. CHK("$1 definition without comment\n" . $herecurr);
  1296. }
  1297. }
  1298. # check for memory barriers without a comment.
  1299. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  1300. if (!ctx_has_comment($first_line, $linenr)) {
  1301. CHK("memory barrier without comment\n" . $herecurr);
  1302. }
  1303. }
  1304. # check of hardware specific defines
  1305. if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  1306. CHK("architecture specific defines should be avoided\n" . $herecurr);
  1307. }
  1308. # check the location of the inline attribute, that it is between
  1309. # storage class and type.
  1310. if ($line =~ /\b$Type\s+$Inline\b/ ||
  1311. $line =~ /\b$Inline\s+$Storage\b/) {
  1312. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  1313. }
  1314. # Check for __inline__ and __inline, prefer inline
  1315. if ($line =~ /\b(__inline__|__inline)\b/) {
  1316. WARN("plain inline is preferred over $1\n" . $herecurr);
  1317. }
  1318. # check for new externs in .c files.
  1319. if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
  1320. WARN("externs should be avoided in .c files\n" . $herecurr);
  1321. }
  1322. # checks for new __setup's
  1323. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  1324. my $name = $1;
  1325. if (!grep(/$name/, @setup_docs)) {
  1326. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  1327. }
  1328. }
  1329. # check for pointless casting of kmalloc return
  1330. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  1331. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  1332. }
  1333. }
  1334. # In mailback mode only produce a report in the negative, for
  1335. # things that appear to be patches.
  1336. if ($mailback && ($clean == 1 || !$is_patch)) {
  1337. exit(0);
  1338. }
  1339. # This is not a patch, and we are are in 'no-patch' mode so
  1340. # just keep quiet.
  1341. if (!$chk_patch && !$is_patch) {
  1342. exit(0);
  1343. }
  1344. if (!$is_patch) {
  1345. ERROR("Does not appear to be a unified-diff format patch\n");
  1346. }
  1347. if ($is_patch && $chk_signoff && $signoff == 0) {
  1348. ERROR("Missing Signed-off-by: line(s)\n");
  1349. }
  1350. print report_dump();
  1351. if ($summary) {
  1352. print "total: $cnt_error errors, $cnt_warn warnings, " .
  1353. (($check)? "$cnt_chk checks, " : "") .
  1354. "$cnt_lines lines checked\n";
  1355. print "\n" if ($quiet == 0);
  1356. }
  1357. if ($clean == 1 && $quiet == 0) {
  1358. print "Your patch has no obvious style problems and is ready for submission.\n"
  1359. }
  1360. if ($clean == 0 && $quiet == 0) {
  1361. print "Your patch has style problems, please review. If any of these errors\n";
  1362. print "are false positives report them to the maintainer, see\n";
  1363. print "CHECKPATCH in MAINTAINERS.\n";
  1364. }
  1365. return $clean;
  1366. }