checkpatch.pl 50 KB

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