checkpatch.pl 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  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.20';
  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. $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
  156. $NonptrType = qr{
  157. (?:const\s+)?
  158. (?:$mods\s+)?
  159. (?:
  160. (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
  161. (?:${all}\b)
  162. )
  163. (?:\s+$Modifier|\s+const)*
  164. }x;
  165. $Type = qr{
  166. $NonptrType
  167. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  168. (?:\s+$Inline|\s+$Modifier)*
  169. }x;
  170. $Declare = qr{(?:$Storage\s+)?$Type};
  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)\s*(?:$Ident|,|\))/) {
  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)\s*\(/o) {
  679. print "FUNC($1)\n" if ($dbg_values > 1);
  680. $type = 'V';
  681. $av_pending = 'V';
  682. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  683. print "IDENT($1)\n" if ($dbg_values > 1);
  684. $type = 'V';
  685. } elsif ($cur =~ /^($Assignment)/o) {
  686. print "ASSIGN($1)\n" if ($dbg_values > 1);
  687. $type = 'N';
  688. } elsif ($cur =~/^(;|{|})/) {
  689. print "END($1)\n" if ($dbg_values > 1);
  690. $type = 'E';
  691. } elsif ($cur =~ /^(;|\?|:|\[)/o) {
  692. print "CLOSE($1)\n" if ($dbg_values > 1);
  693. $type = 'N';
  694. } elsif ($cur =~ /^($Operators)/o) {
  695. print "OP($1)\n" if ($dbg_values > 1);
  696. if ($1 ne '++' && $1 ne '--') {
  697. $type = 'N';
  698. }
  699. } elsif ($cur =~ /(^.)/o) {
  700. print "C($1)\n" if ($dbg_values > 1);
  701. }
  702. if (defined $1) {
  703. $cur = substr($cur, length($1));
  704. $res .= $type x length($1);
  705. }
  706. }
  707. return $res;
  708. }
  709. sub possible {
  710. my ($possible, $line) = @_;
  711. print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
  712. if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
  713. $possible ne 'goto' && $possible ne 'return' &&
  714. $possible ne 'case' && $possible ne 'else' &&
  715. $possible ne 'asm' &&
  716. $possible !~ /^(typedef|struct|enum)\b/) {
  717. # Check for modifiers.
  718. $possible =~ s/\s*$Storage\s*//g;
  719. $possible =~ s/\s*$Sparse\s*//g;
  720. if ($possible =~ /^\s*$/) {
  721. } elsif ($possible =~ /\s/) {
  722. $possible =~ s/\s*$Type\s*//g;
  723. warn "MODIFIER: $possible ($line)\n" if ($dbg_possible);
  724. push(@modifierList, $possible);
  725. } else {
  726. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  727. push(@typeList, $possible);
  728. }
  729. build_types();
  730. }
  731. }
  732. my $prefix = '';
  733. sub report {
  734. if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
  735. return 0;
  736. }
  737. my $line = $prefix . $_[0];
  738. $line = (split('\n', $line))[0] . "\n" if ($terse);
  739. push(our @report, $line);
  740. return 1;
  741. }
  742. sub report_dump {
  743. our @report;
  744. }
  745. sub ERROR {
  746. if (report("ERROR: $_[0]\n")) {
  747. our $clean = 0;
  748. our $cnt_error++;
  749. }
  750. }
  751. sub WARN {
  752. if (report("WARNING: $_[0]\n")) {
  753. our $clean = 0;
  754. our $cnt_warn++;
  755. }
  756. }
  757. sub CHK {
  758. if ($check && report("CHECK: $_[0]\n")) {
  759. our $clean = 0;
  760. our $cnt_chk++;
  761. }
  762. }
  763. sub process {
  764. my $filename = shift;
  765. my $linenr=0;
  766. my $prevline="";
  767. my $prevrawline="";
  768. my $stashline="";
  769. my $stashrawline="";
  770. my $length;
  771. my $indent;
  772. my $previndent=0;
  773. my $stashindent=0;
  774. our $clean = 1;
  775. my $signoff = 0;
  776. my $is_patch = 0;
  777. our @report = ();
  778. our $cnt_lines = 0;
  779. our $cnt_error = 0;
  780. our $cnt_warn = 0;
  781. our $cnt_chk = 0;
  782. # Trace the real file/line as we go.
  783. my $realfile = '';
  784. my $realline = 0;
  785. my $realcnt = 0;
  786. my $here = '';
  787. my $in_comment = 0;
  788. my $comment_edge = 0;
  789. my $first_line = 0;
  790. my $prev_values = 'E';
  791. # suppression flags
  792. my %suppress_ifbraces;
  793. # Pre-scan the patch sanitizing the lines.
  794. # Pre-scan the patch looking for any __setup documentation.
  795. #
  796. my @setup_docs = ();
  797. my $setup_docs = 0;
  798. sanitise_line_reset();
  799. my $line;
  800. foreach my $rawline (@rawlines) {
  801. $linenr++;
  802. $line = $rawline;
  803. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  804. $setup_docs = 0;
  805. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  806. $setup_docs = 1;
  807. }
  808. #next;
  809. }
  810. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  811. $realline=$1-1;
  812. if (defined $2) {
  813. $realcnt=$3+1;
  814. } else {
  815. $realcnt=1+1;
  816. }
  817. $in_comment = 0;
  818. # Guestimate if this is a continuing comment. Run
  819. # the context looking for a comment "edge". If this
  820. # edge is a close comment then we must be in a comment
  821. # at context start.
  822. my $edge;
  823. for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
  824. next if ($line =~ /^-/);
  825. ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
  826. last if (defined $edge);
  827. }
  828. if (defined $edge && $edge eq '*/') {
  829. $in_comment = 1;
  830. }
  831. # Guestimate if this is a continuing comment. If this
  832. # is the start of a diff block and this line starts
  833. # ' *' then it is very likely a comment.
  834. if (!defined $edge &&
  835. $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
  836. {
  837. $in_comment = 1;
  838. }
  839. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  840. sanitise_line_reset($in_comment);
  841. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  842. # Standardise the strings and chars within the input to
  843. # simplify matching -- only bother with positive lines.
  844. $line = sanitise_line($rawline);
  845. }
  846. push(@lines, $line);
  847. if ($realcnt > 1) {
  848. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  849. } else {
  850. $realcnt = 0;
  851. }
  852. #print "==>$rawline\n";
  853. #print "-->$line\n";
  854. if ($setup_docs && $line =~ /^\+/) {
  855. push(@setup_docs, $line);
  856. }
  857. }
  858. $prefix = '';
  859. $realcnt = 0;
  860. $linenr = 0;
  861. foreach my $line (@lines) {
  862. $linenr++;
  863. my $rawline = $rawlines[$linenr - 1];
  864. #extract the line range in the file after the patch is applied
  865. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  866. $is_patch = 1;
  867. $first_line = $linenr + 1;
  868. $realline=$1-1;
  869. if (defined $2) {
  870. $realcnt=$3+1;
  871. } else {
  872. $realcnt=1+1;
  873. }
  874. annotate_reset();
  875. $prev_values = 'E';
  876. %suppress_ifbraces = ();
  877. next;
  878. # track the line number as we move through the hunk, note that
  879. # new versions of GNU diff omit the leading space on completely
  880. # blank context lines so we need to count that too.
  881. } elsif ($line =~ /^( |\+|$)/) {
  882. $realline++;
  883. $realcnt-- if ($realcnt != 0);
  884. # Measure the line length and indent.
  885. ($length, $indent) = line_stats($rawline);
  886. # Track the previous line.
  887. ($prevline, $stashline) = ($stashline, $line);
  888. ($previndent, $stashindent) = ($stashindent, $indent);
  889. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  890. #warn "line<$line>\n";
  891. } elsif ($realcnt == 1) {
  892. $realcnt--;
  893. }
  894. #make up the handle for any error we report on this line
  895. $prefix = "$filename:$realline: " if ($emacs && $file);
  896. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  897. $here = "#$linenr: " if (!$file);
  898. $here = "#$realline: " if ($file);
  899. # extract the filename as it passes
  900. if ($line=~/^\+\+\+\s+(\S+)/) {
  901. $realfile = $1;
  902. $realfile =~ s@^[^/]*/@@;
  903. if ($realfile =~ m@include/asm/@) {
  904. ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  905. }
  906. next;
  907. }
  908. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  909. my $hereline = "$here\n$rawline\n";
  910. my $herecurr = "$here\n$rawline\n";
  911. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  912. $cnt_lines++ if ($realcnt != 0);
  913. #check the patch for a signoff:
  914. if ($line =~ /^\s*signed-off-by:/i) {
  915. # This is a signoff, if ugly, so do not double report.
  916. $signoff++;
  917. if (!($line =~ /^\s*Signed-off-by:/)) {
  918. WARN("Signed-off-by: is the preferred form\n" .
  919. $herecurr);
  920. }
  921. if ($line =~ /^\s*signed-off-by:\S/i) {
  922. WARN("space required after Signed-off-by:\n" .
  923. $herecurr);
  924. }
  925. }
  926. # Check for wrappage within a valid hunk of the file
  927. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  928. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  929. $herecurr) if (!$emitted_corrupt++);
  930. }
  931. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  932. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  933. $rawline !~ m/^$UTF8*$/) {
  934. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  935. my $blank = copy_spacing($rawline);
  936. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  937. my $hereptr = "$hereline$ptr\n";
  938. ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  939. }
  940. #ignore lines being removed
  941. if ($line=~/^-/) {next;}
  942. # check we are in a valid source file if not then ignore this hunk
  943. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  944. #trailing whitespace
  945. if ($line =~ /^\+.*\015/) {
  946. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  947. ERROR("DOS line endings\n" . $herevet);
  948. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  949. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  950. ERROR("trailing whitespace\n" . $herevet);
  951. }
  952. #80 column limit
  953. if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
  954. $rawline !~ /^.\s*\*\s*\@$Ident\s/ && $length > 80)
  955. {
  956. WARN("line over 80 characters\n" . $herecurr);
  957. }
  958. # check for adding lines without a newline.
  959. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  960. WARN("adding a line without newline at end of file\n" . $herecurr);
  961. }
  962. # check we are in a valid source file *.[hc] if not then ignore this hunk
  963. next if ($realfile !~ /\.[hc]$/);
  964. # at the beginning of a line any tabs must come first and anything
  965. # more than 8 must use tabs.
  966. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  967. $rawline =~ /^\+\s* \s*/) {
  968. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  969. ERROR("code indent should use tabs where possible\n" . $herevet);
  970. }
  971. # check for RCS/CVS revision markers
  972. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  973. WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  974. }
  975. # Check for potential 'bare' types
  976. my ($stat, $cond);
  977. if ($realcnt && $line =~ /.\s*\S/) {
  978. ($stat, $cond) = ctx_statement_block($linenr,
  979. $realcnt, 0);
  980. $stat =~ s/\n./\n /g;
  981. $cond =~ s/\n./\n /g;
  982. my $s = $stat;
  983. $s =~ s/{.*$//s;
  984. # Ignore goto labels.
  985. if ($s =~ /$Ident:\*$/s) {
  986. # Ignore functions being called
  987. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  988. # declarations always start with types
  989. } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))\s*(?:;|=|,|\()/s) {
  990. my $type = $1;
  991. $type =~ s/\s+/ /g;
  992. possible($type, "A:" . $s);
  993. # definitions in global scope can only start with types
  994. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
  995. possible($1, "B:" . $s);
  996. }
  997. # any (foo ... *) is a pointer cast, and foo is a type
  998. while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
  999. possible($1, "C:" . $s);
  1000. }
  1001. # Check for any sort of function declaration.
  1002. # int foo(something bar, other baz);
  1003. # void (*store_gdt)(x86_descr_ptr *);
  1004. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1005. my ($name_len) = length($1);
  1006. my $ctx = $s;
  1007. substr($ctx, 0, $name_len + 1, '');
  1008. $ctx =~ s/\)[^\)]*$//;
  1009. for my $arg (split(/\s*,\s*/, $ctx)) {
  1010. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1011. possible($1, "D:" . $s);
  1012. }
  1013. }
  1014. }
  1015. }
  1016. #
  1017. # Checks which may be anchored in the context.
  1018. #
  1019. # Check for switch () and associated case and default
  1020. # statements should be at the same indent.
  1021. if ($line=~/\bswitch\s*\(.*\)/) {
  1022. my $err = '';
  1023. my $sep = '';
  1024. my @ctx = ctx_block_outer($linenr, $realcnt);
  1025. shift(@ctx);
  1026. for my $ctx (@ctx) {
  1027. my ($clen, $cindent) = line_stats($ctx);
  1028. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1029. $indent != $cindent) {
  1030. $err .= "$sep$ctx\n";
  1031. $sep = '';
  1032. } else {
  1033. $sep = "[...]\n";
  1034. }
  1035. }
  1036. if ($err ne '') {
  1037. ERROR("switch and case should be at the same indent\n$hereline$err");
  1038. }
  1039. }
  1040. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1041. # or if that brace on the next line is for something else
  1042. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1043. my $pre_ctx = "$1$2";
  1044. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1045. my $ctx_ln = $linenr + $#ctx + 1;
  1046. my $ctx_cnt = $realcnt - $#ctx - 1;
  1047. my $ctx = join("\n", @ctx);
  1048. ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1049. # Skip over any removed lines in the context following statement.
  1050. while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
  1051. $ctx_ln++;
  1052. }
  1053. ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1054. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  1055. ERROR("that open brace { should be on the previous line\n" .
  1056. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1057. }
  1058. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1059. $ctx =~ /\)\s*\;\s*$/ &&
  1060. defined $lines[$ctx_ln - 1])
  1061. {
  1062. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1063. if ($nindent > $indent) {
  1064. WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
  1065. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1066. }
  1067. }
  1068. }
  1069. # Track the 'values' across context and added lines.
  1070. my $opline = $line; $opline =~ s/^./ /;
  1071. my $curr_values = annotate_values($opline . "\n", $prev_values);
  1072. $curr_values = $prev_values . $curr_values;
  1073. if ($dbg_values) {
  1074. my $outline = $opline; $outline =~ s/\t/ /g;
  1075. print "$linenr > .$outline\n";
  1076. print "$linenr > $curr_values\n";
  1077. }
  1078. $prev_values = substr($curr_values, -1);
  1079. #ignore lines not being added
  1080. if ($line=~/^[^\+]/) {next;}
  1081. # TEST: allow direct testing of the type matcher.
  1082. if ($tst_type && $line =~ /^.$Declare$/) {
  1083. ERROR("TEST: is type $Declare\n" . $herecurr);
  1084. next;
  1085. }
  1086. # check for initialisation to aggregates open brace on the next line
  1087. if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
  1088. $line =~ /^.\s*{/) {
  1089. ERROR("that open brace { should be on the previous line\n" . $hereprev);
  1090. }
  1091. #
  1092. # Checks which are anchored on the added line.
  1093. #
  1094. # check for malformed paths in #include statements (uses RAW line)
  1095. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  1096. my $path = $1;
  1097. if ($path =~ m{//}) {
  1098. ERROR("malformed #include filename\n" .
  1099. $herecurr);
  1100. }
  1101. }
  1102. # no C99 // comments
  1103. if ($line =~ m{//}) {
  1104. ERROR("do not use C99 // comments\n" . $herecurr);
  1105. }
  1106. # Remove C99 comments.
  1107. $line =~ s@//.*@@;
  1108. $opline =~ s@//.*@@;
  1109. #EXPORT_SYMBOL should immediately follow its function closing }.
  1110. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  1111. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1112. my $name = $1;
  1113. if (($prevline !~ /^}/) &&
  1114. ($prevline !~ /^\+}/) &&
  1115. ($prevline !~ /^ }/) &&
  1116. ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
  1117. ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
  1118. ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
  1119. ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
  1120. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  1121. }
  1122. }
  1123. # check for external initialisers.
  1124. if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
  1125. ERROR("do not initialise externals to 0 or NULL\n" .
  1126. $herecurr);
  1127. }
  1128. # check for static initialisers.
  1129. if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
  1130. ERROR("do not initialise statics to 0 or NULL\n" .
  1131. $herecurr);
  1132. }
  1133. # check for new typedefs, only function parameters and sparse annotations
  1134. # make sense.
  1135. if ($line =~ /\btypedef\s/ &&
  1136. $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
  1137. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  1138. $line !~ /\b__bitwise(?:__|)\b/) {
  1139. WARN("do not add new typedefs\n" . $herecurr);
  1140. }
  1141. # * goes on variable not on type
  1142. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  1143. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  1144. $herecurr);
  1145. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  1146. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  1147. $herecurr);
  1148. } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  1149. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  1150. $herecurr);
  1151. } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  1152. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  1153. $herecurr);
  1154. }
  1155. # # no BUG() or BUG_ON()
  1156. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1157. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1158. # print "$herecurr";
  1159. # $clean = 0;
  1160. # }
  1161. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1162. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1163. }
  1164. # printk should use KERN_* levels. Note that follow on printk's on the
  1165. # same line do not need a level, so we use the current block context
  1166. # to try and find and validate the current printk. In summary the current
  1167. # printk includes all preceeding printk's which have no newline on the end.
  1168. # we assume the first bad printk is the one to report.
  1169. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1170. my $ok = 0;
  1171. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1172. #print "CHECK<$lines[$ln - 1]\n";
  1173. # we have a preceeding printk if it ends
  1174. # with "\n" ignore it, else it is to blame
  1175. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1176. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1177. $ok = 1;
  1178. }
  1179. last;
  1180. }
  1181. }
  1182. if ($ok == 0) {
  1183. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  1184. }
  1185. }
  1186. # function brace can't be on same line, except for #defines of do while,
  1187. # or if closed on same line
  1188. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  1189. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  1190. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1191. }
  1192. # open braces for enum, union and struct go on the same line.
  1193. if ($line =~ /^.\s*{/ &&
  1194. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1195. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1196. }
  1197. # check for spaces between functions and their parentheses.
  1198. while ($line =~ /($Ident)\s+\(/g) {
  1199. my $name = $1;
  1200. my $ctx_before = substr($line, 0, $-[1]);
  1201. my $ctx = "$ctx_before$name";
  1202. # Ignore those directives where spaces _are_ permitted.
  1203. if ($name =~ /^(?:
  1204. if|for|while|switch|return|case|
  1205. volatile|__volatile__|
  1206. __attribute__|format|__extension__|
  1207. asm|__asm__)$/x)
  1208. {
  1209. # cpp #define statements have non-optional spaces, ie
  1210. # if there is a space between the name and the open
  1211. # parenthesis it is simply not a parameter group.
  1212. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1213. # cpp #elif statement condition may start with a (
  1214. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1215. # If this whole things ends with a type its most
  1216. # likely a typedef for a function.
  1217. } elsif ($ctx =~ /$Type$/) {
  1218. } else {
  1219. WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1220. }
  1221. }
  1222. # Check operator spacing.
  1223. if (!($line=~/\#\s*include/)) {
  1224. my $ops = qr{
  1225. <<=|>>=|<=|>=|==|!=|
  1226. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1227. =>|->|<<|>>|<|>|=|!|~|
  1228. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  1229. }x;
  1230. my @elements = split(/($ops|;)/, $opline);
  1231. my $off = 0;
  1232. my $blank = copy_spacing($opline);
  1233. for (my $n = 0; $n < $#elements; $n += 2) {
  1234. $off += length($elements[$n]);
  1235. # Pick up the preceeding and succeeding characters.
  1236. my $ca = substr($opline, 0, $off);
  1237. my $cc = '';
  1238. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1239. $cc = substr($opline, $off + length($elements[$n + 1]));
  1240. }
  1241. my $cb = "$ca$;$cc";
  1242. my $a = '';
  1243. $a = 'V' if ($elements[$n] ne '');
  1244. $a = 'W' if ($elements[$n] =~ /\s$/);
  1245. $a = 'C' if ($elements[$n] =~ /$;$/);
  1246. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1247. $a = 'O' if ($elements[$n] eq '');
  1248. $a = 'E' if ($ca =~ /^\s*$/);
  1249. my $op = $elements[$n + 1];
  1250. my $c = '';
  1251. if (defined $elements[$n + 2]) {
  1252. $c = 'V' if ($elements[$n + 2] ne '');
  1253. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1254. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1255. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1256. $c = 'O' if ($elements[$n + 2] eq '');
  1257. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  1258. } else {
  1259. $c = 'E';
  1260. }
  1261. my $ctx = "${a}x${c}";
  1262. my $at = "(ctx:$ctx)";
  1263. my $ptr = substr($blank, 0, $off) . "^";
  1264. my $hereptr = "$hereline$ptr\n";
  1265. # Classify operators into binary, unary, or
  1266. # definitions (* only) where they have more
  1267. # than one mode.
  1268. my $op_type = substr($curr_values, $off + 1, 1);
  1269. my $op_left = substr($curr_values, $off, 1);
  1270. my $is_unary;
  1271. if ($op_type eq 'T') {
  1272. $is_unary = 2;
  1273. } elsif ($op_left eq 'V') {
  1274. $is_unary = 0;
  1275. } else {
  1276. $is_unary = 1;
  1277. }
  1278. #if ($op eq '-' || $op eq '&' || $op eq '*') {
  1279. # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
  1280. #}
  1281. # Ignore operators passed as parameters.
  1282. if ($op_type ne 'V' &&
  1283. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1284. # # Ignore comments
  1285. # } elsif ($op =~ /^$;+$/) {
  1286. # ; should have either the end of line or a space or \ after it
  1287. } elsif ($op eq ';') {
  1288. if ($ctx !~ /.x[WEBC]/ &&
  1289. $cc !~ /^\\/ && $cc !~ /^;/) {
  1290. ERROR("space required after that '$op' $at\n" . $hereptr);
  1291. }
  1292. # // is a comment
  1293. } elsif ($op eq '//') {
  1294. # -> should have no spaces
  1295. } elsif ($op eq '->') {
  1296. if ($ctx =~ /Wx.|.xW/) {
  1297. ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
  1298. }
  1299. # , must have a space on the right.
  1300. } elsif ($op eq ',') {
  1301. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  1302. ERROR("space required after that '$op' $at\n" . $hereptr);
  1303. }
  1304. # '*' as part of a type definition -- reported already.
  1305. } elsif ($op eq '*' && $is_unary == 2) {
  1306. #warn "'*' is part of type\n";
  1307. # unary operators should have a space before and
  1308. # none after. May be left adjacent to another
  1309. # unary operator, or a cast
  1310. } elsif ($op eq '!' || $op eq '~' ||
  1311. ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
  1312. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1313. ERROR("space required before that '$op' $at\n" . $hereptr);
  1314. }
  1315. if ($op eq '*' && $cc =~/\s*const\b/) {
  1316. # A unary '*' may be const
  1317. } elsif ($ctx =~ /.xW/) {
  1318. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1319. }
  1320. # unary ++ and unary -- are allowed no space on one side.
  1321. } elsif ($op eq '++' or $op eq '--') {
  1322. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  1323. ERROR("space required one side of that '$op' $at\n" . $hereptr);
  1324. }
  1325. if ($ctx =~ /Wx[BE]/ ||
  1326. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  1327. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1328. }
  1329. if ($ctx =~ /ExW/) {
  1330. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1331. }
  1332. # << and >> may either have or not have spaces both sides
  1333. } elsif ($op eq '<<' or $op eq '>>' or
  1334. $op eq '&' or $op eq '^' or $op eq '|' or
  1335. $op eq '+' or $op eq '-' or
  1336. $op eq '*' or $op eq '/' or
  1337. $op eq '%')
  1338. {
  1339. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  1340. ERROR("need consistent spacing around '$op' $at\n" .
  1341. $hereptr);
  1342. }
  1343. # All the others need spaces both sides.
  1344. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  1345. # Ignore email addresses <foo@bar>
  1346. if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
  1347. !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
  1348. ERROR("spaces required around that '$op' $at\n" . $hereptr);
  1349. }
  1350. }
  1351. $off += length($elements[$n + 1]);
  1352. }
  1353. }
  1354. # check for multiple assignments
  1355. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1356. CHK("multiple assignments should be avoided\n" . $herecurr);
  1357. }
  1358. ## # check for multiple declarations, allowing for a function declaration
  1359. ## # continuation.
  1360. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1361. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1362. ##
  1363. ## # Remove any bracketed sections to ensure we do not
  1364. ## # falsly report the parameters of functions.
  1365. ## my $ln = $line;
  1366. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1367. ## }
  1368. ## if ($ln =~ /,/) {
  1369. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1370. ## }
  1371. ## }
  1372. #need space before brace following if, while, etc
  1373. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1374. $line =~ /do{/) {
  1375. ERROR("space required before the open brace '{'\n" . $herecurr);
  1376. }
  1377. # closing brace should have a space following it when it has anything
  1378. # on the line
  1379. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1380. ERROR("space required after that close brace '}'\n" . $herecurr);
  1381. }
  1382. # check spacing on square brackets
  1383. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1384. ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
  1385. }
  1386. if ($line =~ /\s\]/) {
  1387. ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
  1388. }
  1389. # check spacing on parentheses
  1390. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1391. $line !~ /for\s*\(\s+;/) {
  1392. ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
  1393. }
  1394. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  1395. $line !~ /for\s*\(.*;\s+\)/ &&
  1396. $line !~ /:\s+\)/) {
  1397. ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
  1398. }
  1399. #goto labels aren't indented, allow a single space however
  1400. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1401. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1402. WARN("labels should not be indented\n" . $herecurr);
  1403. }
  1404. # Return is not a function.
  1405. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  1406. my $spacing = $1;
  1407. my $value = $2;
  1408. # Flatten any parentheses and braces
  1409. $value =~ s/\)\(/\) \(/g;
  1410. while ($value =~ s/\([^\(\)]*\)/1/) {
  1411. }
  1412. if ($value =~ /^(?:$Ident|-?$Constant)$/) {
  1413. ERROR("return is not a function, parentheses are not required\n" . $herecurr);
  1414. } elsif ($spacing !~ /\s+/) {
  1415. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1416. }
  1417. }
  1418. # Need a space before open parenthesis after if, while etc
  1419. if ($line=~/\b(if|while|for|switch)\(/) {
  1420. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1421. }
  1422. # Check for illegal assignment in if conditional.
  1423. if ($line =~ /\bif\s*\(/) {
  1424. my ($s, $c) = ($stat, $cond);
  1425. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
  1426. ERROR("do not use assignment in if condition\n" . $herecurr);
  1427. }
  1428. # Find out what is on the end of the line after the
  1429. # conditional.
  1430. substr($s, 0, length($c), '');
  1431. $s =~ s/\n.*//g;
  1432. $s =~ s/$;//g; # Remove any comments
  1433. if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
  1434. $c !~ /^.\s*\#\s*if/)
  1435. {
  1436. ERROR("trailing statements should be on next line\n" . $herecurr);
  1437. }
  1438. }
  1439. # Check for bitwise tests written as boolean
  1440. if ($line =~ /
  1441. (?:
  1442. (?:\[|\(|\&\&|\|\|)
  1443. \s*0[xX][0-9]+\s*
  1444. (?:\&\&|\|\|)
  1445. |
  1446. (?:\&\&|\|\|)
  1447. \s*0[xX][0-9]+\s*
  1448. (?:\&\&|\|\||\)|\])
  1449. )/x)
  1450. {
  1451. WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  1452. }
  1453. # if and else should not have general statements after it
  1454. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  1455. my $s = $1;
  1456. $s =~ s/$;//g; # Remove any comments
  1457. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  1458. ERROR("trailing statements should be on next line\n" . $herecurr);
  1459. }
  1460. }
  1461. # Check for }<nl>else {, these must be at the same
  1462. # indent level to be relevant to each other.
  1463. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1464. $previndent == $indent) {
  1465. ERROR("else should follow close brace '}'\n" . $hereprev);
  1466. }
  1467. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  1468. $previndent == $indent) {
  1469. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1470. # Find out what is on the end of the line after the
  1471. # conditional.
  1472. substr($s, 0, length($c), '');
  1473. $s =~ s/\n.*//g;
  1474. if ($s =~ /^\s*;/) {
  1475. ERROR("while should follow close brace '}'\n" . $hereprev);
  1476. }
  1477. }
  1478. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1479. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1480. # print "No studly caps, use _\n";
  1481. # print "$herecurr";
  1482. # $clean = 0;
  1483. # }
  1484. #no spaces allowed after \ in define
  1485. if ($line=~/\#\s*define.*\\\s$/) {
  1486. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1487. }
  1488. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1489. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1490. my $checkfile = "include/linux/$1.h";
  1491. if (-f "$root/$checkfile" && $realfile ne $checkfile &&
  1492. $1 ne 'irq')
  1493. {
  1494. WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
  1495. $herecurr);
  1496. }
  1497. }
  1498. # multi-statement macros should be enclosed in a do while loop, grab the
  1499. # first statement and ensure its the whole macro if its not enclosed
  1500. # in a known good container
  1501. if ($line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  1502. my $ln = $linenr;
  1503. my $cnt = $realcnt;
  1504. my ($off, $dstat, $dcond, $rest);
  1505. my $ctx = '';
  1506. my $args = defined($1);
  1507. # Find the end of the macro and limit our statement
  1508. # search to that.
  1509. while ($cnt > 0 && defined $lines[$ln - 1] &&
  1510. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  1511. {
  1512. $ctx .= $rawlines[$ln - 1] . "\n";
  1513. $ln++;
  1514. $cnt--;
  1515. }
  1516. $ctx .= $rawlines[$ln - 1];
  1517. ($dstat, $dcond, $ln, $cnt, $off) =
  1518. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  1519. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  1520. #print "LINE<$lines[$ln]> len<" . length($lines[$ln]) . "\n";
  1521. # Extract the remainder of the define (if any) and
  1522. # rip off surrounding spaces, and trailing \'s.
  1523. $rest = '';
  1524. if (defined $lines[$ln - 1] &&
  1525. $off > length($lines[$ln - 1]))
  1526. {
  1527. $ln++;
  1528. $cnt--;
  1529. $off = 0;
  1530. }
  1531. while ($cnt > 0) {
  1532. $rest .= substr($lines[$ln - 1], $off) . "\n";
  1533. $ln++;
  1534. $cnt--;
  1535. $off = 0;
  1536. }
  1537. $rest =~ s/\\\n.//g;
  1538. $rest =~ s/^\s*//s;
  1539. $rest =~ s/\s*$//s;
  1540. # Clean up the original statement.
  1541. if ($args) {
  1542. substr($dstat, 0, length($dcond), '');
  1543. } else {
  1544. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  1545. }
  1546. $dstat =~ s/\\\n.//g;
  1547. $dstat =~ s/^\s*//s;
  1548. $dstat =~ s/\s*$//s;
  1549. # Flatten any parentheses and braces
  1550. while ($dstat =~ s/\([^\(\)]*\)/1/) {
  1551. }
  1552. while ($dstat =~ s/\{[^\{\}]*\}/1/) {
  1553. }
  1554. my $exceptions = qr{
  1555. $Declare|
  1556. module_param_named|
  1557. MODULE_PARAM_DESC|
  1558. DECLARE_PER_CPU|
  1559. DEFINE_PER_CPU|
  1560. __typeof__\(
  1561. }x;
  1562. if ($rest ne '') {
  1563. if ($rest !~ /while\s*\(/ &&
  1564. $dstat !~ /$exceptions/)
  1565. {
  1566. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  1567. }
  1568. } elsif ($ctx !~ /;/) {
  1569. if ($dstat ne '' &&
  1570. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  1571. $dstat !~ /$exceptions/ &&
  1572. $dstat =~ /$Operators/)
  1573. {
  1574. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  1575. }
  1576. }
  1577. }
  1578. # check for redundant bracing round if etc
  1579. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  1580. my ($level, $endln, @chunks) =
  1581. ctx_statement_full($linenr, $realcnt, 1);
  1582. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  1583. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  1584. if ($#chunks > 0 && $level == 0) {
  1585. my $allowed = 0;
  1586. my $seen = 0;
  1587. my $herectx = $here . "\n";
  1588. my $ln = $linenr - 1;
  1589. for my $chunk (@chunks) {
  1590. my ($cond, $block) = @{$chunk};
  1591. # If the condition carries leading newlines, then count those as offsets.
  1592. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  1593. my $offset = statement_rawlines($whitespace) - 1;
  1594. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  1595. # We have looked at and allowed this specific line.
  1596. $suppress_ifbraces{$ln + $offset} = 1;
  1597. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  1598. $ln += statement_rawlines($block) - 1;
  1599. substr($block, 0, length($cond), '');
  1600. $seen++ if ($block =~ /^\s*{/);
  1601. #print "cond<$cond> block<$block> allowed<$allowed>\n";
  1602. if (statement_lines($cond) > 1) {
  1603. #print "APW: ALLOWED: cond<$cond>\n";
  1604. $allowed = 1;
  1605. }
  1606. if ($block =~/\b(?:if|for|while)\b/) {
  1607. #print "APW: ALLOWED: block<$block>\n";
  1608. $allowed = 1;
  1609. }
  1610. if (statement_block_size($block) > 1) {
  1611. #print "APW: ALLOWED: lines block<$block>\n";
  1612. $allowed = 1;
  1613. }
  1614. }
  1615. if ($seen && !$allowed) {
  1616. WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
  1617. }
  1618. }
  1619. }
  1620. if (!defined $suppress_ifbraces{$linenr - 1} &&
  1621. $line =~ /\b(if|while|for|else)\b/) {
  1622. my $allowed = 0;
  1623. # Check the pre-context.
  1624. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  1625. #print "APW: ALLOWED: pre<$1>\n";
  1626. $allowed = 1;
  1627. }
  1628. my ($level, $endln, @chunks) =
  1629. ctx_statement_full($linenr, $realcnt, $-[0]);
  1630. # Check the condition.
  1631. my ($cond, $block) = @{$chunks[0]};
  1632. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  1633. if (defined $cond) {
  1634. substr($block, 0, length($cond), '');
  1635. }
  1636. if (statement_lines($cond) > 1) {
  1637. #print "APW: ALLOWED: cond<$cond>\n";
  1638. $allowed = 1;
  1639. }
  1640. if ($block =~/\b(?:if|for|while)\b/) {
  1641. #print "APW: ALLOWED: block<$block>\n";
  1642. $allowed = 1;
  1643. }
  1644. if (statement_block_size($block) > 1) {
  1645. #print "APW: ALLOWED: lines block<$block>\n";
  1646. $allowed = 1;
  1647. }
  1648. # Check the post-context.
  1649. if (defined $chunks[1]) {
  1650. my ($cond, $block) = @{$chunks[1]};
  1651. if (defined $cond) {
  1652. substr($block, 0, length($cond), '');
  1653. }
  1654. if ($block =~ /^\s*\{/) {
  1655. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  1656. $allowed = 1;
  1657. }
  1658. }
  1659. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  1660. my $herectx = $here . "\n";;
  1661. my $end = $linenr + statement_rawlines($block) - 1;
  1662. for (my $ln = $linenr - 1; $ln < $end; $ln++) {
  1663. $herectx .= $rawlines[$ln] . "\n";;
  1664. }
  1665. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  1666. }
  1667. }
  1668. # don't include deprecated include files (uses RAW line)
  1669. for my $inc (@dep_includes) {
  1670. if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
  1671. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1672. }
  1673. }
  1674. # don't use deprecated functions
  1675. for my $func (@dep_functions) {
  1676. if ($line =~ /\b$func\b/) {
  1677. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1678. }
  1679. }
  1680. # no volatiles please
  1681. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  1682. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  1683. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1684. }
  1685. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1686. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1687. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1688. }
  1689. # warn about #if 0
  1690. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  1691. CHK("if this code is redundant consider removing it\n" .
  1692. $herecurr);
  1693. }
  1694. # check for needless kfree() checks
  1695. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1696. my $expr = $1;
  1697. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1698. WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
  1699. }
  1700. }
  1701. # warn about #ifdefs in C files
  1702. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1703. # print "#ifdef in C files should be avoided\n";
  1704. # print "$herecurr";
  1705. # $clean = 0;
  1706. # }
  1707. # warn about spacing in #ifdefs
  1708. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  1709. ERROR("exactly one space required after that #$1\n" . $herecurr);
  1710. }
  1711. # check for spinlock_t definitions without a comment.
  1712. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  1713. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  1714. my $which = $1;
  1715. if (!ctx_has_comment($first_line, $linenr)) {
  1716. CHK("$1 definition without comment\n" . $herecurr);
  1717. }
  1718. }
  1719. # check for memory barriers without a comment.
  1720. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  1721. if (!ctx_has_comment($first_line, $linenr)) {
  1722. CHK("memory barrier without comment\n" . $herecurr);
  1723. }
  1724. }
  1725. # check of hardware specific defines
  1726. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  1727. CHK("architecture specific defines should be avoided\n" . $herecurr);
  1728. }
  1729. # check the location of the inline attribute, that it is between
  1730. # storage class and type.
  1731. if ($line =~ /\b$Type\s+$Inline\b/ ||
  1732. $line =~ /\b$Inline\s+$Storage\b/) {
  1733. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  1734. }
  1735. # Check for __inline__ and __inline, prefer inline
  1736. if ($line =~ /\b(__inline__|__inline)\b/) {
  1737. WARN("plain inline is preferred over $1\n" . $herecurr);
  1738. }
  1739. # check for new externs in .c files.
  1740. if ($realfile =~ /\.c$/ && defined $stat &&
  1741. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  1742. {
  1743. my $function_name = $1;
  1744. my $paren_space = $2;
  1745. my $s = $stat;
  1746. if (defined $cond) {
  1747. substr($s, 0, length($cond), '');
  1748. }
  1749. if ($s =~ /^\s*;/ &&
  1750. $function_name ne 'uninitialized_var')
  1751. {
  1752. WARN("externs should be avoided in .c files\n" . $herecurr);
  1753. }
  1754. if ($paren_space =~ /\n/) {
  1755. WARN("arguments for function declarations should follow identifier\n" . $herecurr);
  1756. }
  1757. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  1758. $stat =~ /^.\s*extern\s+/)
  1759. {
  1760. WARN("externs should be avoided in .c files\n" . $herecurr);
  1761. }
  1762. # checks for new __setup's
  1763. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  1764. my $name = $1;
  1765. if (!grep(/$name/, @setup_docs)) {
  1766. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  1767. }
  1768. }
  1769. # check for pointless casting of kmalloc return
  1770. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  1771. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  1772. }
  1773. # check for gcc specific __FUNCTION__
  1774. if ($line =~ /__FUNCTION__/) {
  1775. WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  1776. }
  1777. # check for semaphores used as mutexes
  1778. if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
  1779. WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
  1780. }
  1781. # check for semaphores used as mutexes
  1782. if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
  1783. WARN("consider using a completion\n" . $herecurr);
  1784. }
  1785. # recommend strict_strto* over simple_strto*
  1786. if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
  1787. WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
  1788. }
  1789. # check for __initcall(), use device_initcall() explicitly please
  1790. if ($line =~ /^.\s*__initcall\s*\(/) {
  1791. WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
  1792. }
  1793. # use of NR_CPUS is usually wrong
  1794. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  1795. if ($line =~ /\bNR_CPUS\b/ &&
  1796. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  1797. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  1798. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  1799. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  1800. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  1801. {
  1802. WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  1803. }
  1804. # check for %L{u,d,i} in strings
  1805. my $string;
  1806. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  1807. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  1808. if ($string =~ /(?<!%)%L[udi]/) {
  1809. WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  1810. last;
  1811. }
  1812. }
  1813. }
  1814. # If we have no input at all, then there is nothing to report on
  1815. # so just keep quiet.
  1816. if ($#rawlines == -1) {
  1817. exit(0);
  1818. }
  1819. # In mailback mode only produce a report in the negative, for
  1820. # things that appear to be patches.
  1821. if ($mailback && ($clean == 1 || !$is_patch)) {
  1822. exit(0);
  1823. }
  1824. # This is not a patch, and we are are in 'no-patch' mode so
  1825. # just keep quiet.
  1826. if (!$chk_patch && !$is_patch) {
  1827. exit(0);
  1828. }
  1829. if (!$is_patch) {
  1830. ERROR("Does not appear to be a unified-diff format patch\n");
  1831. }
  1832. if ($is_patch && $chk_signoff && $signoff == 0) {
  1833. ERROR("Missing Signed-off-by: line(s)\n");
  1834. }
  1835. print report_dump();
  1836. if ($summary && !($clean == 1 && $quiet == 1)) {
  1837. print "$filename " if ($summary_file);
  1838. print "total: $cnt_error errors, $cnt_warn warnings, " .
  1839. (($check)? "$cnt_chk checks, " : "") .
  1840. "$cnt_lines lines checked\n";
  1841. print "\n" if ($quiet == 0);
  1842. }
  1843. if ($clean == 1 && $quiet == 0) {
  1844. print "$vname has no obvious style problems and is ready for submission.\n"
  1845. }
  1846. if ($clean == 0 && $quiet == 0) {
  1847. print "$vname has style problems, please review. If any of these errors\n";
  1848. print "are false positives report them to the maintainer, see\n";
  1849. print "CHECKPATCH in MAINTAINERS.\n";
  1850. }
  1851. return $clean;
  1852. }