checkpatch.pl 69 KB

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