checkpatch.pl 195 KB

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