checkpatch.pl 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993
  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. use File::Basename;
  10. use Cwd 'abs_path';
  11. use Term::ANSIColor qw(:constants);
  12. my $P = $0;
  13. my $D = dirname(abs_path($P));
  14. my $V = '0.32';
  15. use Getopt::Long qw(:config no_auto_abbrev);
  16. my $quiet = 0;
  17. my $tree = 1;
  18. my $chk_signoff = 1;
  19. my $chk_patch = 1;
  20. my $tst_only;
  21. my $emacs = 0;
  22. my $terse = 0;
  23. my $showfile = 0;
  24. my $file = 0;
  25. my $check = 0;
  26. my $check_orig = 0;
  27. my $summary = 1;
  28. my $mailback = 0;
  29. my $summary_file = 0;
  30. my $show_types = 0;
  31. my $fix = 0;
  32. my $fix_inplace = 0;
  33. my $root;
  34. my %debug;
  35. my %camelcase = ();
  36. my %use_type = ();
  37. my @use = ();
  38. my %ignore_type = ();
  39. my @ignore = ();
  40. my $help = 0;
  41. my $configuration_file = ".checkpatch.conf";
  42. my $max_line_length = 80;
  43. my $ignore_perl_version = 0;
  44. my $minimum_perl_version = 5.10.0;
  45. my $min_conf_desc_length = 4;
  46. my $spelling_file = "$D/spelling.txt";
  47. my $codespell = 0;
  48. my $codespellfile = "/usr/share/codespell/dictionary.txt";
  49. my $color = 1;
  50. sub help {
  51. my ($exitcode) = @_;
  52. print << "EOM";
  53. Usage: $P [OPTION]... [FILE]...
  54. Version: $V
  55. Options:
  56. -q, --quiet quiet
  57. --no-tree run without a kernel tree
  58. --no-signoff do not check for 'Signed-off-by' line
  59. --patch treat FILE as patchfile (default)
  60. --emacs emacs compile window format
  61. --terse one line per report
  62. --showfile emit diffed file position, not input file position
  63. -f, --file treat FILE as regular source file
  64. --subjective, --strict enable more subjective tests
  65. --types TYPE(,TYPE2...) show only these comma separated message types
  66. --ignore TYPE(,TYPE2...) ignore various comma separated message types
  67. --max-line-length=n set the maximum line length, if exceeded, warn
  68. --min-conf-desc-length=n set the min description length, if shorter, warn
  69. --show-types show the message "types" in the output
  70. --root=PATH PATH to the kernel tree root
  71. --no-summary suppress the per-file summary
  72. --mailback only produce a report in case of warnings/errors
  73. --summary-file include the filename in summary
  74. --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
  75. 'values', 'possible', 'type', and 'attr' (default
  76. is all off)
  77. --test-only=WORD report only warnings/errors containing WORD
  78. literally
  79. --fix EXPERIMENTAL - may create horrible results
  80. If correctable single-line errors exist, create
  81. "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
  82. with potential errors corrected to the preferred
  83. checkpatch style
  84. --fix-inplace EXPERIMENTAL - may create horrible results
  85. Is the same as --fix, but overwrites the input
  86. file. It's your fault if there's no backup or git
  87. --ignore-perl-version override checking of perl version. expect
  88. runtime errors.
  89. --codespell Use the codespell dictionary for spelling/typos
  90. (default:/usr/share/codespell/dictionary.txt)
  91. --codespellfile Use this codespell dictionary
  92. --color Use colors when output is STDOUT (default: on)
  93. -h, --help, --version display this help and exit
  94. When FILE is - read standard input.
  95. EOM
  96. exit($exitcode);
  97. }
  98. my $conf = which_conf($configuration_file);
  99. if (-f $conf) {
  100. my @conf_args;
  101. open(my $conffile, '<', "$conf")
  102. or warn "$P: Can't find a readable $configuration_file file $!\n";
  103. while (<$conffile>) {
  104. my $line = $_;
  105. $line =~ s/\s*\n?$//g;
  106. $line =~ s/^\s*//g;
  107. $line =~ s/\s+/ /g;
  108. next if ($line =~ m/^\s*#/);
  109. next if ($line =~ m/^\s*$/);
  110. my @words = split(" ", $line);
  111. foreach my $word (@words) {
  112. last if ($word =~ m/^#/);
  113. push (@conf_args, $word);
  114. }
  115. }
  116. close($conffile);
  117. unshift(@ARGV, @conf_args) if @conf_args;
  118. }
  119. GetOptions(
  120. 'q|quiet+' => \$quiet,
  121. 'tree!' => \$tree,
  122. 'signoff!' => \$chk_signoff,
  123. 'patch!' => \$chk_patch,
  124. 'emacs!' => \$emacs,
  125. 'terse!' => \$terse,
  126. 'showfile!' => \$showfile,
  127. 'f|file!' => \$file,
  128. 'subjective!' => \$check,
  129. 'strict!' => \$check,
  130. 'ignore=s' => \@ignore,
  131. 'types=s' => \@use,
  132. 'show-types!' => \$show_types,
  133. 'max-line-length=i' => \$max_line_length,
  134. 'min-conf-desc-length=i' => \$min_conf_desc_length,
  135. 'root=s' => \$root,
  136. 'summary!' => \$summary,
  137. 'mailback!' => \$mailback,
  138. 'summary-file!' => \$summary_file,
  139. 'fix!' => \$fix,
  140. 'fix-inplace!' => \$fix_inplace,
  141. 'ignore-perl-version!' => \$ignore_perl_version,
  142. 'debug=s' => \%debug,
  143. 'test-only=s' => \$tst_only,
  144. 'codespell!' => \$codespell,
  145. 'codespellfile=s' => \$codespellfile,
  146. 'color!' => \$color,
  147. 'h|help' => \$help,
  148. 'version' => \$help
  149. ) or help(1);
  150. help(0) if ($help);
  151. $fix = 1 if ($fix_inplace);
  152. $check_orig = $check;
  153. my $exit = 0;
  154. if ($^V && $^V lt $minimum_perl_version) {
  155. printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
  156. if (!$ignore_perl_version) {
  157. exit(1);
  158. }
  159. }
  160. if ($#ARGV < 0) {
  161. print "$P: no input files\n";
  162. exit(1);
  163. }
  164. sub hash_save_array_words {
  165. my ($hashRef, $arrayRef) = @_;
  166. my @array = split(/,/, join(',', @$arrayRef));
  167. foreach my $word (@array) {
  168. $word =~ s/\s*\n?$//g;
  169. $word =~ s/^\s*//g;
  170. $word =~ s/\s+/ /g;
  171. $word =~ tr/[a-z]/[A-Z]/;
  172. next if ($word =~ m/^\s*#/);
  173. next if ($word =~ m/^\s*$/);
  174. $hashRef->{$word}++;
  175. }
  176. }
  177. sub hash_show_words {
  178. my ($hashRef, $prefix) = @_;
  179. if (keys %$hashRef) {
  180. print "\nNOTE: $prefix message types:";
  181. foreach my $word (sort keys %$hashRef) {
  182. print " $word";
  183. }
  184. print "\n";
  185. }
  186. }
  187. hash_save_array_words(\%ignore_type, \@ignore);
  188. hash_save_array_words(\%use_type, \@use);
  189. my $dbg_values = 0;
  190. my $dbg_possible = 0;
  191. my $dbg_type = 0;
  192. my $dbg_attr = 0;
  193. for my $key (keys %debug) {
  194. ## no critic
  195. eval "\${dbg_$key} = '$debug{$key}';";
  196. die "$@" if ($@);
  197. }
  198. my $rpt_cleaners = 0;
  199. if ($terse) {
  200. $emacs = 1;
  201. $quiet++;
  202. }
  203. if ($tree) {
  204. if (defined $root) {
  205. if (!top_of_kernel_tree($root)) {
  206. die "$P: $root: --root does not point at a valid tree\n";
  207. }
  208. } else {
  209. if (top_of_kernel_tree('.')) {
  210. $root = '.';
  211. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  212. top_of_kernel_tree($1)) {
  213. $root = $1;
  214. }
  215. }
  216. if (!defined $root) {
  217. print "Must be run from the top-level dir. of a kernel tree\n";
  218. exit(2);
  219. }
  220. }
  221. my $emitted_corrupt = 0;
  222. our $Ident = qr{
  223. [A-Za-z_][A-Za-z\d_]*
  224. (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
  225. }x;
  226. our $Storage = qr{extern|static|asmlinkage};
  227. our $Sparse = qr{
  228. __user|
  229. __kernel|
  230. __force|
  231. __iomem|
  232. __pmem|
  233. __must_check|
  234. __init_refok|
  235. __kprobes|
  236. __ref|
  237. __rcu|
  238. __private
  239. }x;
  240. our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
  241. our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
  242. our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
  243. our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
  244. our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
  245. # Notes to $Attribute:
  246. # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
  247. our $Attribute = qr{
  248. const|
  249. __percpu|
  250. __nocast|
  251. __safe|
  252. __bitwise__|
  253. __packed__|
  254. __packed2__|
  255. __naked|
  256. __maybe_unused|
  257. __always_unused|
  258. __noreturn|
  259. __used|
  260. __cold|
  261. __pure|
  262. __noclone|
  263. __deprecated|
  264. __read_mostly|
  265. __kprobes|
  266. $InitAttribute|
  267. ____cacheline_aligned|
  268. ____cacheline_aligned_in_smp|
  269. ____cacheline_internodealigned_in_smp|
  270. __weak
  271. }x;
  272. our $Modifier;
  273. our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
  274. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  275. our $Lval = qr{$Ident(?:$Member)*};
  276. our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
  277. our $Binary = qr{(?i)0b[01]+$Int_type?};
  278. our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
  279. our $Int = qr{[0-9]+$Int_type?};
  280. our $Octal = qr{0[0-7]+$Int_type?};
  281. our $String = qr{"[X\t]*"};
  282. our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
  283. our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
  284. our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
  285. our $Float = qr{$Float_hex|$Float_dec|$Float_int};
  286. our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
  287. our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
  288. our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
  289. our $Arithmetic = qr{\+|-|\*|\/|%};
  290. our $Operators = qr{
  291. <=|>=|==|!=|
  292. =>|->|<<|>>|<|>|!|~|
  293. &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
  294. }x;
  295. our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
  296. our $BasicType;
  297. our $NonptrType;
  298. our $NonptrTypeMisordered;
  299. our $NonptrTypeWithAttr;
  300. our $Type;
  301. our $TypeMisordered;
  302. our $Declare;
  303. our $DeclareMisordered;
  304. our $NON_ASCII_UTF8 = qr{
  305. [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  306. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  307. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  308. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  309. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  310. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  311. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  312. }x;
  313. our $UTF8 = qr{
  314. [\x09\x0A\x0D\x20-\x7E] # ASCII
  315. | $NON_ASCII_UTF8
  316. }x;
  317. our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
  318. our $typeOtherOSTypedefs = qr{(?x:
  319. u_(?:char|short|int|long) | # bsd
  320. u(?:nchar|short|int|long) # sysv
  321. )};
  322. our $typeKernelTypedefs = qr{(?x:
  323. (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
  324. atomic_t
  325. )};
  326. our $typeTypedefs = qr{(?x:
  327. $typeC99Typedefs\b|
  328. $typeOtherOSTypedefs\b|
  329. $typeKernelTypedefs\b
  330. )};
  331. our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
  332. our $logFunctions = qr{(?x:
  333. printk(?:_ratelimited|_once|)|
  334. (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
  335. WARN(?:_RATELIMIT|_ONCE|)|
  336. panic|
  337. MODULE_[A-Z_]+|
  338. seq_vprintf|seq_printf|seq_puts
  339. )};
  340. our $signature_tags = qr{(?xi:
  341. Signed-off-by:|
  342. Acked-by:|
  343. Tested-by:|
  344. Reviewed-by:|
  345. Reported-by:|
  346. Suggested-by:|
  347. To:|
  348. Cc:
  349. )};
  350. our @typeListMisordered = (
  351. qr{char\s+(?:un)?signed},
  352. qr{int\s+(?:(?:un)?signed\s+)?short\s},
  353. qr{int\s+short(?:\s+(?:un)?signed)},
  354. qr{short\s+int(?:\s+(?:un)?signed)},
  355. qr{(?:un)?signed\s+int\s+short},
  356. qr{short\s+(?:un)?signed},
  357. qr{long\s+int\s+(?:un)?signed},
  358. qr{int\s+long\s+(?:un)?signed},
  359. qr{long\s+(?:un)?signed\s+int},
  360. qr{int\s+(?:un)?signed\s+long},
  361. qr{int\s+(?:un)?signed},
  362. qr{int\s+long\s+long\s+(?:un)?signed},
  363. qr{long\s+long\s+int\s+(?:un)?signed},
  364. qr{long\s+long\s+(?:un)?signed\s+int},
  365. qr{long\s+long\s+(?:un)?signed},
  366. qr{long\s+(?:un)?signed},
  367. );
  368. our @typeList = (
  369. qr{void},
  370. qr{(?:(?:un)?signed\s+)?char},
  371. qr{(?:(?:un)?signed\s+)?short\s+int},
  372. qr{(?:(?:un)?signed\s+)?short},
  373. qr{(?:(?:un)?signed\s+)?int},
  374. qr{(?:(?:un)?signed\s+)?long\s+int},
  375. qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
  376. qr{(?:(?:un)?signed\s+)?long\s+long},
  377. qr{(?:(?:un)?signed\s+)?long},
  378. qr{(?:un)?signed},
  379. qr{float},
  380. qr{double},
  381. qr{bool},
  382. qr{struct\s+$Ident},
  383. qr{union\s+$Ident},
  384. qr{enum\s+$Ident},
  385. qr{${Ident}_t},
  386. qr{${Ident}_handler},
  387. qr{${Ident}_handler_fn},
  388. @typeListMisordered,
  389. );
  390. our $C90_int_types = qr{(?x:
  391. long\s+long\s+int\s+(?:un)?signed|
  392. long\s+long\s+(?:un)?signed\s+int|
  393. long\s+long\s+(?:un)?signed|
  394. (?:(?:un)?signed\s+)?long\s+long\s+int|
  395. (?:(?:un)?signed\s+)?long\s+long|
  396. int\s+long\s+long\s+(?:un)?signed|
  397. int\s+(?:(?:un)?signed\s+)?long\s+long|
  398. long\s+int\s+(?:un)?signed|
  399. long\s+(?:un)?signed\s+int|
  400. long\s+(?:un)?signed|
  401. (?:(?:un)?signed\s+)?long\s+int|
  402. (?:(?:un)?signed\s+)?long|
  403. int\s+long\s+(?:un)?signed|
  404. int\s+(?:(?:un)?signed\s+)?long|
  405. int\s+(?:un)?signed|
  406. (?:(?:un)?signed\s+)?int
  407. )};
  408. our @typeListFile = ();
  409. our @typeListWithAttr = (
  410. @typeList,
  411. qr{struct\s+$InitAttribute\s+$Ident},
  412. qr{union\s+$InitAttribute\s+$Ident},
  413. );
  414. our @modifierList = (
  415. qr{fastcall},
  416. );
  417. our @modifierListFile = ();
  418. our @mode_permission_funcs = (
  419. ["module_param", 3],
  420. ["module_param_(?:array|named|string)", 4],
  421. ["module_param_array_named", 5],
  422. ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
  423. ["proc_create(?:_data|)", 2],
  424. ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
  425. );
  426. #Create a search pattern for all these functions to speed up a loop below
  427. our $mode_perms_search = "";
  428. foreach my $entry (@mode_permission_funcs) {
  429. $mode_perms_search .= '|' if ($mode_perms_search ne "");
  430. $mode_perms_search .= $entry->[0];
  431. }
  432. our $mode_perms_world_writable = qr{
  433. S_IWUGO |
  434. S_IWOTH |
  435. S_IRWXUGO |
  436. S_IALLUGO |
  437. 0[0-7][0-7][2367]
  438. }x;
  439. our $allowed_asm_includes = qr{(?x:
  440. irq|
  441. memory|
  442. time|
  443. reboot
  444. )};
  445. # memory.h: ARM has a custom one
  446. # Load common spelling mistakes and build regular expression list.
  447. my $misspellings;
  448. my %spelling_fix;
  449. if (open(my $spelling, '<', $spelling_file)) {
  450. while (<$spelling>) {
  451. my $line = $_;
  452. $line =~ s/\s*\n?$//g;
  453. $line =~ s/^\s*//g;
  454. next if ($line =~ m/^\s*#/);
  455. next if ($line =~ m/^\s*$/);
  456. my ($suspect, $fix) = split(/\|\|/, $line);
  457. $spelling_fix{$suspect} = $fix;
  458. }
  459. close($spelling);
  460. } else {
  461. warn "No typos will be found - file '$spelling_file': $!\n";
  462. }
  463. if ($codespell) {
  464. if (open(my $spelling, '<', $codespellfile)) {
  465. while (<$spelling>) {
  466. my $line = $_;
  467. $line =~ s/\s*\n?$//g;
  468. $line =~ s/^\s*//g;
  469. next if ($line =~ m/^\s*#/);
  470. next if ($line =~ m/^\s*$/);
  471. next if ($line =~ m/, disabled/i);
  472. $line =~ s/,.*$//;
  473. my ($suspect, $fix) = split(/->/, $line);
  474. $spelling_fix{$suspect} = $fix;
  475. }
  476. close($spelling);
  477. } else {
  478. warn "No codespell typos will be found - file '$codespellfile': $!\n";
  479. }
  480. }
  481. $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
  482. sub build_types {
  483. my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
  484. my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
  485. my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
  486. my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
  487. $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
  488. $BasicType = qr{
  489. (?:$typeTypedefs\b)|
  490. (?:${all}\b)
  491. }x;
  492. $NonptrType = qr{
  493. (?:$Modifier\s+|const\s+)*
  494. (?:
  495. (?:typeof|__typeof__)\s*\([^\)]*\)|
  496. (?:$typeTypedefs\b)|
  497. (?:${all}\b)
  498. )
  499. (?:\s+$Modifier|\s+const)*
  500. }x;
  501. $NonptrTypeMisordered = qr{
  502. (?:$Modifier\s+|const\s+)*
  503. (?:
  504. (?:${Misordered}\b)
  505. )
  506. (?:\s+$Modifier|\s+const)*
  507. }x;
  508. $NonptrTypeWithAttr = qr{
  509. (?:$Modifier\s+|const\s+)*
  510. (?:
  511. (?:typeof|__typeof__)\s*\([^\)]*\)|
  512. (?:$typeTypedefs\b)|
  513. (?:${allWithAttr}\b)
  514. )
  515. (?:\s+$Modifier|\s+const)*
  516. }x;
  517. $Type = qr{
  518. $NonptrType
  519. (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
  520. (?:\s+$Inline|\s+$Modifier)*
  521. }x;
  522. $TypeMisordered = qr{
  523. $NonptrTypeMisordered
  524. (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
  525. (?:\s+$Inline|\s+$Modifier)*
  526. }x;
  527. $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
  528. $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
  529. }
  530. build_types();
  531. our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
  532. # Using $balanced_parens, $LvalOrFunc, or $FuncArg
  533. # requires at least perl version v5.10.0
  534. # Any use must be runtime checked with $^V
  535. our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
  536. our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
  537. our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
  538. our $declaration_macros = qr{(?x:
  539. (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
  540. (?:$Storage\s+)?LIST_HEAD\s*\(|
  541. (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
  542. )};
  543. sub deparenthesize {
  544. my ($string) = @_;
  545. return "" if (!defined($string));
  546. while ($string =~ /^\s*\(.*\)\s*$/) {
  547. $string =~ s@^\s*\(\s*@@;
  548. $string =~ s@\s*\)\s*$@@;
  549. }
  550. $string =~ s@\s+@ @g;
  551. return $string;
  552. }
  553. sub seed_camelcase_file {
  554. my ($file) = @_;
  555. return if (!(-f $file));
  556. local $/;
  557. open(my $include_file, '<', "$file")
  558. or warn "$P: Can't read '$file' $!\n";
  559. my $text = <$include_file>;
  560. close($include_file);
  561. my @lines = split('\n', $text);
  562. foreach my $line (@lines) {
  563. next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
  564. if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
  565. $camelcase{$1} = 1;
  566. } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
  567. $camelcase{$1} = 1;
  568. } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
  569. $camelcase{$1} = 1;
  570. }
  571. }
  572. }
  573. my $camelcase_seeded = 0;
  574. sub seed_camelcase_includes {
  575. return if ($camelcase_seeded);
  576. my $files;
  577. my $camelcase_cache = "";
  578. my @include_files = ();
  579. $camelcase_seeded = 1;
  580. if (-e ".git") {
  581. my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
  582. chomp $git_last_include_commit;
  583. $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
  584. } else {
  585. my $last_mod_date = 0;
  586. $files = `find $root/include -name "*.h"`;
  587. @include_files = split('\n', $files);
  588. foreach my $file (@include_files) {
  589. my $date = POSIX::strftime("%Y%m%d%H%M",
  590. localtime((stat $file)[9]));
  591. $last_mod_date = $date if ($last_mod_date < $date);
  592. }
  593. $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
  594. }
  595. if ($camelcase_cache ne "" && -f $camelcase_cache) {
  596. open(my $camelcase_file, '<', "$camelcase_cache")
  597. or warn "$P: Can't read '$camelcase_cache' $!\n";
  598. while (<$camelcase_file>) {
  599. chomp;
  600. $camelcase{$_} = 1;
  601. }
  602. close($camelcase_file);
  603. return;
  604. }
  605. if (-e ".git") {
  606. $files = `git ls-files "include/*.h"`;
  607. @include_files = split('\n', $files);
  608. }
  609. foreach my $file (@include_files) {
  610. seed_camelcase_file($file);
  611. }
  612. if ($camelcase_cache ne "") {
  613. unlink glob ".checkpatch-camelcase.*";
  614. open(my $camelcase_file, '>', "$camelcase_cache")
  615. or warn "$P: Can't write '$camelcase_cache' $!\n";
  616. foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
  617. print $camelcase_file ("$_\n");
  618. }
  619. close($camelcase_file);
  620. }
  621. }
  622. sub git_commit_info {
  623. my ($commit, $id, $desc) = @_;
  624. return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
  625. my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
  626. $output =~ s/^\s*//gm;
  627. my @lines = split("\n", $output);
  628. return ($id, $desc) if ($#lines < 0);
  629. if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
  630. # Maybe one day convert this block of bash into something that returns
  631. # all matching commit ids, but it's very slow...
  632. #
  633. # echo "checking commits $1..."
  634. # git rev-list --remotes | grep -i "^$1" |
  635. # while read line ; do
  636. # git log --format='%H %s' -1 $line |
  637. # echo "commit $(cut -c 1-12,41-)"
  638. # done
  639. } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
  640. } else {
  641. $id = substr($lines[0], 0, 12);
  642. $desc = substr($lines[0], 41);
  643. }
  644. return ($id, $desc);
  645. }
  646. $chk_signoff = 0 if ($file);
  647. my @rawlines = ();
  648. my @lines = ();
  649. my @fixed = ();
  650. my @fixed_inserted = ();
  651. my @fixed_deleted = ();
  652. my $fixlinenr = -1;
  653. my $vname;
  654. for my $filename (@ARGV) {
  655. my $FILE;
  656. if ($file) {
  657. open($FILE, '-|', "diff -u /dev/null $filename") ||
  658. die "$P: $filename: diff failed - $!\n";
  659. } elsif ($filename eq '-') {
  660. open($FILE, '<&STDIN');
  661. } else {
  662. open($FILE, '<', "$filename") ||
  663. die "$P: $filename: open failed - $!\n";
  664. }
  665. if ($filename eq '-') {
  666. $vname = 'Your patch';
  667. } else {
  668. $vname = $filename;
  669. }
  670. while (<$FILE>) {
  671. chomp;
  672. push(@rawlines, $_);
  673. }
  674. close($FILE);
  675. if ($#ARGV > 0 && $quiet == 0) {
  676. print '-' x length($vname) . "\n";
  677. print "$vname\n";
  678. print '-' x length($vname) . "\n";
  679. }
  680. if (!process($filename)) {
  681. $exit = 1;
  682. }
  683. @rawlines = ();
  684. @lines = ();
  685. @fixed = ();
  686. @fixed_inserted = ();
  687. @fixed_deleted = ();
  688. $fixlinenr = -1;
  689. @modifierListFile = ();
  690. @typeListFile = ();
  691. build_types();
  692. }
  693. if (!$quiet) {
  694. hash_show_words(\%use_type, "Used");
  695. hash_show_words(\%ignore_type, "Ignored");
  696. if ($^V lt 5.10.0) {
  697. print << "EOM"
  698. NOTE: perl $^V is not modern enough to detect all possible issues.
  699. An upgrade to at least perl v5.10.0 is suggested.
  700. EOM
  701. }
  702. if ($exit) {
  703. print << "EOM"
  704. NOTE: If any of the errors are false positives, please report
  705. them to the maintainer, see CHECKPATCH in MAINTAINERS.
  706. EOM
  707. }
  708. }
  709. exit($exit);
  710. sub top_of_kernel_tree {
  711. my ($root) = @_;
  712. my @tree_check = (
  713. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  714. "README", "Documentation", "arch", "include", "drivers",
  715. "fs", "init", "ipc", "kernel", "lib", "scripts",
  716. );
  717. foreach my $check (@tree_check) {
  718. if (! -e $root . '/' . $check) {
  719. return 0;
  720. }
  721. }
  722. return 1;
  723. }
  724. sub parse_email {
  725. my ($formatted_email) = @_;
  726. my $name = "";
  727. my $address = "";
  728. my $comment = "";
  729. if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
  730. $name = $1;
  731. $address = $2;
  732. $comment = $3 if defined $3;
  733. } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
  734. $address = $1;
  735. $comment = $2 if defined $2;
  736. } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
  737. $address = $1;
  738. $comment = $2 if defined $2;
  739. $formatted_email =~ s/$address.*$//;
  740. $name = $formatted_email;
  741. $name = trim($name);
  742. $name =~ s/^\"|\"$//g;
  743. # If there's a name left after stripping spaces and
  744. # leading quotes, and the address doesn't have both
  745. # leading and trailing angle brackets, the address
  746. # is invalid. ie:
  747. # "joe smith joe@smith.com" bad
  748. # "joe smith <joe@smith.com" bad
  749. if ($name ne "" && $address !~ /^<[^>]+>$/) {
  750. $name = "";
  751. $address = "";
  752. $comment = "";
  753. }
  754. }
  755. $name = trim($name);
  756. $name =~ s/^\"|\"$//g;
  757. $address = trim($address);
  758. $address =~ s/^\<|\>$//g;
  759. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  760. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  761. $name = "\"$name\"";
  762. }
  763. return ($name, $address, $comment);
  764. }
  765. sub format_email {
  766. my ($name, $address) = @_;
  767. my $formatted_email;
  768. $name = trim($name);
  769. $name =~ s/^\"|\"$//g;
  770. $address = trim($address);
  771. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  772. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  773. $name = "\"$name\"";
  774. }
  775. if ("$name" eq "") {
  776. $formatted_email = "$address";
  777. } else {
  778. $formatted_email = "$name <$address>";
  779. }
  780. return $formatted_email;
  781. }
  782. sub which {
  783. my ($bin) = @_;
  784. foreach my $path (split(/:/, $ENV{PATH})) {
  785. if (-e "$path/$bin") {
  786. return "$path/$bin";
  787. }
  788. }
  789. return "";
  790. }
  791. sub which_conf {
  792. my ($conf) = @_;
  793. foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
  794. if (-e "$path/$conf") {
  795. return "$path/$conf";
  796. }
  797. }
  798. return "";
  799. }
  800. sub expand_tabs {
  801. my ($str) = @_;
  802. my $res = '';
  803. my $n = 0;
  804. for my $c (split(//, $str)) {
  805. if ($c eq "\t") {
  806. $res .= ' ';
  807. $n++;
  808. for (; ($n % 8) != 0; $n++) {
  809. $res .= ' ';
  810. }
  811. next;
  812. }
  813. $res .= $c;
  814. $n++;
  815. }
  816. return $res;
  817. }
  818. sub copy_spacing {
  819. (my $res = shift) =~ tr/\t/ /c;
  820. return $res;
  821. }
  822. sub line_stats {
  823. my ($line) = @_;
  824. # Drop the diff line leader and expand tabs
  825. $line =~ s/^.//;
  826. $line = expand_tabs($line);
  827. # Pick the indent from the front of the line.
  828. my ($white) = ($line =~ /^(\s*)/);
  829. return (length($line), length($white));
  830. }
  831. my $sanitise_quote = '';
  832. sub sanitise_line_reset {
  833. my ($in_comment) = @_;
  834. if ($in_comment) {
  835. $sanitise_quote = '*/';
  836. } else {
  837. $sanitise_quote = '';
  838. }
  839. }
  840. sub sanitise_line {
  841. my ($line) = @_;
  842. my $res = '';
  843. my $l = '';
  844. my $qlen = 0;
  845. my $off = 0;
  846. my $c;
  847. # Always copy over the diff marker.
  848. $res = substr($line, 0, 1);
  849. for ($off = 1; $off < length($line); $off++) {
  850. $c = substr($line, $off, 1);
  851. # Comments we are wacking completly including the begin
  852. # and end, all to $;.
  853. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
  854. $sanitise_quote = '*/';
  855. substr($res, $off, 2, "$;$;");
  856. $off++;
  857. next;
  858. }
  859. if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
  860. $sanitise_quote = '';
  861. substr($res, $off, 2, "$;$;");
  862. $off++;
  863. next;
  864. }
  865. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
  866. $sanitise_quote = '//';
  867. substr($res, $off, 2, $sanitise_quote);
  868. $off++;
  869. next;
  870. }
  871. # A \ in a string means ignore the next character.
  872. if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
  873. $c eq "\\") {
  874. substr($res, $off, 2, 'XX');
  875. $off++;
  876. next;
  877. }
  878. # Regular quotes.
  879. if ($c eq "'" || $c eq '"') {
  880. if ($sanitise_quote eq '') {
  881. $sanitise_quote = $c;
  882. substr($res, $off, 1, $c);
  883. next;
  884. } elsif ($sanitise_quote eq $c) {
  885. $sanitise_quote = '';
  886. }
  887. }
  888. #print "c<$c> SQ<$sanitise_quote>\n";
  889. if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
  890. substr($res, $off, 1, $;);
  891. } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
  892. substr($res, $off, 1, $;);
  893. } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
  894. substr($res, $off, 1, 'X');
  895. } else {
  896. substr($res, $off, 1, $c);
  897. }
  898. }
  899. if ($sanitise_quote eq '//') {
  900. $sanitise_quote = '';
  901. }
  902. # The pathname on a #include may be surrounded by '<' and '>'.
  903. if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
  904. my $clean = 'X' x length($1);
  905. $res =~ s@\<.*\>@<$clean>@;
  906. # The whole of a #error is a string.
  907. } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
  908. my $clean = 'X' x length($1);
  909. $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
  910. }
  911. return $res;
  912. }
  913. sub get_quoted_string {
  914. my ($line, $rawline) = @_;
  915. return "" if ($line !~ m/($String)/g);
  916. return substr($rawline, $-[0], $+[0] - $-[0]);
  917. }
  918. sub ctx_statement_block {
  919. my ($linenr, $remain, $off) = @_;
  920. my $line = $linenr - 1;
  921. my $blk = '';
  922. my $soff = $off;
  923. my $coff = $off - 1;
  924. my $coff_set = 0;
  925. my $loff = 0;
  926. my $type = '';
  927. my $level = 0;
  928. my @stack = ();
  929. my $p;
  930. my $c;
  931. my $len = 0;
  932. my $remainder;
  933. while (1) {
  934. @stack = (['', 0]) if ($#stack == -1);
  935. #warn "CSB: blk<$blk> remain<$remain>\n";
  936. # If we are about to drop off the end, pull in more
  937. # context.
  938. if ($off >= $len) {
  939. for (; $remain > 0; $line++) {
  940. last if (!defined $lines[$line]);
  941. next if ($lines[$line] =~ /^-/);
  942. $remain--;
  943. $loff = $len;
  944. $blk .= $lines[$line] . "\n";
  945. $len = length($blk);
  946. $line++;
  947. last;
  948. }
  949. # Bail if there is no further context.
  950. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  951. if ($off >= $len) {
  952. last;
  953. }
  954. if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
  955. $level++;
  956. $type = '#';
  957. }
  958. }
  959. $p = $c;
  960. $c = substr($blk, $off, 1);
  961. $remainder = substr($blk, $off);
  962. #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
  963. # Handle nested #if/#else.
  964. if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
  965. push(@stack, [ $type, $level ]);
  966. } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
  967. ($type, $level) = @{$stack[$#stack - 1]};
  968. } elsif ($remainder =~ /^#\s*endif\b/) {
  969. ($type, $level) = @{pop(@stack)};
  970. }
  971. # Statement ends at the ';' or a close '}' at the
  972. # outermost level.
  973. if ($level == 0 && $c eq ';') {
  974. last;
  975. }
  976. # An else is really a conditional as long as its not else if
  977. if ($level == 0 && $coff_set == 0 &&
  978. (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
  979. $remainder =~ /^(else)(?:\s|{)/ &&
  980. $remainder !~ /^else\s+if\b/) {
  981. $coff = $off + length($1) - 1;
  982. $coff_set = 1;
  983. #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
  984. #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
  985. }
  986. if (($type eq '' || $type eq '(') && $c eq '(') {
  987. $level++;
  988. $type = '(';
  989. }
  990. if ($type eq '(' && $c eq ')') {
  991. $level--;
  992. $type = ($level != 0)? '(' : '';
  993. if ($level == 0 && $coff < $soff) {
  994. $coff = $off;
  995. $coff_set = 1;
  996. #warn "CSB: mark coff<$coff>\n";
  997. }
  998. }
  999. if (($type eq '' || $type eq '{') && $c eq '{') {
  1000. $level++;
  1001. $type = '{';
  1002. }
  1003. if ($type eq '{' && $c eq '}') {
  1004. $level--;
  1005. $type = ($level != 0)? '{' : '';
  1006. if ($level == 0) {
  1007. if (substr($blk, $off + 1, 1) eq ';') {
  1008. $off++;
  1009. }
  1010. last;
  1011. }
  1012. }
  1013. # Preprocessor commands end at the newline unless escaped.
  1014. if ($type eq '#' && $c eq "\n" && $p ne "\\") {
  1015. $level--;
  1016. $type = '';
  1017. $off++;
  1018. last;
  1019. }
  1020. $off++;
  1021. }
  1022. # We are truly at the end, so shuffle to the next line.
  1023. if ($off == $len) {
  1024. $loff = $len + 1;
  1025. $line++;
  1026. $remain--;
  1027. }
  1028. my $statement = substr($blk, $soff, $off - $soff + 1);
  1029. my $condition = substr($blk, $soff, $coff - $soff + 1);
  1030. #warn "STATEMENT<$statement>\n";
  1031. #warn "CONDITION<$condition>\n";
  1032. #print "coff<$coff> soff<$off> loff<$loff>\n";
  1033. return ($statement, $condition,
  1034. $line, $remain + 1, $off - $loff + 1, $level);
  1035. }
  1036. sub statement_lines {
  1037. my ($stmt) = @_;
  1038. # Strip the diff line prefixes and rip blank lines at start and end.
  1039. $stmt =~ s/(^|\n)./$1/g;
  1040. $stmt =~ s/^\s*//;
  1041. $stmt =~ s/\s*$//;
  1042. my @stmt_lines = ($stmt =~ /\n/g);
  1043. return $#stmt_lines + 2;
  1044. }
  1045. sub statement_rawlines {
  1046. my ($stmt) = @_;
  1047. my @stmt_lines = ($stmt =~ /\n/g);
  1048. return $#stmt_lines + 2;
  1049. }
  1050. sub statement_block_size {
  1051. my ($stmt) = @_;
  1052. $stmt =~ s/(^|\n)./$1/g;
  1053. $stmt =~ s/^\s*{//;
  1054. $stmt =~ s/}\s*$//;
  1055. $stmt =~ s/^\s*//;
  1056. $stmt =~ s/\s*$//;
  1057. my @stmt_lines = ($stmt =~ /\n/g);
  1058. my @stmt_statements = ($stmt =~ /;/g);
  1059. my $stmt_lines = $#stmt_lines + 2;
  1060. my $stmt_statements = $#stmt_statements + 1;
  1061. if ($stmt_lines > $stmt_statements) {
  1062. return $stmt_lines;
  1063. } else {
  1064. return $stmt_statements;
  1065. }
  1066. }
  1067. sub ctx_statement_full {
  1068. my ($linenr, $remain, $off) = @_;
  1069. my ($statement, $condition, $level);
  1070. my (@chunks);
  1071. # Grab the first conditional/block pair.
  1072. ($statement, $condition, $linenr, $remain, $off, $level) =
  1073. ctx_statement_block($linenr, $remain, $off);
  1074. #print "F: c<$condition> s<$statement> remain<$remain>\n";
  1075. push(@chunks, [ $condition, $statement ]);
  1076. if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
  1077. return ($level, $linenr, @chunks);
  1078. }
  1079. # Pull in the following conditional/block pairs and see if they
  1080. # could continue the statement.
  1081. for (;;) {
  1082. ($statement, $condition, $linenr, $remain, $off, $level) =
  1083. ctx_statement_block($linenr, $remain, $off);
  1084. #print "C: c<$condition> s<$statement> remain<$remain>\n";
  1085. last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
  1086. #print "C: push\n";
  1087. push(@chunks, [ $condition, $statement ]);
  1088. }
  1089. return ($level, $linenr, @chunks);
  1090. }
  1091. sub ctx_block_get {
  1092. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  1093. my $line;
  1094. my $start = $linenr - 1;
  1095. my $blk = '';
  1096. my @o;
  1097. my @c;
  1098. my @res = ();
  1099. my $level = 0;
  1100. my @stack = ($level);
  1101. for ($line = $start; $remain > 0; $line++) {
  1102. next if ($rawlines[$line] =~ /^-/);
  1103. $remain--;
  1104. $blk .= $rawlines[$line];
  1105. # Handle nested #if/#else.
  1106. if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
  1107. push(@stack, $level);
  1108. } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
  1109. $level = $stack[$#stack - 1];
  1110. } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
  1111. $level = pop(@stack);
  1112. }
  1113. foreach my $c (split(//, $lines[$line])) {
  1114. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  1115. if ($off > 0) {
  1116. $off--;
  1117. next;
  1118. }
  1119. if ($c eq $close && $level > 0) {
  1120. $level--;
  1121. last if ($level == 0);
  1122. } elsif ($c eq $open) {
  1123. $level++;
  1124. }
  1125. }
  1126. if (!$outer || $level <= 1) {
  1127. push(@res, $rawlines[$line]);
  1128. }
  1129. last if ($level == 0);
  1130. }
  1131. return ($level, @res);
  1132. }
  1133. sub ctx_block_outer {
  1134. my ($linenr, $remain) = @_;
  1135. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  1136. return @r;
  1137. }
  1138. sub ctx_block {
  1139. my ($linenr, $remain) = @_;
  1140. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  1141. return @r;
  1142. }
  1143. sub ctx_statement {
  1144. my ($linenr, $remain, $off) = @_;
  1145. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  1146. return @r;
  1147. }
  1148. sub ctx_block_level {
  1149. my ($linenr, $remain) = @_;
  1150. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  1151. }
  1152. sub ctx_statement_level {
  1153. my ($linenr, $remain, $off) = @_;
  1154. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  1155. }
  1156. sub ctx_locate_comment {
  1157. my ($first_line, $end_line) = @_;
  1158. # Catch a comment on the end of the line itself.
  1159. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
  1160. return $current_comment if (defined $current_comment);
  1161. # Look through the context and try and figure out if there is a
  1162. # comment.
  1163. my $in_comment = 0;
  1164. $current_comment = '';
  1165. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  1166. my $line = $rawlines[$linenr - 1];
  1167. #warn " $line\n";
  1168. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  1169. $in_comment = 1;
  1170. }
  1171. if ($line =~ m@/\*@) {
  1172. $in_comment = 1;
  1173. }
  1174. if (!$in_comment && $current_comment ne '') {
  1175. $current_comment = '';
  1176. }
  1177. $current_comment .= $line . "\n" if ($in_comment);
  1178. if ($line =~ m@\*/@) {
  1179. $in_comment = 0;
  1180. }
  1181. }
  1182. chomp($current_comment);
  1183. return($current_comment);
  1184. }
  1185. sub ctx_has_comment {
  1186. my ($first_line, $end_line) = @_;
  1187. my $cmt = ctx_locate_comment($first_line, $end_line);
  1188. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  1189. ##print "CMMT: $cmt\n";
  1190. return ($cmt ne '');
  1191. }
  1192. sub raw_line {
  1193. my ($linenr, $cnt) = @_;
  1194. my $offset = $linenr - 1;
  1195. $cnt++;
  1196. my $line;
  1197. while ($cnt) {
  1198. $line = $rawlines[$offset++];
  1199. next if (defined($line) && $line =~ /^-/);
  1200. $cnt--;
  1201. }
  1202. return $line;
  1203. }
  1204. sub cat_vet {
  1205. my ($vet) = @_;
  1206. my ($res, $coded);
  1207. $res = '';
  1208. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  1209. $res .= $1;
  1210. if ($2 ne '') {
  1211. $coded = sprintf("^%c", unpack('C', $2) + 64);
  1212. $res .= $coded;
  1213. }
  1214. }
  1215. $res =~ s/$/\$/;
  1216. return $res;
  1217. }
  1218. my $av_preprocessor = 0;
  1219. my $av_pending;
  1220. my @av_paren_type;
  1221. my $av_pend_colon;
  1222. sub annotate_reset {
  1223. $av_preprocessor = 0;
  1224. $av_pending = '_';
  1225. @av_paren_type = ('E');
  1226. $av_pend_colon = 'O';
  1227. }
  1228. sub annotate_values {
  1229. my ($stream, $type) = @_;
  1230. my $res;
  1231. my $var = '_' x length($stream);
  1232. my $cur = $stream;
  1233. print "$stream\n" if ($dbg_values > 1);
  1234. while (length($cur)) {
  1235. @av_paren_type = ('E') if ($#av_paren_type < 0);
  1236. print " <" . join('', @av_paren_type) .
  1237. "> <$type> <$av_pending>" if ($dbg_values > 1);
  1238. if ($cur =~ /^(\s+)/o) {
  1239. print "WS($1)\n" if ($dbg_values > 1);
  1240. if ($1 =~ /\n/ && $av_preprocessor) {
  1241. $type = pop(@av_paren_type);
  1242. $av_preprocessor = 0;
  1243. }
  1244. } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
  1245. print "CAST($1)\n" if ($dbg_values > 1);
  1246. push(@av_paren_type, $type);
  1247. $type = 'c';
  1248. } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
  1249. print "DECLARE($1)\n" if ($dbg_values > 1);
  1250. $type = 'T';
  1251. } elsif ($cur =~ /^($Modifier)\s*/) {
  1252. print "MODIFIER($1)\n" if ($dbg_values > 1);
  1253. $type = 'T';
  1254. } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
  1255. print "DEFINE($1,$2)\n" if ($dbg_values > 1);
  1256. $av_preprocessor = 1;
  1257. push(@av_paren_type, $type);
  1258. if ($2 ne '') {
  1259. $av_pending = 'N';
  1260. }
  1261. $type = 'E';
  1262. } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
  1263. print "UNDEF($1)\n" if ($dbg_values > 1);
  1264. $av_preprocessor = 1;
  1265. push(@av_paren_type, $type);
  1266. } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
  1267. print "PRE_START($1)\n" if ($dbg_values > 1);
  1268. $av_preprocessor = 1;
  1269. push(@av_paren_type, $type);
  1270. push(@av_paren_type, $type);
  1271. $type = 'E';
  1272. } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
  1273. print "PRE_RESTART($1)\n" if ($dbg_values > 1);
  1274. $av_preprocessor = 1;
  1275. push(@av_paren_type, $av_paren_type[$#av_paren_type]);
  1276. $type = 'E';
  1277. } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
  1278. print "PRE_END($1)\n" if ($dbg_values > 1);
  1279. $av_preprocessor = 1;
  1280. # Assume all arms of the conditional end as this
  1281. # one does, and continue as if the #endif was not here.
  1282. pop(@av_paren_type);
  1283. push(@av_paren_type, $type);
  1284. $type = 'E';
  1285. } elsif ($cur =~ /^(\\\n)/o) {
  1286. print "PRECONT($1)\n" if ($dbg_values > 1);
  1287. } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
  1288. print "ATTR($1)\n" if ($dbg_values > 1);
  1289. $av_pending = $type;
  1290. $type = 'N';
  1291. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  1292. print "SIZEOF($1)\n" if ($dbg_values > 1);
  1293. if (defined $2) {
  1294. $av_pending = 'V';
  1295. }
  1296. $type = 'N';
  1297. } elsif ($cur =~ /^(if|while|for)\b/o) {
  1298. print "COND($1)\n" if ($dbg_values > 1);
  1299. $av_pending = 'E';
  1300. $type = 'N';
  1301. } elsif ($cur =~/^(case)/o) {
  1302. print "CASE($1)\n" if ($dbg_values > 1);
  1303. $av_pend_colon = 'C';
  1304. $type = 'N';
  1305. } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
  1306. print "KEYWORD($1)\n" if ($dbg_values > 1);
  1307. $type = 'N';
  1308. } elsif ($cur =~ /^(\()/o) {
  1309. print "PAREN('$1')\n" if ($dbg_values > 1);
  1310. push(@av_paren_type, $av_pending);
  1311. $av_pending = '_';
  1312. $type = 'N';
  1313. } elsif ($cur =~ /^(\))/o) {
  1314. my $new_type = pop(@av_paren_type);
  1315. if ($new_type ne '_') {
  1316. $type = $new_type;
  1317. print "PAREN('$1') -> $type\n"
  1318. if ($dbg_values > 1);
  1319. } else {
  1320. print "PAREN('$1')\n" if ($dbg_values > 1);
  1321. }
  1322. } elsif ($cur =~ /^($Ident)\s*\(/o) {
  1323. print "FUNC($1)\n" if ($dbg_values > 1);
  1324. $type = 'V';
  1325. $av_pending = 'V';
  1326. } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
  1327. if (defined $2 && $type eq 'C' || $type eq 'T') {
  1328. $av_pend_colon = 'B';
  1329. } elsif ($type eq 'E') {
  1330. $av_pend_colon = 'L';
  1331. }
  1332. print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
  1333. $type = 'V';
  1334. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  1335. print "IDENT($1)\n" if ($dbg_values > 1);
  1336. $type = 'V';
  1337. } elsif ($cur =~ /^($Assignment)/o) {
  1338. print "ASSIGN($1)\n" if ($dbg_values > 1);
  1339. $type = 'N';
  1340. } elsif ($cur =~/^(;|{|})/) {
  1341. print "END($1)\n" if ($dbg_values > 1);
  1342. $type = 'E';
  1343. $av_pend_colon = 'O';
  1344. } elsif ($cur =~/^(,)/) {
  1345. print "COMMA($1)\n" if ($dbg_values > 1);
  1346. $type = 'C';
  1347. } elsif ($cur =~ /^(\?)/o) {
  1348. print "QUESTION($1)\n" if ($dbg_values > 1);
  1349. $type = 'N';
  1350. } elsif ($cur =~ /^(:)/o) {
  1351. print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
  1352. substr($var, length($res), 1, $av_pend_colon);
  1353. if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
  1354. $type = 'E';
  1355. } else {
  1356. $type = 'N';
  1357. }
  1358. $av_pend_colon = 'O';
  1359. } elsif ($cur =~ /^(\[)/o) {
  1360. print "CLOSE($1)\n" if ($dbg_values > 1);
  1361. $type = 'N';
  1362. } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
  1363. my $variant;
  1364. print "OPV($1)\n" if ($dbg_values > 1);
  1365. if ($type eq 'V') {
  1366. $variant = 'B';
  1367. } else {
  1368. $variant = 'U';
  1369. }
  1370. substr($var, length($res), 1, $variant);
  1371. $type = 'N';
  1372. } elsif ($cur =~ /^($Operators)/o) {
  1373. print "OP($1)\n" if ($dbg_values > 1);
  1374. if ($1 ne '++' && $1 ne '--') {
  1375. $type = 'N';
  1376. }
  1377. } elsif ($cur =~ /(^.)/o) {
  1378. print "C($1)\n" if ($dbg_values > 1);
  1379. }
  1380. if (defined $1) {
  1381. $cur = substr($cur, length($1));
  1382. $res .= $type x length($1);
  1383. }
  1384. }
  1385. return ($res, $var);
  1386. }
  1387. sub possible {
  1388. my ($possible, $line) = @_;
  1389. my $notPermitted = qr{(?:
  1390. ^(?:
  1391. $Modifier|
  1392. $Storage|
  1393. $Type|
  1394. DEFINE_\S+
  1395. )$|
  1396. ^(?:
  1397. goto|
  1398. return|
  1399. case|
  1400. else|
  1401. asm|__asm__|
  1402. do|
  1403. \#|
  1404. \#\#|
  1405. )(?:\s|$)|
  1406. ^(?:typedef|struct|enum)\b
  1407. )}x;
  1408. warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
  1409. if ($possible !~ $notPermitted) {
  1410. # Check for modifiers.
  1411. $possible =~ s/\s*$Storage\s*//g;
  1412. $possible =~ s/\s*$Sparse\s*//g;
  1413. if ($possible =~ /^\s*$/) {
  1414. } elsif ($possible =~ /\s/) {
  1415. $possible =~ s/\s*$Type\s*//g;
  1416. for my $modifier (split(' ', $possible)) {
  1417. if ($modifier !~ $notPermitted) {
  1418. warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
  1419. push(@modifierListFile, $modifier);
  1420. }
  1421. }
  1422. } else {
  1423. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  1424. push(@typeListFile, $possible);
  1425. }
  1426. build_types();
  1427. } else {
  1428. warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
  1429. }
  1430. }
  1431. my $prefix = '';
  1432. sub show_type {
  1433. my ($type) = @_;
  1434. return defined $use_type{$type} if (scalar keys %use_type > 0);
  1435. return !defined $ignore_type{$type};
  1436. }
  1437. sub report {
  1438. my ($level, $type, $msg) = @_;
  1439. if (!show_type($type) ||
  1440. (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
  1441. return 0;
  1442. }
  1443. my $output = '';
  1444. if (-t STDOUT && $color) {
  1445. if ($level eq 'ERROR') {
  1446. $output .= RED;
  1447. } elsif ($level eq 'WARNING') {
  1448. $output .= YELLOW;
  1449. } else {
  1450. $output .= GREEN;
  1451. }
  1452. }
  1453. $output .= $prefix . $level . ':';
  1454. if ($show_types) {
  1455. $output .= BLUE if (-t STDOUT && $color);
  1456. $output .= "$type:";
  1457. }
  1458. $output .= RESET if (-t STDOUT && $color);
  1459. $output .= ' ' . $msg . "\n";
  1460. if ($showfile) {
  1461. my @lines = split("\n", $output, -1);
  1462. splice(@lines, 1, 1);
  1463. $output = join("\n", @lines);
  1464. }
  1465. $output = (split('\n', $output))[0] . "\n" if ($terse);
  1466. push(our @report, $output);
  1467. return 1;
  1468. }
  1469. sub report_dump {
  1470. our @report;
  1471. }
  1472. sub fixup_current_range {
  1473. my ($lineRef, $offset, $length) = @_;
  1474. if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
  1475. my $o = $1;
  1476. my $l = $2;
  1477. my $no = $o + $offset;
  1478. my $nl = $l + $length;
  1479. $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
  1480. }
  1481. }
  1482. sub fix_inserted_deleted_lines {
  1483. my ($linesRef, $insertedRef, $deletedRef) = @_;
  1484. my $range_last_linenr = 0;
  1485. my $delta_offset = 0;
  1486. my $old_linenr = 0;
  1487. my $new_linenr = 0;
  1488. my $next_insert = 0;
  1489. my $next_delete = 0;
  1490. my @lines = ();
  1491. my $inserted = @{$insertedRef}[$next_insert++];
  1492. my $deleted = @{$deletedRef}[$next_delete++];
  1493. foreach my $old_line (@{$linesRef}) {
  1494. my $save_line = 1;
  1495. my $line = $old_line; #don't modify the array
  1496. if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
  1497. $delta_offset = 0;
  1498. } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
  1499. $range_last_linenr = $new_linenr;
  1500. fixup_current_range(\$line, $delta_offset, 0);
  1501. }
  1502. while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
  1503. $deleted = @{$deletedRef}[$next_delete++];
  1504. $save_line = 0;
  1505. fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
  1506. }
  1507. while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
  1508. push(@lines, ${$inserted}{'LINE'});
  1509. $inserted = @{$insertedRef}[$next_insert++];
  1510. $new_linenr++;
  1511. fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
  1512. }
  1513. if ($save_line) {
  1514. push(@lines, $line);
  1515. $new_linenr++;
  1516. }
  1517. $old_linenr++;
  1518. }
  1519. return @lines;
  1520. }
  1521. sub fix_insert_line {
  1522. my ($linenr, $line) = @_;
  1523. my $inserted = {
  1524. LINENR => $linenr,
  1525. LINE => $line,
  1526. };
  1527. push(@fixed_inserted, $inserted);
  1528. }
  1529. sub fix_delete_line {
  1530. my ($linenr, $line) = @_;
  1531. my $deleted = {
  1532. LINENR => $linenr,
  1533. LINE => $line,
  1534. };
  1535. push(@fixed_deleted, $deleted);
  1536. }
  1537. sub ERROR {
  1538. my ($type, $msg) = @_;
  1539. if (report("ERROR", $type, $msg)) {
  1540. our $clean = 0;
  1541. our $cnt_error++;
  1542. return 1;
  1543. }
  1544. return 0;
  1545. }
  1546. sub WARN {
  1547. my ($type, $msg) = @_;
  1548. if (report("WARNING", $type, $msg)) {
  1549. our $clean = 0;
  1550. our $cnt_warn++;
  1551. return 1;
  1552. }
  1553. return 0;
  1554. }
  1555. sub CHK {
  1556. my ($type, $msg) = @_;
  1557. if ($check && report("CHECK", $type, $msg)) {
  1558. our $clean = 0;
  1559. our $cnt_chk++;
  1560. return 1;
  1561. }
  1562. return 0;
  1563. }
  1564. sub check_absolute_file {
  1565. my ($absolute, $herecurr) = @_;
  1566. my $file = $absolute;
  1567. ##print "absolute<$absolute>\n";
  1568. # See if any suffix of this path is a path within the tree.
  1569. while ($file =~ s@^[^/]*/@@) {
  1570. if (-f "$root/$file") {
  1571. ##print "file<$file>\n";
  1572. last;
  1573. }
  1574. }
  1575. if (! -f _) {
  1576. return 0;
  1577. }
  1578. # It is, so see if the prefix is acceptable.
  1579. my $prefix = $absolute;
  1580. substr($prefix, -length($file)) = '';
  1581. ##print "prefix<$prefix>\n";
  1582. if ($prefix ne ".../") {
  1583. WARN("USE_RELATIVE_PATH",
  1584. "use relative pathname instead of absolute in changelog text\n" . $herecurr);
  1585. }
  1586. }
  1587. sub trim {
  1588. my ($string) = @_;
  1589. $string =~ s/^\s+|\s+$//g;
  1590. return $string;
  1591. }
  1592. sub ltrim {
  1593. my ($string) = @_;
  1594. $string =~ s/^\s+//;
  1595. return $string;
  1596. }
  1597. sub rtrim {
  1598. my ($string) = @_;
  1599. $string =~ s/\s+$//;
  1600. return $string;
  1601. }
  1602. sub string_find_replace {
  1603. my ($string, $find, $replace) = @_;
  1604. $string =~ s/$find/$replace/g;
  1605. return $string;
  1606. }
  1607. sub tabify {
  1608. my ($leading) = @_;
  1609. my $source_indent = 8;
  1610. my $max_spaces_before_tab = $source_indent - 1;
  1611. my $spaces_to_tab = " " x $source_indent;
  1612. #convert leading spaces to tabs
  1613. 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
  1614. #Remove spaces before a tab
  1615. 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
  1616. return "$leading";
  1617. }
  1618. sub pos_last_openparen {
  1619. my ($line) = @_;
  1620. my $pos = 0;
  1621. my $opens = $line =~ tr/\(/\(/;
  1622. my $closes = $line =~ tr/\)/\)/;
  1623. my $last_openparen = 0;
  1624. if (($opens == 0) || ($closes >= $opens)) {
  1625. return -1;
  1626. }
  1627. my $len = length($line);
  1628. for ($pos = 0; $pos < $len; $pos++) {
  1629. my $string = substr($line, $pos);
  1630. if ($string =~ /^($FuncArg|$balanced_parens)/) {
  1631. $pos += length($1) - 1;
  1632. } elsif (substr($line, $pos, 1) eq '(') {
  1633. $last_openparen = $pos;
  1634. } elsif (index($string, '(') == -1) {
  1635. last;
  1636. }
  1637. }
  1638. return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
  1639. }
  1640. sub process {
  1641. my $filename = shift;
  1642. my $linenr=0;
  1643. my $prevline="";
  1644. my $prevrawline="";
  1645. my $stashline="";
  1646. my $stashrawline="";
  1647. my $length;
  1648. my $indent;
  1649. my $previndent=0;
  1650. my $stashindent=0;
  1651. our $clean = 1;
  1652. my $signoff = 0;
  1653. my $is_patch = 0;
  1654. my $in_header_lines = $file ? 0 : 1;
  1655. my $in_commit_log = 0; #Scanning lines before patch
  1656. my $commit_log_possible_stack_dump = 0;
  1657. my $commit_log_long_line = 0;
  1658. my $commit_log_has_diff = 0;
  1659. my $reported_maintainer_file = 0;
  1660. my $non_utf8_charset = 0;
  1661. my $last_blank_line = 0;
  1662. my $last_coalesced_string_linenr = -1;
  1663. our @report = ();
  1664. our $cnt_lines = 0;
  1665. our $cnt_error = 0;
  1666. our $cnt_warn = 0;
  1667. our $cnt_chk = 0;
  1668. # Trace the real file/line as we go.
  1669. my $realfile = '';
  1670. my $realline = 0;
  1671. my $realcnt = 0;
  1672. my $here = '';
  1673. my $in_comment = 0;
  1674. my $comment_edge = 0;
  1675. my $first_line = 0;
  1676. my $p1_prefix = '';
  1677. my $prev_values = 'E';
  1678. # suppression flags
  1679. my %suppress_ifbraces;
  1680. my %suppress_whiletrailers;
  1681. my %suppress_export;
  1682. my $suppress_statement = 0;
  1683. my %signatures = ();
  1684. # Pre-scan the patch sanitizing the lines.
  1685. # Pre-scan the patch looking for any __setup documentation.
  1686. #
  1687. my @setup_docs = ();
  1688. my $setup_docs = 0;
  1689. my $camelcase_file_seeded = 0;
  1690. sanitise_line_reset();
  1691. my $line;
  1692. foreach my $rawline (@rawlines) {
  1693. $linenr++;
  1694. $line = $rawline;
  1695. push(@fixed, $rawline) if ($fix);
  1696. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  1697. $setup_docs = 0;
  1698. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  1699. $setup_docs = 1;
  1700. }
  1701. #next;
  1702. }
  1703. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1704. $realline=$1-1;
  1705. if (defined $2) {
  1706. $realcnt=$3+1;
  1707. } else {
  1708. $realcnt=1+1;
  1709. }
  1710. $in_comment = 0;
  1711. # Guestimate if this is a continuing comment. Run
  1712. # the context looking for a comment "edge". If this
  1713. # edge is a close comment then we must be in a comment
  1714. # at context start.
  1715. my $edge;
  1716. my $cnt = $realcnt;
  1717. for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
  1718. next if (defined $rawlines[$ln - 1] &&
  1719. $rawlines[$ln - 1] =~ /^-/);
  1720. $cnt--;
  1721. #print "RAW<$rawlines[$ln - 1]>\n";
  1722. last if (!defined $rawlines[$ln - 1]);
  1723. if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
  1724. $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
  1725. ($edge) = $1;
  1726. last;
  1727. }
  1728. }
  1729. if (defined $edge && $edge eq '*/') {
  1730. $in_comment = 1;
  1731. }
  1732. # Guestimate if this is a continuing comment. If this
  1733. # is the start of a diff block and this line starts
  1734. # ' *' then it is very likely a comment.
  1735. if (!defined $edge &&
  1736. $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
  1737. {
  1738. $in_comment = 1;
  1739. }
  1740. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  1741. sanitise_line_reset($in_comment);
  1742. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  1743. # Standardise the strings and chars within the input to
  1744. # simplify matching -- only bother with positive lines.
  1745. $line = sanitise_line($rawline);
  1746. }
  1747. push(@lines, $line);
  1748. if ($realcnt > 1) {
  1749. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  1750. } else {
  1751. $realcnt = 0;
  1752. }
  1753. #print "==>$rawline\n";
  1754. #print "-->$line\n";
  1755. if ($setup_docs && $line =~ /^\+/) {
  1756. push(@setup_docs, $line);
  1757. }
  1758. }
  1759. $prefix = '';
  1760. $realcnt = 0;
  1761. $linenr = 0;
  1762. $fixlinenr = -1;
  1763. foreach my $line (@lines) {
  1764. $linenr++;
  1765. $fixlinenr++;
  1766. my $sline = $line; #copy of $line
  1767. $sline =~ s/$;/ /g; #with comments as spaces
  1768. my $rawline = $rawlines[$linenr - 1];
  1769. #extract the line range in the file after the patch is applied
  1770. if (!$in_commit_log &&
  1771. $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1772. $is_patch = 1;
  1773. $first_line = $linenr + 1;
  1774. $realline=$1-1;
  1775. if (defined $2) {
  1776. $realcnt=$3+1;
  1777. } else {
  1778. $realcnt=1+1;
  1779. }
  1780. annotate_reset();
  1781. $prev_values = 'E';
  1782. %suppress_ifbraces = ();
  1783. %suppress_whiletrailers = ();
  1784. %suppress_export = ();
  1785. $suppress_statement = 0;
  1786. next;
  1787. # track the line number as we move through the hunk, note that
  1788. # new versions of GNU diff omit the leading space on completely
  1789. # blank context lines so we need to count that too.
  1790. } elsif ($line =~ /^( |\+|$)/) {
  1791. $realline++;
  1792. $realcnt-- if ($realcnt != 0);
  1793. # Measure the line length and indent.
  1794. ($length, $indent) = line_stats($rawline);
  1795. # Track the previous line.
  1796. ($prevline, $stashline) = ($stashline, $line);
  1797. ($previndent, $stashindent) = ($stashindent, $indent);
  1798. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  1799. #warn "line<$line>\n";
  1800. } elsif ($realcnt == 1) {
  1801. $realcnt--;
  1802. }
  1803. my $hunk_line = ($realcnt != 0);
  1804. $here = "#$linenr: " if (!$file);
  1805. $here = "#$realline: " if ($file);
  1806. my $found_file = 0;
  1807. # extract the filename as it passes
  1808. if ($line =~ /^diff --git.*?(\S+)$/) {
  1809. $realfile = $1;
  1810. $realfile =~ s@^([^/]*)/@@ if (!$file);
  1811. $in_commit_log = 0;
  1812. $found_file = 1;
  1813. } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
  1814. $realfile = $1;
  1815. $realfile =~ s@^([^/]*)/@@ if (!$file);
  1816. $in_commit_log = 0;
  1817. $p1_prefix = $1;
  1818. if (!$file && $tree && $p1_prefix ne '' &&
  1819. -e "$root/$p1_prefix") {
  1820. WARN("PATCH_PREFIX",
  1821. "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
  1822. }
  1823. if ($realfile =~ m@^include/asm/@) {
  1824. ERROR("MODIFIED_INCLUDE_ASM",
  1825. "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  1826. }
  1827. $found_file = 1;
  1828. }
  1829. #make up the handle for any error we report on this line
  1830. if ($showfile) {
  1831. $prefix = "$realfile:$realline: "
  1832. } elsif ($emacs) {
  1833. if ($file) {
  1834. $prefix = "$filename:$realline: ";
  1835. } else {
  1836. $prefix = "$filename:$linenr: ";
  1837. }
  1838. }
  1839. if ($found_file) {
  1840. if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
  1841. $check = 1;
  1842. } else {
  1843. $check = $check_orig;
  1844. }
  1845. next;
  1846. }
  1847. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  1848. my $hereline = "$here\n$rawline\n";
  1849. my $herecurr = "$here\n$rawline\n";
  1850. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  1851. $cnt_lines++ if ($realcnt != 0);
  1852. # Check if the commit log has what seems like a diff which can confuse patch
  1853. if ($in_commit_log && !$commit_log_has_diff &&
  1854. (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
  1855. $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
  1856. $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
  1857. $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
  1858. ERROR("DIFF_IN_COMMIT_MSG",
  1859. "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
  1860. $commit_log_has_diff = 1;
  1861. }
  1862. # Check for incorrect file permissions
  1863. if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
  1864. my $permhere = $here . "FILE: $realfile\n";
  1865. if ($realfile !~ m@scripts/@ &&
  1866. $realfile !~ /\.(py|pl|awk|sh)$/) {
  1867. ERROR("EXECUTE_PERMISSIONS",
  1868. "do not set execute permissions for source files\n" . $permhere);
  1869. }
  1870. }
  1871. # Check the patch for a signoff:
  1872. if ($line =~ /^\s*signed-off-by:/i) {
  1873. $signoff++;
  1874. $in_commit_log = 0;
  1875. }
  1876. # Check if MAINTAINERS is being updated. If so, there's probably no need to
  1877. # emit the "does MAINTAINERS need updating?" message on file add/move/delete
  1878. if ($line =~ /^\s*MAINTAINERS\s*\|/) {
  1879. $reported_maintainer_file = 1;
  1880. }
  1881. # Check signature styles
  1882. if (!$in_header_lines &&
  1883. $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
  1884. my $space_before = $1;
  1885. my $sign_off = $2;
  1886. my $space_after = $3;
  1887. my $email = $4;
  1888. my $ucfirst_sign_off = ucfirst(lc($sign_off));
  1889. if ($sign_off !~ /$signature_tags/) {
  1890. WARN("BAD_SIGN_OFF",
  1891. "Non-standard signature: $sign_off\n" . $herecurr);
  1892. }
  1893. if (defined $space_before && $space_before ne "") {
  1894. if (WARN("BAD_SIGN_OFF",
  1895. "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
  1896. $fix) {
  1897. $fixed[$fixlinenr] =
  1898. "$ucfirst_sign_off $email";
  1899. }
  1900. }
  1901. if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
  1902. if (WARN("BAD_SIGN_OFF",
  1903. "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
  1904. $fix) {
  1905. $fixed[$fixlinenr] =
  1906. "$ucfirst_sign_off $email";
  1907. }
  1908. }
  1909. if (!defined $space_after || $space_after ne " ") {
  1910. if (WARN("BAD_SIGN_OFF",
  1911. "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
  1912. $fix) {
  1913. $fixed[$fixlinenr] =
  1914. "$ucfirst_sign_off $email";
  1915. }
  1916. }
  1917. my ($email_name, $email_address, $comment) = parse_email($email);
  1918. my $suggested_email = format_email(($email_name, $email_address));
  1919. if ($suggested_email eq "") {
  1920. ERROR("BAD_SIGN_OFF",
  1921. "Unrecognized email address: '$email'\n" . $herecurr);
  1922. } else {
  1923. my $dequoted = $suggested_email;
  1924. $dequoted =~ s/^"//;
  1925. $dequoted =~ s/" </ </;
  1926. # Don't force email to have quotes
  1927. # Allow just an angle bracketed address
  1928. if ("$dequoted$comment" ne $email &&
  1929. "<$email_address>$comment" ne $email &&
  1930. "$suggested_email$comment" ne $email) {
  1931. WARN("BAD_SIGN_OFF",
  1932. "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
  1933. }
  1934. }
  1935. # Check for duplicate signatures
  1936. my $sig_nospace = $line;
  1937. $sig_nospace =~ s/\s//g;
  1938. $sig_nospace = lc($sig_nospace);
  1939. if (defined $signatures{$sig_nospace}) {
  1940. WARN("BAD_SIGN_OFF",
  1941. "Duplicate signature\n" . $herecurr);
  1942. } else {
  1943. $signatures{$sig_nospace} = 1;
  1944. }
  1945. }
  1946. # Check email subject for common tools that don't need to be mentioned
  1947. if ($in_header_lines &&
  1948. $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
  1949. WARN("EMAIL_SUBJECT",
  1950. "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
  1951. }
  1952. # Check for old stable address
  1953. if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
  1954. ERROR("STABLE_ADDRESS",
  1955. "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
  1956. }
  1957. # Check for unwanted Gerrit info
  1958. if ($in_commit_log && $line =~ /^\s*change-id:/i) {
  1959. ERROR("GERRIT_CHANGE_ID",
  1960. "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
  1961. }
  1962. # Check if the commit log is in a possible stack dump
  1963. if ($in_commit_log && !$commit_log_possible_stack_dump &&
  1964. ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
  1965. $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
  1966. # timestamp
  1967. $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
  1968. # stack dump address
  1969. $commit_log_possible_stack_dump = 1;
  1970. }
  1971. # Check for line lengths > 75 in commit log, warn once
  1972. if ($in_commit_log && !$commit_log_long_line &&
  1973. length($line) > 75 &&
  1974. !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
  1975. # file delta changes
  1976. $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
  1977. # filename then :
  1978. $line =~ /^\s*(?:Fixes:|Link:)/i ||
  1979. # A Fixes: or Link: line
  1980. $commit_log_possible_stack_dump)) {
  1981. WARN("COMMIT_LOG_LONG_LINE",
  1982. "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
  1983. $commit_log_long_line = 1;
  1984. }
  1985. # Reset possible stack dump if a blank line is found
  1986. if ($in_commit_log && $commit_log_possible_stack_dump &&
  1987. $line =~ /^\s*$/) {
  1988. $commit_log_possible_stack_dump = 0;
  1989. }
  1990. # Check for git id commit length and improperly formed commit descriptions
  1991. if ($in_commit_log && !$commit_log_possible_stack_dump &&
  1992. ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
  1993. ($line =~ /\b[0-9a-f]{12,40}\b/i &&
  1994. $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
  1995. $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
  1996. my $init_char = "c";
  1997. my $orig_commit = "";
  1998. my $short = 1;
  1999. my $long = 0;
  2000. my $case = 1;
  2001. my $space = 1;
  2002. my $hasdesc = 0;
  2003. my $hasparens = 0;
  2004. my $id = '0123456789ab';
  2005. my $orig_desc = "commit description";
  2006. my $description = "";
  2007. if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
  2008. $init_char = $1;
  2009. $orig_commit = lc($2);
  2010. } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
  2011. $orig_commit = lc($1);
  2012. }
  2013. $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
  2014. $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
  2015. $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
  2016. $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
  2017. if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
  2018. $orig_desc = $1;
  2019. $hasparens = 1;
  2020. } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
  2021. defined $rawlines[$linenr] &&
  2022. $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
  2023. $orig_desc = $1;
  2024. $hasparens = 1;
  2025. } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
  2026. defined $rawlines[$linenr] &&
  2027. $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
  2028. $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
  2029. $orig_desc = $1;
  2030. $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
  2031. $orig_desc .= " " . $1;
  2032. $hasparens = 1;
  2033. }
  2034. ($id, $description) = git_commit_info($orig_commit,
  2035. $id, $orig_desc);
  2036. if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
  2037. ERROR("GIT_COMMIT_ID",
  2038. "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
  2039. }
  2040. }
  2041. # Check for added, moved or deleted files
  2042. if (!$reported_maintainer_file && !$in_commit_log &&
  2043. ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
  2044. $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
  2045. ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
  2046. (defined($1) || defined($2))))) {
  2047. $reported_maintainer_file = 1;
  2048. WARN("FILE_PATH_CHANGES",
  2049. "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
  2050. }
  2051. # Check for wrappage within a valid hunk of the file
  2052. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  2053. ERROR("CORRUPTED_PATCH",
  2054. "patch seems to be corrupt (line wrapped?)\n" .
  2055. $herecurr) if (!$emitted_corrupt++);
  2056. }
  2057. # Check for absolute kernel paths.
  2058. if ($tree) {
  2059. while ($line =~ m{(?:^|\s)(/\S*)}g) {
  2060. my $file = $1;
  2061. if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
  2062. check_absolute_file($1, $herecurr)) {
  2063. #
  2064. } else {
  2065. check_absolute_file($file, $herecurr);
  2066. }
  2067. }
  2068. }
  2069. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  2070. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  2071. $rawline !~ m/^$UTF8*$/) {
  2072. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  2073. my $blank = copy_spacing($rawline);
  2074. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  2075. my $hereptr = "$hereline$ptr\n";
  2076. CHK("INVALID_UTF8",
  2077. "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  2078. }
  2079. # Check if it's the start of a commit log
  2080. # (not a header line and we haven't seen the patch filename)
  2081. if ($in_header_lines && $realfile =~ /^$/ &&
  2082. !($rawline =~ /^\s+\S/ ||
  2083. $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
  2084. $in_header_lines = 0;
  2085. $in_commit_log = 1;
  2086. }
  2087. # Check if there is UTF-8 in a commit log when a mail header has explicitly
  2088. # declined it, i.e defined some charset where it is missing.
  2089. if ($in_header_lines &&
  2090. $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
  2091. $1 !~ /utf-8/i) {
  2092. $non_utf8_charset = 1;
  2093. }
  2094. if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
  2095. $rawline =~ /$NON_ASCII_UTF8/) {
  2096. WARN("UTF8_BEFORE_PATCH",
  2097. "8-bit UTF-8 used in possible commit log\n" . $herecurr);
  2098. }
  2099. # Check for various typo / spelling mistakes
  2100. if (defined($misspellings) &&
  2101. ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
  2102. while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
  2103. my $typo = $1;
  2104. my $typo_fix = $spelling_fix{lc($typo)};
  2105. $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
  2106. $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
  2107. my $msg_type = \&WARN;
  2108. $msg_type = \&CHK if ($file);
  2109. if (&{$msg_type}("TYPO_SPELLING",
  2110. "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
  2111. $fix) {
  2112. $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
  2113. }
  2114. }
  2115. }
  2116. # ignore non-hunk lines and lines being removed
  2117. next if (!$hunk_line || $line =~ /^-/);
  2118. #trailing whitespace
  2119. if ($line =~ /^\+.*\015/) {
  2120. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  2121. if (ERROR("DOS_LINE_ENDINGS",
  2122. "DOS line endings\n" . $herevet) &&
  2123. $fix) {
  2124. $fixed[$fixlinenr] =~ s/[\s\015]+$//;
  2125. }
  2126. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  2127. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  2128. if (ERROR("TRAILING_WHITESPACE",
  2129. "trailing whitespace\n" . $herevet) &&
  2130. $fix) {
  2131. $fixed[$fixlinenr] =~ s/\s+$//;
  2132. }
  2133. $rpt_cleaners = 1;
  2134. }
  2135. # Check for FSF mailing addresses.
  2136. if ($rawline =~ /\bwrite to the Free/i ||
  2137. $rawline =~ /\b59\s+Temple\s+Pl/i ||
  2138. $rawline =~ /\b51\s+Franklin\s+St/i) {
  2139. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  2140. my $msg_type = \&ERROR;
  2141. $msg_type = \&CHK if ($file);
  2142. &{$msg_type}("FSF_MAILING_ADDRESS",
  2143. "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
  2144. }
  2145. # check for Kconfig help text having a real description
  2146. # Only applies when adding the entry originally, after that we do not have
  2147. # sufficient context to determine whether it is indeed long enough.
  2148. if ($realfile =~ /Kconfig/ &&
  2149. $line =~ /^\+\s*config\s+/) {
  2150. my $length = 0;
  2151. my $cnt = $realcnt;
  2152. my $ln = $linenr + 1;
  2153. my $f;
  2154. my $is_start = 0;
  2155. my $is_end = 0;
  2156. for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
  2157. $f = $lines[$ln - 1];
  2158. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  2159. $is_end = $lines[$ln - 1] =~ /^\+/;
  2160. next if ($f =~ /^-/);
  2161. last if (!$file && $f =~ /^\@\@/);
  2162. if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
  2163. $is_start = 1;
  2164. } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
  2165. $length = -1;
  2166. }
  2167. $f =~ s/^.//;
  2168. $f =~ s/#.*//;
  2169. $f =~ s/^\s+//;
  2170. next if ($f =~ /^$/);
  2171. if ($f =~ /^\s*config\s/) {
  2172. $is_end = 1;
  2173. last;
  2174. }
  2175. $length++;
  2176. }
  2177. if ($is_start && $is_end && $length < $min_conf_desc_length) {
  2178. WARN("CONFIG_DESCRIPTION",
  2179. "please write a paragraph that describes the config symbol fully\n" . $herecurr);
  2180. }
  2181. #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
  2182. }
  2183. # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
  2184. if ($realfile =~ /Kconfig/ &&
  2185. $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
  2186. WARN("CONFIG_EXPERIMENTAL",
  2187. "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
  2188. }
  2189. # discourage the use of boolean for type definition attributes of Kconfig options
  2190. if ($realfile =~ /Kconfig/ &&
  2191. $line =~ /^\+\s*\bboolean\b/) {
  2192. WARN("CONFIG_TYPE_BOOLEAN",
  2193. "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
  2194. }
  2195. if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
  2196. ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
  2197. my $flag = $1;
  2198. my $replacement = {
  2199. 'EXTRA_AFLAGS' => 'asflags-y',
  2200. 'EXTRA_CFLAGS' => 'ccflags-y',
  2201. 'EXTRA_CPPFLAGS' => 'cppflags-y',
  2202. 'EXTRA_LDFLAGS' => 'ldflags-y',
  2203. };
  2204. WARN("DEPRECATED_VARIABLE",
  2205. "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
  2206. }
  2207. # check for DT compatible documentation
  2208. if (defined $root &&
  2209. (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
  2210. ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
  2211. my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
  2212. my $dt_path = $root . "/Documentation/devicetree/bindings/";
  2213. my $vp_file = $dt_path . "vendor-prefixes.txt";
  2214. foreach my $compat (@compats) {
  2215. my $compat2 = $compat;
  2216. $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
  2217. my $compat3 = $compat;
  2218. $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
  2219. `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
  2220. if ( $? >> 8 ) {
  2221. WARN("UNDOCUMENTED_DT_STRING",
  2222. "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
  2223. }
  2224. next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
  2225. my $vendor = $1;
  2226. `grep -Eq "^$vendor\\b" $vp_file`;
  2227. if ( $? >> 8 ) {
  2228. WARN("UNDOCUMENTED_DT_STRING",
  2229. "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
  2230. }
  2231. }
  2232. }
  2233. # check we are in a valid source file if not then ignore this hunk
  2234. next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
  2235. # line length limit (with some exclusions)
  2236. #
  2237. # There are a few types of lines that may extend beyond $max_line_length:
  2238. # logging functions like pr_info that end in a string
  2239. # lines with a single string
  2240. # #defines that are a single string
  2241. #
  2242. # There are 3 different line length message types:
  2243. # LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
  2244. # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
  2245. # LONG_LINE all other lines longer than $max_line_length
  2246. #
  2247. # if LONG_LINE is ignored, the other 2 types are also ignored
  2248. #
  2249. if ($line =~ /^\+/ && $length > $max_line_length) {
  2250. my $msg_type = "LONG_LINE";
  2251. # Check the allowed long line types first
  2252. # logging functions that end in a string that starts
  2253. # before $max_line_length
  2254. if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
  2255. length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
  2256. $msg_type = "";
  2257. # lines with only strings (w/ possible termination)
  2258. # #defines with only strings
  2259. } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
  2260. $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
  2261. $msg_type = "";
  2262. # Otherwise set the alternate message types
  2263. # a comment starts before $max_line_length
  2264. } elsif ($line =~ /($;[\s$;]*)$/ &&
  2265. length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
  2266. $msg_type = "LONG_LINE_COMMENT"
  2267. # a quoted string starts before $max_line_length
  2268. } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
  2269. length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
  2270. $msg_type = "LONG_LINE_STRING"
  2271. }
  2272. if ($msg_type ne "" &&
  2273. (show_type("LONG_LINE") || show_type($msg_type))) {
  2274. WARN($msg_type,
  2275. "line over $max_line_length characters\n" . $herecurr);
  2276. }
  2277. }
  2278. # check for adding lines without a newline.
  2279. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  2280. WARN("MISSING_EOF_NEWLINE",
  2281. "adding a line without newline at end of file\n" . $herecurr);
  2282. }
  2283. # Blackfin: use hi/lo macros
  2284. if ($realfile =~ m@arch/blackfin/.*\.S$@) {
  2285. if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
  2286. my $herevet = "$here\n" . cat_vet($line) . "\n";
  2287. ERROR("LO_MACRO",
  2288. "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
  2289. }
  2290. if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
  2291. my $herevet = "$here\n" . cat_vet($line) . "\n";
  2292. ERROR("HI_MACRO",
  2293. "use the HI() macro, not (... >> 16)\n" . $herevet);
  2294. }
  2295. }
  2296. # check we are in a valid source file C or perl if not then ignore this hunk
  2297. next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
  2298. # at the beginning of a line any tabs must come first and anything
  2299. # more than 8 must use tabs.
  2300. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  2301. $rawline =~ /^\+\s* \s*/) {
  2302. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  2303. $rpt_cleaners = 1;
  2304. if (ERROR("CODE_INDENT",
  2305. "code indent should use tabs where possible\n" . $herevet) &&
  2306. $fix) {
  2307. $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
  2308. }
  2309. }
  2310. # check for space before tabs.
  2311. if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
  2312. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  2313. if (WARN("SPACE_BEFORE_TAB",
  2314. "please, no space before tabs\n" . $herevet) &&
  2315. $fix) {
  2316. while ($fixed[$fixlinenr] =~
  2317. s/(^\+.*) {8,8}\t/$1\t\t/) {}
  2318. while ($fixed[$fixlinenr] =~
  2319. s/(^\+.*) +\t/$1\t/) {}
  2320. }
  2321. }
  2322. # check for && or || at the start of a line
  2323. if ($rawline =~ /^\+\s*(&&|\|\|)/) {
  2324. CHK("LOGICAL_CONTINUATIONS",
  2325. "Logical continuations should be on the previous line\n" . $hereprev);
  2326. }
  2327. # check multi-line statement indentation matches previous line
  2328. if ($^V && $^V ge 5.10.0 &&
  2329. $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
  2330. $prevline =~ /^\+(\t*)(.*)$/;
  2331. my $oldindent = $1;
  2332. my $rest = $2;
  2333. my $pos = pos_last_openparen($rest);
  2334. if ($pos >= 0) {
  2335. $line =~ /^(\+| )([ \t]*)/;
  2336. my $newindent = $2;
  2337. my $goodtabindent = $oldindent .
  2338. "\t" x ($pos / 8) .
  2339. " " x ($pos % 8);
  2340. my $goodspaceindent = $oldindent . " " x $pos;
  2341. if ($newindent ne $goodtabindent &&
  2342. $newindent ne $goodspaceindent) {
  2343. if (CHK("PARENTHESIS_ALIGNMENT",
  2344. "Alignment should match open parenthesis\n" . $hereprev) &&
  2345. $fix && $line =~ /^\+/) {
  2346. $fixed[$fixlinenr] =~
  2347. s/^\+[ \t]*/\+$goodtabindent/;
  2348. }
  2349. }
  2350. }
  2351. }
  2352. # check for space after cast like "(int) foo" or "(struct foo) bar"
  2353. # avoid checking a few false positives:
  2354. # "sizeof(<type>)" or "__alignof__(<type>)"
  2355. # function pointer declarations like "(*foo)(int) = bar;"
  2356. # structure definitions like "(struct foo) { 0 };"
  2357. # multiline macros that define functions
  2358. # known attributes or the __attribute__ keyword
  2359. if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
  2360. (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
  2361. if (CHK("SPACING",
  2362. "No space is necessary after a cast\n" . $herecurr) &&
  2363. $fix) {
  2364. $fixed[$fixlinenr] =~
  2365. s/(\(\s*$Type\s*\))[ \t]+/$1/;
  2366. }
  2367. }
  2368. # Block comment styles
  2369. # Networking with an initial /*
  2370. if ($realfile =~ m@^(drivers/net/|net/)@ &&
  2371. $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
  2372. $rawline =~ /^\+[ \t]*\*/ &&
  2373. $realline > 2) {
  2374. WARN("NETWORKING_BLOCK_COMMENT_STYLE",
  2375. "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
  2376. }
  2377. # Block comments use * on subsequent lines
  2378. if ($prevline =~ /$;[ \t]*$/ && #ends in comment
  2379. $prevrawline =~ /^\+.*?\/\*/ && #starting /*
  2380. $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
  2381. $rawline =~ /^\+/ && #line is new
  2382. $rawline !~ /^\+[ \t]*\*/) { #no leading *
  2383. WARN("BLOCK_COMMENT_STYLE",
  2384. "Block comments use * on subsequent lines\n" . $hereprev);
  2385. }
  2386. # Block comments use */ on trailing lines
  2387. if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
  2388. $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
  2389. $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
  2390. $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
  2391. WARN("BLOCK_COMMENT_STYLE",
  2392. "Block comments use a trailing */ on a separate line\n" . $herecurr);
  2393. }
  2394. # check for missing blank lines after struct/union declarations
  2395. # with exceptions for various attributes and macros
  2396. if ($prevline =~ /^[\+ ]};?\s*$/ &&
  2397. $line =~ /^\+/ &&
  2398. !($line =~ /^\+\s*$/ ||
  2399. $line =~ /^\+\s*EXPORT_SYMBOL/ ||
  2400. $line =~ /^\+\s*MODULE_/i ||
  2401. $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
  2402. $line =~ /^\+[a-z_]*init/ ||
  2403. $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
  2404. $line =~ /^\+\s*DECLARE/ ||
  2405. $line =~ /^\+\s*__setup/)) {
  2406. if (CHK("LINE_SPACING",
  2407. "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
  2408. $fix) {
  2409. fix_insert_line($fixlinenr, "\+");
  2410. }
  2411. }
  2412. # check for multiple consecutive blank lines
  2413. if ($prevline =~ /^[\+ ]\s*$/ &&
  2414. $line =~ /^\+\s*$/ &&
  2415. $last_blank_line != ($linenr - 1)) {
  2416. if (CHK("LINE_SPACING",
  2417. "Please don't use multiple blank lines\n" . $hereprev) &&
  2418. $fix) {
  2419. fix_delete_line($fixlinenr, $rawline);
  2420. }
  2421. $last_blank_line = $linenr;
  2422. }
  2423. # check for missing blank lines after declarations
  2424. if ($sline =~ /^\+\s+\S/ && #Not at char 1
  2425. # actual declarations
  2426. ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
  2427. # function pointer declarations
  2428. $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
  2429. # foo bar; where foo is some local typedef or #define
  2430. $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
  2431. # known declaration macros
  2432. $prevline =~ /^\+\s+$declaration_macros/) &&
  2433. # for "else if" which can look like "$Ident $Ident"
  2434. !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
  2435. # other possible extensions of declaration lines
  2436. $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
  2437. # not starting a section or a macro "\" extended line
  2438. $prevline =~ /(?:\{\s*|\\)$/) &&
  2439. # looks like a declaration
  2440. !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
  2441. # function pointer declarations
  2442. $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
  2443. # foo bar; where foo is some local typedef or #define
  2444. $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
  2445. # known declaration macros
  2446. $sline =~ /^\+\s+$declaration_macros/ ||
  2447. # start of struct or union or enum
  2448. $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
  2449. # start or end of block or continuation of declaration
  2450. $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
  2451. # bitfield continuation
  2452. $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
  2453. # other possible extensions of declaration lines
  2454. $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
  2455. # indentation of previous and current line are the same
  2456. (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
  2457. if (WARN("LINE_SPACING",
  2458. "Missing a blank line after declarations\n" . $hereprev) &&
  2459. $fix) {
  2460. fix_insert_line($fixlinenr, "\+");
  2461. }
  2462. }
  2463. # check for spaces at the beginning of a line.
  2464. # Exceptions:
  2465. # 1) within comments
  2466. # 2) indented preprocessor commands
  2467. # 3) hanging labels
  2468. if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
  2469. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  2470. if (WARN("LEADING_SPACE",
  2471. "please, no spaces at the start of a line\n" . $herevet) &&
  2472. $fix) {
  2473. $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
  2474. }
  2475. }
  2476. # check we are in a valid C source file if not then ignore this hunk
  2477. next if ($realfile !~ /\.(h|c)$/);
  2478. # check indentation of any line with a bare else
  2479. # (but not if it is a multiple line "if (foo) return bar; else return baz;")
  2480. # if the previous line is a break or return and is indented 1 tab more...
  2481. if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
  2482. my $tabs = length($1) + 1;
  2483. if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
  2484. ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
  2485. defined $lines[$linenr] &&
  2486. $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
  2487. WARN("UNNECESSARY_ELSE",
  2488. "else is not generally useful after a break or return\n" . $hereprev);
  2489. }
  2490. }
  2491. # check indentation of a line with a break;
  2492. # if the previous line is a goto or return and is indented the same # of tabs
  2493. if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
  2494. my $tabs = $1;
  2495. if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
  2496. WARN("UNNECESSARY_BREAK",
  2497. "break is not useful after a goto or return\n" . $hereprev);
  2498. }
  2499. }
  2500. # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
  2501. if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
  2502. WARN("CONFIG_EXPERIMENTAL",
  2503. "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
  2504. }
  2505. # check for RCS/CVS revision markers
  2506. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  2507. WARN("CVS_KEYWORD",
  2508. "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  2509. }
  2510. # Blackfin: don't use __builtin_bfin_[cs]sync
  2511. if ($line =~ /__builtin_bfin_csync/) {
  2512. my $herevet = "$here\n" . cat_vet($line) . "\n";
  2513. ERROR("CSYNC",
  2514. "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
  2515. }
  2516. if ($line =~ /__builtin_bfin_ssync/) {
  2517. my $herevet = "$here\n" . cat_vet($line) . "\n";
  2518. ERROR("SSYNC",
  2519. "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
  2520. }
  2521. # check for old HOTPLUG __dev<foo> section markings
  2522. if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
  2523. WARN("HOTPLUG_SECTION",
  2524. "Using $1 is unnecessary\n" . $herecurr);
  2525. }
  2526. # Check for potential 'bare' types
  2527. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  2528. $realline_next);
  2529. #print "LINE<$line>\n";
  2530. if ($linenr >= $suppress_statement &&
  2531. $realcnt && $sline =~ /.\s*\S/) {
  2532. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  2533. ctx_statement_block($linenr, $realcnt, 0);
  2534. $stat =~ s/\n./\n /g;
  2535. $cond =~ s/\n./\n /g;
  2536. #print "linenr<$linenr> <$stat>\n";
  2537. # If this statement has no statement boundaries within
  2538. # it there is no point in retrying a statement scan
  2539. # until we hit end of it.
  2540. my $frag = $stat; $frag =~ s/;+\s*$//;
  2541. if ($frag !~ /(?:{|;)/) {
  2542. #print "skip<$line_nr_next>\n";
  2543. $suppress_statement = $line_nr_next;
  2544. }
  2545. # Find the real next line.
  2546. $realline_next = $line_nr_next;
  2547. if (defined $realline_next &&
  2548. (!defined $lines[$realline_next - 1] ||
  2549. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  2550. $realline_next++;
  2551. }
  2552. my $s = $stat;
  2553. $s =~ s/{.*$//s;
  2554. # Ignore goto labels.
  2555. if ($s =~ /$Ident:\*$/s) {
  2556. # Ignore functions being called
  2557. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  2558. } elsif ($s =~ /^.\s*else\b/s) {
  2559. # declarations always start with types
  2560. } 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) {
  2561. my $type = $1;
  2562. $type =~ s/\s+/ /g;
  2563. possible($type, "A:" . $s);
  2564. # definitions in global scope can only start with types
  2565. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  2566. possible($1, "B:" . $s);
  2567. }
  2568. # any (foo ... *) is a pointer cast, and foo is a type
  2569. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  2570. possible($1, "C:" . $s);
  2571. }
  2572. # Check for any sort of function declaration.
  2573. # int foo(something bar, other baz);
  2574. # void (*store_gdt)(x86_descr_ptr *);
  2575. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  2576. my ($name_len) = length($1);
  2577. my $ctx = $s;
  2578. substr($ctx, 0, $name_len + 1, '');
  2579. $ctx =~ s/\)[^\)]*$//;
  2580. for my $arg (split(/\s*,\s*/, $ctx)) {
  2581. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  2582. possible($1, "D:" . $s);
  2583. }
  2584. }
  2585. }
  2586. }
  2587. #
  2588. # Checks which may be anchored in the context.
  2589. #
  2590. # Check for switch () and associated case and default
  2591. # statements should be at the same indent.
  2592. if ($line=~/\bswitch\s*\(.*\)/) {
  2593. my $err = '';
  2594. my $sep = '';
  2595. my @ctx = ctx_block_outer($linenr, $realcnt);
  2596. shift(@ctx);
  2597. for my $ctx (@ctx) {
  2598. my ($clen, $cindent) = line_stats($ctx);
  2599. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  2600. $indent != $cindent) {
  2601. $err .= "$sep$ctx\n";
  2602. $sep = '';
  2603. } else {
  2604. $sep = "[...]\n";
  2605. }
  2606. }
  2607. if ($err ne '') {
  2608. ERROR("SWITCH_CASE_INDENT_LEVEL",
  2609. "switch and case should be at the same indent\n$hereline$err");
  2610. }
  2611. }
  2612. # if/while/etc brace do not go on next line, unless defining a do while loop,
  2613. # or if that brace on the next line is for something else
  2614. if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  2615. my $pre_ctx = "$1$2";
  2616. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  2617. if ($line =~ /^\+\t{6,}/) {
  2618. WARN("DEEP_INDENTATION",
  2619. "Too many leading tabs - consider code refactoring\n" . $herecurr);
  2620. }
  2621. my $ctx_cnt = $realcnt - $#ctx - 1;
  2622. my $ctx = join("\n", @ctx);
  2623. my $ctx_ln = $linenr;
  2624. my $ctx_skip = $realcnt;
  2625. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  2626. defined $lines[$ctx_ln - 1] &&
  2627. $lines[$ctx_ln - 1] =~ /^-/)) {
  2628. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  2629. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  2630. $ctx_ln++;
  2631. }
  2632. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  2633. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  2634. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  2635. ERROR("OPEN_BRACE",
  2636. "that open brace { should be on the previous line\n" .
  2637. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  2638. }
  2639. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  2640. $ctx =~ /\)\s*\;\s*$/ &&
  2641. defined $lines[$ctx_ln - 1])
  2642. {
  2643. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  2644. if ($nindent > $indent) {
  2645. WARN("TRAILING_SEMICOLON",
  2646. "trailing semicolon indicates no statements, indent implies otherwise\n" .
  2647. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  2648. }
  2649. }
  2650. }
  2651. # Check relative indent for conditionals and blocks.
  2652. if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  2653. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  2654. ctx_statement_block($linenr, $realcnt, 0)
  2655. if (!defined $stat);
  2656. my ($s, $c) = ($stat, $cond);
  2657. substr($s, 0, length($c), '');
  2658. # remove inline comments
  2659. $s =~ s/$;/ /g;
  2660. $c =~ s/$;/ /g;
  2661. # Find out how long the conditional actually is.
  2662. my @newlines = ($c =~ /\n/gs);
  2663. my $cond_lines = 1 + $#newlines;
  2664. # Make sure we remove the line prefixes as we have
  2665. # none on the first line, and are going to readd them
  2666. # where necessary.
  2667. $s =~ s/\n./\n/gs;
  2668. while ($s =~ /\n\s+\\\n/) {
  2669. $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
  2670. }
  2671. # We want to check the first line inside the block
  2672. # starting at the end of the conditional, so remove:
  2673. # 1) any blank line termination
  2674. # 2) any opening brace { on end of the line
  2675. # 3) any do (...) {
  2676. my $continuation = 0;
  2677. my $check = 0;
  2678. $s =~ s/^.*\bdo\b//;
  2679. $s =~ s/^\s*{//;
  2680. if ($s =~ s/^\s*\\//) {
  2681. $continuation = 1;
  2682. }
  2683. if ($s =~ s/^\s*?\n//) {
  2684. $check = 1;
  2685. $cond_lines++;
  2686. }
  2687. # Also ignore a loop construct at the end of a
  2688. # preprocessor statement.
  2689. if (($prevline =~ /^.\s*#\s*define\s/ ||
  2690. $prevline =~ /\\\s*$/) && $continuation == 0) {
  2691. $check = 0;
  2692. }
  2693. my $cond_ptr = -1;
  2694. $continuation = 0;
  2695. while ($cond_ptr != $cond_lines) {
  2696. $cond_ptr = $cond_lines;
  2697. # If we see an #else/#elif then the code
  2698. # is not linear.
  2699. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  2700. $check = 0;
  2701. }
  2702. # Ignore:
  2703. # 1) blank lines, they should be at 0,
  2704. # 2) preprocessor lines, and
  2705. # 3) labels.
  2706. if ($continuation ||
  2707. $s =~ /^\s*?\n/ ||
  2708. $s =~ /^\s*#\s*?/ ||
  2709. $s =~ /^\s*$Ident\s*:/) {
  2710. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  2711. if ($s =~ s/^.*?\n//) {
  2712. $cond_lines++;
  2713. }
  2714. }
  2715. }
  2716. my (undef, $sindent) = line_stats("+" . $s);
  2717. my $stat_real = raw_line($linenr, $cond_lines);
  2718. # Check if either of these lines are modified, else
  2719. # this is not this patch's fault.
  2720. if (!defined($stat_real) ||
  2721. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  2722. $check = 0;
  2723. }
  2724. if (defined($stat_real) && $cond_lines > 1) {
  2725. $stat_real = "[...]\n$stat_real";
  2726. }
  2727. #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";
  2728. if ($check && $s ne '' &&
  2729. (($sindent % 8) != 0 ||
  2730. ($sindent < $indent) ||
  2731. ($sindent > $indent + 8))) {
  2732. WARN("SUSPECT_CODE_INDENT",
  2733. "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  2734. }
  2735. }
  2736. # Track the 'values' across context and added lines.
  2737. my $opline = $line; $opline =~ s/^./ /;
  2738. my ($curr_values, $curr_vars) =
  2739. annotate_values($opline . "\n", $prev_values);
  2740. $curr_values = $prev_values . $curr_values;
  2741. if ($dbg_values) {
  2742. my $outline = $opline; $outline =~ s/\t/ /g;
  2743. print "$linenr > .$outline\n";
  2744. print "$linenr > $curr_values\n";
  2745. print "$linenr > $curr_vars\n";
  2746. }
  2747. $prev_values = substr($curr_values, -1);
  2748. #ignore lines not being added
  2749. next if ($line =~ /^[^\+]/);
  2750. # check for declarations of signed or unsigned without int
  2751. while ($line =~ m{($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
  2752. my $type = $1;
  2753. my $var = $2;
  2754. $var = "" if (!defined $var);
  2755. if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
  2756. my $sign = $1;
  2757. my $pointer = $2;
  2758. $pointer = "" if (!defined $pointer);
  2759. if (WARN("UNSPECIFIED_INT",
  2760. "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
  2761. $fix) {
  2762. my $decl = trim($sign) . " int ";
  2763. my $comp_pointer = $pointer;
  2764. $comp_pointer =~ s/\s//g;
  2765. $decl .= $comp_pointer;
  2766. $decl = rtrim($decl) if ($var eq "");
  2767. $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
  2768. }
  2769. }
  2770. }
  2771. # TEST: allow direct testing of the type matcher.
  2772. if ($dbg_type) {
  2773. if ($line =~ /^.\s*$Declare\s*$/) {
  2774. ERROR("TEST_TYPE",
  2775. "TEST: is type\n" . $herecurr);
  2776. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  2777. ERROR("TEST_NOT_TYPE",
  2778. "TEST: is not type ($1 is)\n". $herecurr);
  2779. }
  2780. next;
  2781. }
  2782. # TEST: allow direct testing of the attribute matcher.
  2783. if ($dbg_attr) {
  2784. if ($line =~ /^.\s*$Modifier\s*$/) {
  2785. ERROR("TEST_ATTR",
  2786. "TEST: is attr\n" . $herecurr);
  2787. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  2788. ERROR("TEST_NOT_ATTR",
  2789. "TEST: is not attr ($1 is)\n". $herecurr);
  2790. }
  2791. next;
  2792. }
  2793. # check for initialisation to aggregates open brace on the next line
  2794. if ($line =~ /^.\s*{/ &&
  2795. $prevline =~ /(?:^|[^=])=\s*$/) {
  2796. if (ERROR("OPEN_BRACE",
  2797. "that open brace { should be on the previous line\n" . $hereprev) &&
  2798. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  2799. fix_delete_line($fixlinenr - 1, $prevrawline);
  2800. fix_delete_line($fixlinenr, $rawline);
  2801. my $fixedline = $prevrawline;
  2802. $fixedline =~ s/\s*=\s*$/ = {/;
  2803. fix_insert_line($fixlinenr, $fixedline);
  2804. $fixedline = $line;
  2805. $fixedline =~ s/^(.\s*){\s*/$1/;
  2806. fix_insert_line($fixlinenr, $fixedline);
  2807. }
  2808. }
  2809. #
  2810. # Checks which are anchored on the added line.
  2811. #
  2812. # check for malformed paths in #include statements (uses RAW line)
  2813. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  2814. my $path = $1;
  2815. if ($path =~ m{//}) {
  2816. ERROR("MALFORMED_INCLUDE",
  2817. "malformed #include filename\n" . $herecurr);
  2818. }
  2819. if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
  2820. ERROR("UAPI_INCLUDE",
  2821. "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
  2822. }
  2823. }
  2824. # no C99 // comments
  2825. if ($line =~ m{//}) {
  2826. if (ERROR("C99_COMMENTS",
  2827. "do not use C99 // comments\n" . $herecurr) &&
  2828. $fix) {
  2829. my $line = $fixed[$fixlinenr];
  2830. if ($line =~ /\/\/(.*)$/) {
  2831. my $comment = trim($1);
  2832. $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
  2833. }
  2834. }
  2835. }
  2836. # Remove C99 comments.
  2837. $line =~ s@//.*@@;
  2838. $opline =~ s@//.*@@;
  2839. # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
  2840. # the whole statement.
  2841. #print "APW <$lines[$realline_next - 1]>\n";
  2842. if (defined $realline_next &&
  2843. exists $lines[$realline_next - 1] &&
  2844. !defined $suppress_export{$realline_next} &&
  2845. ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  2846. $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  2847. # Handle definitions which produce identifiers with
  2848. # a prefix:
  2849. # XXX(foo);
  2850. # EXPORT_SYMBOL(something_foo);
  2851. my $name = $1;
  2852. if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
  2853. $name =~ /^${Ident}_$2/) {
  2854. #print "FOO C name<$name>\n";
  2855. $suppress_export{$realline_next} = 1;
  2856. } elsif ($stat !~ /(?:
  2857. \n.}\s*$|
  2858. ^.DEFINE_$Ident\(\Q$name\E\)|
  2859. ^.DECLARE_$Ident\(\Q$name\E\)|
  2860. ^.LIST_HEAD\(\Q$name\E\)|
  2861. ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
  2862. \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
  2863. )/x) {
  2864. #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
  2865. $suppress_export{$realline_next} = 2;
  2866. } else {
  2867. $suppress_export{$realline_next} = 1;
  2868. }
  2869. }
  2870. if (!defined $suppress_export{$linenr} &&
  2871. $prevline =~ /^.\s*$/ &&
  2872. ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  2873. $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  2874. #print "FOO B <$lines[$linenr - 1]>\n";
  2875. $suppress_export{$linenr} = 2;
  2876. }
  2877. if (defined $suppress_export{$linenr} &&
  2878. $suppress_export{$linenr} == 2) {
  2879. WARN("EXPORT_SYMBOL",
  2880. "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  2881. }
  2882. # check for global initialisers.
  2883. if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
  2884. if (ERROR("GLOBAL_INITIALISERS",
  2885. "do not initialise globals to $1\n" . $herecurr) &&
  2886. $fix) {
  2887. $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
  2888. }
  2889. }
  2890. # check for static initialisers.
  2891. if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
  2892. if (ERROR("INITIALISED_STATIC",
  2893. "do not initialise statics to $1\n" .
  2894. $herecurr) &&
  2895. $fix) {
  2896. $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
  2897. }
  2898. }
  2899. # check for misordered declarations of char/short/int/long with signed/unsigned
  2900. while ($sline =~ m{(\b$TypeMisordered\b)}g) {
  2901. my $tmp = trim($1);
  2902. WARN("MISORDERED_TYPE",
  2903. "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
  2904. }
  2905. # check for static const char * arrays.
  2906. if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
  2907. WARN("STATIC_CONST_CHAR_ARRAY",
  2908. "static const char * array should probably be static const char * const\n" .
  2909. $herecurr);
  2910. }
  2911. # check for static char foo[] = "bar" declarations.
  2912. if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
  2913. WARN("STATIC_CONST_CHAR_ARRAY",
  2914. "static char array declaration should probably be static const char\n" .
  2915. $herecurr);
  2916. }
  2917. # check for const <foo> const where <foo> is not a pointer or array type
  2918. if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
  2919. my $found = $1;
  2920. if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
  2921. WARN("CONST_CONST",
  2922. "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
  2923. } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
  2924. WARN("CONST_CONST",
  2925. "'const $found const' should probably be 'const $found'\n" . $herecurr);
  2926. }
  2927. }
  2928. # check for non-global char *foo[] = {"bar", ...} declarations.
  2929. if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
  2930. WARN("STATIC_CONST_CHAR_ARRAY",
  2931. "char * array declaration might be better as static const\n" .
  2932. $herecurr);
  2933. }
  2934. # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
  2935. if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
  2936. my $array = $1;
  2937. if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
  2938. my $array_div = $1;
  2939. if (WARN("ARRAY_SIZE",
  2940. "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
  2941. $fix) {
  2942. $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
  2943. }
  2944. }
  2945. }
  2946. # check for function declarations without arguments like "int foo()"
  2947. if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
  2948. if (ERROR("FUNCTION_WITHOUT_ARGS",
  2949. "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
  2950. $fix) {
  2951. $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
  2952. }
  2953. }
  2954. # check for uses of DEFINE_PCI_DEVICE_TABLE
  2955. if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
  2956. if (WARN("DEFINE_PCI_DEVICE_TABLE",
  2957. "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
  2958. $fix) {
  2959. $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
  2960. }
  2961. }
  2962. # check for new typedefs, only function parameters and sparse annotations
  2963. # make sense.
  2964. if ($line =~ /\btypedef\s/ &&
  2965. $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
  2966. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  2967. $line !~ /\b$typeTypedefs\b/ &&
  2968. $line !~ /\b__bitwise(?:__|)\b/) {
  2969. WARN("NEW_TYPEDEFS",
  2970. "do not add new typedefs\n" . $herecurr);
  2971. }
  2972. # * goes on variable not on type
  2973. # (char*[ const])
  2974. while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
  2975. #print "AA<$1>\n";
  2976. my ($ident, $from, $to) = ($1, $2, $2);
  2977. # Should start with a space.
  2978. $to =~ s/^(\S)/ $1/;
  2979. # Should not end with a space.
  2980. $to =~ s/\s+$//;
  2981. # '*'s should not have spaces between.
  2982. while ($to =~ s/\*\s+\*/\*\*/) {
  2983. }
  2984. ## print "1: from<$from> to<$to> ident<$ident>\n";
  2985. if ($from ne $to) {
  2986. if (ERROR("POINTER_LOCATION",
  2987. "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
  2988. $fix) {
  2989. my $sub_from = $ident;
  2990. my $sub_to = $ident;
  2991. $sub_to =~ s/\Q$from\E/$to/;
  2992. $fixed[$fixlinenr] =~
  2993. s@\Q$sub_from\E@$sub_to@;
  2994. }
  2995. }
  2996. }
  2997. while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
  2998. #print "BB<$1>\n";
  2999. my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
  3000. # Should start with a space.
  3001. $to =~ s/^(\S)/ $1/;
  3002. # Should not end with a space.
  3003. $to =~ s/\s+$//;
  3004. # '*'s should not have spaces between.
  3005. while ($to =~ s/\*\s+\*/\*\*/) {
  3006. }
  3007. # Modifiers should have spaces.
  3008. $to =~ s/(\b$Modifier$)/$1 /;
  3009. ## print "2: from<$from> to<$to> ident<$ident>\n";
  3010. if ($from ne $to && $ident !~ /^$Modifier$/) {
  3011. if (ERROR("POINTER_LOCATION",
  3012. "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
  3013. $fix) {
  3014. my $sub_from = $match;
  3015. my $sub_to = $match;
  3016. $sub_to =~ s/\Q$from\E/$to/;
  3017. $fixed[$fixlinenr] =~
  3018. s@\Q$sub_from\E@$sub_to@;
  3019. }
  3020. }
  3021. }
  3022. # avoid BUG() or BUG_ON()
  3023. if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
  3024. my $msg_type = \&WARN;
  3025. $msg_type = \&CHK if ($file);
  3026. &{$msg_type}("AVOID_BUG",
  3027. "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
  3028. }
  3029. # avoid LINUX_VERSION_CODE
  3030. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  3031. WARN("LINUX_VERSION_CODE",
  3032. "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  3033. }
  3034. # check for uses of printk_ratelimit
  3035. if ($line =~ /\bprintk_ratelimit\s*\(/) {
  3036. WARN("PRINTK_RATELIMITED",
  3037. "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
  3038. }
  3039. # printk should use KERN_* levels. Note that follow on printk's on the
  3040. # same line do not need a level, so we use the current block context
  3041. # to try and find and validate the current printk. In summary the current
  3042. # printk includes all preceding printk's which have no newline on the end.
  3043. # we assume the first bad printk is the one to report.
  3044. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  3045. my $ok = 0;
  3046. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  3047. #print "CHECK<$lines[$ln - 1]\n";
  3048. # we have a preceding printk if it ends
  3049. # with "\n" ignore it, else it is to blame
  3050. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  3051. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  3052. $ok = 1;
  3053. }
  3054. last;
  3055. }
  3056. }
  3057. if ($ok == 0) {
  3058. WARN("PRINTK_WITHOUT_KERN_LEVEL",
  3059. "printk() should include KERN_ facility level\n" . $herecurr);
  3060. }
  3061. }
  3062. if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
  3063. my $orig = $1;
  3064. my $level = lc($orig);
  3065. $level = "warn" if ($level eq "warning");
  3066. my $level2 = $level;
  3067. $level2 = "dbg" if ($level eq "debug");
  3068. WARN("PREFER_PR_LEVEL",
  3069. "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
  3070. }
  3071. if ($line =~ /\bpr_warning\s*\(/) {
  3072. if (WARN("PREFER_PR_LEVEL",
  3073. "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
  3074. $fix) {
  3075. $fixed[$fixlinenr] =~
  3076. s/\bpr_warning\b/pr_warn/;
  3077. }
  3078. }
  3079. if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
  3080. my $orig = $1;
  3081. my $level = lc($orig);
  3082. $level = "warn" if ($level eq "warning");
  3083. $level = "dbg" if ($level eq "debug");
  3084. WARN("PREFER_DEV_LEVEL",
  3085. "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
  3086. }
  3087. # ENOSYS means "bad syscall nr" and nothing else. This will have a small
  3088. # number of false positives, but assembly files are not checked, so at
  3089. # least the arch entry code will not trigger this warning.
  3090. if ($line =~ /\bENOSYS\b/) {
  3091. WARN("ENOSYS",
  3092. "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
  3093. }
  3094. # function brace can't be on same line, except for #defines of do while,
  3095. # or if closed on same line
  3096. if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
  3097. !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
  3098. if (ERROR("OPEN_BRACE",
  3099. "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
  3100. $fix) {
  3101. fix_delete_line($fixlinenr, $rawline);
  3102. my $fixed_line = $rawline;
  3103. $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
  3104. my $line1 = $1;
  3105. my $line2 = $2;
  3106. fix_insert_line($fixlinenr, ltrim($line1));
  3107. fix_insert_line($fixlinenr, "\+{");
  3108. if ($line2 !~ /^\s*$/) {
  3109. fix_insert_line($fixlinenr, "\+\t" . trim($line2));
  3110. }
  3111. }
  3112. }
  3113. # open braces for enum, union and struct go on the same line.
  3114. if ($line =~ /^.\s*{/ &&
  3115. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  3116. if (ERROR("OPEN_BRACE",
  3117. "open brace '{' following $1 go on the same line\n" . $hereprev) &&
  3118. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  3119. fix_delete_line($fixlinenr - 1, $prevrawline);
  3120. fix_delete_line($fixlinenr, $rawline);
  3121. my $fixedline = rtrim($prevrawline) . " {";
  3122. fix_insert_line($fixlinenr, $fixedline);
  3123. $fixedline = $rawline;
  3124. $fixedline =~ s/^(.\s*){\s*/$1\t/;
  3125. if ($fixedline !~ /^\+\s*$/) {
  3126. fix_insert_line($fixlinenr, $fixedline);
  3127. }
  3128. }
  3129. }
  3130. # missing space after union, struct or enum definition
  3131. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
  3132. if (WARN("SPACING",
  3133. "missing space after $1 definition\n" . $herecurr) &&
  3134. $fix) {
  3135. $fixed[$fixlinenr] =~
  3136. s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
  3137. }
  3138. }
  3139. # Function pointer declarations
  3140. # check spacing between type, funcptr, and args
  3141. # canonical declaration is "type (*funcptr)(args...)"
  3142. if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
  3143. my $declare = $1;
  3144. my $pre_pointer_space = $2;
  3145. my $post_pointer_space = $3;
  3146. my $funcname = $4;
  3147. my $post_funcname_space = $5;
  3148. my $pre_args_space = $6;
  3149. # the $Declare variable will capture all spaces after the type
  3150. # so check it for a missing trailing missing space but pointer return types
  3151. # don't need a space so don't warn for those.
  3152. my $post_declare_space = "";
  3153. if ($declare =~ /(\s+)$/) {
  3154. $post_declare_space = $1;
  3155. $declare = rtrim($declare);
  3156. }
  3157. if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
  3158. WARN("SPACING",
  3159. "missing space after return type\n" . $herecurr);
  3160. $post_declare_space = " ";
  3161. }
  3162. # unnecessary space "type (*funcptr)(args...)"
  3163. # This test is not currently implemented because these declarations are
  3164. # equivalent to
  3165. # int foo(int bar, ...)
  3166. # and this is form shouldn't/doesn't generate a checkpatch warning.
  3167. #
  3168. # elsif ($declare =~ /\s{2,}$/) {
  3169. # WARN("SPACING",
  3170. # "Multiple spaces after return type\n" . $herecurr);
  3171. # }
  3172. # unnecessary space "type ( *funcptr)(args...)"
  3173. if (defined $pre_pointer_space &&
  3174. $pre_pointer_space =~ /^\s/) {
  3175. WARN("SPACING",
  3176. "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
  3177. }
  3178. # unnecessary space "type (* funcptr)(args...)"
  3179. if (defined $post_pointer_space &&
  3180. $post_pointer_space =~ /^\s/) {
  3181. WARN("SPACING",
  3182. "Unnecessary space before function pointer name\n" . $herecurr);
  3183. }
  3184. # unnecessary space "type (*funcptr )(args...)"
  3185. if (defined $post_funcname_space &&
  3186. $post_funcname_space =~ /^\s/) {
  3187. WARN("SPACING",
  3188. "Unnecessary space after function pointer name\n" . $herecurr);
  3189. }
  3190. # unnecessary space "type (*funcptr) (args...)"
  3191. if (defined $pre_args_space &&
  3192. $pre_args_space =~ /^\s/) {
  3193. WARN("SPACING",
  3194. "Unnecessary space before function pointer arguments\n" . $herecurr);
  3195. }
  3196. if (show_type("SPACING") && $fix) {
  3197. $fixed[$fixlinenr] =~
  3198. s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
  3199. }
  3200. }
  3201. # check for spacing round square brackets; allowed:
  3202. # 1. with a type on the left -- int [] a;
  3203. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  3204. # 3. inside a curly brace -- = { [0...10] = 5 }
  3205. while ($line =~ /(.*?\s)\[/g) {
  3206. my ($where, $prefix) = ($-[1], $1);
  3207. if ($prefix !~ /$Type\s+$/ &&
  3208. ($where != 0 || $prefix !~ /^.\s+$/) &&
  3209. $prefix !~ /[{,]\s+$/) {
  3210. if (ERROR("BRACKET_SPACE",
  3211. "space prohibited before open square bracket '['\n" . $herecurr) &&
  3212. $fix) {
  3213. $fixed[$fixlinenr] =~
  3214. s/^(\+.*?)\s+\[/$1\[/;
  3215. }
  3216. }
  3217. }
  3218. # check for spaces between functions and their parentheses.
  3219. while ($line =~ /($Ident)\s+\(/g) {
  3220. my $name = $1;
  3221. my $ctx_before = substr($line, 0, $-[1]);
  3222. my $ctx = "$ctx_before$name";
  3223. # Ignore those directives where spaces _are_ permitted.
  3224. if ($name =~ /^(?:
  3225. if|for|while|switch|return|case|
  3226. volatile|__volatile__|
  3227. __attribute__|format|__extension__|
  3228. asm|__asm__)$/x)
  3229. {
  3230. # cpp #define statements have non-optional spaces, ie
  3231. # if there is a space between the name and the open
  3232. # parenthesis it is simply not a parameter group.
  3233. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  3234. # cpp #elif statement condition may start with a (
  3235. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  3236. # If this whole things ends with a type its most
  3237. # likely a typedef for a function.
  3238. } elsif ($ctx =~ /$Type$/) {
  3239. } else {
  3240. if (WARN("SPACING",
  3241. "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
  3242. $fix) {
  3243. $fixed[$fixlinenr] =~
  3244. s/\b$name\s+\(/$name\(/;
  3245. }
  3246. }
  3247. }
  3248. # Check operator spacing.
  3249. if (!($line=~/\#\s*include/)) {
  3250. my $fixed_line = "";
  3251. my $line_fixed = 0;
  3252. my $ops = qr{
  3253. <<=|>>=|<=|>=|==|!=|
  3254. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  3255. =>|->|<<|>>|<|>|=|!|~|
  3256. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  3257. \?:|\?|:
  3258. }x;
  3259. my @elements = split(/($ops|;)/, $opline);
  3260. ## print("element count: <" . $#elements . ">\n");
  3261. ## foreach my $el (@elements) {
  3262. ## print("el: <$el>\n");
  3263. ## }
  3264. my @fix_elements = ();
  3265. my $off = 0;
  3266. foreach my $el (@elements) {
  3267. push(@fix_elements, substr($rawline, $off, length($el)));
  3268. $off += length($el);
  3269. }
  3270. $off = 0;
  3271. my $blank = copy_spacing($opline);
  3272. my $last_after = -1;
  3273. for (my $n = 0; $n < $#elements; $n += 2) {
  3274. my $good = $fix_elements[$n] . $fix_elements[$n + 1];
  3275. ## print("n: <$n> good: <$good>\n");
  3276. $off += length($elements[$n]);
  3277. # Pick up the preceding and succeeding characters.
  3278. my $ca = substr($opline, 0, $off);
  3279. my $cc = '';
  3280. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  3281. $cc = substr($opline, $off + length($elements[$n + 1]));
  3282. }
  3283. my $cb = "$ca$;$cc";
  3284. my $a = '';
  3285. $a = 'V' if ($elements[$n] ne '');
  3286. $a = 'W' if ($elements[$n] =~ /\s$/);
  3287. $a = 'C' if ($elements[$n] =~ /$;$/);
  3288. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  3289. $a = 'O' if ($elements[$n] eq '');
  3290. $a = 'E' if ($ca =~ /^\s*$/);
  3291. my $op = $elements[$n + 1];
  3292. my $c = '';
  3293. if (defined $elements[$n + 2]) {
  3294. $c = 'V' if ($elements[$n + 2] ne '');
  3295. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  3296. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  3297. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  3298. $c = 'O' if ($elements[$n + 2] eq '');
  3299. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  3300. } else {
  3301. $c = 'E';
  3302. }
  3303. my $ctx = "${a}x${c}";
  3304. my $at = "(ctx:$ctx)";
  3305. my $ptr = substr($blank, 0, $off) . "^";
  3306. my $hereptr = "$hereline$ptr\n";
  3307. # Pull out the value of this operator.
  3308. my $op_type = substr($curr_values, $off + 1, 1);
  3309. # Get the full operator variant.
  3310. my $opv = $op . substr($curr_vars, $off, 1);
  3311. # Ignore operators passed as parameters.
  3312. if ($op_type ne 'V' &&
  3313. $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
  3314. # # Ignore comments
  3315. # } elsif ($op =~ /^$;+$/) {
  3316. # ; should have either the end of line or a space or \ after it
  3317. } elsif ($op eq ';') {
  3318. if ($ctx !~ /.x[WEBC]/ &&
  3319. $cc !~ /^\\/ && $cc !~ /^;/) {
  3320. if (ERROR("SPACING",
  3321. "space required after that '$op' $at\n" . $hereptr)) {
  3322. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  3323. $line_fixed = 1;
  3324. }
  3325. }
  3326. # // is a comment
  3327. } elsif ($op eq '//') {
  3328. # : when part of a bitfield
  3329. } elsif ($opv eq ':B') {
  3330. # skip the bitfield test for now
  3331. # No spaces for:
  3332. # ->
  3333. } elsif ($op eq '->') {
  3334. if ($ctx =~ /Wx.|.xW/) {
  3335. if (ERROR("SPACING",
  3336. "spaces prohibited around that '$op' $at\n" . $hereptr)) {
  3337. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  3338. if (defined $fix_elements[$n + 2]) {
  3339. $fix_elements[$n + 2] =~ s/^\s+//;
  3340. }
  3341. $line_fixed = 1;
  3342. }
  3343. }
  3344. # , must not have a space before and must have a space on the right.
  3345. } elsif ($op eq ',') {
  3346. my $rtrim_before = 0;
  3347. my $space_after = 0;
  3348. if ($ctx =~ /Wx./) {
  3349. if (ERROR("SPACING",
  3350. "space prohibited before that '$op' $at\n" . $hereptr)) {
  3351. $line_fixed = 1;
  3352. $rtrim_before = 1;
  3353. }
  3354. }
  3355. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  3356. if (ERROR("SPACING",
  3357. "space required after that '$op' $at\n" . $hereptr)) {
  3358. $line_fixed = 1;
  3359. $last_after = $n;
  3360. $space_after = 1;
  3361. }
  3362. }
  3363. if ($rtrim_before || $space_after) {
  3364. if ($rtrim_before) {
  3365. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  3366. } else {
  3367. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
  3368. }
  3369. if ($space_after) {
  3370. $good .= " ";
  3371. }
  3372. }
  3373. # '*' as part of a type definition -- reported already.
  3374. } elsif ($opv eq '*_') {
  3375. #warn "'*' is part of type\n";
  3376. # unary operators should have a space before and
  3377. # none after. May be left adjacent to another
  3378. # unary operator, or a cast
  3379. } elsif ($op eq '!' || $op eq '~' ||
  3380. $opv eq '*U' || $opv eq '-U' ||
  3381. $opv eq '&U' || $opv eq '&&U') {
  3382. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  3383. if (ERROR("SPACING",
  3384. "space required before that '$op' $at\n" . $hereptr)) {
  3385. if ($n != $last_after + 2) {
  3386. $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
  3387. $line_fixed = 1;
  3388. }
  3389. }
  3390. }
  3391. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  3392. # A unary '*' may be const
  3393. } elsif ($ctx =~ /.xW/) {
  3394. if (ERROR("SPACING",
  3395. "space prohibited after that '$op' $at\n" . $hereptr)) {
  3396. $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
  3397. if (defined $fix_elements[$n + 2]) {
  3398. $fix_elements[$n + 2] =~ s/^\s+//;
  3399. }
  3400. $line_fixed = 1;
  3401. }
  3402. }
  3403. # unary ++ and unary -- are allowed no space on one side.
  3404. } elsif ($op eq '++' or $op eq '--') {
  3405. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  3406. if (ERROR("SPACING",
  3407. "space required one side of that '$op' $at\n" . $hereptr)) {
  3408. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  3409. $line_fixed = 1;
  3410. }
  3411. }
  3412. if ($ctx =~ /Wx[BE]/ ||
  3413. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  3414. if (ERROR("SPACING",
  3415. "space prohibited before that '$op' $at\n" . $hereptr)) {
  3416. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  3417. $line_fixed = 1;
  3418. }
  3419. }
  3420. if ($ctx =~ /ExW/) {
  3421. if (ERROR("SPACING",
  3422. "space prohibited after that '$op' $at\n" . $hereptr)) {
  3423. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
  3424. if (defined $fix_elements[$n + 2]) {
  3425. $fix_elements[$n + 2] =~ s/^\s+//;
  3426. }
  3427. $line_fixed = 1;
  3428. }
  3429. }
  3430. # << and >> may either have or not have spaces both sides
  3431. } elsif ($op eq '<<' or $op eq '>>' or
  3432. $op eq '&' or $op eq '^' or $op eq '|' or
  3433. $op eq '+' or $op eq '-' or
  3434. $op eq '*' or $op eq '/' or
  3435. $op eq '%')
  3436. {
  3437. if ($check) {
  3438. if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
  3439. if (CHK("SPACING",
  3440. "spaces preferred around that '$op' $at\n" . $hereptr)) {
  3441. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  3442. $fix_elements[$n + 2] =~ s/^\s+//;
  3443. $line_fixed = 1;
  3444. }
  3445. } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
  3446. if (CHK("SPACING",
  3447. "space preferred before that '$op' $at\n" . $hereptr)) {
  3448. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
  3449. $line_fixed = 1;
  3450. }
  3451. }
  3452. } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  3453. if (ERROR("SPACING",
  3454. "need consistent spacing around '$op' $at\n" . $hereptr)) {
  3455. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  3456. if (defined $fix_elements[$n + 2]) {
  3457. $fix_elements[$n + 2] =~ s/^\s+//;
  3458. }
  3459. $line_fixed = 1;
  3460. }
  3461. }
  3462. # A colon needs no spaces before when it is
  3463. # terminating a case value or a label.
  3464. } elsif ($opv eq ':C' || $opv eq ':L') {
  3465. if ($ctx =~ /Wx./) {
  3466. if (ERROR("SPACING",
  3467. "space prohibited before that '$op' $at\n" . $hereptr)) {
  3468. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  3469. $line_fixed = 1;
  3470. }
  3471. }
  3472. # All the others need spaces both sides.
  3473. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  3474. my $ok = 0;
  3475. # Ignore email addresses <foo@bar>
  3476. if (($op eq '<' &&
  3477. $cc =~ /^\S+\@\S+>/) ||
  3478. ($op eq '>' &&
  3479. $ca =~ /<\S+\@\S+$/))
  3480. {
  3481. $ok = 1;
  3482. }
  3483. # for asm volatile statements
  3484. # ignore a colon with another
  3485. # colon immediately before or after
  3486. if (($op eq ':') &&
  3487. ($ca =~ /:$/ || $cc =~ /^:/)) {
  3488. $ok = 1;
  3489. }
  3490. # messages are ERROR, but ?: are CHK
  3491. if ($ok == 0) {
  3492. my $msg_type = \&ERROR;
  3493. $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
  3494. if (&{$msg_type}("SPACING",
  3495. "spaces required around that '$op' $at\n" . $hereptr)) {
  3496. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  3497. if (defined $fix_elements[$n + 2]) {
  3498. $fix_elements[$n + 2] =~ s/^\s+//;
  3499. }
  3500. $line_fixed = 1;
  3501. }
  3502. }
  3503. }
  3504. $off += length($elements[$n + 1]);
  3505. ## print("n: <$n> GOOD: <$good>\n");
  3506. $fixed_line = $fixed_line . $good;
  3507. }
  3508. if (($#elements % 2) == 0) {
  3509. $fixed_line = $fixed_line . $fix_elements[$#elements];
  3510. }
  3511. if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
  3512. $fixed[$fixlinenr] = $fixed_line;
  3513. }
  3514. }
  3515. # check for whitespace before a non-naked semicolon
  3516. if ($line =~ /^\+.*\S\s+;\s*$/) {
  3517. if (WARN("SPACING",
  3518. "space prohibited before semicolon\n" . $herecurr) &&
  3519. $fix) {
  3520. 1 while $fixed[$fixlinenr] =~
  3521. s/^(\+.*\S)\s+;/$1;/;
  3522. }
  3523. }
  3524. # check for multiple assignments
  3525. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  3526. CHK("MULTIPLE_ASSIGNMENTS",
  3527. "multiple assignments should be avoided\n" . $herecurr);
  3528. }
  3529. ## # check for multiple declarations, allowing for a function declaration
  3530. ## # continuation.
  3531. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  3532. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  3533. ##
  3534. ## # Remove any bracketed sections to ensure we do not
  3535. ## # falsly report the parameters of functions.
  3536. ## my $ln = $line;
  3537. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  3538. ## }
  3539. ## if ($ln =~ /,/) {
  3540. ## WARN("MULTIPLE_DECLARATION",
  3541. ## "declaring multiple variables together should be avoided\n" . $herecurr);
  3542. ## }
  3543. ## }
  3544. #need space before brace following if, while, etc
  3545. if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
  3546. $line =~ /do\{/) {
  3547. if (ERROR("SPACING",
  3548. "space required before the open brace '{'\n" . $herecurr) &&
  3549. $fix) {
  3550. $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
  3551. }
  3552. }
  3553. ## # check for blank lines before declarations
  3554. ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
  3555. ## $prevrawline =~ /^.\s*$/) {
  3556. ## WARN("SPACING",
  3557. ## "No blank lines before declarations\n" . $hereprev);
  3558. ## }
  3559. ##
  3560. # closing brace should have a space following it when it has anything
  3561. # on the line
  3562. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  3563. if (ERROR("SPACING",
  3564. "space required after that close brace '}'\n" . $herecurr) &&
  3565. $fix) {
  3566. $fixed[$fixlinenr] =~
  3567. s/}((?!(?:,|;|\)))\S)/} $1/;
  3568. }
  3569. }
  3570. # check spacing on square brackets
  3571. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  3572. if (ERROR("SPACING",
  3573. "space prohibited after that open square bracket '['\n" . $herecurr) &&
  3574. $fix) {
  3575. $fixed[$fixlinenr] =~
  3576. s/\[\s+/\[/;
  3577. }
  3578. }
  3579. if ($line =~ /\s\]/) {
  3580. if (ERROR("SPACING",
  3581. "space prohibited before that close square bracket ']'\n" . $herecurr) &&
  3582. $fix) {
  3583. $fixed[$fixlinenr] =~
  3584. s/\s+\]/\]/;
  3585. }
  3586. }
  3587. # check spacing on parentheses
  3588. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  3589. $line !~ /for\s*\(\s+;/) {
  3590. if (ERROR("SPACING",
  3591. "space prohibited after that open parenthesis '('\n" . $herecurr) &&
  3592. $fix) {
  3593. $fixed[$fixlinenr] =~
  3594. s/\(\s+/\(/;
  3595. }
  3596. }
  3597. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  3598. $line !~ /for\s*\(.*;\s+\)/ &&
  3599. $line !~ /:\s+\)/) {
  3600. if (ERROR("SPACING",
  3601. "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
  3602. $fix) {
  3603. $fixed[$fixlinenr] =~
  3604. s/\s+\)/\)/;
  3605. }
  3606. }
  3607. # check unnecessary parentheses around addressof/dereference single $Lvals
  3608. # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
  3609. while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
  3610. my $var = $1;
  3611. if (CHK("UNNECESSARY_PARENTHESES",
  3612. "Unnecessary parentheses around $var\n" . $herecurr) &&
  3613. $fix) {
  3614. $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
  3615. }
  3616. }
  3617. # check for unnecessary parentheses around function pointer uses
  3618. # ie: (foo->bar)(); should be foo->bar();
  3619. # but not "if (foo->bar) (" to avoid some false positives
  3620. if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
  3621. my $var = $2;
  3622. if (CHK("UNNECESSARY_PARENTHESES",
  3623. "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
  3624. $fix) {
  3625. my $var2 = deparenthesize($var);
  3626. $var2 =~ s/\s//g;
  3627. $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
  3628. }
  3629. }
  3630. #goto labels aren't indented, allow a single space however
  3631. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  3632. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  3633. if (WARN("INDENTED_LABEL",
  3634. "labels should not be indented\n" . $herecurr) &&
  3635. $fix) {
  3636. $fixed[$fixlinenr] =~
  3637. s/^(.)\s+/$1/;
  3638. }
  3639. }
  3640. # return is not a function
  3641. if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
  3642. my $spacing = $1;
  3643. if ($^V && $^V ge 5.10.0 &&
  3644. $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
  3645. my $value = $1;
  3646. $value = deparenthesize($value);
  3647. if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
  3648. ERROR("RETURN_PARENTHESES",
  3649. "return is not a function, parentheses are not required\n" . $herecurr);
  3650. }
  3651. } elsif ($spacing !~ /\s+/) {
  3652. ERROR("SPACING",
  3653. "space required before the open parenthesis '('\n" . $herecurr);
  3654. }
  3655. }
  3656. # unnecessary return in a void function
  3657. # at end-of-function, with the previous line a single leading tab, then return;
  3658. # and the line before that not a goto label target like "out:"
  3659. if ($sline =~ /^[ \+]}\s*$/ &&
  3660. $prevline =~ /^\+\treturn\s*;\s*$/ &&
  3661. $linenr >= 3 &&
  3662. $lines[$linenr - 3] =~ /^[ +]/ &&
  3663. $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
  3664. WARN("RETURN_VOID",
  3665. "void function return statements are not generally useful\n" . $hereprev);
  3666. }
  3667. # if statements using unnecessary parentheses - ie: if ((foo == bar))
  3668. if ($^V && $^V ge 5.10.0 &&
  3669. $line =~ /\bif\s*((?:\(\s*){2,})/) {
  3670. my $openparens = $1;
  3671. my $count = $openparens =~ tr@\(@\(@;
  3672. my $msg = "";
  3673. if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
  3674. my $comp = $4; #Not $1 because of $LvalOrFunc
  3675. $msg = " - maybe == should be = ?" if ($comp eq "==");
  3676. WARN("UNNECESSARY_PARENTHESES",
  3677. "Unnecessary parentheses$msg\n" . $herecurr);
  3678. }
  3679. }
  3680. # comparisons with a constant or upper case identifier on the left
  3681. # avoid cases like "foo + BAR < baz"
  3682. # only fix matches surrounded by parentheses to avoid incorrect
  3683. # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
  3684. if ($^V && $^V ge 5.10.0 &&
  3685. $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
  3686. my $lead = $1;
  3687. my $const = $2;
  3688. my $comp = $3;
  3689. my $to = $4;
  3690. my $newcomp = $comp;
  3691. if ($lead !~ /$Operators\s*$/ &&
  3692. $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
  3693. WARN("CONSTANT_COMPARISON",
  3694. "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
  3695. $fix) {
  3696. if ($comp eq "<") {
  3697. $newcomp = ">";
  3698. } elsif ($comp eq "<=") {
  3699. $newcomp = ">=";
  3700. } elsif ($comp eq ">") {
  3701. $newcomp = "<";
  3702. } elsif ($comp eq ">=") {
  3703. $newcomp = "<=";
  3704. }
  3705. $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
  3706. }
  3707. }
  3708. # Return of what appears to be an errno should normally be negative
  3709. if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
  3710. my $name = $1;
  3711. if ($name ne 'EOF' && $name ne 'ERROR') {
  3712. WARN("USE_NEGATIVE_ERRNO",
  3713. "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
  3714. }
  3715. }
  3716. # Need a space before open parenthesis after if, while etc
  3717. if ($line =~ /\b(if|while|for|switch)\(/) {
  3718. if (ERROR("SPACING",
  3719. "space required before the open parenthesis '('\n" . $herecurr) &&
  3720. $fix) {
  3721. $fixed[$fixlinenr] =~
  3722. s/\b(if|while|for|switch)\(/$1 \(/;
  3723. }
  3724. }
  3725. # Check for illegal assignment in if conditional -- and check for trailing
  3726. # statements after the conditional.
  3727. if ($line =~ /do\s*(?!{)/) {
  3728. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  3729. ctx_statement_block($linenr, $realcnt, 0)
  3730. if (!defined $stat);
  3731. my ($stat_next) = ctx_statement_block($line_nr_next,
  3732. $remain_next, $off_next);
  3733. $stat_next =~ s/\n./\n /g;
  3734. ##print "stat<$stat> stat_next<$stat_next>\n";
  3735. if ($stat_next =~ /^\s*while\b/) {
  3736. # If the statement carries leading newlines,
  3737. # then count those as offsets.
  3738. my ($whitespace) =
  3739. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  3740. my $offset =
  3741. statement_rawlines($whitespace) - 1;
  3742. $suppress_whiletrailers{$line_nr_next +
  3743. $offset} = 1;
  3744. }
  3745. }
  3746. if (!defined $suppress_whiletrailers{$linenr} &&
  3747. defined($stat) && defined($cond) &&
  3748. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  3749. my ($s, $c) = ($stat, $cond);
  3750. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  3751. ERROR("ASSIGN_IN_IF",
  3752. "do not use assignment in if condition\n" . $herecurr);
  3753. }
  3754. # Find out what is on the end of the line after the
  3755. # conditional.
  3756. substr($s, 0, length($c), '');
  3757. $s =~ s/\n.*//g;
  3758. $s =~ s/$;//g; # Remove any comments
  3759. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  3760. $c !~ /}\s*while\s*/)
  3761. {
  3762. # Find out how long the conditional actually is.
  3763. my @newlines = ($c =~ /\n/gs);
  3764. my $cond_lines = 1 + $#newlines;
  3765. my $stat_real = '';
  3766. $stat_real = raw_line($linenr, $cond_lines)
  3767. . "\n" if ($cond_lines);
  3768. if (defined($stat_real) && $cond_lines > 1) {
  3769. $stat_real = "[...]\n$stat_real";
  3770. }
  3771. ERROR("TRAILING_STATEMENTS",
  3772. "trailing statements should be on next line\n" . $herecurr . $stat_real);
  3773. }
  3774. }
  3775. # Check for bitwise tests written as boolean
  3776. if ($line =~ /
  3777. (?:
  3778. (?:\[|\(|\&\&|\|\|)
  3779. \s*0[xX][0-9]+\s*
  3780. (?:\&\&|\|\|)
  3781. |
  3782. (?:\&\&|\|\|)
  3783. \s*0[xX][0-9]+\s*
  3784. (?:\&\&|\|\||\)|\])
  3785. )/x)
  3786. {
  3787. WARN("HEXADECIMAL_BOOLEAN_TEST",
  3788. "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  3789. }
  3790. # if and else should not have general statements after it
  3791. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  3792. my $s = $1;
  3793. $s =~ s/$;//g; # Remove any comments
  3794. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  3795. ERROR("TRAILING_STATEMENTS",
  3796. "trailing statements should be on next line\n" . $herecurr);
  3797. }
  3798. }
  3799. # if should not continue a brace
  3800. if ($line =~ /}\s*if\b/) {
  3801. ERROR("TRAILING_STATEMENTS",
  3802. "trailing statements should be on next line (or did you mean 'else if'?)\n" .
  3803. $herecurr);
  3804. }
  3805. # case and default should not have general statements after them
  3806. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  3807. $line !~ /\G(?:
  3808. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  3809. \s*return\s+
  3810. )/xg)
  3811. {
  3812. ERROR("TRAILING_STATEMENTS",
  3813. "trailing statements should be on next line\n" . $herecurr);
  3814. }
  3815. # Check for }<nl>else {, these must be at the same
  3816. # indent level to be relevant to each other.
  3817. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
  3818. $previndent == $indent) {
  3819. if (ERROR("ELSE_AFTER_BRACE",
  3820. "else should follow close brace '}'\n" . $hereprev) &&
  3821. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  3822. fix_delete_line($fixlinenr - 1, $prevrawline);
  3823. fix_delete_line($fixlinenr, $rawline);
  3824. my $fixedline = $prevrawline;
  3825. $fixedline =~ s/}\s*$//;
  3826. if ($fixedline !~ /^\+\s*$/) {
  3827. fix_insert_line($fixlinenr, $fixedline);
  3828. }
  3829. $fixedline = $rawline;
  3830. $fixedline =~ s/^(.\s*)else/$1} else/;
  3831. fix_insert_line($fixlinenr, $fixedline);
  3832. }
  3833. }
  3834. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
  3835. $previndent == $indent) {
  3836. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  3837. # Find out what is on the end of the line after the
  3838. # conditional.
  3839. substr($s, 0, length($c), '');
  3840. $s =~ s/\n.*//g;
  3841. if ($s =~ /^\s*;/) {
  3842. if (ERROR("WHILE_AFTER_BRACE",
  3843. "while should follow close brace '}'\n" . $hereprev) &&
  3844. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  3845. fix_delete_line($fixlinenr - 1, $prevrawline);
  3846. fix_delete_line($fixlinenr, $rawline);
  3847. my $fixedline = $prevrawline;
  3848. my $trailing = $rawline;
  3849. $trailing =~ s/^\+//;
  3850. $trailing = trim($trailing);
  3851. $fixedline =~ s/}\s*$/} $trailing/;
  3852. fix_insert_line($fixlinenr, $fixedline);
  3853. }
  3854. }
  3855. }
  3856. #Specific variable tests
  3857. while ($line =~ m{($Constant|$Lval)}g) {
  3858. my $var = $1;
  3859. #gcc binary extension
  3860. if ($var =~ /^$Binary$/) {
  3861. if (WARN("GCC_BINARY_CONSTANT",
  3862. "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
  3863. $fix) {
  3864. my $hexval = sprintf("0x%x", oct($var));
  3865. $fixed[$fixlinenr] =~
  3866. s/\b$var\b/$hexval/;
  3867. }
  3868. }
  3869. #CamelCase
  3870. if ($var !~ /^$Constant$/ &&
  3871. $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
  3872. #Ignore Page<foo> variants
  3873. $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
  3874. #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
  3875. $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
  3876. #Ignore some three character SI units explicitly, like MiB and KHz
  3877. $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
  3878. while ($var =~ m{($Ident)}g) {
  3879. my $word = $1;
  3880. next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
  3881. if ($check) {
  3882. seed_camelcase_includes();
  3883. if (!$file && !$camelcase_file_seeded) {
  3884. seed_camelcase_file($realfile);
  3885. $camelcase_file_seeded = 1;
  3886. }
  3887. }
  3888. if (!defined $camelcase{$word}) {
  3889. $camelcase{$word} = 1;
  3890. CHK("CAMELCASE",
  3891. "Avoid CamelCase: <$word>\n" . $herecurr);
  3892. }
  3893. }
  3894. }
  3895. }
  3896. #no spaces allowed after \ in define
  3897. if ($line =~ /\#\s*define.*\\\s+$/) {
  3898. if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
  3899. "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
  3900. $fix) {
  3901. $fixed[$fixlinenr] =~ s/\s+$//;
  3902. }
  3903. }
  3904. # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
  3905. # itself <asm/foo.h> (uses RAW line)
  3906. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  3907. my $file = "$1.h";
  3908. my $checkfile = "include/linux/$file";
  3909. if (-f "$root/$checkfile" &&
  3910. $realfile ne $checkfile &&
  3911. $1 !~ /$allowed_asm_includes/)
  3912. {
  3913. my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
  3914. if ($asminclude > 0) {
  3915. if ($realfile =~ m{^arch/}) {
  3916. CHK("ARCH_INCLUDE_LINUX",
  3917. "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  3918. } else {
  3919. WARN("INCLUDE_LINUX",
  3920. "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  3921. }
  3922. }
  3923. }
  3924. }
  3925. # multi-statement macros should be enclosed in a do while loop, grab the
  3926. # first statement and ensure its the whole macro if its not enclosed
  3927. # in a known good container
  3928. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  3929. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  3930. my $ln = $linenr;
  3931. my $cnt = $realcnt;
  3932. my ($off, $dstat, $dcond, $rest);
  3933. my $ctx = '';
  3934. my $has_flow_statement = 0;
  3935. my $has_arg_concat = 0;
  3936. ($dstat, $dcond, $ln, $cnt, $off) =
  3937. ctx_statement_block($linenr, $realcnt, 0);
  3938. $ctx = $dstat;
  3939. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  3940. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  3941. $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
  3942. $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
  3943. $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
  3944. $dstat =~ s/$;//g;
  3945. $dstat =~ s/\\\n.//g;
  3946. $dstat =~ s/^\s*//s;
  3947. $dstat =~ s/\s*$//s;
  3948. # Flatten any parentheses and braces
  3949. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  3950. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  3951. $dstat =~ s/.\[[^\[\]]*\]/1/)
  3952. {
  3953. }
  3954. # Flatten any obvious string concatentation.
  3955. while ($dstat =~ s/($String)\s*$Ident/$1/ ||
  3956. $dstat =~ s/$Ident\s*($String)/$1/)
  3957. {
  3958. }
  3959. # Make asm volatile uses seem like a generic function
  3960. $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
  3961. my $exceptions = qr{
  3962. $Declare|
  3963. module_param_named|
  3964. MODULE_PARM_DESC|
  3965. DECLARE_PER_CPU|
  3966. DEFINE_PER_CPU|
  3967. __typeof__\(|
  3968. union|
  3969. struct|
  3970. \.$Ident\s*=\s*|
  3971. ^\"|\"$|
  3972. ^\[
  3973. }x;
  3974. #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
  3975. if ($dstat ne '' &&
  3976. $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
  3977. $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
  3978. $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
  3979. $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
  3980. $dstat !~ /$exceptions/ &&
  3981. $dstat !~ /^\.$Ident\s*=/ && # .foo =
  3982. $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
  3983. $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
  3984. $dstat !~ /^for\s*$Constant$/ && # for (...)
  3985. $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
  3986. $dstat !~ /^do\s*{/ && # do {...
  3987. $dstat !~ /^\(\{/ && # ({...
  3988. $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
  3989. {
  3990. $ctx =~ s/\n*$//;
  3991. my $herectx = $here . "\n";
  3992. my $cnt = statement_rawlines($ctx);
  3993. for (my $n = 0; $n < $cnt; $n++) {
  3994. $herectx .= raw_line($linenr, $n) . "\n";
  3995. }
  3996. if ($dstat =~ /;/) {
  3997. ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
  3998. "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
  3999. } else {
  4000. ERROR("COMPLEX_MACRO",
  4001. "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
  4002. }
  4003. }
  4004. # check for macros with flow control, but without ## concatenation
  4005. # ## concatenation is commonly a macro that defines a function so ignore those
  4006. if ($has_flow_statement && !$has_arg_concat) {
  4007. my $herectx = $here . "\n";
  4008. my $cnt = statement_rawlines($ctx);
  4009. for (my $n = 0; $n < $cnt; $n++) {
  4010. $herectx .= raw_line($linenr, $n) . "\n";
  4011. }
  4012. WARN("MACRO_WITH_FLOW_CONTROL",
  4013. "Macros with flow control statements should be avoided\n" . "$herectx");
  4014. }
  4015. # check for line continuations outside of #defines, preprocessor #, and asm
  4016. } else {
  4017. if ($prevline !~ /^..*\\$/ &&
  4018. $line !~ /^\+\s*\#.*\\$/ && # preprocessor
  4019. $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
  4020. $line =~ /^\+.*\\$/) {
  4021. WARN("LINE_CONTINUATIONS",
  4022. "Avoid unnecessary line continuations\n" . $herecurr);
  4023. }
  4024. }
  4025. # do {} while (0) macro tests:
  4026. # single-statement macros do not need to be enclosed in do while (0) loop,
  4027. # macro should not end with a semicolon
  4028. if ($^V && $^V ge 5.10.0 &&
  4029. $realfile !~ m@/vmlinux.lds.h$@ &&
  4030. $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
  4031. my $ln = $linenr;
  4032. my $cnt = $realcnt;
  4033. my ($off, $dstat, $dcond, $rest);
  4034. my $ctx = '';
  4035. ($dstat, $dcond, $ln, $cnt, $off) =
  4036. ctx_statement_block($linenr, $realcnt, 0);
  4037. $ctx = $dstat;
  4038. $dstat =~ s/\\\n.//g;
  4039. $dstat =~ s/$;/ /g;
  4040. if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
  4041. my $stmts = $2;
  4042. my $semis = $3;
  4043. $ctx =~ s/\n*$//;
  4044. my $cnt = statement_rawlines($ctx);
  4045. my $herectx = $here . "\n";
  4046. for (my $n = 0; $n < $cnt; $n++) {
  4047. $herectx .= raw_line($linenr, $n) . "\n";
  4048. }
  4049. if (($stmts =~ tr/;/;/) == 1 &&
  4050. $stmts !~ /^\s*(if|while|for|switch)\b/) {
  4051. WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
  4052. "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
  4053. }
  4054. if (defined $semis && $semis ne "") {
  4055. WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
  4056. "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
  4057. }
  4058. } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
  4059. $ctx =~ s/\n*$//;
  4060. my $cnt = statement_rawlines($ctx);
  4061. my $herectx = $here . "\n";
  4062. for (my $n = 0; $n < $cnt; $n++) {
  4063. $herectx .= raw_line($linenr, $n) . "\n";
  4064. }
  4065. WARN("TRAILING_SEMICOLON",
  4066. "macros should not use a trailing semicolon\n" . "$herectx");
  4067. }
  4068. }
  4069. # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
  4070. # all assignments may have only one of the following with an assignment:
  4071. # .
  4072. # ALIGN(...)
  4073. # VMLINUX_SYMBOL(...)
  4074. if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
  4075. WARN("MISSING_VMLINUX_SYMBOL",
  4076. "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
  4077. }
  4078. # check for redundant bracing round if etc
  4079. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  4080. my ($level, $endln, @chunks) =
  4081. ctx_statement_full($linenr, $realcnt, 1);
  4082. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  4083. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  4084. if ($#chunks > 0 && $level == 0) {
  4085. my @allowed = ();
  4086. my $allow = 0;
  4087. my $seen = 0;
  4088. my $herectx = $here . "\n";
  4089. my $ln = $linenr - 1;
  4090. for my $chunk (@chunks) {
  4091. my ($cond, $block) = @{$chunk};
  4092. # If the condition carries leading newlines, then count those as offsets.
  4093. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  4094. my $offset = statement_rawlines($whitespace) - 1;
  4095. $allowed[$allow] = 0;
  4096. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  4097. # We have looked at and allowed this specific line.
  4098. $suppress_ifbraces{$ln + $offset} = 1;
  4099. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  4100. $ln += statement_rawlines($block) - 1;
  4101. substr($block, 0, length($cond), '');
  4102. $seen++ if ($block =~ /^\s*{/);
  4103. #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
  4104. if (statement_lines($cond) > 1) {
  4105. #print "APW: ALLOWED: cond<$cond>\n";
  4106. $allowed[$allow] = 1;
  4107. }
  4108. if ($block =~/\b(?:if|for|while)\b/) {
  4109. #print "APW: ALLOWED: block<$block>\n";
  4110. $allowed[$allow] = 1;
  4111. }
  4112. if (statement_block_size($block) > 1) {
  4113. #print "APW: ALLOWED: lines block<$block>\n";
  4114. $allowed[$allow] = 1;
  4115. }
  4116. $allow++;
  4117. }
  4118. if ($seen) {
  4119. my $sum_allowed = 0;
  4120. foreach (@allowed) {
  4121. $sum_allowed += $_;
  4122. }
  4123. if ($sum_allowed == 0) {
  4124. WARN("BRACES",
  4125. "braces {} are not necessary for any arm of this statement\n" . $herectx);
  4126. } elsif ($sum_allowed != $allow &&
  4127. $seen != $allow) {
  4128. CHK("BRACES",
  4129. "braces {} should be used on all arms of this statement\n" . $herectx);
  4130. }
  4131. }
  4132. }
  4133. }
  4134. if (!defined $suppress_ifbraces{$linenr - 1} &&
  4135. $line =~ /\b(if|while|for|else)\b/) {
  4136. my $allowed = 0;
  4137. # Check the pre-context.
  4138. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  4139. #print "APW: ALLOWED: pre<$1>\n";
  4140. $allowed = 1;
  4141. }
  4142. my ($level, $endln, @chunks) =
  4143. ctx_statement_full($linenr, $realcnt, $-[0]);
  4144. # Check the condition.
  4145. my ($cond, $block) = @{$chunks[0]};
  4146. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  4147. if (defined $cond) {
  4148. substr($block, 0, length($cond), '');
  4149. }
  4150. if (statement_lines($cond) > 1) {
  4151. #print "APW: ALLOWED: cond<$cond>\n";
  4152. $allowed = 1;
  4153. }
  4154. if ($block =~/\b(?:if|for|while)\b/) {
  4155. #print "APW: ALLOWED: block<$block>\n";
  4156. $allowed = 1;
  4157. }
  4158. if (statement_block_size($block) > 1) {
  4159. #print "APW: ALLOWED: lines block<$block>\n";
  4160. $allowed = 1;
  4161. }
  4162. # Check the post-context.
  4163. if (defined $chunks[1]) {
  4164. my ($cond, $block) = @{$chunks[1]};
  4165. if (defined $cond) {
  4166. substr($block, 0, length($cond), '');
  4167. }
  4168. if ($block =~ /^\s*\{/) {
  4169. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  4170. $allowed = 1;
  4171. }
  4172. }
  4173. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  4174. my $herectx = $here . "\n";
  4175. my $cnt = statement_rawlines($block);
  4176. for (my $n = 0; $n < $cnt; $n++) {
  4177. $herectx .= raw_line($linenr, $n) . "\n";
  4178. }
  4179. WARN("BRACES",
  4180. "braces {} are not necessary for single statement blocks\n" . $herectx);
  4181. }
  4182. }
  4183. # check for unnecessary blank lines around braces
  4184. if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
  4185. if (CHK("BRACES",
  4186. "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
  4187. $fix && $prevrawline =~ /^\+/) {
  4188. fix_delete_line($fixlinenr - 1, $prevrawline);
  4189. }
  4190. }
  4191. if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
  4192. if (CHK("BRACES",
  4193. "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
  4194. $fix) {
  4195. fix_delete_line($fixlinenr, $rawline);
  4196. }
  4197. }
  4198. # no volatiles please
  4199. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  4200. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  4201. WARN("VOLATILE",
  4202. "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  4203. }
  4204. # Check for user-visible strings broken across lines, which breaks the ability
  4205. # to grep for the string. Make exceptions when the previous string ends in a
  4206. # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
  4207. # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
  4208. if ($line =~ /^\+\s*$String/ &&
  4209. $prevline =~ /"\s*$/ &&
  4210. $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
  4211. if (WARN("SPLIT_STRING",
  4212. "quoted string split across lines\n" . $hereprev) &&
  4213. $fix &&
  4214. $prevrawline =~ /^\+.*"\s*$/ &&
  4215. $last_coalesced_string_linenr != $linenr - 1) {
  4216. my $extracted_string = get_quoted_string($line, $rawline);
  4217. my $comma_close = "";
  4218. if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
  4219. $comma_close = $1;
  4220. }
  4221. fix_delete_line($fixlinenr - 1, $prevrawline);
  4222. fix_delete_line($fixlinenr, $rawline);
  4223. my $fixedline = $prevrawline;
  4224. $fixedline =~ s/"\s*$//;
  4225. $fixedline .= substr($extracted_string, 1) . trim($comma_close);
  4226. fix_insert_line($fixlinenr - 1, $fixedline);
  4227. $fixedline = $rawline;
  4228. $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
  4229. if ($fixedline !~ /\+\s*$/) {
  4230. fix_insert_line($fixlinenr, $fixedline);
  4231. }
  4232. $last_coalesced_string_linenr = $linenr;
  4233. }
  4234. }
  4235. # check for missing a space in a string concatenation
  4236. if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
  4237. WARN('MISSING_SPACE',
  4238. "break quoted strings at a space character\n" . $hereprev);
  4239. }
  4240. # check for spaces before a quoted newline
  4241. if ($rawline =~ /^.*\".*\s\\n/) {
  4242. if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
  4243. "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
  4244. $fix) {
  4245. $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
  4246. }
  4247. }
  4248. # concatenated string without spaces between elements
  4249. if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
  4250. CHK("CONCATENATED_STRING",
  4251. "Concatenated strings should use spaces between elements\n" . $herecurr);
  4252. }
  4253. # uncoalesced string fragments
  4254. if ($line =~ /$String\s*"/) {
  4255. WARN("STRING_FRAGMENTS",
  4256. "Consecutive strings are generally better as a single string\n" . $herecurr);
  4257. }
  4258. # check for %L{u,d,i} and 0x%[udi] in strings
  4259. my $string;
  4260. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  4261. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  4262. $string =~ s/%%/__/g;
  4263. if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
  4264. WARN("PRINTF_L",
  4265. "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  4266. last;
  4267. }
  4268. if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
  4269. ERROR("PRINTF_0xDECIMAL",
  4270. "Prefixing 0x with decimal output is defective\n" . $herecurr);
  4271. }
  4272. }
  4273. # check for line continuations in quoted strings with odd counts of "
  4274. if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
  4275. WARN("LINE_CONTINUATIONS",
  4276. "Avoid line continuations in quoted strings\n" . $herecurr);
  4277. }
  4278. # warn about #if 0
  4279. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  4280. CHK("REDUNDANT_CODE",
  4281. "if this code is redundant consider removing it\n" .
  4282. $herecurr);
  4283. }
  4284. # check for needless "if (<foo>) fn(<foo>)" uses
  4285. if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
  4286. my $tested = quotemeta($1);
  4287. my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
  4288. if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
  4289. my $func = $1;
  4290. if (WARN('NEEDLESS_IF',
  4291. "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
  4292. $fix) {
  4293. my $do_fix = 1;
  4294. my $leading_tabs = "";
  4295. my $new_leading_tabs = "";
  4296. if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
  4297. $leading_tabs = $1;
  4298. } else {
  4299. $do_fix = 0;
  4300. }
  4301. if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
  4302. $new_leading_tabs = $1;
  4303. if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
  4304. $do_fix = 0;
  4305. }
  4306. } else {
  4307. $do_fix = 0;
  4308. }
  4309. if ($do_fix) {
  4310. fix_delete_line($fixlinenr - 1, $prevrawline);
  4311. $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
  4312. }
  4313. }
  4314. }
  4315. }
  4316. # check for unnecessary "Out of Memory" messages
  4317. if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
  4318. $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
  4319. (defined $1 || defined $3) &&
  4320. $linenr > 3) {
  4321. my $testval = $2;
  4322. my $testline = $lines[$linenr - 3];
  4323. my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
  4324. # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
  4325. if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
  4326. WARN("OOM_MESSAGE",
  4327. "Possible unnecessary 'out of memory' message\n" . $hereprev);
  4328. }
  4329. }
  4330. # check for logging functions with KERN_<LEVEL>
  4331. if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
  4332. $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
  4333. my $level = $1;
  4334. if (WARN("UNNECESSARY_KERN_LEVEL",
  4335. "Possible unnecessary $level\n" . $herecurr) &&
  4336. $fix) {
  4337. $fixed[$fixlinenr] =~ s/\s*$level\s*//;
  4338. }
  4339. }
  4340. # check for mask then right shift without a parentheses
  4341. if ($^V && $^V ge 5.10.0 &&
  4342. $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
  4343. $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
  4344. WARN("MASK_THEN_SHIFT",
  4345. "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
  4346. }
  4347. # check for pointer comparisons to NULL
  4348. if ($^V && $^V ge 5.10.0) {
  4349. while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
  4350. my $val = $1;
  4351. my $equal = "!";
  4352. $equal = "" if ($4 eq "!=");
  4353. if (CHK("COMPARISON_TO_NULL",
  4354. "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
  4355. $fix) {
  4356. $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
  4357. }
  4358. }
  4359. }
  4360. # check for bad placement of section $InitAttribute (e.g.: __initdata)
  4361. if ($line =~ /(\b$InitAttribute\b)/) {
  4362. my $attr = $1;
  4363. if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
  4364. my $ptr = $1;
  4365. my $var = $2;
  4366. if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
  4367. ERROR("MISPLACED_INIT",
  4368. "$attr should be placed after $var\n" . $herecurr)) ||
  4369. ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
  4370. WARN("MISPLACED_INIT",
  4371. "$attr should be placed after $var\n" . $herecurr))) &&
  4372. $fix) {
  4373. $fixed[$fixlinenr] =~ 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;
  4374. }
  4375. }
  4376. }
  4377. # check for $InitAttributeData (ie: __initdata) with const
  4378. if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
  4379. my $attr = $1;
  4380. $attr =~ /($InitAttributePrefix)(.*)/;
  4381. my $attr_prefix = $1;
  4382. my $attr_type = $2;
  4383. if (ERROR("INIT_ATTRIBUTE",
  4384. "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
  4385. $fix) {
  4386. $fixed[$fixlinenr] =~
  4387. s/$InitAttributeData/${attr_prefix}initconst/;
  4388. }
  4389. }
  4390. # check for $InitAttributeConst (ie: __initconst) without const
  4391. if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
  4392. my $attr = $1;
  4393. if (ERROR("INIT_ATTRIBUTE",
  4394. "Use of $attr requires a separate use of const\n" . $herecurr) &&
  4395. $fix) {
  4396. my $lead = $fixed[$fixlinenr] =~
  4397. /(^\+\s*(?:static\s+))/;
  4398. $lead = rtrim($1);
  4399. $lead = "$lead " if ($lead !~ /^\+$/);
  4400. $lead = "${lead}const ";
  4401. $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
  4402. }
  4403. }
  4404. # check for __read_mostly with const non-pointer (should just be const)
  4405. if ($line =~ /\b__read_mostly\b/ &&
  4406. $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
  4407. if (ERROR("CONST_READ_MOSTLY",
  4408. "Invalid use of __read_mostly with const type\n" . $herecurr) &&
  4409. $fix) {
  4410. $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
  4411. }
  4412. }
  4413. # don't use __constant_<foo> functions outside of include/uapi/
  4414. if ($realfile !~ m@^include/uapi/@ &&
  4415. $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
  4416. my $constant_func = $1;
  4417. my $func = $constant_func;
  4418. $func =~ s/^__constant_//;
  4419. if (WARN("CONSTANT_CONVERSION",
  4420. "$constant_func should be $func\n" . $herecurr) &&
  4421. $fix) {
  4422. $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
  4423. }
  4424. }
  4425. # prefer usleep_range over udelay
  4426. if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
  4427. my $delay = $1;
  4428. # ignore udelay's < 10, however
  4429. if (! ($delay < 10) ) {
  4430. CHK("USLEEP_RANGE",
  4431. "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
  4432. }
  4433. if ($delay > 2000) {
  4434. WARN("LONG_UDELAY",
  4435. "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
  4436. }
  4437. }
  4438. # warn about unexpectedly long msleep's
  4439. if ($line =~ /\bmsleep\s*\((\d+)\);/) {
  4440. if ($1 < 20) {
  4441. WARN("MSLEEP",
  4442. "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
  4443. }
  4444. }
  4445. # check for comparisons of jiffies
  4446. if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
  4447. WARN("JIFFIES_COMPARISON",
  4448. "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
  4449. }
  4450. # check for comparisons of get_jiffies_64()
  4451. if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
  4452. WARN("JIFFIES_COMPARISON",
  4453. "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
  4454. }
  4455. # warn about #ifdefs in C files
  4456. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  4457. # print "#ifdef in C files should be avoided\n";
  4458. # print "$herecurr";
  4459. # $clean = 0;
  4460. # }
  4461. # warn about spacing in #ifdefs
  4462. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  4463. if (ERROR("SPACING",
  4464. "exactly one space required after that #$1\n" . $herecurr) &&
  4465. $fix) {
  4466. $fixed[$fixlinenr] =~
  4467. s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
  4468. }
  4469. }
  4470. # check for spinlock_t definitions without a comment.
  4471. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  4472. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  4473. my $which = $1;
  4474. if (!ctx_has_comment($first_line, $linenr)) {
  4475. CHK("UNCOMMENTED_DEFINITION",
  4476. "$1 definition without comment\n" . $herecurr);
  4477. }
  4478. }
  4479. # check for memory barriers without a comment.
  4480. my $barriers = qr{
  4481. mb|
  4482. rmb|
  4483. wmb|
  4484. read_barrier_depends
  4485. }x;
  4486. my $barrier_stems = qr{
  4487. mb__before_atomic|
  4488. mb__after_atomic|
  4489. store_release|
  4490. load_acquire|
  4491. store_mb|
  4492. (?:$barriers)
  4493. }x;
  4494. my $all_barriers = qr{
  4495. (?:$barriers)|
  4496. smp_(?:$barrier_stems)|
  4497. virt_(?:$barrier_stems)
  4498. }x;
  4499. if ($line =~ /\b(?:$all_barriers)\s*\(/) {
  4500. if (!ctx_has_comment($first_line, $linenr)) {
  4501. WARN("MEMORY_BARRIER",
  4502. "memory barrier without comment\n" . $herecurr);
  4503. }
  4504. }
  4505. my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
  4506. if ($realfile !~ m@^include/asm-generic/@ &&
  4507. $realfile !~ m@/barrier\.h$@ &&
  4508. $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
  4509. $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
  4510. WARN("MEMORY_BARRIER",
  4511. "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
  4512. }
  4513. # check for waitqueue_active without a comment.
  4514. if ($line =~ /\bwaitqueue_active\s*\(/) {
  4515. if (!ctx_has_comment($first_line, $linenr)) {
  4516. WARN("WAITQUEUE_ACTIVE",
  4517. "waitqueue_active without comment\n" . $herecurr);
  4518. }
  4519. }
  4520. # Check for expedited grace periods that interrupt non-idle non-nohz
  4521. # online CPUs. These expedited can therefore degrade real-time response
  4522. # if used carelessly, and should be avoided where not absolutely
  4523. # needed. It is always OK to use synchronize_rcu_expedited() and
  4524. # synchronize_sched_expedited() at boot time (before real-time applications
  4525. # start) and in error situations where real-time response is compromised in
  4526. # any case. Note that synchronize_srcu_expedited() does -not- interrupt
  4527. # other CPUs, so don't warn on uses of synchronize_srcu_expedited().
  4528. # Of course, nothing comes for free, and srcu_read_lock() and
  4529. # srcu_read_unlock() do contain full memory barriers in payment for
  4530. # synchronize_srcu_expedited() non-interruption properties.
  4531. if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
  4532. WARN("EXPEDITED_RCU_GRACE_PERIOD",
  4533. "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
  4534. }
  4535. # check of hardware specific defines
  4536. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  4537. CHK("ARCH_DEFINES",
  4538. "architecture specific defines should be avoided\n" . $herecurr);
  4539. }
  4540. # Check that the storage class is at the beginning of a declaration
  4541. if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
  4542. WARN("STORAGE_CLASS",
  4543. "storage class should be at the beginning of the declaration\n" . $herecurr)
  4544. }
  4545. # check the location of the inline attribute, that it is between
  4546. # storage class and type.
  4547. if ($line =~ /\b$Type\s+$Inline\b/ ||
  4548. $line =~ /\b$Inline\s+$Storage\b/) {
  4549. ERROR("INLINE_LOCATION",
  4550. "inline keyword should sit between storage class and type\n" . $herecurr);
  4551. }
  4552. # Check for __inline__ and __inline, prefer inline
  4553. if ($realfile !~ m@\binclude/uapi/@ &&
  4554. $line =~ /\b(__inline__|__inline)\b/) {
  4555. if (WARN("INLINE",
  4556. "plain inline is preferred over $1\n" . $herecurr) &&
  4557. $fix) {
  4558. $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
  4559. }
  4560. }
  4561. # Check for __attribute__ packed, prefer __packed
  4562. if ($realfile !~ m@\binclude/uapi/@ &&
  4563. $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
  4564. WARN("PREFER_PACKED",
  4565. "__packed is preferred over __attribute__((packed))\n" . $herecurr);
  4566. }
  4567. # Check for __attribute__ aligned, prefer __aligned
  4568. if ($realfile !~ m@\binclude/uapi/@ &&
  4569. $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
  4570. WARN("PREFER_ALIGNED",
  4571. "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
  4572. }
  4573. # Check for __attribute__ format(printf, prefer __printf
  4574. if ($realfile !~ m@\binclude/uapi/@ &&
  4575. $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
  4576. if (WARN("PREFER_PRINTF",
  4577. "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
  4578. $fix) {
  4579. $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
  4580. }
  4581. }
  4582. # Check for __attribute__ format(scanf, prefer __scanf
  4583. if ($realfile !~ m@\binclude/uapi/@ &&
  4584. $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
  4585. if (WARN("PREFER_SCANF",
  4586. "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
  4587. $fix) {
  4588. $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
  4589. }
  4590. }
  4591. # Check for __attribute__ weak, or __weak declarations (may have link issues)
  4592. if ($^V && $^V ge 5.10.0 &&
  4593. $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
  4594. ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
  4595. $line =~ /\b__weak\b/)) {
  4596. ERROR("WEAK_DECLARATION",
  4597. "Using weak declarations can have unintended link defects\n" . $herecurr);
  4598. }
  4599. # check for c99 types like uint8_t used outside of uapi/
  4600. if ($realfile !~ m@\binclude/uapi/@ &&
  4601. $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
  4602. my $type = $1;
  4603. if ($type =~ /\b($typeC99Typedefs)\b/) {
  4604. $type = $1;
  4605. my $kernel_type = 'u';
  4606. $kernel_type = 's' if ($type =~ /^_*[si]/);
  4607. $type =~ /(\d+)/;
  4608. $kernel_type .= $1;
  4609. if (CHK("PREFER_KERNEL_TYPES",
  4610. "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
  4611. $fix) {
  4612. $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
  4613. }
  4614. }
  4615. }
  4616. # check for cast of C90 native int or longer types constants
  4617. if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
  4618. my $cast = $1;
  4619. my $const = $2;
  4620. if (WARN("TYPECAST_INT_CONSTANT",
  4621. "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
  4622. $fix) {
  4623. my $suffix = "";
  4624. my $newconst = $const;
  4625. $newconst =~ s/${Int_type}$//;
  4626. $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
  4627. if ($cast =~ /\blong\s+long\b/) {
  4628. $suffix .= 'LL';
  4629. } elsif ($cast =~ /\blong\b/) {
  4630. $suffix .= 'L';
  4631. }
  4632. $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
  4633. }
  4634. }
  4635. # check for sizeof(&)
  4636. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  4637. WARN("SIZEOF_ADDRESS",
  4638. "sizeof(& should be avoided\n" . $herecurr);
  4639. }
  4640. # check for sizeof without parenthesis
  4641. if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
  4642. if (WARN("SIZEOF_PARENTHESIS",
  4643. "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
  4644. $fix) {
  4645. $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
  4646. }
  4647. }
  4648. # check for struct spinlock declarations
  4649. if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
  4650. WARN("USE_SPINLOCK_T",
  4651. "struct spinlock should be spinlock_t\n" . $herecurr);
  4652. }
  4653. # check for seq_printf uses that could be seq_puts
  4654. if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
  4655. my $fmt = get_quoted_string($line, $rawline);
  4656. $fmt =~ s/%%//g;
  4657. if ($fmt !~ /%/) {
  4658. if (WARN("PREFER_SEQ_PUTS",
  4659. "Prefer seq_puts to seq_printf\n" . $herecurr) &&
  4660. $fix) {
  4661. $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
  4662. }
  4663. }
  4664. }
  4665. # Check for misused memsets
  4666. if ($^V && $^V ge 5.10.0 &&
  4667. defined $stat &&
  4668. $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
  4669. my $ms_addr = $2;
  4670. my $ms_val = $7;
  4671. my $ms_size = $12;
  4672. if ($ms_size =~ /^(0x|)0$/i) {
  4673. ERROR("MEMSET",
  4674. "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
  4675. } elsif ($ms_size =~ /^(0x|)1$/i) {
  4676. WARN("MEMSET",
  4677. "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
  4678. }
  4679. }
  4680. # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
  4681. if ($^V && $^V ge 5.10.0 &&
  4682. defined $stat &&
  4683. $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
  4684. if (WARN("PREFER_ETHER_ADDR_COPY",
  4685. "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
  4686. $fix) {
  4687. $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
  4688. }
  4689. }
  4690. # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
  4691. if ($^V && $^V ge 5.10.0 &&
  4692. defined $stat &&
  4693. $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
  4694. WARN("PREFER_ETHER_ADDR_EQUAL",
  4695. "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
  4696. }
  4697. # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
  4698. # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
  4699. if ($^V && $^V ge 5.10.0 &&
  4700. defined $stat &&
  4701. $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
  4702. my $ms_val = $7;
  4703. if ($ms_val =~ /^(?:0x|)0+$/i) {
  4704. if (WARN("PREFER_ETH_ZERO_ADDR",
  4705. "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
  4706. $fix) {
  4707. $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
  4708. }
  4709. } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
  4710. if (WARN("PREFER_ETH_BROADCAST_ADDR",
  4711. "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
  4712. $fix) {
  4713. $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
  4714. }
  4715. }
  4716. }
  4717. # typecasts on min/max could be min_t/max_t
  4718. if ($^V && $^V ge 5.10.0 &&
  4719. defined $stat &&
  4720. $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
  4721. if (defined $2 || defined $7) {
  4722. my $call = $1;
  4723. my $cast1 = deparenthesize($2);
  4724. my $arg1 = $3;
  4725. my $cast2 = deparenthesize($7);
  4726. my $arg2 = $8;
  4727. my $cast;
  4728. if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
  4729. $cast = "$cast1 or $cast2";
  4730. } elsif ($cast1 ne "") {
  4731. $cast = $cast1;
  4732. } else {
  4733. $cast = $cast2;
  4734. }
  4735. WARN("MINMAX",
  4736. "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
  4737. }
  4738. }
  4739. # check usleep_range arguments
  4740. if ($^V && $^V ge 5.10.0 &&
  4741. defined $stat &&
  4742. $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
  4743. my $min = $1;
  4744. my $max = $7;
  4745. if ($min eq $max) {
  4746. WARN("USLEEP_RANGE",
  4747. "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
  4748. } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
  4749. $min > $max) {
  4750. WARN("USLEEP_RANGE",
  4751. "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
  4752. }
  4753. }
  4754. # check for naked sscanf
  4755. if ($^V && $^V ge 5.10.0 &&
  4756. defined $stat &&
  4757. $line =~ /\bsscanf\b/ &&
  4758. ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
  4759. $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
  4760. $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
  4761. my $lc = $stat =~ tr@\n@@;
  4762. $lc = $lc + $linenr;
  4763. my $stat_real = raw_line($linenr, 0);
  4764. for (my $count = $linenr + 1; $count <= $lc; $count++) {
  4765. $stat_real = $stat_real . "\n" . raw_line($count, 0);
  4766. }
  4767. WARN("NAKED_SSCANF",
  4768. "unchecked sscanf return value\n" . "$here\n$stat_real\n");
  4769. }
  4770. # check for simple sscanf that should be kstrto<foo>
  4771. if ($^V && $^V ge 5.10.0 &&
  4772. defined $stat &&
  4773. $line =~ /\bsscanf\b/) {
  4774. my $lc = $stat =~ tr@\n@@;
  4775. $lc = $lc + $linenr;
  4776. my $stat_real = raw_line($linenr, 0);
  4777. for (my $count = $linenr + 1; $count <= $lc; $count++) {
  4778. $stat_real = $stat_real . "\n" . raw_line($count, 0);
  4779. }
  4780. if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
  4781. my $format = $6;
  4782. my $count = $format =~ tr@%@%@;
  4783. if ($count == 1 &&
  4784. $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
  4785. WARN("SSCANF_TO_KSTRTO",
  4786. "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
  4787. }
  4788. }
  4789. }
  4790. # check for new externs in .h files.
  4791. if ($realfile =~ /\.h$/ &&
  4792. $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
  4793. if (CHK("AVOID_EXTERNS",
  4794. "extern prototypes should be avoided in .h files\n" . $herecurr) &&
  4795. $fix) {
  4796. $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
  4797. }
  4798. }
  4799. # check for new externs in .c files.
  4800. if ($realfile =~ /\.c$/ && defined $stat &&
  4801. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  4802. {
  4803. my $function_name = $1;
  4804. my $paren_space = $2;
  4805. my $s = $stat;
  4806. if (defined $cond) {
  4807. substr($s, 0, length($cond), '');
  4808. }
  4809. if ($s =~ /^\s*;/ &&
  4810. $function_name ne 'uninitialized_var')
  4811. {
  4812. WARN("AVOID_EXTERNS",
  4813. "externs should be avoided in .c files\n" . $herecurr);
  4814. }
  4815. if ($paren_space =~ /\n/) {
  4816. WARN("FUNCTION_ARGUMENTS",
  4817. "arguments for function declarations should follow identifier\n" . $herecurr);
  4818. }
  4819. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  4820. $stat =~ /^.\s*extern\s+/)
  4821. {
  4822. WARN("AVOID_EXTERNS",
  4823. "externs should be avoided in .c files\n" . $herecurr);
  4824. }
  4825. # checks for new __setup's
  4826. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  4827. my $name = $1;
  4828. if (!grep(/$name/, @setup_docs)) {
  4829. CHK("UNDOCUMENTED_SETUP",
  4830. "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  4831. }
  4832. }
  4833. # check for pointless casting of kmalloc return
  4834. if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
  4835. WARN("UNNECESSARY_CASTS",
  4836. "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  4837. }
  4838. # alloc style
  4839. # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
  4840. if ($^V && $^V ge 5.10.0 &&
  4841. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
  4842. CHK("ALLOC_SIZEOF_STRUCT",
  4843. "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
  4844. }
  4845. # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
  4846. if ($^V && $^V ge 5.10.0 &&
  4847. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
  4848. my $oldfunc = $3;
  4849. my $a1 = $4;
  4850. my $a2 = $10;
  4851. my $newfunc = "kmalloc_array";
  4852. $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
  4853. my $r1 = $a1;
  4854. my $r2 = $a2;
  4855. if ($a1 =~ /^sizeof\s*\S/) {
  4856. $r1 = $a2;
  4857. $r2 = $a1;
  4858. }
  4859. if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
  4860. !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
  4861. if (WARN("ALLOC_WITH_MULTIPLY",
  4862. "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
  4863. $fix) {
  4864. $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
  4865. }
  4866. }
  4867. }
  4868. # check for krealloc arg reuse
  4869. if ($^V && $^V ge 5.10.0 &&
  4870. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
  4871. WARN("KREALLOC_ARG_REUSE",
  4872. "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
  4873. }
  4874. # check for alloc argument mismatch
  4875. if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
  4876. WARN("ALLOC_ARRAY_ARGS",
  4877. "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
  4878. }
  4879. # check for multiple semicolons
  4880. if ($line =~ /;\s*;\s*$/) {
  4881. if (WARN("ONE_SEMICOLON",
  4882. "Statements terminations use 1 semicolon\n" . $herecurr) &&
  4883. $fix) {
  4884. $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
  4885. }
  4886. }
  4887. # check for #defines like: 1 << <digit> that could be BIT(digit)
  4888. if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
  4889. my $ull = "";
  4890. $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
  4891. if (CHK("BIT_MACRO",
  4892. "Prefer using the BIT$ull macro\n" . $herecurr) &&
  4893. $fix) {
  4894. $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
  4895. }
  4896. }
  4897. # check for case / default statements not preceded by break/fallthrough/switch
  4898. if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
  4899. my $has_break = 0;
  4900. my $has_statement = 0;
  4901. my $count = 0;
  4902. my $prevline = $linenr;
  4903. while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
  4904. $prevline--;
  4905. my $rline = $rawlines[$prevline - 1];
  4906. my $fline = $lines[$prevline - 1];
  4907. last if ($fline =~ /^\@\@/);
  4908. next if ($fline =~ /^\-/);
  4909. next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
  4910. $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
  4911. next if ($fline =~ /^.[\s$;]*$/);
  4912. $has_statement = 1;
  4913. $count++;
  4914. $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
  4915. }
  4916. if (!$has_break && $has_statement) {
  4917. WARN("MISSING_BREAK",
  4918. "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
  4919. }
  4920. }
  4921. # check for switch/default statements without a break;
  4922. if ($^V && $^V ge 5.10.0 &&
  4923. defined $stat &&
  4924. $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
  4925. my $ctx = '';
  4926. my $herectx = $here . "\n";
  4927. my $cnt = statement_rawlines($stat);
  4928. for (my $n = 0; $n < $cnt; $n++) {
  4929. $herectx .= raw_line($linenr, $n) . "\n";
  4930. }
  4931. WARN("DEFAULT_NO_BREAK",
  4932. "switch default: should use break\n" . $herectx);
  4933. }
  4934. # check for gcc specific __FUNCTION__
  4935. if ($line =~ /\b__FUNCTION__\b/) {
  4936. if (WARN("USE_FUNC",
  4937. "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
  4938. $fix) {
  4939. $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
  4940. }
  4941. }
  4942. # check for uses of __DATE__, __TIME__, __TIMESTAMP__
  4943. while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
  4944. ERROR("DATE_TIME",
  4945. "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
  4946. }
  4947. # check for use of yield()
  4948. if ($line =~ /\byield\s*\(\s*\)/) {
  4949. WARN("YIELD",
  4950. "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
  4951. }
  4952. # check for comparisons against true and false
  4953. if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
  4954. my $lead = $1;
  4955. my $arg = $2;
  4956. my $test = $3;
  4957. my $otype = $4;
  4958. my $trail = $5;
  4959. my $op = "!";
  4960. ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
  4961. my $type = lc($otype);
  4962. if ($type =~ /^(?:true|false)$/) {
  4963. if (("$test" eq "==" && "$type" eq "true") ||
  4964. ("$test" eq "!=" && "$type" eq "false")) {
  4965. $op = "";
  4966. }
  4967. CHK("BOOL_COMPARISON",
  4968. "Using comparison to $otype is error prone\n" . $herecurr);
  4969. ## maybe suggesting a correct construct would better
  4970. ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
  4971. }
  4972. }
  4973. # check for semaphores initialized locked
  4974. if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
  4975. WARN("CONSIDER_COMPLETION",
  4976. "consider using a completion\n" . $herecurr);
  4977. }
  4978. # recommend kstrto* over simple_strto* and strict_strto*
  4979. if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
  4980. WARN("CONSIDER_KSTRTO",
  4981. "$1 is obsolete, use k$3 instead\n" . $herecurr);
  4982. }
  4983. # check for __initcall(), use device_initcall() explicitly or more appropriate function please
  4984. if ($line =~ /^.\s*__initcall\s*\(/) {
  4985. WARN("USE_DEVICE_INITCALL",
  4986. "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
  4987. }
  4988. # check for various structs that are normally const (ops, kgdb, device_tree)
  4989. my $const_structs = qr{
  4990. acpi_dock_ops|
  4991. address_space_operations|
  4992. backlight_ops|
  4993. block_device_operations|
  4994. dentry_operations|
  4995. dev_pm_ops|
  4996. dma_map_ops|
  4997. extent_io_ops|
  4998. file_lock_operations|
  4999. file_operations|
  5000. hv_ops|
  5001. ide_dma_ops|
  5002. intel_dvo_dev_ops|
  5003. item_operations|
  5004. iwl_ops|
  5005. kgdb_arch|
  5006. kgdb_io|
  5007. kset_uevent_ops|
  5008. lock_manager_operations|
  5009. microcode_ops|
  5010. mtrr_ops|
  5011. neigh_ops|
  5012. nlmsvc_binding|
  5013. of_device_id|
  5014. pci_raw_ops|
  5015. pipe_buf_operations|
  5016. platform_hibernation_ops|
  5017. platform_suspend_ops|
  5018. proto_ops|
  5019. rpc_pipe_ops|
  5020. seq_operations|
  5021. snd_ac97_build_ops|
  5022. soc_pcmcia_socket_ops|
  5023. stacktrace_ops|
  5024. sysfs_ops|
  5025. tty_operations|
  5026. uart_ops|
  5027. usb_mon_operations|
  5028. wd_ops}x;
  5029. if ($line !~ /\bconst\b/ &&
  5030. $line =~ /\bstruct\s+($const_structs)\b/) {
  5031. WARN("CONST_STRUCT",
  5032. "struct $1 should normally be const\n" .
  5033. $herecurr);
  5034. }
  5035. # use of NR_CPUS is usually wrong
  5036. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  5037. if ($line =~ /\bNR_CPUS\b/ &&
  5038. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  5039. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  5040. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  5041. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  5042. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  5043. {
  5044. WARN("NR_CPUS",
  5045. "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  5046. }
  5047. # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
  5048. if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
  5049. ERROR("DEFINE_ARCH_HAS",
  5050. "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
  5051. }
  5052. # likely/unlikely comparisons similar to "(likely(foo) > 0)"
  5053. if ($^V && $^V ge 5.10.0 &&
  5054. $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
  5055. WARN("LIKELY_MISUSE",
  5056. "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
  5057. }
  5058. # whine mightly about in_atomic
  5059. if ($line =~ /\bin_atomic\s*\(/) {
  5060. if ($realfile =~ m@^drivers/@) {
  5061. ERROR("IN_ATOMIC",
  5062. "do not use in_atomic in drivers\n" . $herecurr);
  5063. } elsif ($realfile !~ m@^kernel/@) {
  5064. WARN("IN_ATOMIC",
  5065. "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  5066. }
  5067. }
  5068. # check for lockdep_set_novalidate_class
  5069. if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
  5070. $line =~ /__lockdep_no_validate__\s*\)/ ) {
  5071. if ($realfile !~ m@^kernel/lockdep@ &&
  5072. $realfile !~ m@^include/linux/lockdep@ &&
  5073. $realfile !~ m@^drivers/base/core@) {
  5074. ERROR("LOCKDEP",
  5075. "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
  5076. }
  5077. }
  5078. if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
  5079. $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
  5080. WARN("EXPORTED_WORLD_WRITABLE",
  5081. "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
  5082. }
  5083. # Mode permission misuses where it seems decimal should be octal
  5084. # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
  5085. if ($^V && $^V ge 5.10.0 &&
  5086. $line =~ /$mode_perms_search/) {
  5087. foreach my $entry (@mode_permission_funcs) {
  5088. my $func = $entry->[0];
  5089. my $arg_pos = $entry->[1];
  5090. my $skip_args = "";
  5091. if ($arg_pos > 1) {
  5092. $arg_pos--;
  5093. $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
  5094. }
  5095. my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
  5096. if ($line =~ /$test/) {
  5097. my $val = $1;
  5098. $val = $6 if ($skip_args ne "");
  5099. if ($val !~ /^0$/ &&
  5100. (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
  5101. length($val) ne 4)) {
  5102. ERROR("NON_OCTAL_PERMISSIONS",
  5103. "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
  5104. } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
  5105. ERROR("EXPORTED_WORLD_WRITABLE",
  5106. "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
  5107. }
  5108. }
  5109. }
  5110. }
  5111. # validate content of MODULE_LICENSE against list from include/linux/module.h
  5112. if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
  5113. my $extracted_string = get_quoted_string($line, $rawline);
  5114. my $valid_licenses = qr{
  5115. GPL|
  5116. GPL\ v2|
  5117. GPL\ and\ additional\ rights|
  5118. Dual\ BSD/GPL|
  5119. Dual\ MIT/GPL|
  5120. Dual\ MPL/GPL|
  5121. Proprietary
  5122. }x;
  5123. if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
  5124. WARN("MODULE_LICENSE",
  5125. "unknown module license " . $extracted_string . "\n" . $herecurr);
  5126. }
  5127. }
  5128. }
  5129. # If we have no input at all, then there is nothing to report on
  5130. # so just keep quiet.
  5131. if ($#rawlines == -1) {
  5132. exit(0);
  5133. }
  5134. # In mailback mode only produce a report in the negative, for
  5135. # things that appear to be patches.
  5136. if ($mailback && ($clean == 1 || !$is_patch)) {
  5137. exit(0);
  5138. }
  5139. # This is not a patch, and we are are in 'no-patch' mode so
  5140. # just keep quiet.
  5141. if (!$chk_patch && !$is_patch) {
  5142. exit(0);
  5143. }
  5144. if (!$is_patch && $file !~ /cover-letter\.patch$/) {
  5145. ERROR("NOT_UNIFIED_DIFF",
  5146. "Does not appear to be a unified-diff format patch\n");
  5147. }
  5148. if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) {
  5149. ERROR("MISSING_SIGN_OFF",
  5150. "Missing Signed-off-by: line(s)\n");
  5151. }
  5152. print report_dump();
  5153. if ($summary && !($clean == 1 && $quiet == 1)) {
  5154. print "$filename " if ($summary_file);
  5155. print "total: $cnt_error errors, $cnt_warn warnings, " .
  5156. (($check)? "$cnt_chk checks, " : "") .
  5157. "$cnt_lines lines checked\n";
  5158. }
  5159. if ($quiet == 0) {
  5160. # If there were whitespace errors which cleanpatch can fix
  5161. # then suggest that.
  5162. if ($rpt_cleaners) {
  5163. $rpt_cleaners = 0;
  5164. print << "EOM"
  5165. NOTE: Whitespace errors detected.
  5166. You may wish to use scripts/cleanpatch or scripts/cleanfile
  5167. EOM
  5168. }
  5169. }
  5170. if ($clean == 0 && $fix &&
  5171. ("@rawlines" ne "@fixed" ||
  5172. $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
  5173. my $newfile = $filename;
  5174. $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
  5175. my $linecount = 0;
  5176. my $f;
  5177. @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
  5178. open($f, '>', $newfile)
  5179. or die "$P: Can't open $newfile for write\n";
  5180. foreach my $fixed_line (@fixed) {
  5181. $linecount++;
  5182. if ($file) {
  5183. if ($linecount > 3) {
  5184. $fixed_line =~ s/^\+//;
  5185. print $f $fixed_line . "\n";
  5186. }
  5187. } else {
  5188. print $f $fixed_line . "\n";
  5189. }
  5190. }
  5191. close($f);
  5192. if (!$quiet) {
  5193. print << "EOM";
  5194. Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
  5195. Do _NOT_ trust the results written to this file.
  5196. Do _NOT_ submit these changes without inspecting them for correctness.
  5197. This EXPERIMENTAL file is simply a convenience to help rewrite patches.
  5198. No warranties, expressed or implied...
  5199. EOM
  5200. }
  5201. }
  5202. if ($quiet == 0) {
  5203. print "\n";
  5204. if ($clean == 1) {
  5205. print "$vname has no obvious style problems and is ready for submission.\n";
  5206. } else {
  5207. print "$vname has style problems, please review.\n";
  5208. }
  5209. }
  5210. return $clean;
  5211. }