checkpatch.pl 228 KB

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