checkpatch.pl 45 KB

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