1
0

checkpatch.pl 203 KB

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