checkpatch.pl 67 KB

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