checkpatch.pl 65 KB

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