checkpatch.pl 57 KB

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