checkpatch.pl 195 KB

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