checkpatch.pl 199 KB

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