checkpatch.pl 234 KB

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