checkpatch.pl 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382
  1. #!/usr/bin/perl -w
  2. # (c) 2001, Dave Jones. (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-2010 Andy Whitcroft <apw@canonical.com>
  6. # Licensed under the terms of the GNU GPL License version 2
  7. use strict;
  8. use POSIX;
  9. my $P = $0;
  10. $P =~ s@.*/@@g;
  11. my $V = '0.32';
  12. use Getopt::Long qw(:config no_auto_abbrev);
  13. my $quiet = 0;
  14. my $tree = 1;
  15. my $chk_signoff = 1;
  16. my $chk_patch = 1;
  17. my $tst_only;
  18. my $emacs = 0;
  19. my $terse = 0;
  20. my $file = 0;
  21. my $check = 0;
  22. my $summary = 1;
  23. my $mailback = 0;
  24. my $summary_file = 0;
  25. my $show_types = 0;
  26. my $fix = 0;
  27. my $root;
  28. my %debug;
  29. my %camelcase = ();
  30. my %use_type = ();
  31. my @use = ();
  32. my %ignore_type = ();
  33. my @ignore = ();
  34. my $help = 0;
  35. my $configuration_file = ".checkpatch.conf";
  36. my $max_line_length = 80;
  37. my $ignore_perl_version = 0;
  38. my $minimum_perl_version = 5.10.0;
  39. sub help {
  40. my ($exitcode) = @_;
  41. print << "EOM";
  42. Usage: $P [OPTION]... [FILE]...
  43. Version: $V
  44. Options:
  45. -q, --quiet quiet
  46. --no-tree run without a kernel tree
  47. --no-signoff do not check for 'Signed-off-by' line
  48. --patch treat FILE as patchfile (default)
  49. --emacs emacs compile window format
  50. --terse one line per report
  51. -f, --file treat FILE as regular source file
  52. --subjective, --strict enable more subjective tests
  53. --types TYPE(,TYPE2...) show only these comma separated message types
  54. --ignore TYPE(,TYPE2...) ignore various comma separated message types
  55. --max-line-length=n set the maximum line length, if exceeded, warn
  56. --show-types show the message "types" in the output
  57. --root=PATH PATH to the kernel tree root
  58. --no-summary suppress the per-file summary
  59. --mailback only produce a report in case of warnings/errors
  60. --summary-file include the filename in summary
  61. --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
  62. 'values', 'possible', 'type', and 'attr' (default
  63. is all off)
  64. --test-only=WORD report only warnings/errors containing WORD
  65. literally
  66. --fix EXPERIMENTAL - may create horrible results
  67. If correctable single-line errors exist, create
  68. "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
  69. with potential errors corrected to the preferred
  70. checkpatch style
  71. --ignore-perl-version override checking of perl version. expect
  72. runtime errors.
  73. -h, --help, --version display this help and exit
  74. When FILE is - read standard input.
  75. EOM
  76. exit($exitcode);
  77. }
  78. my $conf = which_conf($configuration_file);
  79. if (-f $conf) {
  80. my @conf_args;
  81. open(my $conffile, '<', "$conf")
  82. or warn "$P: Can't find a readable $configuration_file file $!\n";
  83. while (<$conffile>) {
  84. my $line = $_;
  85. $line =~ s/\s*\n?$//g;
  86. $line =~ s/^\s*//g;
  87. $line =~ s/\s+/ /g;
  88. next if ($line =~ m/^\s*#/);
  89. next if ($line =~ m/^\s*$/);
  90. my @words = split(" ", $line);
  91. foreach my $word (@words) {
  92. last if ($word =~ m/^#/);
  93. push (@conf_args, $word);
  94. }
  95. }
  96. close($conffile);
  97. unshift(@ARGV, @conf_args) if @conf_args;
  98. }
  99. GetOptions(
  100. 'q|quiet+' => \$quiet,
  101. 'tree!' => \$tree,
  102. 'signoff!' => \$chk_signoff,
  103. 'patch!' => \$chk_patch,
  104. 'emacs!' => \$emacs,
  105. 'terse!' => \$terse,
  106. 'f|file!' => \$file,
  107. 'subjective!' => \$check,
  108. 'strict!' => \$check,
  109. 'ignore=s' => \@ignore,
  110. 'types=s' => \@use,
  111. 'show-types!' => \$show_types,
  112. 'max-line-length=i' => \$max_line_length,
  113. 'root=s' => \$root,
  114. 'summary!' => \$summary,
  115. 'mailback!' => \$mailback,
  116. 'summary-file!' => \$summary_file,
  117. 'fix!' => \$fix,
  118. 'ignore-perl-version!' => \$ignore_perl_version,
  119. 'debug=s' => \%debug,
  120. 'test-only=s' => \$tst_only,
  121. 'h|help' => \$help,
  122. 'version' => \$help
  123. ) or help(1);
  124. help(0) if ($help);
  125. my $exit = 0;
  126. if ($^V && $^V lt $minimum_perl_version) {
  127. printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
  128. if (!$ignore_perl_version) {
  129. exit(1);
  130. }
  131. }
  132. if ($#ARGV < 0) {
  133. print "$P: no input files\n";
  134. exit(1);
  135. }
  136. sub hash_save_array_words {
  137. my ($hashRef, $arrayRef) = @_;
  138. my @array = split(/,/, join(',', @$arrayRef));
  139. foreach my $word (@array) {
  140. $word =~ s/\s*\n?$//g;
  141. $word =~ s/^\s*//g;
  142. $word =~ s/\s+/ /g;
  143. $word =~ tr/[a-z]/[A-Z]/;
  144. next if ($word =~ m/^\s*#/);
  145. next if ($word =~ m/^\s*$/);
  146. $hashRef->{$word}++;
  147. }
  148. }
  149. sub hash_show_words {
  150. my ($hashRef, $prefix) = @_;
  151. if ($quiet == 0 && keys %$hashRef) {
  152. print "NOTE: $prefix message types:";
  153. foreach my $word (sort keys %$hashRef) {
  154. print " $word";
  155. }
  156. print "\n\n";
  157. }
  158. }
  159. hash_save_array_words(\%ignore_type, \@ignore);
  160. hash_save_array_words(\%use_type, \@use);
  161. my $dbg_values = 0;
  162. my $dbg_possible = 0;
  163. my $dbg_type = 0;
  164. my $dbg_attr = 0;
  165. for my $key (keys %debug) {
  166. ## no critic
  167. eval "\${dbg_$key} = '$debug{$key}';";
  168. die "$@" if ($@);
  169. }
  170. my $rpt_cleaners = 0;
  171. if ($terse) {
  172. $emacs = 1;
  173. $quiet++;
  174. }
  175. if ($tree) {
  176. if (defined $root) {
  177. if (!top_of_kernel_tree($root)) {
  178. die "$P: $root: --root does not point at a valid tree\n";
  179. }
  180. } else {
  181. if (top_of_kernel_tree('.')) {
  182. $root = '.';
  183. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  184. top_of_kernel_tree($1)) {
  185. $root = $1;
  186. }
  187. }
  188. if (!defined $root) {
  189. print "Must be run from the top-level dir. of a kernel tree\n";
  190. exit(2);
  191. }
  192. }
  193. my $emitted_corrupt = 0;
  194. our $Ident = qr{
  195. [A-Za-z_][A-Za-z\d_]*
  196. (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
  197. }x;
  198. our $Storage = qr{extern|static|asmlinkage};
  199. our $Sparse = qr{
  200. __user|
  201. __kernel|
  202. __force|
  203. __iomem|
  204. __must_check|
  205. __init_refok|
  206. __kprobes|
  207. __ref|
  208. __rcu
  209. }x;
  210. our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
  211. our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
  212. our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
  213. our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
  214. our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
  215. # Notes to $Attribute:
  216. # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
  217. our $Attribute = qr{
  218. const|
  219. __percpu|
  220. __nocast|
  221. __safe|
  222. __bitwise__|
  223. __packed__|
  224. __packed2__|
  225. __naked|
  226. __maybe_unused|
  227. __always_unused|
  228. __noreturn|
  229. __used|
  230. __cold|
  231. __noclone|
  232. __deprecated|
  233. __read_mostly|
  234. __kprobes|
  235. $InitAttribute|
  236. ____cacheline_aligned|
  237. ____cacheline_aligned_in_smp|
  238. ____cacheline_internodealigned_in_smp|
  239. __weak
  240. }x;
  241. our $Modifier;
  242. our $Inline = qr{inline|__always_inline|noinline};
  243. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  244. our $Lval = qr{$Ident(?:$Member)*};
  245. our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
  246. our $Binary = qr{(?i)0b[01]+$Int_type?};
  247. our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
  248. our $Int = qr{[0-9]+$Int_type?};
  249. our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
  250. our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
  251. our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
  252. our $Float = qr{$Float_hex|$Float_dec|$Float_int};
  253. our $Constant = qr{$Float|$Binary|$Hex|$Int};
  254. our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
  255. our $Compare = qr{<=|>=|==|!=|<|>};
  256. our $Arithmetic = qr{\+|-|\*|\/|%};
  257. our $Operators = qr{
  258. <=|>=|==|!=|
  259. =>|->|<<|>>|<|>|!|~|
  260. &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
  261. }x;
  262. our $NonptrType;
  263. our $NonptrTypeWithAttr;
  264. our $Type;
  265. our $Declare;
  266. our $NON_ASCII_UTF8 = qr{
  267. [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  268. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  269. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  270. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  271. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  272. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  273. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  274. }x;
  275. our $UTF8 = qr{
  276. [\x09\x0A\x0D\x20-\x7E] # ASCII
  277. | $NON_ASCII_UTF8
  278. }x;
  279. our $typeTypedefs = qr{(?x:
  280. (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
  281. atomic_t
  282. )};
  283. our $logFunctions = qr{(?x:
  284. printk(?:_ratelimited|_once|)|
  285. (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
  286. WARN(?:_RATELIMIT|_ONCE|)|
  287. panic|
  288. MODULE_[A-Z_]+|
  289. seq_vprintf|seq_printf|seq_puts
  290. )};
  291. our $signature_tags = qr{(?xi:
  292. Signed-off-by:|
  293. Acked-by:|
  294. Tested-by:|
  295. Reviewed-by:|
  296. Reported-by:|
  297. Suggested-by:|
  298. To:|
  299. Cc:
  300. )};
  301. our @typeList = (
  302. qr{void},
  303. qr{(?:unsigned\s+)?char},
  304. qr{(?:unsigned\s+)?short},
  305. qr{(?:unsigned\s+)?int},
  306. qr{(?:unsigned\s+)?long},
  307. qr{(?:unsigned\s+)?long\s+int},
  308. qr{(?:unsigned\s+)?long\s+long},
  309. qr{(?:unsigned\s+)?long\s+long\s+int},
  310. qr{unsigned},
  311. qr{float},
  312. qr{double},
  313. qr{bool},
  314. qr{struct\s+$Ident},
  315. qr{union\s+$Ident},
  316. qr{enum\s+$Ident},
  317. qr{${Ident}_t},
  318. qr{${Ident}_handler},
  319. qr{${Ident}_handler_fn},
  320. );
  321. our @typeListWithAttr = (
  322. @typeList,
  323. qr{struct\s+$InitAttribute\s+$Ident},
  324. qr{union\s+$InitAttribute\s+$Ident},
  325. );
  326. our @modifierList = (
  327. qr{fastcall},
  328. );
  329. our $allowed_asm_includes = qr{(?x:
  330. irq|
  331. memory
  332. )};
  333. # memory.h: ARM has a custom one
  334. sub build_types {
  335. my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
  336. my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
  337. my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
  338. $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
  339. $NonptrType = qr{
  340. (?:$Modifier\s+|const\s+)*
  341. (?:
  342. (?:typeof|__typeof__)\s*\([^\)]*\)|
  343. (?:$typeTypedefs\b)|
  344. (?:${all}\b)
  345. )
  346. (?:\s+$Modifier|\s+const)*
  347. }x;
  348. $NonptrTypeWithAttr = qr{
  349. (?:$Modifier\s+|const\s+)*
  350. (?:
  351. (?:typeof|__typeof__)\s*\([^\)]*\)|
  352. (?:$typeTypedefs\b)|
  353. (?:${allWithAttr}\b)
  354. )
  355. (?:\s+$Modifier|\s+const)*
  356. }x;
  357. $Type = qr{
  358. $NonptrType
  359. (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
  360. (?:\s+$Inline|\s+$Modifier)*
  361. }x;
  362. $Declare = qr{(?:$Storage\s+)?$Type};
  363. }
  364. build_types();
  365. our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
  366. # Using $balanced_parens, $LvalOrFunc, or $FuncArg
  367. # requires at least perl version v5.10.0
  368. # Any use must be runtime checked with $^V
  369. our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
  370. our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
  371. our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
  372. sub deparenthesize {
  373. my ($string) = @_;
  374. return "" if (!defined($string));
  375. $string =~ s@^\s*\(\s*@@g;
  376. $string =~ s@\s*\)\s*$@@g;
  377. $string =~ s@\s+@ @g;
  378. return $string;
  379. }
  380. sub seed_camelcase_file {
  381. my ($file) = @_;
  382. return if (!(-f $file));
  383. local $/;
  384. open(my $include_file, '<', "$file")
  385. or warn "$P: Can't read '$file' $!\n";
  386. my $text = <$include_file>;
  387. close($include_file);
  388. my @lines = split('\n', $text);
  389. foreach my $line (@lines) {
  390. next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
  391. if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
  392. $camelcase{$1} = 1;
  393. } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
  394. $camelcase{$1} = 1;
  395. } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
  396. $camelcase{$1} = 1;
  397. }
  398. }
  399. }
  400. my $camelcase_seeded = 0;
  401. sub seed_camelcase_includes {
  402. return if ($camelcase_seeded);
  403. my $files;
  404. my $camelcase_cache = "";
  405. my @include_files = ();
  406. $camelcase_seeded = 1;
  407. if (-d ".git") {
  408. my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
  409. chomp $git_last_include_commit;
  410. $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
  411. } else {
  412. my $last_mod_date = 0;
  413. $files = `find $root/include -name "*.h"`;
  414. @include_files = split('\n', $files);
  415. foreach my $file (@include_files) {
  416. my $date = POSIX::strftime("%Y%m%d%H%M",
  417. localtime((stat $file)[9]));
  418. $last_mod_date = $date if ($last_mod_date < $date);
  419. }
  420. $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
  421. }
  422. if ($camelcase_cache ne "" && -f $camelcase_cache) {
  423. open(my $camelcase_file, '<', "$camelcase_cache")
  424. or warn "$P: Can't read '$camelcase_cache' $!\n";
  425. while (<$camelcase_file>) {
  426. chomp;
  427. $camelcase{$_} = 1;
  428. }
  429. close($camelcase_file);
  430. return;
  431. }
  432. if (-d ".git") {
  433. $files = `git ls-files "include/*.h"`;
  434. @include_files = split('\n', $files);
  435. }
  436. foreach my $file (@include_files) {
  437. seed_camelcase_file($file);
  438. }
  439. if ($camelcase_cache ne "") {
  440. unlink glob ".checkpatch-camelcase.*";
  441. open(my $camelcase_file, '>', "$camelcase_cache")
  442. or warn "$P: Can't write '$camelcase_cache' $!\n";
  443. foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
  444. print $camelcase_file ("$_\n");
  445. }
  446. close($camelcase_file);
  447. }
  448. }
  449. $chk_signoff = 0 if ($file);
  450. my @rawlines = ();
  451. my @lines = ();
  452. my @fixed = ();
  453. my $vname;
  454. for my $filename (@ARGV) {
  455. my $FILE;
  456. if ($file) {
  457. open($FILE, '-|', "diff -u /dev/null $filename") ||
  458. die "$P: $filename: diff failed - $!\n";
  459. } elsif ($filename eq '-') {
  460. open($FILE, '<&STDIN');
  461. } else {
  462. open($FILE, '<', "$filename") ||
  463. die "$P: $filename: open failed - $!\n";
  464. }
  465. if ($filename eq '-') {
  466. $vname = 'Your patch';
  467. } else {
  468. $vname = $filename;
  469. }
  470. while (<$FILE>) {
  471. chomp;
  472. push(@rawlines, $_);
  473. }
  474. close($FILE);
  475. if (!process($filename)) {
  476. $exit = 1;
  477. }
  478. @rawlines = ();
  479. @lines = ();
  480. @fixed = ();
  481. }
  482. exit($exit);
  483. sub top_of_kernel_tree {
  484. my ($root) = @_;
  485. my @tree_check = (
  486. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  487. "README", "Documentation", "arch", "include", "drivers",
  488. "fs", "init", "ipc", "kernel", "lib", "scripts",
  489. );
  490. foreach my $check (@tree_check) {
  491. if (! -e $root . '/' . $check) {
  492. return 0;
  493. }
  494. }
  495. return 1;
  496. }
  497. sub parse_email {
  498. my ($formatted_email) = @_;
  499. my $name = "";
  500. my $address = "";
  501. my $comment = "";
  502. if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
  503. $name = $1;
  504. $address = $2;
  505. $comment = $3 if defined $3;
  506. } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
  507. $address = $1;
  508. $comment = $2 if defined $2;
  509. } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
  510. $address = $1;
  511. $comment = $2 if defined $2;
  512. $formatted_email =~ s/$address.*$//;
  513. $name = $formatted_email;
  514. $name = trim($name);
  515. $name =~ s/^\"|\"$//g;
  516. # If there's a name left after stripping spaces and
  517. # leading quotes, and the address doesn't have both
  518. # leading and trailing angle brackets, the address
  519. # is invalid. ie:
  520. # "joe smith joe@smith.com" bad
  521. # "joe smith <joe@smith.com" bad
  522. if ($name ne "" && $address !~ /^<[^>]+>$/) {
  523. $name = "";
  524. $address = "";
  525. $comment = "";
  526. }
  527. }
  528. $name = trim($name);
  529. $name =~ s/^\"|\"$//g;
  530. $address = trim($address);
  531. $address =~ s/^\<|\>$//g;
  532. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  533. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  534. $name = "\"$name\"";
  535. }
  536. return ($name, $address, $comment);
  537. }
  538. sub format_email {
  539. my ($name, $address) = @_;
  540. my $formatted_email;
  541. $name = trim($name);
  542. $name =~ s/^\"|\"$//g;
  543. $address = trim($address);
  544. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  545. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  546. $name = "\"$name\"";
  547. }
  548. if ("$name" eq "") {
  549. $formatted_email = "$address";
  550. } else {
  551. $formatted_email = "$name <$address>";
  552. }
  553. return $formatted_email;
  554. }
  555. sub which_conf {
  556. my ($conf) = @_;
  557. foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
  558. if (-e "$path/$conf") {
  559. return "$path/$conf";
  560. }
  561. }
  562. return "";
  563. }
  564. sub expand_tabs {
  565. my ($str) = @_;
  566. my $res = '';
  567. my $n = 0;
  568. for my $c (split(//, $str)) {
  569. if ($c eq "\t") {
  570. $res .= ' ';
  571. $n++;
  572. for (; ($n % 8) != 0; $n++) {
  573. $res .= ' ';
  574. }
  575. next;
  576. }
  577. $res .= $c;
  578. $n++;
  579. }
  580. return $res;
  581. }
  582. sub copy_spacing {
  583. (my $res = shift) =~ tr/\t/ /c;
  584. return $res;
  585. }
  586. sub line_stats {
  587. my ($line) = @_;
  588. # Drop the diff line leader and expand tabs
  589. $line =~ s/^.//;
  590. $line = expand_tabs($line);
  591. # Pick the indent from the front of the line.
  592. my ($white) = ($line =~ /^(\s*)/);
  593. return (length($line), length($white));
  594. }
  595. my $sanitise_quote = '';
  596. sub sanitise_line_reset {
  597. my ($in_comment) = @_;
  598. if ($in_comment) {
  599. $sanitise_quote = '*/';
  600. } else {
  601. $sanitise_quote = '';
  602. }
  603. }
  604. sub sanitise_line {
  605. my ($line) = @_;
  606. my $res = '';
  607. my $l = '';
  608. my $qlen = 0;
  609. my $off = 0;
  610. my $c;
  611. # Always copy over the diff marker.
  612. $res = substr($line, 0, 1);
  613. for ($off = 1; $off < length($line); $off++) {
  614. $c = substr($line, $off, 1);
  615. # Comments we are wacking completly including the begin
  616. # and end, all to $;.
  617. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
  618. $sanitise_quote = '*/';
  619. substr($res, $off, 2, "$;$;");
  620. $off++;
  621. next;
  622. }
  623. if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
  624. $sanitise_quote = '';
  625. substr($res, $off, 2, "$;$;");
  626. $off++;
  627. next;
  628. }
  629. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
  630. $sanitise_quote = '//';
  631. substr($res, $off, 2, $sanitise_quote);
  632. $off++;
  633. next;
  634. }
  635. # A \ in a string means ignore the next character.
  636. if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
  637. $c eq "\\") {
  638. substr($res, $off, 2, 'XX');
  639. $off++;
  640. next;
  641. }
  642. # Regular quotes.
  643. if ($c eq "'" || $c eq '"') {
  644. if ($sanitise_quote eq '') {
  645. $sanitise_quote = $c;
  646. substr($res, $off, 1, $c);
  647. next;
  648. } elsif ($sanitise_quote eq $c) {
  649. $sanitise_quote = '';
  650. }
  651. }
  652. #print "c<$c> SQ<$sanitise_quote>\n";
  653. if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
  654. substr($res, $off, 1, $;);
  655. } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
  656. substr($res, $off, 1, $;);
  657. } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
  658. substr($res, $off, 1, 'X');
  659. } else {
  660. substr($res, $off, 1, $c);
  661. }
  662. }
  663. if ($sanitise_quote eq '//') {
  664. $sanitise_quote = '';
  665. }
  666. # The pathname on a #include may be surrounded by '<' and '>'.
  667. if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
  668. my $clean = 'X' x length($1);
  669. $res =~ s@\<.*\>@<$clean>@;
  670. # The whole of a #error is a string.
  671. } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
  672. my $clean = 'X' x length($1);
  673. $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
  674. }
  675. return $res;
  676. }
  677. sub get_quoted_string {
  678. my ($line, $rawline) = @_;
  679. return "" if ($line !~ m/(\"[X]+\")/g);
  680. return substr($rawline, $-[0], $+[0] - $-[0]);
  681. }
  682. sub ctx_statement_block {
  683. my ($linenr, $remain, $off) = @_;
  684. my $line = $linenr - 1;
  685. my $blk = '';
  686. my $soff = $off;
  687. my $coff = $off - 1;
  688. my $coff_set = 0;
  689. my $loff = 0;
  690. my $type = '';
  691. my $level = 0;
  692. my @stack = ();
  693. my $p;
  694. my $c;
  695. my $len = 0;
  696. my $remainder;
  697. while (1) {
  698. @stack = (['', 0]) if ($#stack == -1);
  699. #warn "CSB: blk<$blk> remain<$remain>\n";
  700. # If we are about to drop off the end, pull in more
  701. # context.
  702. if ($off >= $len) {
  703. for (; $remain > 0; $line++) {
  704. last if (!defined $lines[$line]);
  705. next if ($lines[$line] =~ /^-/);
  706. $remain--;
  707. $loff = $len;
  708. $blk .= $lines[$line] . "\n";
  709. $len = length($blk);
  710. $line++;
  711. last;
  712. }
  713. # Bail if there is no further context.
  714. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  715. if ($off >= $len) {
  716. last;
  717. }
  718. if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
  719. $level++;
  720. $type = '#';
  721. }
  722. }
  723. $p = $c;
  724. $c = substr($blk, $off, 1);
  725. $remainder = substr($blk, $off);
  726. #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
  727. # Handle nested #if/#else.
  728. if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
  729. push(@stack, [ $type, $level ]);
  730. } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
  731. ($type, $level) = @{$stack[$#stack - 1]};
  732. } elsif ($remainder =~ /^#\s*endif\b/) {
  733. ($type, $level) = @{pop(@stack)};
  734. }
  735. # Statement ends at the ';' or a close '}' at the
  736. # outermost level.
  737. if ($level == 0 && $c eq ';') {
  738. last;
  739. }
  740. # An else is really a conditional as long as its not else if
  741. if ($level == 0 && $coff_set == 0 &&
  742. (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
  743. $remainder =~ /^(else)(?:\s|{)/ &&
  744. $remainder !~ /^else\s+if\b/) {
  745. $coff = $off + length($1) - 1;
  746. $coff_set = 1;
  747. #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
  748. #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
  749. }
  750. if (($type eq '' || $type eq '(') && $c eq '(') {
  751. $level++;
  752. $type = '(';
  753. }
  754. if ($type eq '(' && $c eq ')') {
  755. $level--;
  756. $type = ($level != 0)? '(' : '';
  757. if ($level == 0 && $coff < $soff) {
  758. $coff = $off;
  759. $coff_set = 1;
  760. #warn "CSB: mark coff<$coff>\n";
  761. }
  762. }
  763. if (($type eq '' || $type eq '{') && $c eq '{') {
  764. $level++;
  765. $type = '{';
  766. }
  767. if ($type eq '{' && $c eq '}') {
  768. $level--;
  769. $type = ($level != 0)? '{' : '';
  770. if ($level == 0) {
  771. if (substr($blk, $off + 1, 1) eq ';') {
  772. $off++;
  773. }
  774. last;
  775. }
  776. }
  777. # Preprocessor commands end at the newline unless escaped.
  778. if ($type eq '#' && $c eq "\n" && $p ne "\\") {
  779. $level--;
  780. $type = '';
  781. $off++;
  782. last;
  783. }
  784. $off++;
  785. }
  786. # We are truly at the end, so shuffle to the next line.
  787. if ($off == $len) {
  788. $loff = $len + 1;
  789. $line++;
  790. $remain--;
  791. }
  792. my $statement = substr($blk, $soff, $off - $soff + 1);
  793. my $condition = substr($blk, $soff, $coff - $soff + 1);
  794. #warn "STATEMENT<$statement>\n";
  795. #warn "CONDITION<$condition>\n";
  796. #print "coff<$coff> soff<$off> loff<$loff>\n";
  797. return ($statement, $condition,
  798. $line, $remain + 1, $off - $loff + 1, $level);
  799. }
  800. sub statement_lines {
  801. my ($stmt) = @_;
  802. # Strip the diff line prefixes and rip blank lines at start and end.
  803. $stmt =~ s/(^|\n)./$1/g;
  804. $stmt =~ s/^\s*//;
  805. $stmt =~ s/\s*$//;
  806. my @stmt_lines = ($stmt =~ /\n/g);
  807. return $#stmt_lines + 2;
  808. }
  809. sub statement_rawlines {
  810. my ($stmt) = @_;
  811. my @stmt_lines = ($stmt =~ /\n/g);
  812. return $#stmt_lines + 2;
  813. }
  814. sub statement_block_size {
  815. my ($stmt) = @_;
  816. $stmt =~ s/(^|\n)./$1/g;
  817. $stmt =~ s/^\s*{//;
  818. $stmt =~ s/}\s*$//;
  819. $stmt =~ s/^\s*//;
  820. $stmt =~ s/\s*$//;
  821. my @stmt_lines = ($stmt =~ /\n/g);
  822. my @stmt_statements = ($stmt =~ /;/g);
  823. my $stmt_lines = $#stmt_lines + 2;
  824. my $stmt_statements = $#stmt_statements + 1;
  825. if ($stmt_lines > $stmt_statements) {
  826. return $stmt_lines;
  827. } else {
  828. return $stmt_statements;
  829. }
  830. }
  831. sub ctx_statement_full {
  832. my ($linenr, $remain, $off) = @_;
  833. my ($statement, $condition, $level);
  834. my (@chunks);
  835. # Grab the first conditional/block pair.
  836. ($statement, $condition, $linenr, $remain, $off, $level) =
  837. ctx_statement_block($linenr, $remain, $off);
  838. #print "F: c<$condition> s<$statement> remain<$remain>\n";
  839. push(@chunks, [ $condition, $statement ]);
  840. if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
  841. return ($level, $linenr, @chunks);
  842. }
  843. # Pull in the following conditional/block pairs and see if they
  844. # could continue the statement.
  845. for (;;) {
  846. ($statement, $condition, $linenr, $remain, $off, $level) =
  847. ctx_statement_block($linenr, $remain, $off);
  848. #print "C: c<$condition> s<$statement> remain<$remain>\n";
  849. last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
  850. #print "C: push\n";
  851. push(@chunks, [ $condition, $statement ]);
  852. }
  853. return ($level, $linenr, @chunks);
  854. }
  855. sub ctx_block_get {
  856. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  857. my $line;
  858. my $start = $linenr - 1;
  859. my $blk = '';
  860. my @o;
  861. my @c;
  862. my @res = ();
  863. my $level = 0;
  864. my @stack = ($level);
  865. for ($line = $start; $remain > 0; $line++) {
  866. next if ($rawlines[$line] =~ /^-/);
  867. $remain--;
  868. $blk .= $rawlines[$line];
  869. # Handle nested #if/#else.
  870. if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
  871. push(@stack, $level);
  872. } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
  873. $level = $stack[$#stack - 1];
  874. } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
  875. $level = pop(@stack);
  876. }
  877. foreach my $c (split(//, $lines[$line])) {
  878. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  879. if ($off > 0) {
  880. $off--;
  881. next;
  882. }
  883. if ($c eq $close && $level > 0) {
  884. $level--;
  885. last if ($level == 0);
  886. } elsif ($c eq $open) {
  887. $level++;
  888. }
  889. }
  890. if (!$outer || $level <= 1) {
  891. push(@res, $rawlines[$line]);
  892. }
  893. last if ($level == 0);
  894. }
  895. return ($level, @res);
  896. }
  897. sub ctx_block_outer {
  898. my ($linenr, $remain) = @_;
  899. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  900. return @r;
  901. }
  902. sub ctx_block {
  903. my ($linenr, $remain) = @_;
  904. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  905. return @r;
  906. }
  907. sub ctx_statement {
  908. my ($linenr, $remain, $off) = @_;
  909. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  910. return @r;
  911. }
  912. sub ctx_block_level {
  913. my ($linenr, $remain) = @_;
  914. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  915. }
  916. sub ctx_statement_level {
  917. my ($linenr, $remain, $off) = @_;
  918. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  919. }
  920. sub ctx_locate_comment {
  921. my ($first_line, $end_line) = @_;
  922. # Catch a comment on the end of the line itself.
  923. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
  924. return $current_comment if (defined $current_comment);
  925. # Look through the context and try and figure out if there is a
  926. # comment.
  927. my $in_comment = 0;
  928. $current_comment = '';
  929. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  930. my $line = $rawlines[$linenr - 1];
  931. #warn " $line\n";
  932. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  933. $in_comment = 1;
  934. }
  935. if ($line =~ m@/\*@) {
  936. $in_comment = 1;
  937. }
  938. if (!$in_comment && $current_comment ne '') {
  939. $current_comment = '';
  940. }
  941. $current_comment .= $line . "\n" if ($in_comment);
  942. if ($line =~ m@\*/@) {
  943. $in_comment = 0;
  944. }
  945. }
  946. chomp($current_comment);
  947. return($current_comment);
  948. }
  949. sub ctx_has_comment {
  950. my ($first_line, $end_line) = @_;
  951. my $cmt = ctx_locate_comment($first_line, $end_line);
  952. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  953. ##print "CMMT: $cmt\n";
  954. return ($cmt ne '');
  955. }
  956. sub raw_line {
  957. my ($linenr, $cnt) = @_;
  958. my $offset = $linenr - 1;
  959. $cnt++;
  960. my $line;
  961. while ($cnt) {
  962. $line = $rawlines[$offset++];
  963. next if (defined($line) && $line =~ /^-/);
  964. $cnt--;
  965. }
  966. return $line;
  967. }
  968. sub cat_vet {
  969. my ($vet) = @_;
  970. my ($res, $coded);
  971. $res = '';
  972. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  973. $res .= $1;
  974. if ($2 ne '') {
  975. $coded = sprintf("^%c", unpack('C', $2) + 64);
  976. $res .= $coded;
  977. }
  978. }
  979. $res =~ s/$/\$/;
  980. return $res;
  981. }
  982. my $av_preprocessor = 0;
  983. my $av_pending;
  984. my @av_paren_type;
  985. my $av_pend_colon;
  986. sub annotate_reset {
  987. $av_preprocessor = 0;
  988. $av_pending = '_';
  989. @av_paren_type = ('E');
  990. $av_pend_colon = 'O';
  991. }
  992. sub annotate_values {
  993. my ($stream, $type) = @_;
  994. my $res;
  995. my $var = '_' x length($stream);
  996. my $cur = $stream;
  997. print "$stream\n" if ($dbg_values > 1);
  998. while (length($cur)) {
  999. @av_paren_type = ('E') if ($#av_paren_type < 0);
  1000. print " <" . join('', @av_paren_type) .
  1001. "> <$type> <$av_pending>" if ($dbg_values > 1);
  1002. if ($cur =~ /^(\s+)/o) {
  1003. print "WS($1)\n" if ($dbg_values > 1);
  1004. if ($1 =~ /\n/ && $av_preprocessor) {
  1005. $type = pop(@av_paren_type);
  1006. $av_preprocessor = 0;
  1007. }
  1008. } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
  1009. print "CAST($1)\n" if ($dbg_values > 1);
  1010. push(@av_paren_type, $type);
  1011. $type = 'c';
  1012. } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
  1013. print "DECLARE($1)\n" if ($dbg_values > 1);
  1014. $type = 'T';
  1015. } elsif ($cur =~ /^($Modifier)\s*/) {
  1016. print "MODIFIER($1)\n" if ($dbg_values > 1);
  1017. $type = 'T';
  1018. } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
  1019. print "DEFINE($1,$2)\n" if ($dbg_values > 1);
  1020. $av_preprocessor = 1;
  1021. push(@av_paren_type, $type);
  1022. if ($2 ne '') {
  1023. $av_pending = 'N';
  1024. }
  1025. $type = 'E';
  1026. } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
  1027. print "UNDEF($1)\n" if ($dbg_values > 1);
  1028. $av_preprocessor = 1;
  1029. push(@av_paren_type, $type);
  1030. } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
  1031. print "PRE_START($1)\n" if ($dbg_values > 1);
  1032. $av_preprocessor = 1;
  1033. push(@av_paren_type, $type);
  1034. push(@av_paren_type, $type);
  1035. $type = 'E';
  1036. } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
  1037. print "PRE_RESTART($1)\n" if ($dbg_values > 1);
  1038. $av_preprocessor = 1;
  1039. push(@av_paren_type, $av_paren_type[$#av_paren_type]);
  1040. $type = 'E';
  1041. } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
  1042. print "PRE_END($1)\n" if ($dbg_values > 1);
  1043. $av_preprocessor = 1;
  1044. # Assume all arms of the conditional end as this
  1045. # one does, and continue as if the #endif was not here.
  1046. pop(@av_paren_type);
  1047. push(@av_paren_type, $type);
  1048. $type = 'E';
  1049. } elsif ($cur =~ /^(\\\n)/o) {
  1050. print "PRECONT($1)\n" if ($dbg_values > 1);
  1051. } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
  1052. print "ATTR($1)\n" if ($dbg_values > 1);
  1053. $av_pending = $type;
  1054. $type = 'N';
  1055. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  1056. print "SIZEOF($1)\n" if ($dbg_values > 1);
  1057. if (defined $2) {
  1058. $av_pending = 'V';
  1059. }
  1060. $type = 'N';
  1061. } elsif ($cur =~ /^(if|while|for)\b/o) {
  1062. print "COND($1)\n" if ($dbg_values > 1);
  1063. $av_pending = 'E';
  1064. $type = 'N';
  1065. } elsif ($cur =~/^(case)/o) {
  1066. print "CASE($1)\n" if ($dbg_values > 1);
  1067. $av_pend_colon = 'C';
  1068. $type = 'N';
  1069. } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
  1070. print "KEYWORD($1)\n" if ($dbg_values > 1);
  1071. $type = 'N';
  1072. } elsif ($cur =~ /^(\()/o) {
  1073. print "PAREN('$1')\n" if ($dbg_values > 1);
  1074. push(@av_paren_type, $av_pending);
  1075. $av_pending = '_';
  1076. $type = 'N';
  1077. } elsif ($cur =~ /^(\))/o) {
  1078. my $new_type = pop(@av_paren_type);
  1079. if ($new_type ne '_') {
  1080. $type = $new_type;
  1081. print "PAREN('$1') -> $type\n"
  1082. if ($dbg_values > 1);
  1083. } else {
  1084. print "PAREN('$1')\n" if ($dbg_values > 1);
  1085. }
  1086. } elsif ($cur =~ /^($Ident)\s*\(/o) {
  1087. print "FUNC($1)\n" if ($dbg_values > 1);
  1088. $type = 'V';
  1089. $av_pending = 'V';
  1090. } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
  1091. if (defined $2 && $type eq 'C' || $type eq 'T') {
  1092. $av_pend_colon = 'B';
  1093. } elsif ($type eq 'E') {
  1094. $av_pend_colon = 'L';
  1095. }
  1096. print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
  1097. $type = 'V';
  1098. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  1099. print "IDENT($1)\n" if ($dbg_values > 1);
  1100. $type = 'V';
  1101. } elsif ($cur =~ /^($Assignment)/o) {
  1102. print "ASSIGN($1)\n" if ($dbg_values > 1);
  1103. $type = 'N';
  1104. } elsif ($cur =~/^(;|{|})/) {
  1105. print "END($1)\n" if ($dbg_values > 1);
  1106. $type = 'E';
  1107. $av_pend_colon = 'O';
  1108. } elsif ($cur =~/^(,)/) {
  1109. print "COMMA($1)\n" if ($dbg_values > 1);
  1110. $type = 'C';
  1111. } elsif ($cur =~ /^(\?)/o) {
  1112. print "QUESTION($1)\n" if ($dbg_values > 1);
  1113. $type = 'N';
  1114. } elsif ($cur =~ /^(:)/o) {
  1115. print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
  1116. substr($var, length($res), 1, $av_pend_colon);
  1117. if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
  1118. $type = 'E';
  1119. } else {
  1120. $type = 'N';
  1121. }
  1122. $av_pend_colon = 'O';
  1123. } elsif ($cur =~ /^(\[)/o) {
  1124. print "CLOSE($1)\n" if ($dbg_values > 1);
  1125. $type = 'N';
  1126. } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
  1127. my $variant;
  1128. print "OPV($1)\n" if ($dbg_values > 1);
  1129. if ($type eq 'V') {
  1130. $variant = 'B';
  1131. } else {
  1132. $variant = 'U';
  1133. }
  1134. substr($var, length($res), 1, $variant);
  1135. $type = 'N';
  1136. } elsif ($cur =~ /^($Operators)/o) {
  1137. print "OP($1)\n" if ($dbg_values > 1);
  1138. if ($1 ne '++' && $1 ne '--') {
  1139. $type = 'N';
  1140. }
  1141. } elsif ($cur =~ /(^.)/o) {
  1142. print "C($1)\n" if ($dbg_values > 1);
  1143. }
  1144. if (defined $1) {
  1145. $cur = substr($cur, length($1));
  1146. $res .= $type x length($1);
  1147. }
  1148. }
  1149. return ($res, $var);
  1150. }
  1151. sub possible {
  1152. my ($possible, $line) = @_;
  1153. my $notPermitted = qr{(?:
  1154. ^(?:
  1155. $Modifier|
  1156. $Storage|
  1157. $Type|
  1158. DEFINE_\S+
  1159. )$|
  1160. ^(?:
  1161. goto|
  1162. return|
  1163. case|
  1164. else|
  1165. asm|__asm__|
  1166. do|
  1167. \#|
  1168. \#\#|
  1169. )(?:\s|$)|
  1170. ^(?:typedef|struct|enum)\b
  1171. )}x;
  1172. warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
  1173. if ($possible !~ $notPermitted) {
  1174. # Check for modifiers.
  1175. $possible =~ s/\s*$Storage\s*//g;
  1176. $possible =~ s/\s*$Sparse\s*//g;
  1177. if ($possible =~ /^\s*$/) {
  1178. } elsif ($possible =~ /\s/) {
  1179. $possible =~ s/\s*$Type\s*//g;
  1180. for my $modifier (split(' ', $possible)) {
  1181. if ($modifier !~ $notPermitted) {
  1182. warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
  1183. push(@modifierList, $modifier);
  1184. }
  1185. }
  1186. } else {
  1187. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  1188. push(@typeList, $possible);
  1189. }
  1190. build_types();
  1191. } else {
  1192. warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
  1193. }
  1194. }
  1195. my $prefix = '';
  1196. sub show_type {
  1197. return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
  1198. return !defined $ignore_type{$_[0]};
  1199. }
  1200. sub report {
  1201. if (!show_type($_[1]) ||
  1202. (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
  1203. return 0;
  1204. }
  1205. my $line;
  1206. if ($show_types) {
  1207. $line = "$prefix$_[0]:$_[1]: $_[2]\n";
  1208. } else {
  1209. $line = "$prefix$_[0]: $_[2]\n";
  1210. }
  1211. $line = (split('\n', $line))[0] . "\n" if ($terse);
  1212. push(our @report, $line);
  1213. return 1;
  1214. }
  1215. sub report_dump {
  1216. our @report;
  1217. }
  1218. sub ERROR {
  1219. if (report("ERROR", $_[0], $_[1])) {
  1220. our $clean = 0;
  1221. our $cnt_error++;
  1222. return 1;
  1223. }
  1224. return 0;
  1225. }
  1226. sub WARN {
  1227. if (report("WARNING", $_[0], $_[1])) {
  1228. our $clean = 0;
  1229. our $cnt_warn++;
  1230. return 1;
  1231. }
  1232. return 0;
  1233. }
  1234. sub CHK {
  1235. if ($check && report("CHECK", $_[0], $_[1])) {
  1236. our $clean = 0;
  1237. our $cnt_chk++;
  1238. return 1;
  1239. }
  1240. return 0;
  1241. }
  1242. sub check_absolute_file {
  1243. my ($absolute, $herecurr) = @_;
  1244. my $file = $absolute;
  1245. ##print "absolute<$absolute>\n";
  1246. # See if any suffix of this path is a path within the tree.
  1247. while ($file =~ s@^[^/]*/@@) {
  1248. if (-f "$root/$file") {
  1249. ##print "file<$file>\n";
  1250. last;
  1251. }
  1252. }
  1253. if (! -f _) {
  1254. return 0;
  1255. }
  1256. # It is, so see if the prefix is acceptable.
  1257. my $prefix = $absolute;
  1258. substr($prefix, -length($file)) = '';
  1259. ##print "prefix<$prefix>\n";
  1260. if ($prefix ne ".../") {
  1261. WARN("USE_RELATIVE_PATH",
  1262. "use relative pathname instead of absolute in changelog text\n" . $herecurr);
  1263. }
  1264. }
  1265. sub trim {
  1266. my ($string) = @_;
  1267. $string =~ s/^\s+|\s+$//g;
  1268. return $string;
  1269. }
  1270. sub ltrim {
  1271. my ($string) = @_;
  1272. $string =~ s/^\s+//;
  1273. return $string;
  1274. }
  1275. sub rtrim {
  1276. my ($string) = @_;
  1277. $string =~ s/\s+$//;
  1278. return $string;
  1279. }
  1280. sub string_find_replace {
  1281. my ($string, $find, $replace) = @_;
  1282. $string =~ s/$find/$replace/g;
  1283. return $string;
  1284. }
  1285. sub tabify {
  1286. my ($leading) = @_;
  1287. my $source_indent = 8;
  1288. my $max_spaces_before_tab = $source_indent - 1;
  1289. my $spaces_to_tab = " " x $source_indent;
  1290. #convert leading spaces to tabs
  1291. 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
  1292. #Remove spaces before a tab
  1293. 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
  1294. return "$leading";
  1295. }
  1296. sub pos_last_openparen {
  1297. my ($line) = @_;
  1298. my $pos = 0;
  1299. my $opens = $line =~ tr/\(/\(/;
  1300. my $closes = $line =~ tr/\)/\)/;
  1301. my $last_openparen = 0;
  1302. if (($opens == 0) || ($closes >= $opens)) {
  1303. return -1;
  1304. }
  1305. my $len = length($line);
  1306. for ($pos = 0; $pos < $len; $pos++) {
  1307. my $string = substr($line, $pos);
  1308. if ($string =~ /^($FuncArg|$balanced_parens)/) {
  1309. $pos += length($1) - 1;
  1310. } elsif (substr($line, $pos, 1) eq '(') {
  1311. $last_openparen = $pos;
  1312. } elsif (index($string, '(') == -1) {
  1313. last;
  1314. }
  1315. }
  1316. return $last_openparen + 1;
  1317. }
  1318. sub process {
  1319. my $filename = shift;
  1320. my $linenr=0;
  1321. my $prevline="";
  1322. my $prevrawline="";
  1323. my $stashline="";
  1324. my $stashrawline="";
  1325. my $length;
  1326. my $indent;
  1327. my $previndent=0;
  1328. my $stashindent=0;
  1329. our $clean = 1;
  1330. my $signoff = 0;
  1331. my $is_patch = 0;
  1332. my $in_header_lines = 1;
  1333. my $in_commit_log = 0; #Scanning lines before patch
  1334. my $non_utf8_charset = 0;
  1335. our @report = ();
  1336. our $cnt_lines = 0;
  1337. our $cnt_error = 0;
  1338. our $cnt_warn = 0;
  1339. our $cnt_chk = 0;
  1340. # Trace the real file/line as we go.
  1341. my $realfile = '';
  1342. my $realline = 0;
  1343. my $realcnt = 0;
  1344. my $here = '';
  1345. my $in_comment = 0;
  1346. my $comment_edge = 0;
  1347. my $first_line = 0;
  1348. my $p1_prefix = '';
  1349. my $prev_values = 'E';
  1350. # suppression flags
  1351. my %suppress_ifbraces;
  1352. my %suppress_whiletrailers;
  1353. my %suppress_export;
  1354. my $suppress_statement = 0;
  1355. my %signatures = ();
  1356. # Pre-scan the patch sanitizing the lines.
  1357. # Pre-scan the patch looking for any __setup documentation.
  1358. #
  1359. my @setup_docs = ();
  1360. my $setup_docs = 0;
  1361. my $camelcase_file_seeded = 0;
  1362. sanitise_line_reset();
  1363. my $line;
  1364. foreach my $rawline (@rawlines) {
  1365. $linenr++;
  1366. $line = $rawline;
  1367. push(@fixed, $rawline) if ($fix);
  1368. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  1369. $setup_docs = 0;
  1370. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  1371. $setup_docs = 1;
  1372. }
  1373. #next;
  1374. }
  1375. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1376. $realline=$1-1;
  1377. if (defined $2) {
  1378. $realcnt=$3+1;
  1379. } else {
  1380. $realcnt=1+1;
  1381. }
  1382. $in_comment = 0;
  1383. # Guestimate if this is a continuing comment. Run
  1384. # the context looking for a comment "edge". If this
  1385. # edge is a close comment then we must be in a comment
  1386. # at context start.
  1387. my $edge;
  1388. my $cnt = $realcnt;
  1389. for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
  1390. next if (defined $rawlines[$ln - 1] &&
  1391. $rawlines[$ln - 1] =~ /^-/);
  1392. $cnt--;
  1393. #print "RAW<$rawlines[$ln - 1]>\n";
  1394. last if (!defined $rawlines[$ln - 1]);
  1395. if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
  1396. $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
  1397. ($edge) = $1;
  1398. last;
  1399. }
  1400. }
  1401. if (defined $edge && $edge eq '*/') {
  1402. $in_comment = 1;
  1403. }
  1404. # Guestimate if this is a continuing comment. If this
  1405. # is the start of a diff block and this line starts
  1406. # ' *' then it is very likely a comment.
  1407. if (!defined $edge &&
  1408. $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
  1409. {
  1410. $in_comment = 1;
  1411. }
  1412. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  1413. sanitise_line_reset($in_comment);
  1414. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  1415. # Standardise the strings and chars within the input to
  1416. # simplify matching -- only bother with positive lines.
  1417. $line = sanitise_line($rawline);
  1418. }
  1419. push(@lines, $line);
  1420. if ($realcnt > 1) {
  1421. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  1422. } else {
  1423. $realcnt = 0;
  1424. }
  1425. #print "==>$rawline\n";
  1426. #print "-->$line\n";
  1427. if ($setup_docs && $line =~ /^\+/) {
  1428. push(@setup_docs, $line);
  1429. }
  1430. }
  1431. $prefix = '';
  1432. $realcnt = 0;
  1433. $linenr = 0;
  1434. foreach my $line (@lines) {
  1435. $linenr++;
  1436. my $sline = $line; #copy of $line
  1437. $sline =~ s/$;/ /g; #with comments as spaces
  1438. my $rawline = $rawlines[$linenr - 1];
  1439. #extract the line range in the file after the patch is applied
  1440. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1441. $is_patch = 1;
  1442. $first_line = $linenr + 1;
  1443. $realline=$1-1;
  1444. if (defined $2) {
  1445. $realcnt=$3+1;
  1446. } else {
  1447. $realcnt=1+1;
  1448. }
  1449. annotate_reset();
  1450. $prev_values = 'E';
  1451. %suppress_ifbraces = ();
  1452. %suppress_whiletrailers = ();
  1453. %suppress_export = ();
  1454. $suppress_statement = 0;
  1455. next;
  1456. # track the line number as we move through the hunk, note that
  1457. # new versions of GNU diff omit the leading space on completely
  1458. # blank context lines so we need to count that too.
  1459. } elsif ($line =~ /^( |\+|$)/) {
  1460. $realline++;
  1461. $realcnt-- if ($realcnt != 0);
  1462. # Measure the line length and indent.
  1463. ($length, $indent) = line_stats($rawline);
  1464. # Track the previous line.
  1465. ($prevline, $stashline) = ($stashline, $line);
  1466. ($previndent, $stashindent) = ($stashindent, $indent);
  1467. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  1468. #warn "line<$line>\n";
  1469. } elsif ($realcnt == 1) {
  1470. $realcnt--;
  1471. }
  1472. my $hunk_line = ($realcnt != 0);
  1473. #make up the handle for any error we report on this line
  1474. $prefix = "$filename:$realline: " if ($emacs && $file);
  1475. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  1476. $here = "#$linenr: " if (!$file);
  1477. $here = "#$realline: " if ($file);
  1478. # extract the filename as it passes
  1479. if ($line =~ /^diff --git.*?(\S+)$/) {
  1480. $realfile = $1;
  1481. $realfile =~ s@^([^/]*)/@@;
  1482. $in_commit_log = 0;
  1483. } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
  1484. $realfile = $1;
  1485. $realfile =~ s@^([^/]*)/@@;
  1486. $in_commit_log = 0;
  1487. $p1_prefix = $1;
  1488. if (!$file && $tree && $p1_prefix ne '' &&
  1489. -e "$root/$p1_prefix") {
  1490. WARN("PATCH_PREFIX",
  1491. "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
  1492. }
  1493. if ($realfile =~ m@^include/asm/@) {
  1494. ERROR("MODIFIED_INCLUDE_ASM",
  1495. "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  1496. }
  1497. next;
  1498. }
  1499. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  1500. my $hereline = "$here\n$rawline\n";
  1501. my $herecurr = "$here\n$rawline\n";
  1502. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  1503. $cnt_lines++ if ($realcnt != 0);
  1504. # Check for incorrect file permissions
  1505. if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
  1506. my $permhere = $here . "FILE: $realfile\n";
  1507. if ($realfile !~ m@scripts/@ &&
  1508. $realfile !~ /\.(py|pl|awk|sh)$/) {
  1509. ERROR("EXECUTE_PERMISSIONS",
  1510. "do not set execute permissions for source files\n" . $permhere);
  1511. }
  1512. }
  1513. # Check the patch for a signoff:
  1514. if ($line =~ /^\s*signed-off-by:/i) {
  1515. $signoff++;
  1516. $in_commit_log = 0;
  1517. }
  1518. # Check signature styles
  1519. if (!$in_header_lines &&
  1520. $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
  1521. my $space_before = $1;
  1522. my $sign_off = $2;
  1523. my $space_after = $3;
  1524. my $email = $4;
  1525. my $ucfirst_sign_off = ucfirst(lc($sign_off));
  1526. if ($sign_off !~ /$signature_tags/) {
  1527. WARN("BAD_SIGN_OFF",
  1528. "Non-standard signature: $sign_off\n" . $herecurr);
  1529. }
  1530. if (defined $space_before && $space_before ne "") {
  1531. if (WARN("BAD_SIGN_OFF",
  1532. "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
  1533. $fix) {
  1534. $fixed[$linenr - 1] =
  1535. "$ucfirst_sign_off $email";
  1536. }
  1537. }
  1538. if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
  1539. if (WARN("BAD_SIGN_OFF",
  1540. "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
  1541. $fix) {
  1542. $fixed[$linenr - 1] =
  1543. "$ucfirst_sign_off $email";
  1544. }
  1545. }
  1546. if (!defined $space_after || $space_after ne " ") {
  1547. if (WARN("BAD_SIGN_OFF",
  1548. "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
  1549. $fix) {
  1550. $fixed[$linenr - 1] =
  1551. "$ucfirst_sign_off $email";
  1552. }
  1553. }
  1554. my ($email_name, $email_address, $comment) = parse_email($email);
  1555. my $suggested_email = format_email(($email_name, $email_address));
  1556. if ($suggested_email eq "") {
  1557. ERROR("BAD_SIGN_OFF",
  1558. "Unrecognized email address: '$email'\n" . $herecurr);
  1559. } else {
  1560. my $dequoted = $suggested_email;
  1561. $dequoted =~ s/^"//;
  1562. $dequoted =~ s/" </ </;
  1563. # Don't force email to have quotes
  1564. # Allow just an angle bracketed address
  1565. if ("$dequoted$comment" ne $email &&
  1566. "<$email_address>$comment" ne $email &&
  1567. "$suggested_email$comment" ne $email) {
  1568. WARN("BAD_SIGN_OFF",
  1569. "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
  1570. }
  1571. }
  1572. # Check for duplicate signatures
  1573. my $sig_nospace = $line;
  1574. $sig_nospace =~ s/\s//g;
  1575. $sig_nospace = lc($sig_nospace);
  1576. if (defined $signatures{$sig_nospace}) {
  1577. WARN("BAD_SIGN_OFF",
  1578. "Duplicate signature\n" . $herecurr);
  1579. } else {
  1580. $signatures{$sig_nospace} = 1;
  1581. }
  1582. }
  1583. # Check for wrappage within a valid hunk of the file
  1584. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  1585. ERROR("CORRUPTED_PATCH",
  1586. "patch seems to be corrupt (line wrapped?)\n" .
  1587. $herecurr) if (!$emitted_corrupt++);
  1588. }
  1589. # Check for absolute kernel paths.
  1590. if ($tree) {
  1591. while ($line =~ m{(?:^|\s)(/\S*)}g) {
  1592. my $file = $1;
  1593. if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
  1594. check_absolute_file($1, $herecurr)) {
  1595. #
  1596. } else {
  1597. check_absolute_file($file, $herecurr);
  1598. }
  1599. }
  1600. }
  1601. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  1602. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  1603. $rawline !~ m/^$UTF8*$/) {
  1604. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  1605. my $blank = copy_spacing($rawline);
  1606. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  1607. my $hereptr = "$hereline$ptr\n";
  1608. CHK("INVALID_UTF8",
  1609. "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  1610. }
  1611. # Check if it's the start of a commit log
  1612. # (not a header line and we haven't seen the patch filename)
  1613. if ($in_header_lines && $realfile =~ /^$/ &&
  1614. $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
  1615. $in_header_lines = 0;
  1616. $in_commit_log = 1;
  1617. }
  1618. # Check if there is UTF-8 in a commit log when a mail header has explicitly
  1619. # declined it, i.e defined some charset where it is missing.
  1620. if ($in_header_lines &&
  1621. $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
  1622. $1 !~ /utf-8/i) {
  1623. $non_utf8_charset = 1;
  1624. }
  1625. if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
  1626. $rawline =~ /$NON_ASCII_UTF8/) {
  1627. WARN("UTF8_BEFORE_PATCH",
  1628. "8-bit UTF-8 used in possible commit log\n" . $herecurr);
  1629. }
  1630. # ignore non-hunk lines and lines being removed
  1631. next if (!$hunk_line || $line =~ /^-/);
  1632. #trailing whitespace
  1633. if ($line =~ /^\+.*\015/) {
  1634. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1635. if (ERROR("DOS_LINE_ENDINGS",
  1636. "DOS line endings\n" . $herevet) &&
  1637. $fix) {
  1638. $fixed[$linenr - 1] =~ s/[\s\015]+$//;
  1639. }
  1640. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  1641. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1642. if (ERROR("TRAILING_WHITESPACE",
  1643. "trailing whitespace\n" . $herevet) &&
  1644. $fix) {
  1645. $fixed[$linenr - 1] =~ s/\s+$//;
  1646. }
  1647. $rpt_cleaners = 1;
  1648. }
  1649. # check for Kconfig help text having a real description
  1650. # Only applies when adding the entry originally, after that we do not have
  1651. # sufficient context to determine whether it is indeed long enough.
  1652. if ($realfile =~ /Kconfig/ &&
  1653. $line =~ /.\s*config\s+/) {
  1654. my $length = 0;
  1655. my $cnt = $realcnt;
  1656. my $ln = $linenr + 1;
  1657. my $f;
  1658. my $is_start = 0;
  1659. my $is_end = 0;
  1660. for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
  1661. $f = $lines[$ln - 1];
  1662. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  1663. $is_end = $lines[$ln - 1] =~ /^\+/;
  1664. next if ($f =~ /^-/);
  1665. if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
  1666. $is_start = 1;
  1667. } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
  1668. $length = -1;
  1669. }
  1670. $f =~ s/^.//;
  1671. $f =~ s/#.*//;
  1672. $f =~ s/^\s+//;
  1673. next if ($f =~ /^$/);
  1674. if ($f =~ /^\s*config\s/) {
  1675. $is_end = 1;
  1676. last;
  1677. }
  1678. $length++;
  1679. }
  1680. WARN("CONFIG_DESCRIPTION",
  1681. "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
  1682. #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
  1683. }
  1684. # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
  1685. if ($realfile =~ /Kconfig/ &&
  1686. $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
  1687. WARN("CONFIG_EXPERIMENTAL",
  1688. "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
  1689. }
  1690. if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
  1691. ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
  1692. my $flag = $1;
  1693. my $replacement = {
  1694. 'EXTRA_AFLAGS' => 'asflags-y',
  1695. 'EXTRA_CFLAGS' => 'ccflags-y',
  1696. 'EXTRA_CPPFLAGS' => 'cppflags-y',
  1697. 'EXTRA_LDFLAGS' => 'ldflags-y',
  1698. };
  1699. WARN("DEPRECATED_VARIABLE",
  1700. "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
  1701. }
  1702. # check we are in a valid source file if not then ignore this hunk
  1703. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  1704. #line length limit
  1705. if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
  1706. $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
  1707. !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
  1708. $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
  1709. $length > $max_line_length)
  1710. {
  1711. WARN("LONG_LINE",
  1712. "line over $max_line_length characters\n" . $herecurr);
  1713. }
  1714. # Check for user-visible strings broken across lines, which breaks the ability
  1715. # to grep for the string. Limited to strings used as parameters (those
  1716. # following an open parenthesis), which almost completely eliminates false
  1717. # positives, as well as warning only once per parameter rather than once per
  1718. # line of the string. Make an exception when the previous string ends in a
  1719. # newline (multiple lines in one string constant) or \n\t (common in inline
  1720. # assembly to indent the instruction on the following line).
  1721. if ($line =~ /^\+\s*"/ &&
  1722. $prevline =~ /"\s*$/ &&
  1723. $prevline =~ /\(/ &&
  1724. $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
  1725. WARN("SPLIT_STRING",
  1726. "quoted string split across lines\n" . $hereprev);
  1727. }
  1728. # check for spaces before a quoted newline
  1729. if ($rawline =~ /^.*\".*\s\\n/) {
  1730. if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
  1731. "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
  1732. $fix) {
  1733. $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
  1734. }
  1735. }
  1736. # check for adding lines without a newline.
  1737. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  1738. WARN("MISSING_EOF_NEWLINE",
  1739. "adding a line without newline at end of file\n" . $herecurr);
  1740. }
  1741. # Blackfin: use hi/lo macros
  1742. if ($realfile =~ m@arch/blackfin/.*\.S$@) {
  1743. if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
  1744. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1745. ERROR("LO_MACRO",
  1746. "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
  1747. }
  1748. if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
  1749. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1750. ERROR("HI_MACRO",
  1751. "use the HI() macro, not (... >> 16)\n" . $herevet);
  1752. }
  1753. }
  1754. # check we are in a valid source file C or perl if not then ignore this hunk
  1755. next if ($realfile !~ /\.(h|c|pl)$/);
  1756. # at the beginning of a line any tabs must come first and anything
  1757. # more than 8 must use tabs.
  1758. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  1759. $rawline =~ /^\+\s* \s*/) {
  1760. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1761. $rpt_cleaners = 1;
  1762. if (ERROR("CODE_INDENT",
  1763. "code indent should use tabs where possible\n" . $herevet) &&
  1764. $fix) {
  1765. $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
  1766. }
  1767. }
  1768. # check for space before tabs.
  1769. if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
  1770. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1771. if (WARN("SPACE_BEFORE_TAB",
  1772. "please, no space before tabs\n" . $herevet) &&
  1773. $fix) {
  1774. $fixed[$linenr - 1] =~
  1775. s/(^\+.*) +\t/$1\t/;
  1776. }
  1777. }
  1778. # check for && or || at the start of a line
  1779. if ($rawline =~ /^\+\s*(&&|\|\|)/) {
  1780. CHK("LOGICAL_CONTINUATIONS",
  1781. "Logical continuations should be on the previous line\n" . $hereprev);
  1782. }
  1783. # check multi-line statement indentation matches previous line
  1784. if ($^V && $^V ge 5.10.0 &&
  1785. $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
  1786. $prevline =~ /^\+(\t*)(.*)$/;
  1787. my $oldindent = $1;
  1788. my $rest = $2;
  1789. my $pos = pos_last_openparen($rest);
  1790. if ($pos >= 0) {
  1791. $line =~ /^(\+| )([ \t]*)/;
  1792. my $newindent = $2;
  1793. my $goodtabindent = $oldindent .
  1794. "\t" x ($pos / 8) .
  1795. " " x ($pos % 8);
  1796. my $goodspaceindent = $oldindent . " " x $pos;
  1797. if ($newindent ne $goodtabindent &&
  1798. $newindent ne $goodspaceindent) {
  1799. if (CHK("PARENTHESIS_ALIGNMENT",
  1800. "Alignment should match open parenthesis\n" . $hereprev) &&
  1801. $fix && $line =~ /^\+/) {
  1802. $fixed[$linenr - 1] =~
  1803. s/^\+[ \t]*/\+$goodtabindent/;
  1804. }
  1805. }
  1806. }
  1807. }
  1808. if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
  1809. if (CHK("SPACING",
  1810. "No space is necessary after a cast\n" . $hereprev) &&
  1811. $fix) {
  1812. $fixed[$linenr - 1] =~
  1813. s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
  1814. }
  1815. }
  1816. if ($realfile =~ m@^(drivers/net/|net/)@ &&
  1817. $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
  1818. $rawline =~ /^\+[ \t]*\*/) {
  1819. WARN("NETWORKING_BLOCK_COMMENT_STYLE",
  1820. "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
  1821. }
  1822. if ($realfile =~ m@^(drivers/net/|net/)@ &&
  1823. $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
  1824. $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
  1825. $rawline =~ /^\+/ && #line is new
  1826. $rawline !~ /^\+[ \t]*\*/) { #no leading *
  1827. WARN("NETWORKING_BLOCK_COMMENT_STYLE",
  1828. "networking block comments start with * on subsequent lines\n" . $hereprev);
  1829. }
  1830. if ($realfile =~ m@^(drivers/net/|net/)@ &&
  1831. $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
  1832. $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
  1833. $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
  1834. $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
  1835. WARN("NETWORKING_BLOCK_COMMENT_STYLE",
  1836. "networking block comments put the trailing */ on a separate line\n" . $herecurr);
  1837. }
  1838. # check for spaces at the beginning of a line.
  1839. # Exceptions:
  1840. # 1) within comments
  1841. # 2) indented preprocessor commands
  1842. # 3) hanging labels
  1843. if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
  1844. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1845. if (WARN("LEADING_SPACE",
  1846. "please, no spaces at the start of a line\n" . $herevet) &&
  1847. $fix) {
  1848. $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
  1849. }
  1850. }
  1851. # check we are in a valid C source file if not then ignore this hunk
  1852. next if ($realfile !~ /\.(h|c)$/);
  1853. # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
  1854. if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
  1855. WARN("CONFIG_EXPERIMENTAL",
  1856. "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
  1857. }
  1858. # check for RCS/CVS revision markers
  1859. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  1860. WARN("CVS_KEYWORD",
  1861. "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  1862. }
  1863. # Blackfin: don't use __builtin_bfin_[cs]sync
  1864. if ($line =~ /__builtin_bfin_csync/) {
  1865. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1866. ERROR("CSYNC",
  1867. "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
  1868. }
  1869. if ($line =~ /__builtin_bfin_ssync/) {
  1870. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1871. ERROR("SSYNC",
  1872. "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
  1873. }
  1874. # check for old HOTPLUG __dev<foo> section markings
  1875. if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
  1876. WARN("HOTPLUG_SECTION",
  1877. "Using $1 is unnecessary\n" . $herecurr);
  1878. }
  1879. # Check for potential 'bare' types
  1880. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  1881. $realline_next);
  1882. #print "LINE<$line>\n";
  1883. if ($linenr >= $suppress_statement &&
  1884. $realcnt && $sline =~ /.\s*\S/) {
  1885. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  1886. ctx_statement_block($linenr, $realcnt, 0);
  1887. $stat =~ s/\n./\n /g;
  1888. $cond =~ s/\n./\n /g;
  1889. #print "linenr<$linenr> <$stat>\n";
  1890. # If this statement has no statement boundaries within
  1891. # it there is no point in retrying a statement scan
  1892. # until we hit end of it.
  1893. my $frag = $stat; $frag =~ s/;+\s*$//;
  1894. if ($frag !~ /(?:{|;)/) {
  1895. #print "skip<$line_nr_next>\n";
  1896. $suppress_statement = $line_nr_next;
  1897. }
  1898. # Find the real next line.
  1899. $realline_next = $line_nr_next;
  1900. if (defined $realline_next &&
  1901. (!defined $lines[$realline_next - 1] ||
  1902. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  1903. $realline_next++;
  1904. }
  1905. my $s = $stat;
  1906. $s =~ s/{.*$//s;
  1907. # Ignore goto labels.
  1908. if ($s =~ /$Ident:\*$/s) {
  1909. # Ignore functions being called
  1910. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  1911. } elsif ($s =~ /^.\s*else\b/s) {
  1912. # declarations always start with types
  1913. } 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) {
  1914. my $type = $1;
  1915. $type =~ s/\s+/ /g;
  1916. possible($type, "A:" . $s);
  1917. # definitions in global scope can only start with types
  1918. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  1919. possible($1, "B:" . $s);
  1920. }
  1921. # any (foo ... *) is a pointer cast, and foo is a type
  1922. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  1923. possible($1, "C:" . $s);
  1924. }
  1925. # Check for any sort of function declaration.
  1926. # int foo(something bar, other baz);
  1927. # void (*store_gdt)(x86_descr_ptr *);
  1928. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1929. my ($name_len) = length($1);
  1930. my $ctx = $s;
  1931. substr($ctx, 0, $name_len + 1, '');
  1932. $ctx =~ s/\)[^\)]*$//;
  1933. for my $arg (split(/\s*,\s*/, $ctx)) {
  1934. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1935. possible($1, "D:" . $s);
  1936. }
  1937. }
  1938. }
  1939. }
  1940. #
  1941. # Checks which may be anchored in the context.
  1942. #
  1943. # Check for switch () and associated case and default
  1944. # statements should be at the same indent.
  1945. if ($line=~/\bswitch\s*\(.*\)/) {
  1946. my $err = '';
  1947. my $sep = '';
  1948. my @ctx = ctx_block_outer($linenr, $realcnt);
  1949. shift(@ctx);
  1950. for my $ctx (@ctx) {
  1951. my ($clen, $cindent) = line_stats($ctx);
  1952. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1953. $indent != $cindent) {
  1954. $err .= "$sep$ctx\n";
  1955. $sep = '';
  1956. } else {
  1957. $sep = "[...]\n";
  1958. }
  1959. }
  1960. if ($err ne '') {
  1961. ERROR("SWITCH_CASE_INDENT_LEVEL",
  1962. "switch and case should be at the same indent\n$hereline$err");
  1963. }
  1964. }
  1965. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1966. # or if that brace on the next line is for something else
  1967. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1968. my $pre_ctx = "$1$2";
  1969. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1970. if ($line =~ /^\+\t{6,}/) {
  1971. WARN("DEEP_INDENTATION",
  1972. "Too many leading tabs - consider code refactoring\n" . $herecurr);
  1973. }
  1974. my $ctx_cnt = $realcnt - $#ctx - 1;
  1975. my $ctx = join("\n", @ctx);
  1976. my $ctx_ln = $linenr;
  1977. my $ctx_skip = $realcnt;
  1978. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  1979. defined $lines[$ctx_ln - 1] &&
  1980. $lines[$ctx_ln - 1] =~ /^-/)) {
  1981. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  1982. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  1983. $ctx_ln++;
  1984. }
  1985. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1986. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1987. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  1988. ERROR("OPEN_BRACE",
  1989. "that open brace { should be on the previous line\n" .
  1990. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  1991. }
  1992. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1993. $ctx =~ /\)\s*\;\s*$/ &&
  1994. defined $lines[$ctx_ln - 1])
  1995. {
  1996. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1997. if ($nindent > $indent) {
  1998. WARN("TRAILING_SEMICOLON",
  1999. "trailing semicolon indicates no statements, indent implies otherwise\n" .
  2000. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  2001. }
  2002. }
  2003. }
  2004. # Check relative indent for conditionals and blocks.
  2005. if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  2006. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  2007. ctx_statement_block($linenr, $realcnt, 0)
  2008. if (!defined $stat);
  2009. my ($s, $c) = ($stat, $cond);
  2010. substr($s, 0, length($c), '');
  2011. # Make sure we remove the line prefixes as we have
  2012. # none on the first line, and are going to readd them
  2013. # where necessary.
  2014. $s =~ s/\n./\n/gs;
  2015. # Find out how long the conditional actually is.
  2016. my @newlines = ($c =~ /\n/gs);
  2017. my $cond_lines = 1 + $#newlines;
  2018. # We want to check the first line inside the block
  2019. # starting at the end of the conditional, so remove:
  2020. # 1) any blank line termination
  2021. # 2) any opening brace { on end of the line
  2022. # 3) any do (...) {
  2023. my $continuation = 0;
  2024. my $check = 0;
  2025. $s =~ s/^.*\bdo\b//;
  2026. $s =~ s/^\s*{//;
  2027. if ($s =~ s/^\s*\\//) {
  2028. $continuation = 1;
  2029. }
  2030. if ($s =~ s/^\s*?\n//) {
  2031. $check = 1;
  2032. $cond_lines++;
  2033. }
  2034. # Also ignore a loop construct at the end of a
  2035. # preprocessor statement.
  2036. if (($prevline =~ /^.\s*#\s*define\s/ ||
  2037. $prevline =~ /\\\s*$/) && $continuation == 0) {
  2038. $check = 0;
  2039. }
  2040. my $cond_ptr = -1;
  2041. $continuation = 0;
  2042. while ($cond_ptr != $cond_lines) {
  2043. $cond_ptr = $cond_lines;
  2044. # If we see an #else/#elif then the code
  2045. # is not linear.
  2046. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  2047. $check = 0;
  2048. }
  2049. # Ignore:
  2050. # 1) blank lines, they should be at 0,
  2051. # 2) preprocessor lines, and
  2052. # 3) labels.
  2053. if ($continuation ||
  2054. $s =~ /^\s*?\n/ ||
  2055. $s =~ /^\s*#\s*?/ ||
  2056. $s =~ /^\s*$Ident\s*:/) {
  2057. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  2058. if ($s =~ s/^.*?\n//) {
  2059. $cond_lines++;
  2060. }
  2061. }
  2062. }
  2063. my (undef, $sindent) = line_stats("+" . $s);
  2064. my $stat_real = raw_line($linenr, $cond_lines);
  2065. # Check if either of these lines are modified, else
  2066. # this is not this patch's fault.
  2067. if (!defined($stat_real) ||
  2068. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  2069. $check = 0;
  2070. }
  2071. if (defined($stat_real) && $cond_lines > 1) {
  2072. $stat_real = "[...]\n$stat_real";
  2073. }
  2074. #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";
  2075. if ($check && (($sindent % 8) != 0 ||
  2076. ($sindent <= $indent && $s ne ''))) {
  2077. WARN("SUSPECT_CODE_INDENT",
  2078. "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  2079. }
  2080. }
  2081. # Track the 'values' across context and added lines.
  2082. my $opline = $line; $opline =~ s/^./ /;
  2083. my ($curr_values, $curr_vars) =
  2084. annotate_values($opline . "\n", $prev_values);
  2085. $curr_values = $prev_values . $curr_values;
  2086. if ($dbg_values) {
  2087. my $outline = $opline; $outline =~ s/\t/ /g;
  2088. print "$linenr > .$outline\n";
  2089. print "$linenr > $curr_values\n";
  2090. print "$linenr > $curr_vars\n";
  2091. }
  2092. $prev_values = substr($curr_values, -1);
  2093. #ignore lines not being added
  2094. next if ($line =~ /^[^\+]/);
  2095. # TEST: allow direct testing of the type matcher.
  2096. if ($dbg_type) {
  2097. if ($line =~ /^.\s*$Declare\s*$/) {
  2098. ERROR("TEST_TYPE",
  2099. "TEST: is type\n" . $herecurr);
  2100. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  2101. ERROR("TEST_NOT_TYPE",
  2102. "TEST: is not type ($1 is)\n". $herecurr);
  2103. }
  2104. next;
  2105. }
  2106. # TEST: allow direct testing of the attribute matcher.
  2107. if ($dbg_attr) {
  2108. if ($line =~ /^.\s*$Modifier\s*$/) {
  2109. ERROR("TEST_ATTR",
  2110. "TEST: is attr\n" . $herecurr);
  2111. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  2112. ERROR("TEST_NOT_ATTR",
  2113. "TEST: is not attr ($1 is)\n". $herecurr);
  2114. }
  2115. next;
  2116. }
  2117. # check for initialisation to aggregates open brace on the next line
  2118. if ($line =~ /^.\s*{/ &&
  2119. $prevline =~ /(?:^|[^=])=\s*$/) {
  2120. ERROR("OPEN_BRACE",
  2121. "that open brace { should be on the previous line\n" . $hereprev);
  2122. }
  2123. #
  2124. # Checks which are anchored on the added line.
  2125. #
  2126. # check for malformed paths in #include statements (uses RAW line)
  2127. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  2128. my $path = $1;
  2129. if ($path =~ m{//}) {
  2130. ERROR("MALFORMED_INCLUDE",
  2131. "malformed #include filename\n" . $herecurr);
  2132. }
  2133. if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
  2134. ERROR("UAPI_INCLUDE",
  2135. "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
  2136. }
  2137. }
  2138. # no C99 // comments
  2139. if ($line =~ m{//}) {
  2140. if (ERROR("C99_COMMENTS",
  2141. "do not use C99 // comments\n" . $herecurr) &&
  2142. $fix) {
  2143. my $line = $fixed[$linenr - 1];
  2144. if ($line =~ /\/\/(.*)$/) {
  2145. my $comment = trim($1);
  2146. $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
  2147. }
  2148. }
  2149. }
  2150. # Remove C99 comments.
  2151. $line =~ s@//.*@@;
  2152. $opline =~ s@//.*@@;
  2153. # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
  2154. # the whole statement.
  2155. #print "APW <$lines[$realline_next - 1]>\n";
  2156. if (defined $realline_next &&
  2157. exists $lines[$realline_next - 1] &&
  2158. !defined $suppress_export{$realline_next} &&
  2159. ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  2160. $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  2161. # Handle definitions which produce identifiers with
  2162. # a prefix:
  2163. # XXX(foo);
  2164. # EXPORT_SYMBOL(something_foo);
  2165. my $name = $1;
  2166. if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
  2167. $name =~ /^${Ident}_$2/) {
  2168. #print "FOO C name<$name>\n";
  2169. $suppress_export{$realline_next} = 1;
  2170. } elsif ($stat !~ /(?:
  2171. \n.}\s*$|
  2172. ^.DEFINE_$Ident\(\Q$name\E\)|
  2173. ^.DECLARE_$Ident\(\Q$name\E\)|
  2174. ^.LIST_HEAD\(\Q$name\E\)|
  2175. ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
  2176. \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
  2177. )/x) {
  2178. #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
  2179. $suppress_export{$realline_next} = 2;
  2180. } else {
  2181. $suppress_export{$realline_next} = 1;
  2182. }
  2183. }
  2184. if (!defined $suppress_export{$linenr} &&
  2185. $prevline =~ /^.\s*$/ &&
  2186. ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  2187. $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  2188. #print "FOO B <$lines[$linenr - 1]>\n";
  2189. $suppress_export{$linenr} = 2;
  2190. }
  2191. if (defined $suppress_export{$linenr} &&
  2192. $suppress_export{$linenr} == 2) {
  2193. WARN("EXPORT_SYMBOL",
  2194. "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  2195. }
  2196. # check for global initialisers.
  2197. if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
  2198. if (ERROR("GLOBAL_INITIALISERS",
  2199. "do not initialise globals to 0 or NULL\n" .
  2200. $herecurr) &&
  2201. $fix) {
  2202. $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
  2203. }
  2204. }
  2205. # check for static initialisers.
  2206. if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
  2207. if (ERROR("INITIALISED_STATIC",
  2208. "do not initialise statics to 0 or NULL\n" .
  2209. $herecurr) &&
  2210. $fix) {
  2211. $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
  2212. }
  2213. }
  2214. # check for static const char * arrays.
  2215. if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
  2216. WARN("STATIC_CONST_CHAR_ARRAY",
  2217. "static const char * array should probably be static const char * const\n" .
  2218. $herecurr);
  2219. }
  2220. # check for static char foo[] = "bar" declarations.
  2221. if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
  2222. WARN("STATIC_CONST_CHAR_ARRAY",
  2223. "static char array declaration should probably be static const char\n" .
  2224. $herecurr);
  2225. }
  2226. # check for declarations of struct pci_device_id
  2227. if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
  2228. WARN("DEFINE_PCI_DEVICE_TABLE",
  2229. "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
  2230. }
  2231. # check for new typedefs, only function parameters and sparse annotations
  2232. # make sense.
  2233. if ($line =~ /\btypedef\s/ &&
  2234. $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
  2235. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  2236. $line !~ /\b$typeTypedefs\b/ &&
  2237. $line !~ /\b__bitwise(?:__|)\b/) {
  2238. WARN("NEW_TYPEDEFS",
  2239. "do not add new typedefs\n" . $herecurr);
  2240. }
  2241. # * goes on variable not on type
  2242. # (char*[ const])
  2243. while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
  2244. #print "AA<$1>\n";
  2245. my ($ident, $from, $to) = ($1, $2, $2);
  2246. # Should start with a space.
  2247. $to =~ s/^(\S)/ $1/;
  2248. # Should not end with a space.
  2249. $to =~ s/\s+$//;
  2250. # '*'s should not have spaces between.
  2251. while ($to =~ s/\*\s+\*/\*\*/) {
  2252. }
  2253. ## print "1: from<$from> to<$to> ident<$ident>\n";
  2254. if ($from ne $to) {
  2255. if (ERROR("POINTER_LOCATION",
  2256. "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
  2257. $fix) {
  2258. my $sub_from = $ident;
  2259. my $sub_to = $ident;
  2260. $sub_to =~ s/\Q$from\E/$to/;
  2261. $fixed[$linenr - 1] =~
  2262. s@\Q$sub_from\E@$sub_to@;
  2263. }
  2264. }
  2265. }
  2266. while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
  2267. #print "BB<$1>\n";
  2268. my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
  2269. # Should start with a space.
  2270. $to =~ s/^(\S)/ $1/;
  2271. # Should not end with a space.
  2272. $to =~ s/\s+$//;
  2273. # '*'s should not have spaces between.
  2274. while ($to =~ s/\*\s+\*/\*\*/) {
  2275. }
  2276. # Modifiers should have spaces.
  2277. $to =~ s/(\b$Modifier$)/$1 /;
  2278. ## print "2: from<$from> to<$to> ident<$ident>\n";
  2279. if ($from ne $to && $ident !~ /^$Modifier$/) {
  2280. if (ERROR("POINTER_LOCATION",
  2281. "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
  2282. $fix) {
  2283. my $sub_from = $match;
  2284. my $sub_to = $match;
  2285. $sub_to =~ s/\Q$from\E/$to/;
  2286. $fixed[$linenr - 1] =~
  2287. s@\Q$sub_from\E@$sub_to@;
  2288. }
  2289. }
  2290. }
  2291. # # no BUG() or BUG_ON()
  2292. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  2293. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  2294. # print "$herecurr";
  2295. # $clean = 0;
  2296. # }
  2297. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  2298. WARN("LINUX_VERSION_CODE",
  2299. "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  2300. }
  2301. # check for uses of printk_ratelimit
  2302. if ($line =~ /\bprintk_ratelimit\s*\(/) {
  2303. WARN("PRINTK_RATELIMITED",
  2304. "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
  2305. }
  2306. # printk should use KERN_* levels. Note that follow on printk's on the
  2307. # same line do not need a level, so we use the current block context
  2308. # to try and find and validate the current printk. In summary the current
  2309. # printk includes all preceding printk's which have no newline on the end.
  2310. # we assume the first bad printk is the one to report.
  2311. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  2312. my $ok = 0;
  2313. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  2314. #print "CHECK<$lines[$ln - 1]\n";
  2315. # we have a preceding printk if it ends
  2316. # with "\n" ignore it, else it is to blame
  2317. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  2318. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  2319. $ok = 1;
  2320. }
  2321. last;
  2322. }
  2323. }
  2324. if ($ok == 0) {
  2325. WARN("PRINTK_WITHOUT_KERN_LEVEL",
  2326. "printk() should include KERN_ facility level\n" . $herecurr);
  2327. }
  2328. }
  2329. if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
  2330. my $orig = $1;
  2331. my $level = lc($orig);
  2332. $level = "warn" if ($level eq "warning");
  2333. my $level2 = $level;
  2334. $level2 = "dbg" if ($level eq "debug");
  2335. WARN("PREFER_PR_LEVEL",
  2336. "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
  2337. }
  2338. if ($line =~ /\bpr_warning\s*\(/) {
  2339. if (WARN("PREFER_PR_LEVEL",
  2340. "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
  2341. $fix) {
  2342. $fixed[$linenr - 1] =~
  2343. s/\bpr_warning\b/pr_warn/;
  2344. }
  2345. }
  2346. if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
  2347. my $orig = $1;
  2348. my $level = lc($orig);
  2349. $level = "warn" if ($level eq "warning");
  2350. $level = "dbg" if ($level eq "debug");
  2351. WARN("PREFER_DEV_LEVEL",
  2352. "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
  2353. }
  2354. # function brace can't be on same line, except for #defines of do while,
  2355. # or if closed on same line
  2356. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  2357. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  2358. ERROR("OPEN_BRACE",
  2359. "open brace '{' following function declarations go on the next line\n" . $herecurr);
  2360. }
  2361. # open braces for enum, union and struct go on the same line.
  2362. if ($line =~ /^.\s*{/ &&
  2363. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  2364. ERROR("OPEN_BRACE",
  2365. "open brace '{' following $1 go on the same line\n" . $hereprev);
  2366. }
  2367. # missing space after union, struct or enum definition
  2368. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
  2369. if (WARN("SPACING",
  2370. "missing space after $1 definition\n" . $herecurr) &&
  2371. $fix) {
  2372. $fixed[$linenr - 1] =~
  2373. s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
  2374. }
  2375. }
  2376. # check for spacing round square brackets; allowed:
  2377. # 1. with a type on the left -- int [] a;
  2378. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  2379. # 3. inside a curly brace -- = { [0...10] = 5 }
  2380. while ($line =~ /(.*?\s)\[/g) {
  2381. my ($where, $prefix) = ($-[1], $1);
  2382. if ($prefix !~ /$Type\s+$/ &&
  2383. ($where != 0 || $prefix !~ /^.\s+$/) &&
  2384. $prefix !~ /[{,]\s+$/) {
  2385. if (ERROR("BRACKET_SPACE",
  2386. "space prohibited before open square bracket '['\n" . $herecurr) &&
  2387. $fix) {
  2388. $fixed[$linenr - 1] =~
  2389. s/^(\+.*?)\s+\[/$1\[/;
  2390. }
  2391. }
  2392. }
  2393. # check for spaces between functions and their parentheses.
  2394. while ($line =~ /($Ident)\s+\(/g) {
  2395. my $name = $1;
  2396. my $ctx_before = substr($line, 0, $-[1]);
  2397. my $ctx = "$ctx_before$name";
  2398. # Ignore those directives where spaces _are_ permitted.
  2399. if ($name =~ /^(?:
  2400. if|for|while|switch|return|case|
  2401. volatile|__volatile__|
  2402. __attribute__|format|__extension__|
  2403. asm|__asm__)$/x)
  2404. {
  2405. # cpp #define statements have non-optional spaces, ie
  2406. # if there is a space between the name and the open
  2407. # parenthesis it is simply not a parameter group.
  2408. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  2409. # cpp #elif statement condition may start with a (
  2410. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  2411. # If this whole things ends with a type its most
  2412. # likely a typedef for a function.
  2413. } elsif ($ctx =~ /$Type$/) {
  2414. } else {
  2415. if (WARN("SPACING",
  2416. "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
  2417. $fix) {
  2418. $fixed[$linenr - 1] =~
  2419. s/\b$name\s+\(/$name\(/;
  2420. }
  2421. }
  2422. }
  2423. # Check operator spacing.
  2424. if (!($line=~/\#\s*include/)) {
  2425. my $fixed_line = "";
  2426. my $line_fixed = 0;
  2427. my $ops = qr{
  2428. <<=|>>=|<=|>=|==|!=|
  2429. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  2430. =>|->|<<|>>|<|>|=|!|~|
  2431. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  2432. \?:|\?|:
  2433. }x;
  2434. my @elements = split(/($ops|;)/, $opline);
  2435. ## print("element count: <" . $#elements . ">\n");
  2436. ## foreach my $el (@elements) {
  2437. ## print("el: <$el>\n");
  2438. ## }
  2439. my @fix_elements = ();
  2440. my $off = 0;
  2441. foreach my $el (@elements) {
  2442. push(@fix_elements, substr($rawline, $off, length($el)));
  2443. $off += length($el);
  2444. }
  2445. $off = 0;
  2446. my $blank = copy_spacing($opline);
  2447. my $last_after = -1;
  2448. for (my $n = 0; $n < $#elements; $n += 2) {
  2449. my $good = $fix_elements[$n] . $fix_elements[$n + 1];
  2450. ## print("n: <$n> good: <$good>\n");
  2451. $off += length($elements[$n]);
  2452. # Pick up the preceding and succeeding characters.
  2453. my $ca = substr($opline, 0, $off);
  2454. my $cc = '';
  2455. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  2456. $cc = substr($opline, $off + length($elements[$n + 1]));
  2457. }
  2458. my $cb = "$ca$;$cc";
  2459. my $a = '';
  2460. $a = 'V' if ($elements[$n] ne '');
  2461. $a = 'W' if ($elements[$n] =~ /\s$/);
  2462. $a = 'C' if ($elements[$n] =~ /$;$/);
  2463. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  2464. $a = 'O' if ($elements[$n] eq '');
  2465. $a = 'E' if ($ca =~ /^\s*$/);
  2466. my $op = $elements[$n + 1];
  2467. my $c = '';
  2468. if (defined $elements[$n + 2]) {
  2469. $c = 'V' if ($elements[$n + 2] ne '');
  2470. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  2471. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  2472. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  2473. $c = 'O' if ($elements[$n + 2] eq '');
  2474. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  2475. } else {
  2476. $c = 'E';
  2477. }
  2478. my $ctx = "${a}x${c}";
  2479. my $at = "(ctx:$ctx)";
  2480. my $ptr = substr($blank, 0, $off) . "^";
  2481. my $hereptr = "$hereline$ptr\n";
  2482. # Pull out the value of this operator.
  2483. my $op_type = substr($curr_values, $off + 1, 1);
  2484. # Get the full operator variant.
  2485. my $opv = $op . substr($curr_vars, $off, 1);
  2486. # Ignore operators passed as parameters.
  2487. if ($op_type ne 'V' &&
  2488. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  2489. # # Ignore comments
  2490. # } elsif ($op =~ /^$;+$/) {
  2491. # ; should have either the end of line or a space or \ after it
  2492. } elsif ($op eq ';') {
  2493. if ($ctx !~ /.x[WEBC]/ &&
  2494. $cc !~ /^\\/ && $cc !~ /^;/) {
  2495. if (ERROR("SPACING",
  2496. "space required after that '$op' $at\n" . $hereptr)) {
  2497. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  2498. $line_fixed = 1;
  2499. }
  2500. }
  2501. # // is a comment
  2502. } elsif ($op eq '//') {
  2503. # No spaces for:
  2504. # ->
  2505. # : when part of a bitfield
  2506. } elsif ($op eq '->' || $opv eq ':B') {
  2507. if ($ctx =~ /Wx.|.xW/) {
  2508. if (ERROR("SPACING",
  2509. "spaces prohibited around that '$op' $at\n" . $hereptr)) {
  2510. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  2511. if (defined $fix_elements[$n + 2]) {
  2512. $fix_elements[$n + 2] =~ s/^\s+//;
  2513. }
  2514. $line_fixed = 1;
  2515. }
  2516. }
  2517. # , must have a space on the right.
  2518. } elsif ($op eq ',') {
  2519. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  2520. if (ERROR("SPACING",
  2521. "space required after that '$op' $at\n" . $hereptr)) {
  2522. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  2523. $line_fixed = 1;
  2524. $last_after = $n;
  2525. }
  2526. }
  2527. # '*' as part of a type definition -- reported already.
  2528. } elsif ($opv eq '*_') {
  2529. #warn "'*' is part of type\n";
  2530. # unary operators should have a space before and
  2531. # none after. May be left adjacent to another
  2532. # unary operator, or a cast
  2533. } elsif ($op eq '!' || $op eq '~' ||
  2534. $opv eq '*U' || $opv eq '-U' ||
  2535. $opv eq '&U' || $opv eq '&&U') {
  2536. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  2537. if (ERROR("SPACING",
  2538. "space required before that '$op' $at\n" . $hereptr)) {
  2539. if ($n != $last_after + 2) {
  2540. $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
  2541. $line_fixed = 1;
  2542. }
  2543. }
  2544. }
  2545. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  2546. # A unary '*' may be const
  2547. } elsif ($ctx =~ /.xW/) {
  2548. if (ERROR("SPACING",
  2549. "space prohibited after that '$op' $at\n" . $hereptr)) {
  2550. $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
  2551. if (defined $fix_elements[$n + 2]) {
  2552. $fix_elements[$n + 2] =~ s/^\s+//;
  2553. }
  2554. $line_fixed = 1;
  2555. }
  2556. }
  2557. # unary ++ and unary -- are allowed no space on one side.
  2558. } elsif ($op eq '++' or $op eq '--') {
  2559. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  2560. if (ERROR("SPACING",
  2561. "space required one side of that '$op' $at\n" . $hereptr)) {
  2562. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  2563. $line_fixed = 1;
  2564. }
  2565. }
  2566. if ($ctx =~ /Wx[BE]/ ||
  2567. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  2568. if (ERROR("SPACING",
  2569. "space prohibited before that '$op' $at\n" . $hereptr)) {
  2570. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  2571. $line_fixed = 1;
  2572. }
  2573. }
  2574. if ($ctx =~ /ExW/) {
  2575. if (ERROR("SPACING",
  2576. "space prohibited after that '$op' $at\n" . $hereptr)) {
  2577. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
  2578. if (defined $fix_elements[$n + 2]) {
  2579. $fix_elements[$n + 2] =~ s/^\s+//;
  2580. }
  2581. $line_fixed = 1;
  2582. }
  2583. }
  2584. # << and >> may either have or not have spaces both sides
  2585. } elsif ($op eq '<<' or $op eq '>>' or
  2586. $op eq '&' or $op eq '^' or $op eq '|' or
  2587. $op eq '+' or $op eq '-' or
  2588. $op eq '*' or $op eq '/' or
  2589. $op eq '%')
  2590. {
  2591. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  2592. if (ERROR("SPACING",
  2593. "need consistent spacing around '$op' $at\n" . $hereptr)) {
  2594. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  2595. if (defined $fix_elements[$n + 2]) {
  2596. $fix_elements[$n + 2] =~ s/^\s+//;
  2597. }
  2598. $line_fixed = 1;
  2599. }
  2600. }
  2601. # A colon needs no spaces before when it is
  2602. # terminating a case value or a label.
  2603. } elsif ($opv eq ':C' || $opv eq ':L') {
  2604. if ($ctx =~ /Wx./) {
  2605. if (ERROR("SPACING",
  2606. "space prohibited before that '$op' $at\n" . $hereptr)) {
  2607. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  2608. $line_fixed = 1;
  2609. }
  2610. }
  2611. # All the others need spaces both sides.
  2612. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  2613. my $ok = 0;
  2614. # Ignore email addresses <foo@bar>
  2615. if (($op eq '<' &&
  2616. $cc =~ /^\S+\@\S+>/) ||
  2617. ($op eq '>' &&
  2618. $ca =~ /<\S+\@\S+$/))
  2619. {
  2620. $ok = 1;
  2621. }
  2622. # messages are ERROR, but ?: are CHK
  2623. if ($ok == 0) {
  2624. my $msg_type = \&ERROR;
  2625. $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
  2626. if (&{$msg_type}("SPACING",
  2627. "spaces required around that '$op' $at\n" . $hereptr)) {
  2628. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  2629. if (defined $fix_elements[$n + 2]) {
  2630. $fix_elements[$n + 2] =~ s/^\s+//;
  2631. }
  2632. $line_fixed = 1;
  2633. }
  2634. }
  2635. }
  2636. $off += length($elements[$n + 1]);
  2637. ## print("n: <$n> GOOD: <$good>\n");
  2638. $fixed_line = $fixed_line . $good;
  2639. }
  2640. if (($#elements % 2) == 0) {
  2641. $fixed_line = $fixed_line . $fix_elements[$#elements];
  2642. }
  2643. if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
  2644. $fixed[$linenr - 1] = $fixed_line;
  2645. }
  2646. }
  2647. # check for whitespace before a non-naked semicolon
  2648. if ($line =~ /^\+.*\S\s+;/) {
  2649. if (WARN("SPACING",
  2650. "space prohibited before semicolon\n" . $herecurr) &&
  2651. $fix) {
  2652. 1 while $fixed[$linenr - 1] =~
  2653. s/^(\+.*\S)\s+;/$1;/;
  2654. }
  2655. }
  2656. # check for multiple assignments
  2657. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  2658. CHK("MULTIPLE_ASSIGNMENTS",
  2659. "multiple assignments should be avoided\n" . $herecurr);
  2660. }
  2661. ## # check for multiple declarations, allowing for a function declaration
  2662. ## # continuation.
  2663. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  2664. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  2665. ##
  2666. ## # Remove any bracketed sections to ensure we do not
  2667. ## # falsly report the parameters of functions.
  2668. ## my $ln = $line;
  2669. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  2670. ## }
  2671. ## if ($ln =~ /,/) {
  2672. ## WARN("MULTIPLE_DECLARATION",
  2673. ## "declaring multiple variables together should be avoided\n" . $herecurr);
  2674. ## }
  2675. ## }
  2676. #need space before brace following if, while, etc
  2677. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  2678. $line =~ /do{/) {
  2679. if (ERROR("SPACING",
  2680. "space required before the open brace '{'\n" . $herecurr) &&
  2681. $fix) {
  2682. $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
  2683. }
  2684. }
  2685. ## # check for blank lines before declarations
  2686. ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
  2687. ## $prevrawline =~ /^.\s*$/) {
  2688. ## WARN("SPACING",
  2689. ## "No blank lines before declarations\n" . $hereprev);
  2690. ## }
  2691. ##
  2692. # closing brace should have a space following it when it has anything
  2693. # on the line
  2694. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  2695. if (ERROR("SPACING",
  2696. "space required after that close brace '}'\n" . $herecurr) &&
  2697. $fix) {
  2698. $fixed[$linenr - 1] =~
  2699. s/}((?!(?:,|;|\)))\S)/} $1/;
  2700. }
  2701. }
  2702. # check spacing on square brackets
  2703. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  2704. if (ERROR("SPACING",
  2705. "space prohibited after that open square bracket '['\n" . $herecurr) &&
  2706. $fix) {
  2707. $fixed[$linenr - 1] =~
  2708. s/\[\s+/\[/;
  2709. }
  2710. }
  2711. if ($line =~ /\s\]/) {
  2712. if (ERROR("SPACING",
  2713. "space prohibited before that close square bracket ']'\n" . $herecurr) &&
  2714. $fix) {
  2715. $fixed[$linenr - 1] =~
  2716. s/\s+\]/\]/;
  2717. }
  2718. }
  2719. # check spacing on parentheses
  2720. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  2721. $line !~ /for\s*\(\s+;/) {
  2722. if (ERROR("SPACING",
  2723. "space prohibited after that open parenthesis '('\n" . $herecurr) &&
  2724. $fix) {
  2725. $fixed[$linenr - 1] =~
  2726. s/\(\s+/\(/;
  2727. }
  2728. }
  2729. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  2730. $line !~ /for\s*\(.*;\s+\)/ &&
  2731. $line !~ /:\s+\)/) {
  2732. if (ERROR("SPACING",
  2733. "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
  2734. $fix) {
  2735. $fixed[$linenr - 1] =~
  2736. s/\s+\)/\)/;
  2737. }
  2738. }
  2739. #goto labels aren't indented, allow a single space however
  2740. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  2741. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  2742. if (WARN("INDENTED_LABEL",
  2743. "labels should not be indented\n" . $herecurr) &&
  2744. $fix) {
  2745. $fixed[$linenr - 1] =~
  2746. s/^(.)\s+/$1/;
  2747. }
  2748. }
  2749. # Return is not a function.
  2750. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  2751. my $spacing = $1;
  2752. my $value = $2;
  2753. # Flatten any parentheses
  2754. $value =~ s/\(/ \(/g;
  2755. $value =~ s/\)/\) /g;
  2756. while ($value =~ s/\[[^\[\]]*\]/1/ ||
  2757. $value !~ /(?:$Ident|-?$Constant)\s*
  2758. $Compare\s*
  2759. (?:$Ident|-?$Constant)/x &&
  2760. $value =~ s/\([^\(\)]*\)/1/) {
  2761. }
  2762. #print "value<$value>\n";
  2763. if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
  2764. ERROR("RETURN_PARENTHESES",
  2765. "return is not a function, parentheses are not required\n" . $herecurr);
  2766. } elsif ($spacing !~ /\s+/) {
  2767. ERROR("SPACING",
  2768. "space required before the open parenthesis '('\n" . $herecurr);
  2769. }
  2770. }
  2771. # Return of what appears to be an errno should normally be -'ve
  2772. if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
  2773. my $name = $1;
  2774. if ($name ne 'EOF' && $name ne 'ERROR') {
  2775. WARN("USE_NEGATIVE_ERRNO",
  2776. "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
  2777. }
  2778. }
  2779. # Need a space before open parenthesis after if, while etc
  2780. if ($line =~ /\b(if|while|for|switch)\(/) {
  2781. if (ERROR("SPACING",
  2782. "space required before the open parenthesis '('\n" . $herecurr) &&
  2783. $fix) {
  2784. $fixed[$linenr - 1] =~
  2785. s/\b(if|while|for|switch)\(/$1 \(/;
  2786. }
  2787. }
  2788. # Check for illegal assignment in if conditional -- and check for trailing
  2789. # statements after the conditional.
  2790. if ($line =~ /do\s*(?!{)/) {
  2791. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  2792. ctx_statement_block($linenr, $realcnt, 0)
  2793. if (!defined $stat);
  2794. my ($stat_next) = ctx_statement_block($line_nr_next,
  2795. $remain_next, $off_next);
  2796. $stat_next =~ s/\n./\n /g;
  2797. ##print "stat<$stat> stat_next<$stat_next>\n";
  2798. if ($stat_next =~ /^\s*while\b/) {
  2799. # If the statement carries leading newlines,
  2800. # then count those as offsets.
  2801. my ($whitespace) =
  2802. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  2803. my $offset =
  2804. statement_rawlines($whitespace) - 1;
  2805. $suppress_whiletrailers{$line_nr_next +
  2806. $offset} = 1;
  2807. }
  2808. }
  2809. if (!defined $suppress_whiletrailers{$linenr} &&
  2810. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  2811. my ($s, $c) = ($stat, $cond);
  2812. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  2813. ERROR("ASSIGN_IN_IF",
  2814. "do not use assignment in if condition\n" . $herecurr);
  2815. }
  2816. # Find out what is on the end of the line after the
  2817. # conditional.
  2818. substr($s, 0, length($c), '');
  2819. $s =~ s/\n.*//g;
  2820. $s =~ s/$;//g; # Remove any comments
  2821. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  2822. $c !~ /}\s*while\s*/)
  2823. {
  2824. # Find out how long the conditional actually is.
  2825. my @newlines = ($c =~ /\n/gs);
  2826. my $cond_lines = 1 + $#newlines;
  2827. my $stat_real = '';
  2828. $stat_real = raw_line($linenr, $cond_lines)
  2829. . "\n" if ($cond_lines);
  2830. if (defined($stat_real) && $cond_lines > 1) {
  2831. $stat_real = "[...]\n$stat_real";
  2832. }
  2833. ERROR("TRAILING_STATEMENTS",
  2834. "trailing statements should be on next line\n" . $herecurr . $stat_real);
  2835. }
  2836. }
  2837. # Check for bitwise tests written as boolean
  2838. if ($line =~ /
  2839. (?:
  2840. (?:\[|\(|\&\&|\|\|)
  2841. \s*0[xX][0-9]+\s*
  2842. (?:\&\&|\|\|)
  2843. |
  2844. (?:\&\&|\|\|)
  2845. \s*0[xX][0-9]+\s*
  2846. (?:\&\&|\|\||\)|\])
  2847. )/x)
  2848. {
  2849. WARN("HEXADECIMAL_BOOLEAN_TEST",
  2850. "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  2851. }
  2852. # if and else should not have general statements after it
  2853. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  2854. my $s = $1;
  2855. $s =~ s/$;//g; # Remove any comments
  2856. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  2857. ERROR("TRAILING_STATEMENTS",
  2858. "trailing statements should be on next line\n" . $herecurr);
  2859. }
  2860. }
  2861. # if should not continue a brace
  2862. if ($line =~ /}\s*if\b/) {
  2863. ERROR("TRAILING_STATEMENTS",
  2864. "trailing statements should be on next line\n" .
  2865. $herecurr);
  2866. }
  2867. # case and default should not have general statements after them
  2868. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  2869. $line !~ /\G(?:
  2870. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  2871. \s*return\s+
  2872. )/xg)
  2873. {
  2874. ERROR("TRAILING_STATEMENTS",
  2875. "trailing statements should be on next line\n" . $herecurr);
  2876. }
  2877. # Check for }<nl>else {, these must be at the same
  2878. # indent level to be relevant to each other.
  2879. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  2880. $previndent == $indent) {
  2881. ERROR("ELSE_AFTER_BRACE",
  2882. "else should follow close brace '}'\n" . $hereprev);
  2883. }
  2884. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  2885. $previndent == $indent) {
  2886. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  2887. # Find out what is on the end of the line after the
  2888. # conditional.
  2889. substr($s, 0, length($c), '');
  2890. $s =~ s/\n.*//g;
  2891. if ($s =~ /^\s*;/) {
  2892. ERROR("WHILE_AFTER_BRACE",
  2893. "while should follow close brace '}'\n" . $hereprev);
  2894. }
  2895. }
  2896. #Specific variable tests
  2897. while ($line =~ m{($Constant|$Lval)}g) {
  2898. my $var = $1;
  2899. #gcc binary extension
  2900. if ($var =~ /^$Binary$/) {
  2901. if (WARN("GCC_BINARY_CONSTANT",
  2902. "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
  2903. $fix) {
  2904. my $hexval = sprintf("0x%x", oct($var));
  2905. $fixed[$linenr - 1] =~
  2906. s/\b$var\b/$hexval/;
  2907. }
  2908. }
  2909. #CamelCase
  2910. if ($var !~ /^$Constant$/ &&
  2911. $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
  2912. #Ignore Page<foo> variants
  2913. $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
  2914. #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
  2915. $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
  2916. while ($var =~ m{($Ident)}g) {
  2917. my $word = $1;
  2918. next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
  2919. if ($check) {
  2920. seed_camelcase_includes();
  2921. if (!$file && !$camelcase_file_seeded) {
  2922. seed_camelcase_file($realfile);
  2923. $camelcase_file_seeded = 1;
  2924. }
  2925. }
  2926. if (!defined $camelcase{$word}) {
  2927. $camelcase{$word} = 1;
  2928. CHK("CAMELCASE",
  2929. "Avoid CamelCase: <$word>\n" . $herecurr);
  2930. }
  2931. }
  2932. }
  2933. }
  2934. #no spaces allowed after \ in define
  2935. if ($line =~ /\#\s*define.*\\\s+$/) {
  2936. if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
  2937. "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
  2938. $fix) {
  2939. $fixed[$linenr - 1] =~ s/\s+$//;
  2940. }
  2941. }
  2942. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  2943. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  2944. my $file = "$1.h";
  2945. my $checkfile = "include/linux/$file";
  2946. if (-f "$root/$checkfile" &&
  2947. $realfile ne $checkfile &&
  2948. $1 !~ /$allowed_asm_includes/)
  2949. {
  2950. if ($realfile =~ m{^arch/}) {
  2951. CHK("ARCH_INCLUDE_LINUX",
  2952. "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2953. } else {
  2954. WARN("INCLUDE_LINUX",
  2955. "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2956. }
  2957. }
  2958. }
  2959. # multi-statement macros should be enclosed in a do while loop, grab the
  2960. # first statement and ensure its the whole macro if its not enclosed
  2961. # in a known good container
  2962. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  2963. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  2964. my $ln = $linenr;
  2965. my $cnt = $realcnt;
  2966. my ($off, $dstat, $dcond, $rest);
  2967. my $ctx = '';
  2968. ($dstat, $dcond, $ln, $cnt, $off) =
  2969. ctx_statement_block($linenr, $realcnt, 0);
  2970. $ctx = $dstat;
  2971. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  2972. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  2973. $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
  2974. $dstat =~ s/$;//g;
  2975. $dstat =~ s/\\\n.//g;
  2976. $dstat =~ s/^\s*//s;
  2977. $dstat =~ s/\s*$//s;
  2978. # Flatten any parentheses and braces
  2979. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  2980. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  2981. $dstat =~ s/\[[^\[\]]*\]/1/)
  2982. {
  2983. }
  2984. # Flatten any obvious string concatentation.
  2985. while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
  2986. $dstat =~ s/$Ident\s*("X*")/$1/)
  2987. {
  2988. }
  2989. my $exceptions = qr{
  2990. $Declare|
  2991. module_param_named|
  2992. MODULE_PARM_DESC|
  2993. DECLARE_PER_CPU|
  2994. DEFINE_PER_CPU|
  2995. __typeof__\(|
  2996. union|
  2997. struct|
  2998. \.$Ident\s*=\s*|
  2999. ^\"|\"$
  3000. }x;
  3001. #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
  3002. if ($dstat ne '' &&
  3003. $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
  3004. $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
  3005. $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
  3006. $dstat !~ /^'X'$/ && # character constants
  3007. $dstat !~ /$exceptions/ &&
  3008. $dstat !~ /^\.$Ident\s*=/ && # .foo =
  3009. $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
  3010. $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
  3011. $dstat !~ /^for\s*$Constant$/ && # for (...)
  3012. $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
  3013. $dstat !~ /^do\s*{/ && # do {...
  3014. $dstat !~ /^\({/ && # ({...
  3015. $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
  3016. {
  3017. $ctx =~ s/\n*$//;
  3018. my $herectx = $here . "\n";
  3019. my $cnt = statement_rawlines($ctx);
  3020. for (my $n = 0; $n < $cnt; $n++) {
  3021. $herectx .= raw_line($linenr, $n) . "\n";
  3022. }
  3023. if ($dstat =~ /;/) {
  3024. ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
  3025. "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
  3026. } else {
  3027. ERROR("COMPLEX_MACRO",
  3028. "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
  3029. }
  3030. }
  3031. # check for line continuations outside of #defines, preprocessor #, and asm
  3032. } else {
  3033. if ($prevline !~ /^..*\\$/ &&
  3034. $line !~ /^\+\s*\#.*\\$/ && # preprocessor
  3035. $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
  3036. $line =~ /^\+.*\\$/) {
  3037. WARN("LINE_CONTINUATIONS",
  3038. "Avoid unnecessary line continuations\n" . $herecurr);
  3039. }
  3040. }
  3041. # do {} while (0) macro tests:
  3042. # single-statement macros do not need to be enclosed in do while (0) loop,
  3043. # macro should not end with a semicolon
  3044. if ($^V && $^V ge 5.10.0 &&
  3045. $realfile !~ m@/vmlinux.lds.h$@ &&
  3046. $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
  3047. my $ln = $linenr;
  3048. my $cnt = $realcnt;
  3049. my ($off, $dstat, $dcond, $rest);
  3050. my $ctx = '';
  3051. ($dstat, $dcond, $ln, $cnt, $off) =
  3052. ctx_statement_block($linenr, $realcnt, 0);
  3053. $ctx = $dstat;
  3054. $dstat =~ s/\\\n.//g;
  3055. if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
  3056. my $stmts = $2;
  3057. my $semis = $3;
  3058. $ctx =~ s/\n*$//;
  3059. my $cnt = statement_rawlines($ctx);
  3060. my $herectx = $here . "\n";
  3061. for (my $n = 0; $n < $cnt; $n++) {
  3062. $herectx .= raw_line($linenr, $n) . "\n";
  3063. }
  3064. if (($stmts =~ tr/;/;/) == 1 &&
  3065. $stmts !~ /^\s*(if|while|for|switch)\b/) {
  3066. WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
  3067. "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
  3068. }
  3069. if (defined $semis && $semis ne "") {
  3070. WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
  3071. "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
  3072. }
  3073. }
  3074. }
  3075. # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
  3076. # all assignments may have only one of the following with an assignment:
  3077. # .
  3078. # ALIGN(...)
  3079. # VMLINUX_SYMBOL(...)
  3080. if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
  3081. WARN("MISSING_VMLINUX_SYMBOL",
  3082. "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
  3083. }
  3084. # check for redundant bracing round if etc
  3085. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  3086. my ($level, $endln, @chunks) =
  3087. ctx_statement_full($linenr, $realcnt, 1);
  3088. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  3089. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  3090. if ($#chunks > 0 && $level == 0) {
  3091. my @allowed = ();
  3092. my $allow = 0;
  3093. my $seen = 0;
  3094. my $herectx = $here . "\n";
  3095. my $ln = $linenr - 1;
  3096. for my $chunk (@chunks) {
  3097. my ($cond, $block) = @{$chunk};
  3098. # If the condition carries leading newlines, then count those as offsets.
  3099. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  3100. my $offset = statement_rawlines($whitespace) - 1;
  3101. $allowed[$allow] = 0;
  3102. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  3103. # We have looked at and allowed this specific line.
  3104. $suppress_ifbraces{$ln + $offset} = 1;
  3105. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  3106. $ln += statement_rawlines($block) - 1;
  3107. substr($block, 0, length($cond), '');
  3108. $seen++ if ($block =~ /^\s*{/);
  3109. #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
  3110. if (statement_lines($cond) > 1) {
  3111. #print "APW: ALLOWED: cond<$cond>\n";
  3112. $allowed[$allow] = 1;
  3113. }
  3114. if ($block =~/\b(?:if|for|while)\b/) {
  3115. #print "APW: ALLOWED: block<$block>\n";
  3116. $allowed[$allow] = 1;
  3117. }
  3118. if (statement_block_size($block) > 1) {
  3119. #print "APW: ALLOWED: lines block<$block>\n";
  3120. $allowed[$allow] = 1;
  3121. }
  3122. $allow++;
  3123. }
  3124. if ($seen) {
  3125. my $sum_allowed = 0;
  3126. foreach (@allowed) {
  3127. $sum_allowed += $_;
  3128. }
  3129. if ($sum_allowed == 0) {
  3130. WARN("BRACES",
  3131. "braces {} are not necessary for any arm of this statement\n" . $herectx);
  3132. } elsif ($sum_allowed != $allow &&
  3133. $seen != $allow) {
  3134. CHK("BRACES",
  3135. "braces {} should be used on all arms of this statement\n" . $herectx);
  3136. }
  3137. }
  3138. }
  3139. }
  3140. if (!defined $suppress_ifbraces{$linenr - 1} &&
  3141. $line =~ /\b(if|while|for|else)\b/) {
  3142. my $allowed = 0;
  3143. # Check the pre-context.
  3144. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  3145. #print "APW: ALLOWED: pre<$1>\n";
  3146. $allowed = 1;
  3147. }
  3148. my ($level, $endln, @chunks) =
  3149. ctx_statement_full($linenr, $realcnt, $-[0]);
  3150. # Check the condition.
  3151. my ($cond, $block) = @{$chunks[0]};
  3152. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  3153. if (defined $cond) {
  3154. substr($block, 0, length($cond), '');
  3155. }
  3156. if (statement_lines($cond) > 1) {
  3157. #print "APW: ALLOWED: cond<$cond>\n";
  3158. $allowed = 1;
  3159. }
  3160. if ($block =~/\b(?:if|for|while)\b/) {
  3161. #print "APW: ALLOWED: block<$block>\n";
  3162. $allowed = 1;
  3163. }
  3164. if (statement_block_size($block) > 1) {
  3165. #print "APW: ALLOWED: lines block<$block>\n";
  3166. $allowed = 1;
  3167. }
  3168. # Check the post-context.
  3169. if (defined $chunks[1]) {
  3170. my ($cond, $block) = @{$chunks[1]};
  3171. if (defined $cond) {
  3172. substr($block, 0, length($cond), '');
  3173. }
  3174. if ($block =~ /^\s*\{/) {
  3175. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  3176. $allowed = 1;
  3177. }
  3178. }
  3179. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  3180. my $herectx = $here . "\n";
  3181. my $cnt = statement_rawlines($block);
  3182. for (my $n = 0; $n < $cnt; $n++) {
  3183. $herectx .= raw_line($linenr, $n) . "\n";
  3184. }
  3185. WARN("BRACES",
  3186. "braces {} are not necessary for single statement blocks\n" . $herectx);
  3187. }
  3188. }
  3189. # check for unnecessary blank lines around braces
  3190. if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
  3191. CHK("BRACES",
  3192. "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
  3193. }
  3194. if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
  3195. CHK("BRACES",
  3196. "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
  3197. }
  3198. # no volatiles please
  3199. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  3200. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  3201. WARN("VOLATILE",
  3202. "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  3203. }
  3204. # warn about #if 0
  3205. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  3206. CHK("REDUNDANT_CODE",
  3207. "if this code is redundant consider removing it\n" .
  3208. $herecurr);
  3209. }
  3210. # check for needless "if (<foo>) fn(<foo>)" uses
  3211. if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
  3212. my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
  3213. if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
  3214. WARN('NEEDLESS_IF',
  3215. "$1(NULL) is safe this check is probably not required\n" . $hereprev);
  3216. }
  3217. }
  3218. # check for bad placement of section $InitAttribute (e.g.: __initdata)
  3219. if ($line =~ /(\b$InitAttribute\b)/) {
  3220. my $attr = $1;
  3221. if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
  3222. my $ptr = $1;
  3223. my $var = $2;
  3224. if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
  3225. ERROR("MISPLACED_INIT",
  3226. "$attr should be placed after $var\n" . $herecurr)) ||
  3227. ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
  3228. WARN("MISPLACED_INIT",
  3229. "$attr should be placed after $var\n" . $herecurr))) &&
  3230. $fix) {
  3231. $fixed[$linenr - 1] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
  3232. }
  3233. }
  3234. }
  3235. # check for $InitAttributeData (ie: __initdata) with const
  3236. if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
  3237. my $attr = $1;
  3238. $attr =~ /($InitAttributePrefix)(.*)/;
  3239. my $attr_prefix = $1;
  3240. my $attr_type = $2;
  3241. if (ERROR("INIT_ATTRIBUTE",
  3242. "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
  3243. $fix) {
  3244. $fixed[$linenr - 1] =~
  3245. s/$InitAttributeData/${attr_prefix}initconst/;
  3246. }
  3247. }
  3248. # check for $InitAttributeConst (ie: __initconst) without const
  3249. if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
  3250. my $attr = $1;
  3251. if (ERROR("INIT_ATTRIBUTE",
  3252. "Use of $attr requires a separate use of const\n" . $herecurr) &&
  3253. $fix) {
  3254. my $lead = $fixed[$linenr - 1] =~
  3255. /(^\+\s*(?:static\s+))/;
  3256. $lead = rtrim($1);
  3257. $lead = "$lead " if ($lead !~ /^\+$/);
  3258. $lead = "${lead}const ";
  3259. $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
  3260. }
  3261. }
  3262. # prefer usleep_range over udelay
  3263. if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
  3264. # ignore udelay's < 10, however
  3265. if (! ($1 < 10) ) {
  3266. CHK("USLEEP_RANGE",
  3267. "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
  3268. }
  3269. }
  3270. # warn about unexpectedly long msleep's
  3271. if ($line =~ /\bmsleep\s*\((\d+)\);/) {
  3272. if ($1 < 20) {
  3273. WARN("MSLEEP",
  3274. "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
  3275. }
  3276. }
  3277. # check for comparisons of jiffies
  3278. if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
  3279. WARN("JIFFIES_COMPARISON",
  3280. "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
  3281. }
  3282. # check for comparisons of get_jiffies_64()
  3283. if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
  3284. WARN("JIFFIES_COMPARISON",
  3285. "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
  3286. }
  3287. # warn about #ifdefs in C files
  3288. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  3289. # print "#ifdef in C files should be avoided\n";
  3290. # print "$herecurr";
  3291. # $clean = 0;
  3292. # }
  3293. # warn about spacing in #ifdefs
  3294. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  3295. if (ERROR("SPACING",
  3296. "exactly one space required after that #$1\n" . $herecurr) &&
  3297. $fix) {
  3298. $fixed[$linenr - 1] =~
  3299. s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
  3300. }
  3301. }
  3302. # check for spinlock_t definitions without a comment.
  3303. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  3304. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  3305. my $which = $1;
  3306. if (!ctx_has_comment($first_line, $linenr)) {
  3307. CHK("UNCOMMENTED_DEFINITION",
  3308. "$1 definition without comment\n" . $herecurr);
  3309. }
  3310. }
  3311. # check for memory barriers without a comment.
  3312. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  3313. if (!ctx_has_comment($first_line, $linenr)) {
  3314. CHK("MEMORY_BARRIER",
  3315. "memory barrier without comment\n" . $herecurr);
  3316. }
  3317. }
  3318. # check of hardware specific defines
  3319. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  3320. CHK("ARCH_DEFINES",
  3321. "architecture specific defines should be avoided\n" . $herecurr);
  3322. }
  3323. # Check that the storage class is at the beginning of a declaration
  3324. if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
  3325. WARN("STORAGE_CLASS",
  3326. "storage class should be at the beginning of the declaration\n" . $herecurr)
  3327. }
  3328. # check the location of the inline attribute, that it is between
  3329. # storage class and type.
  3330. if ($line =~ /\b$Type\s+$Inline\b/ ||
  3331. $line =~ /\b$Inline\s+$Storage\b/) {
  3332. ERROR("INLINE_LOCATION",
  3333. "inline keyword should sit between storage class and type\n" . $herecurr);
  3334. }
  3335. # Check for __inline__ and __inline, prefer inline
  3336. if ($line =~ /\b(__inline__|__inline)\b/) {
  3337. if (WARN("INLINE",
  3338. "plain inline is preferred over $1\n" . $herecurr) &&
  3339. $fix) {
  3340. $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
  3341. }
  3342. }
  3343. # Check for __attribute__ packed, prefer __packed
  3344. if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
  3345. WARN("PREFER_PACKED",
  3346. "__packed is preferred over __attribute__((packed))\n" . $herecurr);
  3347. }
  3348. # Check for __attribute__ aligned, prefer __aligned
  3349. if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
  3350. WARN("PREFER_ALIGNED",
  3351. "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
  3352. }
  3353. # Check for __attribute__ format(printf, prefer __printf
  3354. if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
  3355. if (WARN("PREFER_PRINTF",
  3356. "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
  3357. $fix) {
  3358. $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
  3359. }
  3360. }
  3361. # Check for __attribute__ format(scanf, prefer __scanf
  3362. if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
  3363. if (WARN("PREFER_SCANF",
  3364. "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
  3365. $fix) {
  3366. $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
  3367. }
  3368. }
  3369. # check for sizeof(&)
  3370. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  3371. WARN("SIZEOF_ADDRESS",
  3372. "sizeof(& should be avoided\n" . $herecurr);
  3373. }
  3374. # check for sizeof without parenthesis
  3375. if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
  3376. if (WARN("SIZEOF_PARENTHESIS",
  3377. "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
  3378. $fix) {
  3379. $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
  3380. }
  3381. }
  3382. # check for line continuations in quoted strings with odd counts of "
  3383. if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
  3384. WARN("LINE_CONTINUATIONS",
  3385. "Avoid line continuations in quoted strings\n" . $herecurr);
  3386. }
  3387. # check for struct spinlock declarations
  3388. if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
  3389. WARN("USE_SPINLOCK_T",
  3390. "struct spinlock should be spinlock_t\n" . $herecurr);
  3391. }
  3392. # check for seq_printf uses that could be seq_puts
  3393. if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
  3394. my $fmt = get_quoted_string($line, $rawline);
  3395. if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
  3396. if (WARN("PREFER_SEQ_PUTS",
  3397. "Prefer seq_puts to seq_printf\n" . $herecurr) &&
  3398. $fix) {
  3399. $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
  3400. }
  3401. }
  3402. }
  3403. # Check for misused memsets
  3404. if ($^V && $^V ge 5.10.0 &&
  3405. defined $stat &&
  3406. $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
  3407. my $ms_addr = $2;
  3408. my $ms_val = $7;
  3409. my $ms_size = $12;
  3410. if ($ms_size =~ /^(0x|)0$/i) {
  3411. ERROR("MEMSET",
  3412. "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
  3413. } elsif ($ms_size =~ /^(0x|)1$/i) {
  3414. WARN("MEMSET",
  3415. "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
  3416. }
  3417. }
  3418. # typecasts on min/max could be min_t/max_t
  3419. if ($^V && $^V ge 5.10.0 &&
  3420. defined $stat &&
  3421. $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
  3422. if (defined $2 || defined $7) {
  3423. my $call = $1;
  3424. my $cast1 = deparenthesize($2);
  3425. my $arg1 = $3;
  3426. my $cast2 = deparenthesize($7);
  3427. my $arg2 = $8;
  3428. my $cast;
  3429. if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
  3430. $cast = "$cast1 or $cast2";
  3431. } elsif ($cast1 ne "") {
  3432. $cast = $cast1;
  3433. } else {
  3434. $cast = $cast2;
  3435. }
  3436. WARN("MINMAX",
  3437. "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
  3438. }
  3439. }
  3440. # check usleep_range arguments
  3441. if ($^V && $^V ge 5.10.0 &&
  3442. defined $stat &&
  3443. $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
  3444. my $min = $1;
  3445. my $max = $7;
  3446. if ($min eq $max) {
  3447. WARN("USLEEP_RANGE",
  3448. "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
  3449. } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
  3450. $min > $max) {
  3451. WARN("USLEEP_RANGE",
  3452. "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
  3453. }
  3454. }
  3455. # check for new externs in .h files.
  3456. if ($realfile =~ /\.h$/ &&
  3457. $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
  3458. if (CHK("AVOID_EXTERNS",
  3459. "extern prototypes should be avoided in .h files\n" . $herecurr) &&
  3460. $fix) {
  3461. $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
  3462. }
  3463. }
  3464. # check for new externs in .c files.
  3465. if ($realfile =~ /\.c$/ && defined $stat &&
  3466. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  3467. {
  3468. my $function_name = $1;
  3469. my $paren_space = $2;
  3470. my $s = $stat;
  3471. if (defined $cond) {
  3472. substr($s, 0, length($cond), '');
  3473. }
  3474. if ($s =~ /^\s*;/ &&
  3475. $function_name ne 'uninitialized_var')
  3476. {
  3477. WARN("AVOID_EXTERNS",
  3478. "externs should be avoided in .c files\n" . $herecurr);
  3479. }
  3480. if ($paren_space =~ /\n/) {
  3481. WARN("FUNCTION_ARGUMENTS",
  3482. "arguments for function declarations should follow identifier\n" . $herecurr);
  3483. }
  3484. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  3485. $stat =~ /^.\s*extern\s+/)
  3486. {
  3487. WARN("AVOID_EXTERNS",
  3488. "externs should be avoided in .c files\n" . $herecurr);
  3489. }
  3490. # checks for new __setup's
  3491. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  3492. my $name = $1;
  3493. if (!grep(/$name/, @setup_docs)) {
  3494. CHK("UNDOCUMENTED_SETUP",
  3495. "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  3496. }
  3497. }
  3498. # check for pointless casting of kmalloc return
  3499. if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
  3500. WARN("UNNECESSARY_CASTS",
  3501. "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  3502. }
  3503. # alloc style
  3504. # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
  3505. if ($^V && $^V ge 5.10.0 &&
  3506. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
  3507. CHK("ALLOC_SIZEOF_STRUCT",
  3508. "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
  3509. }
  3510. # check for krealloc arg reuse
  3511. if ($^V && $^V ge 5.10.0 &&
  3512. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
  3513. WARN("KREALLOC_ARG_REUSE",
  3514. "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
  3515. }
  3516. # check for alloc argument mismatch
  3517. if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
  3518. WARN("ALLOC_ARRAY_ARGS",
  3519. "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
  3520. }
  3521. # check for multiple semicolons
  3522. if ($line =~ /;\s*;\s*$/) {
  3523. if (WARN("ONE_SEMICOLON",
  3524. "Statements terminations use 1 semicolon\n" . $herecurr) &&
  3525. $fix) {
  3526. $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
  3527. }
  3528. }
  3529. # check for switch/default statements without a break;
  3530. if ($^V && $^V ge 5.10.0 &&
  3531. defined $stat &&
  3532. $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
  3533. my $ctx = '';
  3534. my $herectx = $here . "\n";
  3535. my $cnt = statement_rawlines($stat);
  3536. for (my $n = 0; $n < $cnt; $n++) {
  3537. $herectx .= raw_line($linenr, $n) . "\n";
  3538. }
  3539. WARN("DEFAULT_NO_BREAK",
  3540. "switch default: should use break\n" . $herectx);
  3541. }
  3542. # check for gcc specific __FUNCTION__
  3543. if ($line =~ /\b__FUNCTION__\b/) {
  3544. if (WARN("USE_FUNC",
  3545. "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
  3546. $fix) {
  3547. $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
  3548. }
  3549. }
  3550. # check for use of yield()
  3551. if ($line =~ /\byield\s*\(\s*\)/) {
  3552. WARN("YIELD",
  3553. "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
  3554. }
  3555. # check for comparisons against true and false
  3556. if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
  3557. my $lead = $1;
  3558. my $arg = $2;
  3559. my $test = $3;
  3560. my $otype = $4;
  3561. my $trail = $5;
  3562. my $op = "!";
  3563. ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
  3564. my $type = lc($otype);
  3565. if ($type =~ /^(?:true|false)$/) {
  3566. if (("$test" eq "==" && "$type" eq "true") ||
  3567. ("$test" eq "!=" && "$type" eq "false")) {
  3568. $op = "";
  3569. }
  3570. CHK("BOOL_COMPARISON",
  3571. "Using comparison to $otype is error prone\n" . $herecurr);
  3572. ## maybe suggesting a correct construct would better
  3573. ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
  3574. }
  3575. }
  3576. # check for semaphores initialized locked
  3577. if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
  3578. WARN("CONSIDER_COMPLETION",
  3579. "consider using a completion\n" . $herecurr);
  3580. }
  3581. # recommend kstrto* over simple_strto* and strict_strto*
  3582. if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
  3583. WARN("CONSIDER_KSTRTO",
  3584. "$1 is obsolete, use k$3 instead\n" . $herecurr);
  3585. }
  3586. # check for __initcall(), use device_initcall() explicitly please
  3587. if ($line =~ /^.\s*__initcall\s*\(/) {
  3588. WARN("USE_DEVICE_INITCALL",
  3589. "please use device_initcall() instead of __initcall()\n" . $herecurr);
  3590. }
  3591. # check for various ops structs, ensure they are const.
  3592. my $struct_ops = qr{acpi_dock_ops|
  3593. address_space_operations|
  3594. backlight_ops|
  3595. block_device_operations|
  3596. dentry_operations|
  3597. dev_pm_ops|
  3598. dma_map_ops|
  3599. extent_io_ops|
  3600. file_lock_operations|
  3601. file_operations|
  3602. hv_ops|
  3603. ide_dma_ops|
  3604. intel_dvo_dev_ops|
  3605. item_operations|
  3606. iwl_ops|
  3607. kgdb_arch|
  3608. kgdb_io|
  3609. kset_uevent_ops|
  3610. lock_manager_operations|
  3611. microcode_ops|
  3612. mtrr_ops|
  3613. neigh_ops|
  3614. nlmsvc_binding|
  3615. pci_raw_ops|
  3616. pipe_buf_operations|
  3617. platform_hibernation_ops|
  3618. platform_suspend_ops|
  3619. proto_ops|
  3620. rpc_pipe_ops|
  3621. seq_operations|
  3622. snd_ac97_build_ops|
  3623. soc_pcmcia_socket_ops|
  3624. stacktrace_ops|
  3625. sysfs_ops|
  3626. tty_operations|
  3627. usb_mon_operations|
  3628. wd_ops}x;
  3629. if ($line !~ /\bconst\b/ &&
  3630. $line =~ /\bstruct\s+($struct_ops)\b/) {
  3631. WARN("CONST_STRUCT",
  3632. "struct $1 should normally be const\n" .
  3633. $herecurr);
  3634. }
  3635. # use of NR_CPUS is usually wrong
  3636. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  3637. if ($line =~ /\bNR_CPUS\b/ &&
  3638. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  3639. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  3640. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  3641. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  3642. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  3643. {
  3644. WARN("NR_CPUS",
  3645. "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  3646. }
  3647. # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
  3648. if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
  3649. ERROR("DEFINE_ARCH_HAS",
  3650. "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
  3651. }
  3652. # check for %L{u,d,i} in strings
  3653. my $string;
  3654. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  3655. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  3656. $string =~ s/%%/__/g;
  3657. if ($string =~ /(?<!%)%L[udi]/) {
  3658. WARN("PRINTF_L",
  3659. "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  3660. last;
  3661. }
  3662. }
  3663. # whine mightly about in_atomic
  3664. if ($line =~ /\bin_atomic\s*\(/) {
  3665. if ($realfile =~ m@^drivers/@) {
  3666. ERROR("IN_ATOMIC",
  3667. "do not use in_atomic in drivers\n" . $herecurr);
  3668. } elsif ($realfile !~ m@^kernel/@) {
  3669. WARN("IN_ATOMIC",
  3670. "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  3671. }
  3672. }
  3673. # check for lockdep_set_novalidate_class
  3674. if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
  3675. $line =~ /__lockdep_no_validate__\s*\)/ ) {
  3676. if ($realfile !~ m@^kernel/lockdep@ &&
  3677. $realfile !~ m@^include/linux/lockdep@ &&
  3678. $realfile !~ m@^drivers/base/core@) {
  3679. ERROR("LOCKDEP",
  3680. "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
  3681. }
  3682. }
  3683. if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
  3684. $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
  3685. WARN("EXPORTED_WORLD_WRITABLE",
  3686. "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
  3687. }
  3688. }
  3689. # If we have no input at all, then there is nothing to report on
  3690. # so just keep quiet.
  3691. if ($#rawlines == -1) {
  3692. exit(0);
  3693. }
  3694. # In mailback mode only produce a report in the negative, for
  3695. # things that appear to be patches.
  3696. if ($mailback && ($clean == 1 || !$is_patch)) {
  3697. exit(0);
  3698. }
  3699. # This is not a patch, and we are are in 'no-patch' mode so
  3700. # just keep quiet.
  3701. if (!$chk_patch && !$is_patch) {
  3702. exit(0);
  3703. }
  3704. if (!$is_patch) {
  3705. ERROR("NOT_UNIFIED_DIFF",
  3706. "Does not appear to be a unified-diff format patch\n");
  3707. }
  3708. if ($is_patch && $chk_signoff && $signoff == 0) {
  3709. ERROR("MISSING_SIGN_OFF",
  3710. "Missing Signed-off-by: line(s)\n");
  3711. }
  3712. print report_dump();
  3713. if ($summary && !($clean == 1 && $quiet == 1)) {
  3714. print "$filename " if ($summary_file);
  3715. print "total: $cnt_error errors, $cnt_warn warnings, " .
  3716. (($check)? "$cnt_chk checks, " : "") .
  3717. "$cnt_lines lines checked\n";
  3718. print "\n" if ($quiet == 0);
  3719. }
  3720. if ($quiet == 0) {
  3721. if ($^V lt 5.10.0) {
  3722. print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
  3723. print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
  3724. }
  3725. # If there were whitespace errors which cleanpatch can fix
  3726. # then suggest that.
  3727. if ($rpt_cleaners) {
  3728. print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
  3729. print " scripts/cleanfile\n\n";
  3730. $rpt_cleaners = 0;
  3731. }
  3732. }
  3733. hash_show_words(\%use_type, "Used");
  3734. hash_show_words(\%ignore_type, "Ignored");
  3735. if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
  3736. my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
  3737. my $linecount = 0;
  3738. my $f;
  3739. open($f, '>', $newfile)
  3740. or die "$P: Can't open $newfile for write\n";
  3741. foreach my $fixed_line (@fixed) {
  3742. $linecount++;
  3743. if ($file) {
  3744. if ($linecount > 3) {
  3745. $fixed_line =~ s/^\+//;
  3746. print $f $fixed_line. "\n";
  3747. }
  3748. } else {
  3749. print $f $fixed_line . "\n";
  3750. }
  3751. }
  3752. close($f);
  3753. if (!$quiet) {
  3754. print << "EOM";
  3755. Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
  3756. Do _NOT_ trust the results written to this file.
  3757. Do _NOT_ submit these changes without inspecting them for correctness.
  3758. This EXPERIMENTAL file is simply a convenience to help rewrite patches.
  3759. No warranties, expressed or implied...
  3760. EOM
  3761. }
  3762. }
  3763. if ($clean == 1 && $quiet == 0) {
  3764. print "$vname has no obvious style problems and is ready for submission.\n"
  3765. }
  3766. if ($clean == 0 && $quiet == 0) {
  3767. print << "EOM";
  3768. $vname has style problems, please review.
  3769. If any of these errors are false positives, please report
  3770. them to the maintainer, see CHECKPATCH in MAINTAINERS.
  3771. EOM
  3772. }
  3773. return $clean;
  3774. }