checkpatch.pl 228 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677
  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 Kconfig for all CONFIG symbols
  2222. if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_\w*)\b/) {
  2223. ERROR("DEFINE_CONFIG_SYM",
  2224. "All CONFIG symbols 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. # Avoid using the pre-schema driver model tags
  2265. if ($line =~ /^\+.*u-boot,dm-.*/) {
  2266. ERROR("PRE_SCHEMA",
  2267. "Driver model schema uses 'bootph-...' tags now\n" . $herecurr);
  2268. }
  2269. }
  2270. sub exclude_global_initialisers {
  2271. my ($realfile) = @_;
  2272. # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
  2273. return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
  2274. $realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
  2275. $realfile =~ m@/bpf/.*\.bpf\.c$@;
  2276. }
  2277. sub process {
  2278. my $filename = shift;
  2279. my $linenr=0;
  2280. my $prevline="";
  2281. my $prevrawline="";
  2282. my $stashline="";
  2283. my $stashrawline="";
  2284. my $length;
  2285. my $indent;
  2286. my $previndent=0;
  2287. my $stashindent=0;
  2288. our $clean = 1;
  2289. my $signoff = 0;
  2290. my $author = '';
  2291. my $authorsignoff = 0;
  2292. my $author_sob = '';
  2293. my $is_patch = 0;
  2294. my $is_binding_patch = -1;
  2295. my $in_header_lines = $file ? 0 : 1;
  2296. my $in_commit_log = 0; #Scanning lines before patch
  2297. my $has_patch_separator = 0; #Found a --- line
  2298. my $has_commit_log = 0; #Encountered lines before patch
  2299. my $commit_log_lines = 0; #Number of commit log lines
  2300. my $commit_log_possible_stack_dump = 0;
  2301. my $commit_log_long_line = 0;
  2302. my $commit_log_has_diff = 0;
  2303. my $reported_maintainer_file = 0;
  2304. my $non_utf8_charset = 0;
  2305. my $last_git_commit_id_linenr = -1;
  2306. my $last_blank_line = 0;
  2307. my $last_coalesced_string_linenr = -1;
  2308. our @report = ();
  2309. our $cnt_lines = 0;
  2310. our $cnt_error = 0;
  2311. our $cnt_warn = 0;
  2312. our $cnt_chk = 0;
  2313. # Trace the real file/line as we go.
  2314. my $realfile = '';
  2315. my $realline = 0;
  2316. my $realcnt = 0;
  2317. my $here = '';
  2318. my $context_function; #undef'd unless there's a known function
  2319. my $in_comment = 0;
  2320. my $comment_edge = 0;
  2321. my $first_line = 0;
  2322. my $p1_prefix = '';
  2323. my $prev_values = 'E';
  2324. # suppression flags
  2325. my %suppress_ifbraces;
  2326. my %suppress_whiletrailers;
  2327. my %suppress_export;
  2328. my $suppress_statement = 0;
  2329. my %signatures = ();
  2330. # Pre-scan the patch sanitizing the lines.
  2331. # Pre-scan the patch looking for any __setup documentation.
  2332. #
  2333. my @setup_docs = ();
  2334. my $setup_docs = 0;
  2335. my $camelcase_file_seeded = 0;
  2336. my $checklicenseline = 1;
  2337. sanitise_line_reset();
  2338. my $line;
  2339. foreach my $rawline (@rawlines) {
  2340. $linenr++;
  2341. $line = $rawline;
  2342. push(@fixed, $rawline) if ($fix);
  2343. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  2344. $setup_docs = 0;
  2345. if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
  2346. $setup_docs = 1;
  2347. }
  2348. #next;
  2349. }
  2350. if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  2351. $realline=$1-1;
  2352. if (defined $2) {
  2353. $realcnt=$3+1;
  2354. } else {
  2355. $realcnt=1+1;
  2356. }
  2357. $in_comment = 0;
  2358. # Guestimate if this is a continuing comment. Run
  2359. # the context looking for a comment "edge". If this
  2360. # edge is a close comment then we must be in a comment
  2361. # at context start.
  2362. my $edge;
  2363. my $cnt = $realcnt;
  2364. for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
  2365. next if (defined $rawlines[$ln - 1] &&
  2366. $rawlines[$ln - 1] =~ /^-/);
  2367. $cnt--;
  2368. #print "RAW<$rawlines[$ln - 1]>\n";
  2369. last if (!defined $rawlines[$ln - 1]);
  2370. if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
  2371. $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
  2372. ($edge) = $1;
  2373. last;
  2374. }
  2375. }
  2376. if (defined $edge && $edge eq '*/') {
  2377. $in_comment = 1;
  2378. }
  2379. # Guestimate if this is a continuing comment. If this
  2380. # is the start of a diff block and this line starts
  2381. # ' *' then it is very likely a comment.
  2382. if (!defined $edge &&
  2383. $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
  2384. {
  2385. $in_comment = 1;
  2386. }
  2387. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  2388. sanitise_line_reset($in_comment);
  2389. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  2390. # Standardise the strings and chars within the input to
  2391. # simplify matching -- only bother with positive lines.
  2392. $line = sanitise_line($rawline);
  2393. }
  2394. push(@lines, $line);
  2395. if ($realcnt > 1) {
  2396. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  2397. } else {
  2398. $realcnt = 0;
  2399. }
  2400. #print "==>$rawline\n";
  2401. #print "-->$line\n";
  2402. if ($setup_docs && $line =~ /^\+/) {
  2403. push(@setup_docs, $line);
  2404. }
  2405. }
  2406. $prefix = '';
  2407. $realcnt = 0;
  2408. $linenr = 0;
  2409. $fixlinenr = -1;
  2410. foreach my $line (@lines) {
  2411. $linenr++;
  2412. $fixlinenr++;
  2413. my $sline = $line; #copy of $line
  2414. $sline =~ s/$;/ /g; #with comments as spaces
  2415. my $rawline = $rawlines[$linenr - 1];
  2416. my $raw_comment = get_raw_comment($line, $rawline);
  2417. # check if it's a mode change, rename or start of a patch
  2418. if (!$in_commit_log &&
  2419. ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
  2420. ($line =~ /^rename (?:from|to) \S+\s*$/ ||
  2421. $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
  2422. $is_patch = 1;
  2423. }
  2424. #extract the line range in the file after the patch is applied
  2425. if (!$in_commit_log &&
  2426. $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
  2427. my $context = $4;
  2428. $is_patch = 1;
  2429. $first_line = $linenr + 1;
  2430. $realline=$1-1;
  2431. if (defined $2) {
  2432. $realcnt=$3+1;
  2433. } else {
  2434. $realcnt=1+1;
  2435. }
  2436. annotate_reset();
  2437. $prev_values = 'E';
  2438. %suppress_ifbraces = ();
  2439. %suppress_whiletrailers = ();
  2440. %suppress_export = ();
  2441. $suppress_statement = 0;
  2442. if ($context =~ /\b(\w+)\s*\(/) {
  2443. $context_function = $1;
  2444. } else {
  2445. undef $context_function;
  2446. }
  2447. next;
  2448. # track the line number as we move through the hunk, note that
  2449. # new versions of GNU diff omit the leading space on completely
  2450. # blank context lines so we need to count that too.
  2451. } elsif ($line =~ /^( |\+|$)/) {
  2452. $realline++;
  2453. $realcnt-- if ($realcnt != 0);
  2454. # Measure the line length and indent.
  2455. ($length, $indent) = line_stats($rawline);
  2456. # Track the previous line.
  2457. ($prevline, $stashline) = ($stashline, $line);
  2458. ($previndent, $stashindent) = ($stashindent, $indent);
  2459. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  2460. #warn "line<$line>\n";
  2461. } elsif ($realcnt == 1) {
  2462. $realcnt--;
  2463. }
  2464. my $hunk_line = ($realcnt != 0);
  2465. $here = "#$linenr: " if (!$file);
  2466. $here = "#$realline: " if ($file);
  2467. my $found_file = 0;
  2468. # extract the filename as it passes
  2469. if ($line =~ /^diff --git.*?(\S+)$/) {
  2470. $realfile = $1;
  2471. $realfile =~ s@^([^/]*)/@@ if (!$file);
  2472. $in_commit_log = 0;
  2473. $found_file = 1;
  2474. } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
  2475. $realfile = $1;
  2476. $realfile =~ s@^([^/]*)/@@ if (!$file);
  2477. $in_commit_log = 0;
  2478. $p1_prefix = $1;
  2479. if (!$file && $tree && $p1_prefix ne '' &&
  2480. -e "$root/$p1_prefix") {
  2481. WARN("PATCH_PREFIX",
  2482. "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
  2483. }
  2484. if ($realfile =~ m@^include/asm/@) {
  2485. ERROR("MODIFIED_INCLUDE_ASM",
  2486. "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  2487. }
  2488. $found_file = 1;
  2489. }
  2490. #make up the handle for any error we report on this line
  2491. if ($showfile) {
  2492. $prefix = "$realfile:$realline: "
  2493. } elsif ($emacs) {
  2494. if ($file) {
  2495. $prefix = "$filename:$realline: ";
  2496. } else {
  2497. $prefix = "$filename:$linenr: ";
  2498. }
  2499. }
  2500. if ($found_file) {
  2501. if (is_maintained_obsolete($realfile)) {
  2502. WARN("OBSOLETE",
  2503. "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
  2504. }
  2505. if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
  2506. $check = 1;
  2507. } else {
  2508. $check = $check_orig;
  2509. }
  2510. $checklicenseline = 1;
  2511. if ($realfile !~ /^MAINTAINERS/) {
  2512. my $last_binding_patch = $is_binding_patch;
  2513. $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
  2514. if (($last_binding_patch != -1) &&
  2515. ($last_binding_patch ^ $is_binding_patch)) {
  2516. WARN("DT_SPLIT_BINDING_PATCH",
  2517. "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
  2518. }
  2519. }
  2520. next;
  2521. }
  2522. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  2523. my $hereline = "$here\n$rawline\n";
  2524. my $herecurr = "$here\n$rawline\n";
  2525. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  2526. $cnt_lines++ if ($realcnt != 0);
  2527. # Verify the existence of a commit log if appropriate
  2528. # 2 is used because a $signature is counted in $commit_log_lines
  2529. if ($in_commit_log) {
  2530. if ($line !~ /^\s*$/) {
  2531. $commit_log_lines++; #could be a $signature
  2532. }
  2533. } elsif ($has_commit_log && $commit_log_lines < 2) {
  2534. WARN("COMMIT_MESSAGE",
  2535. "Missing commit description - Add an appropriate one\n");
  2536. $commit_log_lines = 2; #warn only once
  2537. }
  2538. # Check if the commit log has what seems like a diff which can confuse patch
  2539. if ($in_commit_log && !$commit_log_has_diff &&
  2540. (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
  2541. $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
  2542. $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
  2543. $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
  2544. ERROR("DIFF_IN_COMMIT_MSG",
  2545. "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
  2546. $commit_log_has_diff = 1;
  2547. }
  2548. # Check for incorrect file permissions
  2549. if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
  2550. my $permhere = $here . "FILE: $realfile\n";
  2551. if ($realfile !~ m@scripts/@ &&
  2552. $realfile !~ /\.(py|pl|awk|sh)$/) {
  2553. ERROR("EXECUTE_PERMISSIONS",
  2554. "do not set execute permissions for source files\n" . $permhere);
  2555. }
  2556. }
  2557. # Check the patch for a From:
  2558. if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
  2559. $author = $1;
  2560. my $curline = $linenr;
  2561. while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
  2562. $author .= $1;
  2563. }
  2564. $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
  2565. $author =~ s/"//g;
  2566. $author = reformat_email($author);
  2567. }
  2568. # Check the patch for a signoff:
  2569. if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
  2570. $signoff++;
  2571. $in_commit_log = 0;
  2572. if ($author ne '' && $authorsignoff != 1) {
  2573. if (same_email_addresses($1, $author)) {
  2574. $authorsignoff = 1;
  2575. } else {
  2576. my $ctx = $1;
  2577. my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
  2578. my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
  2579. if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
  2580. $author_sob = $ctx;
  2581. $authorsignoff = 2;
  2582. } elsif (lc $email_address eq lc $author_address) {
  2583. $author_sob = $ctx;
  2584. $authorsignoff = 3;
  2585. } elsif ($email_name eq $author_name) {
  2586. $author_sob = $ctx;
  2587. $authorsignoff = 4;
  2588. my $address1 = $email_address;
  2589. my $address2 = $author_address;
  2590. if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
  2591. $address1 = "$1$2";
  2592. }
  2593. if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
  2594. $address2 = "$1$2";
  2595. }
  2596. if ($address1 eq $address2) {
  2597. $authorsignoff = 5;
  2598. }
  2599. }
  2600. }
  2601. }
  2602. }
  2603. # Check for patch separator
  2604. if ($line =~ /^---$/) {
  2605. $has_patch_separator = 1;
  2606. $in_commit_log = 0;
  2607. }
  2608. # Check if MAINTAINERS is being updated. If so, there's probably no need to
  2609. # emit the "does MAINTAINERS need updating?" message on file add/move/delete
  2610. if ($line =~ /^\s*MAINTAINERS\s*\|/) {
  2611. $reported_maintainer_file = 1;
  2612. }
  2613. # Check signature styles
  2614. if (!$in_header_lines &&
  2615. $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
  2616. my $space_before = $1;
  2617. my $sign_off = $2;
  2618. my $space_after = $3;
  2619. my $email = $4;
  2620. my $ucfirst_sign_off = ucfirst(lc($sign_off));
  2621. if ($sign_off !~ /$signature_tags/) {
  2622. my $suggested_signature = find_standard_signature($sign_off);
  2623. if ($suggested_signature eq "") {
  2624. WARN("BAD_SIGN_OFF",
  2625. "Non-standard signature: $sign_off\n" . $herecurr);
  2626. } else {
  2627. if (WARN("BAD_SIGN_OFF",
  2628. "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
  2629. $fix) {
  2630. $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
  2631. }
  2632. }
  2633. }
  2634. if (defined $space_before && $space_before ne "") {
  2635. if (WARN("BAD_SIGN_OFF",
  2636. "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
  2637. $fix) {
  2638. $fixed[$fixlinenr] =
  2639. "$ucfirst_sign_off $email";
  2640. }
  2641. }
  2642. if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
  2643. if (WARN("BAD_SIGN_OFF",
  2644. "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
  2645. $fix) {
  2646. $fixed[$fixlinenr] =
  2647. "$ucfirst_sign_off $email";
  2648. }
  2649. }
  2650. if (!defined $space_after || $space_after ne " ") {
  2651. if (WARN("BAD_SIGN_OFF",
  2652. "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
  2653. $fix) {
  2654. $fixed[$fixlinenr] =
  2655. "$ucfirst_sign_off $email";
  2656. }
  2657. }
  2658. my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
  2659. my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
  2660. if ($suggested_email eq "") {
  2661. ERROR("BAD_SIGN_OFF",
  2662. "Unrecognized email address: '$email'\n" . $herecurr);
  2663. } else {
  2664. my $dequoted = $suggested_email;
  2665. $dequoted =~ s/^"//;
  2666. $dequoted =~ s/" </ </;
  2667. # Don't force email to have quotes
  2668. # Allow just an angle bracketed address
  2669. if (!same_email_addresses($email, $suggested_email)) {
  2670. if (WARN("BAD_SIGN_OFF",
  2671. "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
  2672. $fix) {
  2673. $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
  2674. }
  2675. }
  2676. # Address part shouldn't have comments
  2677. my $stripped_address = $email_address;
  2678. $stripped_address =~ s/\([^\(\)]*\)//g;
  2679. if ($email_address ne $stripped_address) {
  2680. if (WARN("BAD_SIGN_OFF",
  2681. "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
  2682. $fix) {
  2683. $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
  2684. }
  2685. }
  2686. # Only one name comment should be allowed
  2687. my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
  2688. if ($comment_count > 1) {
  2689. WARN("BAD_SIGN_OFF",
  2690. "Use a single name comment in email: '$email'\n" . $herecurr);
  2691. }
  2692. # stable@vger.kernel.org or stable@kernel.org shouldn't
  2693. # have an email name. In addition comments should strictly
  2694. # begin with a #
  2695. if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
  2696. if (($comment ne "" && $comment !~ /^#.+/) ||
  2697. ($email_name ne "")) {
  2698. my $cur_name = $email_name;
  2699. my $new_comment = $comment;
  2700. $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
  2701. # Remove brackets enclosing comment text
  2702. # and # from start of comments to get comment text
  2703. $new_comment =~ s/^\((.*)\)$/$1/;
  2704. $new_comment =~ s/^\[(.*)\]$/$1/;
  2705. $new_comment =~ s/^[\s\#]+|\s+$//g;
  2706. $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
  2707. $new_comment = " # $new_comment" if ($new_comment ne "");
  2708. my $new_email = "$email_address$new_comment";
  2709. if (WARN("BAD_STABLE_ADDRESS_STYLE",
  2710. "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
  2711. $fix) {
  2712. $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
  2713. }
  2714. }
  2715. } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
  2716. my $new_comment = $comment;
  2717. # Extract comment text from within brackets or
  2718. # c89 style /*...*/ comments
  2719. $new_comment =~ s/^\[(.*)\]$/$1/;
  2720. $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
  2721. $new_comment = trim($new_comment);
  2722. $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
  2723. $new_comment = "($new_comment)" if ($new_comment ne "");
  2724. my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
  2725. if (WARN("BAD_SIGN_OFF",
  2726. "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
  2727. $fix) {
  2728. $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
  2729. }
  2730. }
  2731. }
  2732. # Check for duplicate signatures
  2733. my $sig_nospace = $line;
  2734. $sig_nospace =~ s/\s//g;
  2735. $sig_nospace = lc($sig_nospace);
  2736. if (defined $signatures{$sig_nospace}) {
  2737. WARN("BAD_SIGN_OFF",
  2738. "Duplicate signature\n" . $herecurr);
  2739. } else {
  2740. $signatures{$sig_nospace} = 1;
  2741. }
  2742. # Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
  2743. if ($sign_off =~ /^co-developed-by:$/i) {
  2744. if ($email eq $author) {
  2745. WARN("BAD_SIGN_OFF",
  2746. "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
  2747. }
  2748. if (!defined $lines[$linenr]) {
  2749. WARN("BAD_SIGN_OFF",
  2750. "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
  2751. } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
  2752. WARN("BAD_SIGN_OFF",
  2753. "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
  2754. } elsif ($1 ne $email) {
  2755. WARN("BAD_SIGN_OFF",
  2756. "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
  2757. }
  2758. }
  2759. }
  2760. # Check email subject for common tools that don't need to be mentioned
  2761. if ($in_header_lines &&
  2762. $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
  2763. WARN("EMAIL_SUBJECT",
  2764. "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
  2765. }
  2766. # Check for Gerrit Change-Ids not in any patch context
  2767. if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
  2768. if (ERROR("GERRIT_CHANGE_ID",
  2769. "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
  2770. $fix) {
  2771. fix_delete_line($fixlinenr, $rawline);
  2772. }
  2773. }
  2774. # Check if the commit log is in a possible stack dump
  2775. if ($in_commit_log && !$commit_log_possible_stack_dump &&
  2776. ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
  2777. $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
  2778. # timestamp
  2779. $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
  2780. $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
  2781. $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
  2782. # stack dump address styles
  2783. $commit_log_possible_stack_dump = 1;
  2784. }
  2785. # Check for line lengths > 75 in commit log, warn once
  2786. if ($in_commit_log && !$commit_log_long_line &&
  2787. length($line) > 75 &&
  2788. !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
  2789. # file delta changes
  2790. $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
  2791. # filename then :
  2792. $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
  2793. # A Fixes: or Link: line or signature tag line
  2794. $commit_log_possible_stack_dump)) {
  2795. WARN("COMMIT_LOG_LONG_LINE",
  2796. "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
  2797. $commit_log_long_line = 1;
  2798. }
  2799. # Reset possible stack dump if a blank line is found
  2800. if ($in_commit_log && $commit_log_possible_stack_dump &&
  2801. $line =~ /^\s*$/) {
  2802. $commit_log_possible_stack_dump = 0;
  2803. }
  2804. # Check for lines starting with a #
  2805. if ($in_commit_log && $line =~ /^#/) {
  2806. if (WARN("COMMIT_COMMENT_SYMBOL",
  2807. "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
  2808. $fix) {
  2809. $fixed[$fixlinenr] =~ s/^/ /;
  2810. }
  2811. }
  2812. # Check for git id commit length and improperly formed commit descriptions
  2813. # A correctly formed commit description is:
  2814. # commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
  2815. # with the commit subject '("' prefix and '")' suffix
  2816. # This is a fairly compilicated block as it tests for what appears to be
  2817. # bare SHA-1 hash with minimum length of 5. It also avoids several types of
  2818. # possible SHA-1 matches.
  2819. # A commit match can span multiple lines so this block attempts to find a
  2820. # complete typical commit on a maximum of 3 lines
  2821. if ($perl_version_ok &&
  2822. $in_commit_log && !$commit_log_possible_stack_dump &&
  2823. $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
  2824. $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
  2825. (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
  2826. ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
  2827. ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
  2828. $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
  2829. $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
  2830. my $init_char = "c";
  2831. my $orig_commit = "";
  2832. my $short = 1;
  2833. my $long = 0;
  2834. my $case = 1;
  2835. my $space = 1;
  2836. my $id = '0123456789ab';
  2837. my $orig_desc = "commit description";
  2838. my $description = "";
  2839. my $herectx = $herecurr;
  2840. my $has_parens = 0;
  2841. my $has_quotes = 0;
  2842. my $input = $line;
  2843. if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
  2844. for (my $n = 0; $n < 2; $n++) {
  2845. if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
  2846. $orig_desc = $1;
  2847. $has_parens = 1;
  2848. # Always strip leading/trailing parens then double quotes if existing
  2849. $orig_desc = substr($orig_desc, 1, -1);
  2850. if ($orig_desc =~ /^".*"$/) {
  2851. $orig_desc = substr($orig_desc, 1, -1);
  2852. $has_quotes = 1;
  2853. }
  2854. last;
  2855. }
  2856. last if ($#lines < $linenr + $n);
  2857. $input .= " " . trim($rawlines[$linenr + $n]);
  2858. $herectx .= "$rawlines[$linenr + $n]\n";
  2859. }
  2860. $herectx = $herecurr if (!$has_parens);
  2861. }
  2862. if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
  2863. $init_char = $1;
  2864. $orig_commit = lc($2);
  2865. $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
  2866. $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
  2867. $space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
  2868. $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
  2869. } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
  2870. $orig_commit = lc($1);
  2871. }
  2872. ($id, $description) = git_commit_info($orig_commit,
  2873. $id, $orig_desc);
  2874. if (defined($id) &&
  2875. ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
  2876. $last_git_commit_id_linenr != $linenr - 1) {
  2877. ERROR("GIT_COMMIT_ID",
  2878. "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
  2879. }
  2880. #don't report the next line if this line ends in commit and the sha1 hash is the next line
  2881. $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
  2882. }
  2883. # Check for added, moved or deleted files
  2884. if (!$reported_maintainer_file && !$in_commit_log &&
  2885. ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
  2886. $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
  2887. ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
  2888. (defined($1) || defined($2))))) {
  2889. $is_patch = 1;
  2890. $reported_maintainer_file = 1;
  2891. WARN("FILE_PATH_CHANGES",
  2892. "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
  2893. }
  2894. # Check for adding new DT bindings not in schema format
  2895. if (!$in_commit_log &&
  2896. ($line =~ /^new file mode\s*\d+\s*$/) &&
  2897. ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
  2898. WARN("DT_SCHEMA_BINDING_PATCH",
  2899. "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
  2900. }
  2901. # Check for wrappage within a valid hunk of the file
  2902. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  2903. ERROR("CORRUPTED_PATCH",
  2904. "patch seems to be corrupt (line wrapped?)\n" .
  2905. $herecurr) if (!$emitted_corrupt++);
  2906. }
  2907. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  2908. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  2909. $rawline !~ m/^$UTF8*$/) {
  2910. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  2911. my $blank = copy_spacing($rawline);
  2912. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  2913. my $hereptr = "$hereline$ptr\n";
  2914. CHK("INVALID_UTF8",
  2915. "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  2916. }
  2917. # Check if it's the start of a commit log
  2918. # (not a header line and we haven't seen the patch filename)
  2919. if ($in_header_lines && $realfile =~ /^$/ &&
  2920. !($rawline =~ /^\s+(?:\S|$)/ ||
  2921. $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
  2922. $in_header_lines = 0;
  2923. $in_commit_log = 1;
  2924. $has_commit_log = 1;
  2925. }
  2926. # Check if there is UTF-8 in a commit log when a mail header has explicitly
  2927. # declined it, i.e defined some charset where it is missing.
  2928. if ($in_header_lines &&
  2929. $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
  2930. $1 !~ /utf-8/i) {
  2931. $non_utf8_charset = 1;
  2932. }
  2933. if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
  2934. $rawline =~ /$NON_ASCII_UTF8/) {
  2935. WARN("UTF8_BEFORE_PATCH",
  2936. "8-bit UTF-8 used in possible commit log\n" . $herecurr);
  2937. }
  2938. # Check for absolute kernel paths in commit message
  2939. if ($tree && $in_commit_log) {
  2940. while ($line =~ m{(?:^|\s)(/\S*)}g) {
  2941. my $file = $1;
  2942. if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
  2943. check_absolute_file($1, $herecurr)) {
  2944. #
  2945. } else {
  2946. check_absolute_file($file, $herecurr);
  2947. }
  2948. }
  2949. }
  2950. # Check for various typo / spelling mistakes
  2951. if (defined($misspellings) &&
  2952. ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
  2953. while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
  2954. my $typo = $1;
  2955. my $blank = copy_spacing($rawline);
  2956. my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
  2957. my $hereptr = "$hereline$ptr\n";
  2958. my $typo_fix = $spelling_fix{lc($typo)};
  2959. $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
  2960. $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
  2961. my $msg_level = \&WARN;
  2962. $msg_level = \&CHK if ($file);
  2963. if (&{$msg_level}("TYPO_SPELLING",
  2964. "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
  2965. $fix) {
  2966. $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
  2967. }
  2968. }
  2969. }
  2970. # check for invalid commit id
  2971. if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
  2972. my $id;
  2973. my $description;
  2974. ($id, $description) = git_commit_info($2, undef, undef);
  2975. if (!defined($id)) {
  2976. WARN("UNKNOWN_COMMIT_ID",
  2977. "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
  2978. }
  2979. }
  2980. # check for repeated words separated by a single space
  2981. # avoid false positive from list command eg, '-rw-r--r-- 1 root root'
  2982. if (($rawline =~ /^\+/ || $in_commit_log) &&
  2983. $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
  2984. pos($rawline) = 1 if (!$in_commit_log);
  2985. while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
  2986. my $first = $1;
  2987. my $second = $2;
  2988. my $start_pos = $-[1];
  2989. my $end_pos = $+[2];
  2990. if ($first =~ /(?:struct|union|enum)/) {
  2991. pos($rawline) += length($first) + length($second) + 1;
  2992. next;
  2993. }
  2994. next if (lc($first) ne lc($second));
  2995. next if ($first eq 'long');
  2996. # check for character before and after the word matches
  2997. my $start_char = '';
  2998. my $end_char = '';
  2999. $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
  3000. $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
  3001. next if ($start_char =~ /^\S$/);
  3002. next if (index(" \t.,;?!", $end_char) == -1);
  3003. # avoid repeating hex occurrences like 'ff ff fe 09 ...'
  3004. if ($first =~ /\b[0-9a-f]{2,}\b/i) {
  3005. next if (!exists($allow_repeated_words{lc($first)}));
  3006. }
  3007. if (WARN("REPEATED_WORD",
  3008. "Possible repeated word: '$first'\n" . $herecurr) &&
  3009. $fix) {
  3010. $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
  3011. }
  3012. }
  3013. # if it's a repeated word on consecutive lines in a comment block
  3014. if ($prevline =~ /$;+\s*$/ &&
  3015. $prevrawline =~ /($word_pattern)\s*$/) {
  3016. my $last_word = $1;
  3017. if ($rawline =~ /^\+\s*\*\s*$last_word /) {
  3018. if (WARN("REPEATED_WORD",
  3019. "Possible repeated word: '$last_word'\n" . $hereprev) &&
  3020. $fix) {
  3021. $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
  3022. }
  3023. }
  3024. }
  3025. }
  3026. # ignore non-hunk lines and lines being removed
  3027. next if (!$hunk_line || $line =~ /^-/);
  3028. #trailing whitespace
  3029. if ($line =~ /^\+.*\015/) {
  3030. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  3031. if (ERROR("DOS_LINE_ENDINGS",
  3032. "DOS line endings\n" . $herevet) &&
  3033. $fix) {
  3034. $fixed[$fixlinenr] =~ s/[\s\015]+$//;
  3035. }
  3036. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  3037. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  3038. if (ERROR("TRAILING_WHITESPACE",
  3039. "trailing whitespace\n" . $herevet) &&
  3040. $fix) {
  3041. $fixed[$fixlinenr] =~ s/\s+$//;
  3042. }
  3043. $rpt_cleaners = 1;
  3044. }
  3045. # Check for FSF mailing addresses.
  3046. if ($rawline =~ /\bwrite to the Free/i ||
  3047. $rawline =~ /\b675\s+Mass\s+Ave/i ||
  3048. $rawline =~ /\b59\s+Temple\s+Pl/i ||
  3049. $rawline =~ /\b51\s+Franklin\s+St/i) {
  3050. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  3051. my $msg_level = \&ERROR;
  3052. $msg_level = \&CHK if ($file);
  3053. &{$msg_level}("FSF_MAILING_ADDRESS",
  3054. "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)
  3055. }
  3056. # check for Kconfig help text having a real description
  3057. # Only applies when adding the entry originally, after that we do not have
  3058. # sufficient context to determine whether it is indeed long enough.
  3059. if ($realfile =~ /Kconfig/ &&
  3060. # 'choice' is usually the last thing on the line (though
  3061. # Kconfig supports named choices), so use a word boundary
  3062. # (\b) rather than a whitespace character (\s)
  3063. $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
  3064. my $length = 0;
  3065. my $cnt = $realcnt;
  3066. my $ln = $linenr + 1;
  3067. my $f;
  3068. my $is_start = 0;
  3069. my $is_end = 0;
  3070. for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
  3071. $f = $lines[$ln - 1];
  3072. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  3073. $is_end = $lines[$ln - 1] =~ /^\+/;
  3074. next if ($f =~ /^-/);
  3075. last if (!$file && $f =~ /^\@\@/);
  3076. if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
  3077. $is_start = 1;
  3078. } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
  3079. $length = -1;
  3080. }
  3081. $f =~ s/^.//;
  3082. $f =~ s/#.*//;
  3083. $f =~ s/^\s+//;
  3084. next if ($f =~ /^$/);
  3085. # This only checks context lines in the patch
  3086. # and so hopefully shouldn't trigger false
  3087. # positives, even though some of these are
  3088. # common words in help texts
  3089. if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
  3090. if|endif|menu|endmenu|source)\b/x) {
  3091. $is_end = 1;
  3092. last;
  3093. }
  3094. $length++;
  3095. }
  3096. if ($is_start && $is_end && $length < $min_conf_desc_length) {
  3097. WARN("CONFIG_DESCRIPTION",
  3098. "please write a paragraph that describes the config symbol fully\n" . $herecurr);
  3099. }
  3100. #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
  3101. }
  3102. # check MAINTAINERS entries
  3103. if ($realfile =~ /^MAINTAINERS$/) {
  3104. # check MAINTAINERS entries for the right form
  3105. if ($rawline =~ /^\+[A-Z]:/ &&
  3106. $rawline !~ /^\+[A-Z]:\t\S/) {
  3107. if (WARN("MAINTAINERS_STYLE",
  3108. "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
  3109. $fix) {
  3110. $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
  3111. }
  3112. }
  3113. # check MAINTAINERS entries for the right ordering too
  3114. my $preferred_order = 'MRLSWQBCPTFXNK';
  3115. if ($rawline =~ /^\+[A-Z]:/ &&
  3116. $prevrawline =~ /^[\+ ][A-Z]:/) {
  3117. $rawline =~ /^\+([A-Z]):\s*(.*)/;
  3118. my $cur = $1;
  3119. my $curval = $2;
  3120. $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
  3121. my $prev = $1;
  3122. my $prevval = $2;
  3123. my $curindex = index($preferred_order, $cur);
  3124. my $previndex = index($preferred_order, $prev);
  3125. if ($curindex < 0) {
  3126. WARN("MAINTAINERS_STYLE",
  3127. "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
  3128. } else {
  3129. if ($previndex >= 0 && $curindex < $previndex) {
  3130. WARN("MAINTAINERS_STYLE",
  3131. "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
  3132. } elsif ((($prev eq 'F' && $cur eq 'F') ||
  3133. ($prev eq 'X' && $cur eq 'X')) &&
  3134. ($prevval cmp $curval) > 0) {
  3135. WARN("MAINTAINERS_STYLE",
  3136. "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
  3137. }
  3138. }
  3139. }
  3140. }
  3141. if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
  3142. ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
  3143. my $flag = $1;
  3144. my $replacement = {
  3145. 'EXTRA_AFLAGS' => 'asflags-y',
  3146. 'EXTRA_CFLAGS' => 'ccflags-y',
  3147. 'EXTRA_CPPFLAGS' => 'cppflags-y',
  3148. 'EXTRA_LDFLAGS' => 'ldflags-y',
  3149. };
  3150. WARN("DEPRECATED_VARIABLE",
  3151. "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
  3152. }
  3153. # check for DT compatible documentation
  3154. if (defined $root &&
  3155. (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
  3156. ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
  3157. my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
  3158. my $dt_path = $root . "/Documentation/devicetree/bindings/";
  3159. my $vp_file = $dt_path . "vendor-prefixes.yaml";
  3160. foreach my $compat (@compats) {
  3161. my $compat2 = $compat;
  3162. $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
  3163. my $compat3 = $compat;
  3164. $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
  3165. `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
  3166. if ( $? >> 8 ) {
  3167. WARN("UNDOCUMENTED_DT_STRING",
  3168. "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
  3169. }
  3170. next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
  3171. my $vendor = $1;
  3172. `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
  3173. if ( $? >> 8 ) {
  3174. WARN("UNDOCUMENTED_DT_STRING",
  3175. "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
  3176. }
  3177. }
  3178. }
  3179. # check for using SPDX license tag at beginning of files
  3180. if ($realline == $checklicenseline) {
  3181. if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
  3182. $checklicenseline = 2;
  3183. } elsif ($rawline =~ /^\+/) {
  3184. my $comment = "";
  3185. if ($realfile =~ /\.(h|s|S)$/) {
  3186. $comment = '/*';
  3187. } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
  3188. $comment = '//';
  3189. } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
  3190. $comment = '#';
  3191. } elsif ($realfile =~ /\.rst$/) {
  3192. $comment = '..';
  3193. }
  3194. # check SPDX comment style for .[chsS] files
  3195. if ($realfile =~ /\.[chsS]$/ &&
  3196. $rawline =~ /SPDX-License-Identifier:/ &&
  3197. $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
  3198. WARN("SPDX_LICENSE_TAG",
  3199. "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
  3200. }
  3201. if ($comment !~ /^$/ &&
  3202. $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
  3203. WARN("SPDX_LICENSE_TAG",
  3204. "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
  3205. } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
  3206. my $spdx_license = $1;
  3207. if (!is_SPDX_License_valid($spdx_license)) {
  3208. WARN("SPDX_LICENSE_TAG",
  3209. "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
  3210. }
  3211. if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
  3212. not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
  3213. my $msg_level = \&WARN;
  3214. $msg_level = \&CHK if ($file);
  3215. if (&{$msg_level}("SPDX_LICENSE_TAG",
  3216. "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
  3217. $fix) {
  3218. $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
  3219. }
  3220. }
  3221. }
  3222. }
  3223. }
  3224. # check for embedded filenames
  3225. if ($rawline =~ /^\+.*\Q$realfile\E/) {
  3226. WARN("EMBEDDED_FILENAME",
  3227. "It's generally not useful to have the filename in the file\n" . $herecurr);
  3228. }
  3229. # check we are in a valid source file if not then ignore this hunk
  3230. next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
  3231. # check for using SPDX-License-Identifier on the wrong line number
  3232. if ($realline != $checklicenseline &&
  3233. $rawline =~ /\bSPDX-License-Identifier:/ &&
  3234. substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
  3235. WARN("SPDX_LICENSE_TAG",
  3236. "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
  3237. }
  3238. # line length limit (with some exclusions)
  3239. #
  3240. # There are a few types of lines that may extend beyond $max_line_length:
  3241. # logging functions like pr_info that end in a string
  3242. # lines with a single string
  3243. # #defines that are a single string
  3244. # lines with an RFC3986 like URL
  3245. #
  3246. # There are 3 different line length message types:
  3247. # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
  3248. # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
  3249. # LONG_LINE all other lines longer than $max_line_length
  3250. #
  3251. # if LONG_LINE is ignored, the other 2 types are also ignored
  3252. #
  3253. if ($line =~ /^\+/ && $length > $max_line_length) {
  3254. my $msg_type = "LONG_LINE";
  3255. # Check the allowed long line types first
  3256. # logging functions that end in a string that starts
  3257. # before $max_line_length
  3258. if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
  3259. length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
  3260. $msg_type = "";
  3261. # lines with only strings (w/ possible termination)
  3262. # #defines with only strings
  3263. } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
  3264. $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
  3265. $msg_type = "";
  3266. # More special cases
  3267. } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
  3268. $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
  3269. $msg_type = "";
  3270. # URL ($rawline is used in case the URL is in a comment)
  3271. } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
  3272. $msg_type = "";
  3273. # Otherwise set the alternate message types
  3274. # a comment starts before $max_line_length
  3275. } elsif ($line =~ /($;[\s$;]*)$/ &&
  3276. length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
  3277. $msg_type = "LONG_LINE_COMMENT"
  3278. # a quoted string starts before $max_line_length
  3279. } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
  3280. length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
  3281. $msg_type = "LONG_LINE_STRING"
  3282. }
  3283. if ($msg_type ne "" &&
  3284. (show_type("LONG_LINE") || show_type($msg_type))) {
  3285. my $msg_level = \&WARN;
  3286. $msg_level = \&CHK if ($file);
  3287. &{$msg_level}($msg_type,
  3288. "line length of $length exceeds $max_line_length columns\n" . $herecurr);
  3289. }
  3290. }
  3291. # check for adding lines without a newline.
  3292. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  3293. if (WARN("MISSING_EOF_NEWLINE",
  3294. "adding a line without newline at end of file\n" . $herecurr) &&
  3295. $fix) {
  3296. fix_delete_line($fixlinenr+1, "No newline at end of file");
  3297. }
  3298. }
  3299. # check for .L prefix local symbols in .S files
  3300. if ($realfile =~ /\.S$/ &&
  3301. $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
  3302. WARN("AVOID_L_PREFIX",
  3303. "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);
  3304. }
  3305. if ($u_boot) {
  3306. u_boot_line($realfile, $line, $rawline, $herecurr);
  3307. }
  3308. # check we are in a valid source file C or perl if not then ignore this hunk
  3309. next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
  3310. # at the beginning of a line any tabs must come first and anything
  3311. # more than $tabsize must use tabs.
  3312. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  3313. $rawline =~ /^\+\s* \s*/) {
  3314. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  3315. $rpt_cleaners = 1;
  3316. if (ERROR("CODE_INDENT",
  3317. "code indent should use tabs where possible\n" . $herevet) &&
  3318. $fix) {
  3319. $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
  3320. }
  3321. }
  3322. # check for space before tabs.
  3323. if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
  3324. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  3325. if (WARN("SPACE_BEFORE_TAB",
  3326. "please, no space before tabs\n" . $herevet) &&
  3327. $fix) {
  3328. while ($fixed[$fixlinenr] =~
  3329. s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
  3330. while ($fixed[$fixlinenr] =~
  3331. s/(^\+.*) +\t/$1\t/) {}
  3332. }
  3333. }
  3334. # check for assignments on the start of a line
  3335. if ($sline =~ /^\+\s+($Assignment)[^=]/) {
  3336. my $operator = $1;
  3337. if (CHK("ASSIGNMENT_CONTINUATIONS",
  3338. "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
  3339. $fix && $prevrawline =~ /^\+/) {
  3340. # add assignment operator to the previous line, remove from current line
  3341. $fixed[$fixlinenr - 1] .= " $operator";
  3342. $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
  3343. }
  3344. }
  3345. # check for && or || at the start of a line
  3346. if ($rawline =~ /^\+\s*(&&|\|\|)/) {
  3347. my $operator = $1;
  3348. if (CHK("LOGICAL_CONTINUATIONS",
  3349. "Logical continuations should be on the previous line\n" . $hereprev) &&
  3350. $fix && $prevrawline =~ /^\+/) {
  3351. # insert logical operator at last non-comment, non-whitepsace char on previous line
  3352. $prevline =~ /[\s$;]*$/;
  3353. my $line_end = substr($prevrawline, $-[0]);
  3354. $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
  3355. $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
  3356. }
  3357. }
  3358. # check indentation starts on a tab stop
  3359. if ($perl_version_ok &&
  3360. $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
  3361. my $indent = length($1);
  3362. if ($indent % $tabsize) {
  3363. if (WARN("TABSTOP",
  3364. "Statements should start on a tabstop\n" . $herecurr) &&
  3365. $fix) {
  3366. $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
  3367. }
  3368. }
  3369. }
  3370. # check multi-line statement indentation matches previous line
  3371. if ($perl_version_ok &&
  3372. $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
  3373. $prevline =~ /^\+(\t*)(.*)$/;
  3374. my $oldindent = $1;
  3375. my $rest = $2;
  3376. my $pos = pos_last_openparen($rest);
  3377. if ($pos >= 0) {
  3378. $line =~ /^(\+| )([ \t]*)/;
  3379. my $newindent = $2;
  3380. my $goodtabindent = $oldindent .
  3381. "\t" x ($pos / $tabsize) .
  3382. " " x ($pos % $tabsize);
  3383. my $goodspaceindent = $oldindent . " " x $pos;
  3384. if ($newindent ne $goodtabindent &&
  3385. $newindent ne $goodspaceindent) {
  3386. if (CHK("PARENTHESIS_ALIGNMENT",
  3387. "Alignment should match open parenthesis\n" . $hereprev) &&
  3388. $fix && $line =~ /^\+/) {
  3389. $fixed[$fixlinenr] =~
  3390. s/^\+[ \t]*/\+$goodtabindent/;
  3391. }
  3392. }
  3393. }
  3394. }
  3395. # check for space after cast like "(int) foo" or "(struct foo) bar"
  3396. # avoid checking a few false positives:
  3397. # "sizeof(<type>)" or "__alignof__(<type>)"
  3398. # function pointer declarations like "(*foo)(int) = bar;"
  3399. # structure definitions like "(struct foo) { 0 };"
  3400. # multiline macros that define functions
  3401. # known attributes or the __attribute__ keyword
  3402. if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
  3403. (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
  3404. if (CHK("SPACING",
  3405. "No space is necessary after a cast\n" . $herecurr) &&
  3406. $fix) {
  3407. $fixed[$fixlinenr] =~
  3408. s/(\(\s*$Type\s*\))[ \t]+/$1/;
  3409. }
  3410. }
  3411. # Block comment styles
  3412. # Networking with an initial /*
  3413. if ($realfile =~ m@^(drivers/net/|net/)@ &&
  3414. $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
  3415. $rawline =~ /^\+[ \t]*\*/ &&
  3416. $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
  3417. WARN("NETWORKING_BLOCK_COMMENT_STYLE",
  3418. "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
  3419. }
  3420. # Block comments use * on subsequent lines
  3421. if ($prevline =~ /$;[ \t]*$/ && #ends in comment
  3422. $prevrawline =~ /^\+.*?\/\*/ && #starting /*
  3423. $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
  3424. $rawline =~ /^\+/ && #line is new
  3425. $rawline !~ /^\+[ \t]*\*/) { #no leading *
  3426. WARN("BLOCK_COMMENT_STYLE",
  3427. "Block comments use * on subsequent lines\n" . $hereprev);
  3428. }
  3429. # Block comments use */ on trailing lines
  3430. if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
  3431. $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
  3432. $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
  3433. $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
  3434. WARN("BLOCK_COMMENT_STYLE",
  3435. "Block comments use a trailing */ on a separate line\n" . $herecurr);
  3436. }
  3437. # Block comment * alignment
  3438. if ($prevline =~ /$;[ \t]*$/ && #ends in comment
  3439. $line =~ /^\+[ \t]*$;/ && #leading comment
  3440. $rawline =~ /^\+[ \t]*\*/ && #leading *
  3441. (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
  3442. $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
  3443. $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
  3444. my $oldindent;
  3445. $prevrawline =~ m@^\+([ \t]*/?)\*@;
  3446. if (defined($1)) {
  3447. $oldindent = expand_tabs($1);
  3448. } else {
  3449. $prevrawline =~ m@^\+(.*/?)\*@;
  3450. $oldindent = expand_tabs($1);
  3451. }
  3452. $rawline =~ m@^\+([ \t]*)\*@;
  3453. my $newindent = $1;
  3454. $newindent = expand_tabs($newindent);
  3455. if (length($oldindent) ne length($newindent)) {
  3456. WARN("BLOCK_COMMENT_STYLE",
  3457. "Block comments should align the * on each line\n" . $hereprev);
  3458. }
  3459. }
  3460. # check for missing blank lines after struct/union declarations
  3461. # with exceptions for various attributes and macros
  3462. if ($prevline =~ /^[\+ ]};?\s*$/ &&
  3463. $line =~ /^\+/ &&
  3464. !($line =~ /^\+\s*$/ ||
  3465. $line =~ /^\+\s*EXPORT_SYMBOL/ ||
  3466. $line =~ /^\+\s*MODULE_/i ||
  3467. $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
  3468. $line =~ /^\+[a-z_]*init/ ||
  3469. $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
  3470. $line =~ /^\+\s*DECLARE/ ||
  3471. $line =~ /^\+\s*builtin_[\w_]*driver/ ||
  3472. $line =~ /^\+\s*__setup/)) {
  3473. if (CHK("LINE_SPACING",
  3474. "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
  3475. $fix) {
  3476. fix_insert_line($fixlinenr, "\+");
  3477. }
  3478. }
  3479. # check for multiple consecutive blank lines
  3480. if ($prevline =~ /^[\+ ]\s*$/ &&
  3481. $line =~ /^\+\s*$/ &&
  3482. $last_blank_line != ($linenr - 1)) {
  3483. if (CHK("LINE_SPACING",
  3484. "Please don't use multiple blank lines\n" . $hereprev) &&
  3485. $fix) {
  3486. fix_delete_line($fixlinenr, $rawline);
  3487. }
  3488. $last_blank_line = $linenr;
  3489. }
  3490. # check for missing blank lines after declarations
  3491. # (declarations must have the same indentation and not be at the start of line)
  3492. if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
  3493. # use temporaries
  3494. my $sl = $sline;
  3495. my $pl = $prevline;
  3496. # remove $Attribute/$Sparse uses to simplify comparisons
  3497. $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
  3498. $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
  3499. if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
  3500. # function pointer declarations
  3501. $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
  3502. # foo bar; where foo is some local typedef or #define
  3503. $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
  3504. # known declaration macros
  3505. $pl =~ /^\+\s+$declaration_macros/) &&
  3506. # for "else if" which can look like "$Ident $Ident"
  3507. !($pl =~ /^\+\s+$c90_Keywords\b/ ||
  3508. # other possible extensions of declaration lines
  3509. $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
  3510. # not starting a section or a macro "\" extended line
  3511. $pl =~ /(?:\{\s*|\\)$/) &&
  3512. # looks like a declaration
  3513. !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
  3514. # function pointer declarations
  3515. $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
  3516. # foo bar; where foo is some local typedef or #define
  3517. $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
  3518. # known declaration macros
  3519. $sl =~ /^\+\s+$declaration_macros/ ||
  3520. # start of struct or union or enum
  3521. $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
  3522. # start or end of block or continuation of declaration
  3523. $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
  3524. # bitfield continuation
  3525. $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
  3526. # other possible extensions of declaration lines
  3527. $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
  3528. if (WARN("LINE_SPACING",
  3529. "Missing a blank line after declarations\n" . $hereprev) &&
  3530. $fix) {
  3531. fix_insert_line($fixlinenr, "\+");
  3532. }
  3533. }
  3534. }
  3535. # check for spaces at the beginning of a line.
  3536. # Exceptions:
  3537. # 1) within comments
  3538. # 2) indented preprocessor commands
  3539. # 3) hanging labels
  3540. if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
  3541. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  3542. if (WARN("LEADING_SPACE",
  3543. "please, no spaces at the start of a line\n" . $herevet) &&
  3544. $fix) {
  3545. $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
  3546. }
  3547. }
  3548. # check we are in a valid C source file if not then ignore this hunk
  3549. next if ($realfile !~ /\.(h|c)$/);
  3550. # check for unusual line ending [ or (
  3551. if ($line =~ /^\+.*([\[\(])\s*$/) {
  3552. CHK("OPEN_ENDED_LINE",
  3553. "Lines should not end with a '$1'\n" . $herecurr);
  3554. }
  3555. # check if this appears to be the start function declaration, save the name
  3556. if ($sline =~ /^\+\{\s*$/ &&
  3557. $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
  3558. $context_function = $1;
  3559. }
  3560. # check if this appears to be the end of function declaration
  3561. if ($sline =~ /^\+\}\s*$/) {
  3562. undef $context_function;
  3563. }
  3564. # check indentation of any line with a bare else
  3565. # (but not if it is a multiple line "if (foo) return bar; else return baz;")
  3566. # if the previous line is a break or return and is indented 1 tab more...
  3567. if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
  3568. my $tabs = length($1) + 1;
  3569. if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
  3570. ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
  3571. defined $lines[$linenr] &&
  3572. $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
  3573. WARN("UNNECESSARY_ELSE",
  3574. "else is not generally useful after a break or return\n" . $hereprev);
  3575. }
  3576. }
  3577. # check indentation of a line with a break;
  3578. # if the previous line is a goto, return or break
  3579. # and is indented the same # of tabs
  3580. if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
  3581. my $tabs = $1;
  3582. if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
  3583. if (WARN("UNNECESSARY_BREAK",
  3584. "break is not useful after a $1\n" . $hereprev) &&
  3585. $fix) {
  3586. fix_delete_line($fixlinenr, $rawline);
  3587. }
  3588. }
  3589. }
  3590. # check for RCS/CVS revision markers
  3591. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  3592. WARN("CVS_KEYWORD",
  3593. "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  3594. }
  3595. # check for old HOTPLUG __dev<foo> section markings
  3596. if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
  3597. WARN("HOTPLUG_SECTION",
  3598. "Using $1 is unnecessary\n" . $herecurr);
  3599. }
  3600. # Check for potential 'bare' types
  3601. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  3602. $realline_next);
  3603. #print "LINE<$line>\n";
  3604. if ($linenr > $suppress_statement &&
  3605. $realcnt && $sline =~ /.\s*\S/) {
  3606. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  3607. ctx_statement_block($linenr, $realcnt, 0);
  3608. $stat =~ s/\n./\n /g;
  3609. $cond =~ s/\n./\n /g;
  3610. #print "linenr<$linenr> <$stat>\n";
  3611. # If this statement has no statement boundaries within
  3612. # it there is no point in retrying a statement scan
  3613. # until we hit end of it.
  3614. my $frag = $stat; $frag =~ s/;+\s*$//;
  3615. if ($frag !~ /(?:{|;)/) {
  3616. #print "skip<$line_nr_next>\n";
  3617. $suppress_statement = $line_nr_next;
  3618. }
  3619. # Find the real next line.
  3620. $realline_next = $line_nr_next;
  3621. if (defined $realline_next &&
  3622. (!defined $lines[$realline_next - 1] ||
  3623. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  3624. $realline_next++;
  3625. }
  3626. my $s = $stat;
  3627. $s =~ s/{.*$//s;
  3628. # Ignore goto labels.
  3629. if ($s =~ /$Ident:\*$/s) {
  3630. # Ignore functions being called
  3631. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  3632. } elsif ($s =~ /^.\s*else\b/s) {
  3633. # declarations always start with types
  3634. } 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) {
  3635. my $type = $1;
  3636. $type =~ s/\s+/ /g;
  3637. possible($type, "A:" . $s);
  3638. # definitions in global scope can only start with types
  3639. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  3640. possible($1, "B:" . $s);
  3641. }
  3642. # any (foo ... *) is a pointer cast, and foo is a type
  3643. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  3644. possible($1, "C:" . $s);
  3645. }
  3646. # Check for any sort of function declaration.
  3647. # int foo(something bar, other baz);
  3648. # void (*store_gdt)(x86_descr_ptr *);
  3649. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  3650. my ($name_len) = length($1);
  3651. my $ctx = $s;
  3652. substr($ctx, 0, $name_len + 1, '');
  3653. $ctx =~ s/\)[^\)]*$//;
  3654. for my $arg (split(/\s*,\s*/, $ctx)) {
  3655. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  3656. possible($1, "D:" . $s);
  3657. }
  3658. }
  3659. }
  3660. }
  3661. #
  3662. # Checks which may be anchored in the context.
  3663. #
  3664. # Check for switch () and associated case and default
  3665. # statements should be at the same indent.
  3666. if ($line=~/\bswitch\s*\(.*\)/) {
  3667. my $err = '';
  3668. my $sep = '';
  3669. my @ctx = ctx_block_outer($linenr, $realcnt);
  3670. shift(@ctx);
  3671. for my $ctx (@ctx) {
  3672. my ($clen, $cindent) = line_stats($ctx);
  3673. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  3674. $indent != $cindent) {
  3675. $err .= "$sep$ctx\n";
  3676. $sep = '';
  3677. } else {
  3678. $sep = "[...]\n";
  3679. }
  3680. }
  3681. if ($err ne '') {
  3682. ERROR("SWITCH_CASE_INDENT_LEVEL",
  3683. "switch and case should be at the same indent\n$hereline$err");
  3684. }
  3685. }
  3686. # if/while/etc brace do not go on next line, unless defining a do while loop,
  3687. # or if that brace on the next line is for something else
  3688. if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  3689. my $pre_ctx = "$1$2";
  3690. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  3691. if ($line =~ /^\+\t{6,}/) {
  3692. WARN("DEEP_INDENTATION",
  3693. "Too many leading tabs - consider code refactoring\n" . $herecurr);
  3694. }
  3695. my $ctx_cnt = $realcnt - $#ctx - 1;
  3696. my $ctx = join("\n", @ctx);
  3697. my $ctx_ln = $linenr;
  3698. my $ctx_skip = $realcnt;
  3699. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  3700. defined $lines[$ctx_ln - 1] &&
  3701. $lines[$ctx_ln - 1] =~ /^-/)) {
  3702. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  3703. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  3704. $ctx_ln++;
  3705. }
  3706. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  3707. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  3708. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  3709. ERROR("OPEN_BRACE",
  3710. "that open brace { should be on the previous line\n" .
  3711. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  3712. }
  3713. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  3714. $ctx =~ /\)\s*\;\s*$/ &&
  3715. defined $lines[$ctx_ln - 1])
  3716. {
  3717. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  3718. if ($nindent > $indent) {
  3719. WARN("TRAILING_SEMICOLON",
  3720. "trailing semicolon indicates no statements, indent implies otherwise\n" .
  3721. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  3722. }
  3723. }
  3724. }
  3725. # Check relative indent for conditionals and blocks.
  3726. if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  3727. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  3728. ctx_statement_block($linenr, $realcnt, 0)
  3729. if (!defined $stat);
  3730. my ($s, $c) = ($stat, $cond);
  3731. substr($s, 0, length($c), '');
  3732. # remove inline comments
  3733. $s =~ s/$;/ /g;
  3734. $c =~ s/$;/ /g;
  3735. # Find out how long the conditional actually is.
  3736. my @newlines = ($c =~ /\n/gs);
  3737. my $cond_lines = 1 + $#newlines;
  3738. # Make sure we remove the line prefixes as we have
  3739. # none on the first line, and are going to readd them
  3740. # where necessary.
  3741. $s =~ s/\n./\n/gs;
  3742. while ($s =~ /\n\s+\\\n/) {
  3743. $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
  3744. }
  3745. # We want to check the first line inside the block
  3746. # starting at the end of the conditional, so remove:
  3747. # 1) any blank line termination
  3748. # 2) any opening brace { on end of the line
  3749. # 3) any do (...) {
  3750. my $continuation = 0;
  3751. my $check = 0;
  3752. $s =~ s/^.*\bdo\b//;
  3753. $s =~ s/^\s*{//;
  3754. if ($s =~ s/^\s*\\//) {
  3755. $continuation = 1;
  3756. }
  3757. if ($s =~ s/^\s*?\n//) {
  3758. $check = 1;
  3759. $cond_lines++;
  3760. }
  3761. # Also ignore a loop construct at the end of a
  3762. # preprocessor statement.
  3763. if (($prevline =~ /^.\s*#\s*define\s/ ||
  3764. $prevline =~ /\\\s*$/) && $continuation == 0) {
  3765. $check = 0;
  3766. }
  3767. my $cond_ptr = -1;
  3768. $continuation = 0;
  3769. while ($cond_ptr != $cond_lines) {
  3770. $cond_ptr = $cond_lines;
  3771. # If we see an #else/#elif then the code
  3772. # is not linear.
  3773. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  3774. $check = 0;
  3775. }
  3776. # Ignore:
  3777. # 1) blank lines, they should be at 0,
  3778. # 2) preprocessor lines, and
  3779. # 3) labels.
  3780. if ($continuation ||
  3781. $s =~ /^\s*?\n/ ||
  3782. $s =~ /^\s*#\s*?/ ||
  3783. $s =~ /^\s*$Ident\s*:/) {
  3784. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  3785. if ($s =~ s/^.*?\n//) {
  3786. $cond_lines++;
  3787. }
  3788. }
  3789. }
  3790. my (undef, $sindent) = line_stats("+" . $s);
  3791. my $stat_real = raw_line($linenr, $cond_lines);
  3792. # Check if either of these lines are modified, else
  3793. # this is not this patch's fault.
  3794. if (!defined($stat_real) ||
  3795. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  3796. $check = 0;
  3797. }
  3798. if (defined($stat_real) && $cond_lines > 1) {
  3799. $stat_real = "[...]\n$stat_real";
  3800. }
  3801. #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";
  3802. if ($check && $s ne '' &&
  3803. (($sindent % $tabsize) != 0 ||
  3804. ($sindent < $indent) ||
  3805. ($sindent == $indent &&
  3806. ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
  3807. ($sindent > $indent + $tabsize))) {
  3808. WARN("SUSPECT_CODE_INDENT",
  3809. "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  3810. }
  3811. }
  3812. # Track the 'values' across context and added lines.
  3813. my $opline = $line; $opline =~ s/^./ /;
  3814. my ($curr_values, $curr_vars) =
  3815. annotate_values($opline . "\n", $prev_values);
  3816. $curr_values = $prev_values . $curr_values;
  3817. if ($dbg_values) {
  3818. my $outline = $opline; $outline =~ s/\t/ /g;
  3819. print "$linenr > .$outline\n";
  3820. print "$linenr > $curr_values\n";
  3821. print "$linenr > $curr_vars\n";
  3822. }
  3823. $prev_values = substr($curr_values, -1);
  3824. #ignore lines not being added
  3825. next if ($line =~ /^[^\+]/);
  3826. # check for self assignments used to avoid compiler warnings
  3827. # e.g.: int foo = foo, *bar = NULL;
  3828. # struct foo bar = *(&(bar));
  3829. if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
  3830. my $var = $1;
  3831. if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
  3832. WARN("SELF_ASSIGNMENT",
  3833. "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
  3834. }
  3835. }
  3836. # check for dereferences that span multiple lines
  3837. if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
  3838. $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
  3839. $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
  3840. my $ref = $1;
  3841. $line =~ /^.\s*($Lval)/;
  3842. $ref .= $1;
  3843. $ref =~ s/\s//g;
  3844. WARN("MULTILINE_DEREFERENCE",
  3845. "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
  3846. }
  3847. # check for declarations of signed or unsigned without int
  3848. while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
  3849. my $type = $1;
  3850. my $var = $2;
  3851. $var = "" if (!defined $var);
  3852. if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
  3853. my $sign = $1;
  3854. my $pointer = $2;
  3855. $pointer = "" if (!defined $pointer);
  3856. if (WARN("UNSPECIFIED_INT",
  3857. "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
  3858. $fix) {
  3859. my $decl = trim($sign) . " int ";
  3860. my $comp_pointer = $pointer;
  3861. $comp_pointer =~ s/\s//g;
  3862. $decl .= $comp_pointer;
  3863. $decl = rtrim($decl) if ($var eq "");
  3864. $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
  3865. }
  3866. }
  3867. }
  3868. # TEST: allow direct testing of the type matcher.
  3869. if ($dbg_type) {
  3870. if ($line =~ /^.\s*$Declare\s*$/) {
  3871. ERROR("TEST_TYPE",
  3872. "TEST: is type\n" . $herecurr);
  3873. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  3874. ERROR("TEST_NOT_TYPE",
  3875. "TEST: is not type ($1 is)\n". $herecurr);
  3876. }
  3877. next;
  3878. }
  3879. # TEST: allow direct testing of the attribute matcher.
  3880. if ($dbg_attr) {
  3881. if ($line =~ /^.\s*$Modifier\s*$/) {
  3882. ERROR("TEST_ATTR",
  3883. "TEST: is attr\n" . $herecurr);
  3884. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  3885. ERROR("TEST_NOT_ATTR",
  3886. "TEST: is not attr ($1 is)\n". $herecurr);
  3887. }
  3888. next;
  3889. }
  3890. # check for initialisation to aggregates open brace on the next line
  3891. if ($line =~ /^.\s*{/ &&
  3892. $prevline =~ /(?:^|[^=])=\s*$/) {
  3893. if (ERROR("OPEN_BRACE",
  3894. "that open brace { should be on the previous line\n" . $hereprev) &&
  3895. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  3896. fix_delete_line($fixlinenr - 1, $prevrawline);
  3897. fix_delete_line($fixlinenr, $rawline);
  3898. my $fixedline = $prevrawline;
  3899. $fixedline =~ s/\s*=\s*$/ = {/;
  3900. fix_insert_line($fixlinenr, $fixedline);
  3901. $fixedline = $line;
  3902. $fixedline =~ s/^(.\s*)\{\s*/$1/;
  3903. fix_insert_line($fixlinenr, $fixedline);
  3904. }
  3905. }
  3906. #
  3907. # Checks which are anchored on the added line.
  3908. #
  3909. # check for malformed paths in #include statements (uses RAW line)
  3910. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  3911. my $path = $1;
  3912. if ($path =~ m{//}) {
  3913. ERROR("MALFORMED_INCLUDE",
  3914. "malformed #include filename\n" . $herecurr);
  3915. }
  3916. if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
  3917. ERROR("UAPI_INCLUDE",
  3918. "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
  3919. }
  3920. }
  3921. # no C99 // comments
  3922. if ($line =~ m{//}) {
  3923. if (ERROR("C99_COMMENTS",
  3924. "do not use C99 // comments\n" . $herecurr) &&
  3925. $fix) {
  3926. my $line = $fixed[$fixlinenr];
  3927. if ($line =~ /\/\/(.*)$/) {
  3928. my $comment = trim($1);
  3929. $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
  3930. }
  3931. }
  3932. }
  3933. # Remove C99 comments.
  3934. $line =~ s@//.*@@;
  3935. $opline =~ s@//.*@@;
  3936. # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
  3937. # the whole statement.
  3938. #print "APW <$lines[$realline_next - 1]>\n";
  3939. if (defined $realline_next &&
  3940. exists $lines[$realline_next - 1] &&
  3941. !defined $suppress_export{$realline_next} &&
  3942. ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
  3943. # Handle definitions which produce identifiers with
  3944. # a prefix:
  3945. # XXX(foo);
  3946. # EXPORT_SYMBOL(something_foo);
  3947. my $name = $1;
  3948. $name =~ s/^\s*($Ident).*/$1/;
  3949. if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
  3950. $name =~ /^${Ident}_$2/) {
  3951. #print "FOO C name<$name>\n";
  3952. $suppress_export{$realline_next} = 1;
  3953. } elsif ($stat !~ /(?:
  3954. \n.}\s*$|
  3955. ^.DEFINE_$Ident\(\Q$name\E\)|
  3956. ^.DECLARE_$Ident\(\Q$name\E\)|
  3957. ^.LIST_HEAD\(\Q$name\E\)|
  3958. ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
  3959. \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
  3960. )/x) {
  3961. #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
  3962. $suppress_export{$realline_next} = 2;
  3963. } else {
  3964. $suppress_export{$realline_next} = 1;
  3965. }
  3966. }
  3967. if (!defined $suppress_export{$linenr} &&
  3968. $prevline =~ /^.\s*$/ &&
  3969. ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
  3970. #print "FOO B <$lines[$linenr - 1]>\n";
  3971. $suppress_export{$linenr} = 2;
  3972. }
  3973. if (defined $suppress_export{$linenr} &&
  3974. $suppress_export{$linenr} == 2) {
  3975. WARN("EXPORT_SYMBOL",
  3976. "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  3977. }
  3978. # check for global initialisers.
  3979. if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
  3980. !exclude_global_initialisers($realfile)) {
  3981. if (ERROR("GLOBAL_INITIALISERS",
  3982. "do not initialise globals to $1\n" . $herecurr) &&
  3983. $fix) {
  3984. $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
  3985. }
  3986. }
  3987. # check for static initialisers.
  3988. if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
  3989. if (ERROR("INITIALISED_STATIC",
  3990. "do not initialise statics to $1\n" .
  3991. $herecurr) &&
  3992. $fix) {
  3993. $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
  3994. }
  3995. }
  3996. # check for misordered declarations of char/short/int/long with signed/unsigned
  3997. while ($sline =~ m{(\b$TypeMisordered\b)}g) {
  3998. my $tmp = trim($1);
  3999. WARN("MISORDERED_TYPE",
  4000. "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
  4001. }
  4002. # check for unnecessary <signed> int declarations of short/long/long long
  4003. while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
  4004. my $type = trim($1);
  4005. next if ($type !~ /\bint\b/);
  4006. next if ($type !~ /\b(?:short|long\s+long|long)\b/);
  4007. my $new_type = $type;
  4008. $new_type =~ s/\b\s*int\s*\b/ /;
  4009. $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
  4010. $new_type =~ s/^const\s+//;
  4011. $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
  4012. $new_type = "const $new_type" if ($type =~ /^const\b/);
  4013. $new_type =~ s/\s+/ /g;
  4014. $new_type = trim($new_type);
  4015. if (WARN("UNNECESSARY_INT",
  4016. "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
  4017. $fix) {
  4018. $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
  4019. }
  4020. }
  4021. # check for static const char * arrays.
  4022. if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
  4023. WARN("STATIC_CONST_CHAR_ARRAY",
  4024. "static const char * array should probably be static const char * const\n" .
  4025. $herecurr);
  4026. }
  4027. # check for initialized const char arrays that should be static const
  4028. if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
  4029. if (WARN("STATIC_CONST_CHAR_ARRAY",
  4030. "const array should probably be static const\n" . $herecurr) &&
  4031. $fix) {
  4032. $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
  4033. }
  4034. }
  4035. # check for static char foo[] = "bar" declarations.
  4036. if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
  4037. WARN("STATIC_CONST_CHAR_ARRAY",
  4038. "static char array declaration should probably be static const char\n" .
  4039. $herecurr);
  4040. }
  4041. # check for const <foo> const where <foo> is not a pointer or array type
  4042. if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
  4043. my $found = $1;
  4044. if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
  4045. WARN("CONST_CONST",
  4046. "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
  4047. } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
  4048. WARN("CONST_CONST",
  4049. "'const $found const' should probably be 'const $found'\n" . $herecurr);
  4050. }
  4051. }
  4052. # check for const static or static <non ptr type> const declarations
  4053. # prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
  4054. if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
  4055. $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
  4056. if (WARN("STATIC_CONST",
  4057. "Move const after static - use 'static const $1'\n" . $herecurr) &&
  4058. $fix) {
  4059. $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
  4060. $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
  4061. }
  4062. }
  4063. # check for non-global char *foo[] = {"bar", ...} declarations.
  4064. if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
  4065. WARN("STATIC_CONST_CHAR_ARRAY",
  4066. "char * array declaration might be better as static const\n" .
  4067. $herecurr);
  4068. }
  4069. # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
  4070. if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
  4071. my $array = $1;
  4072. if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
  4073. my $array_div = $1;
  4074. if (WARN("ARRAY_SIZE",
  4075. "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
  4076. $fix) {
  4077. $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
  4078. }
  4079. }
  4080. }
  4081. # check for function declarations without arguments like "int foo()"
  4082. if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
  4083. if (ERROR("FUNCTION_WITHOUT_ARGS",
  4084. "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
  4085. $fix) {
  4086. $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
  4087. }
  4088. }
  4089. # check for new typedefs, only function parameters and sparse annotations
  4090. # make sense.
  4091. if ($line =~ /\btypedef\s/ &&
  4092. $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
  4093. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  4094. $line !~ /\b$typeTypedefs\b/ &&
  4095. $line !~ /\b__bitwise\b/) {
  4096. WARN("NEW_TYPEDEFS",
  4097. "do not add new typedefs\n" . $herecurr);
  4098. }
  4099. # * goes on variable not on type
  4100. # (char*[ const])
  4101. while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
  4102. #print "AA<$1>\n";
  4103. my ($ident, $from, $to) = ($1, $2, $2);
  4104. # Should start with a space.
  4105. $to =~ s/^(\S)/ $1/;
  4106. # Should not end with a space.
  4107. $to =~ s/\s+$//;
  4108. # '*'s should not have spaces between.
  4109. while ($to =~ s/\*\s+\*/\*\*/) {
  4110. }
  4111. ## print "1: from<$from> to<$to> ident<$ident>\n";
  4112. if ($from ne $to) {
  4113. if (ERROR("POINTER_LOCATION",
  4114. "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
  4115. $fix) {
  4116. my $sub_from = $ident;
  4117. my $sub_to = $ident;
  4118. $sub_to =~ s/\Q$from\E/$to/;
  4119. $fixed[$fixlinenr] =~
  4120. s@\Q$sub_from\E@$sub_to@;
  4121. }
  4122. }
  4123. }
  4124. while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
  4125. #print "BB<$1>\n";
  4126. my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
  4127. # Should start with a space.
  4128. $to =~ s/^(\S)/ $1/;
  4129. # Should not end with a space.
  4130. $to =~ s/\s+$//;
  4131. # '*'s should not have spaces between.
  4132. while ($to =~ s/\*\s+\*/\*\*/) {
  4133. }
  4134. # Modifiers should have spaces.
  4135. $to =~ s/(\b$Modifier$)/$1 /;
  4136. ## print "2: from<$from> to<$to> ident<$ident>\n";
  4137. if ($from ne $to && $ident !~ /^$Modifier$/) {
  4138. if (ERROR("POINTER_LOCATION",
  4139. "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
  4140. $fix) {
  4141. my $sub_from = $match;
  4142. my $sub_to = $match;
  4143. $sub_to =~ s/\Q$from\E/$to/;
  4144. $fixed[$fixlinenr] =~
  4145. s@\Q$sub_from\E@$sub_to@;
  4146. }
  4147. }
  4148. }
  4149. # avoid BUG() or BUG_ON()
  4150. if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
  4151. my $msg_level = \&WARN;
  4152. $msg_level = \&CHK if ($file);
  4153. &{$msg_level}("AVOID_BUG",
  4154. "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
  4155. }
  4156. # avoid LINUX_VERSION_CODE
  4157. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  4158. WARN("LINUX_VERSION_CODE",
  4159. "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  4160. }
  4161. # check for uses of printk_ratelimit
  4162. if ($line =~ /\bprintk_ratelimit\s*\(/) {
  4163. WARN("PRINTK_RATELIMITED",
  4164. "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
  4165. }
  4166. # printk should use KERN_* levels
  4167. if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
  4168. WARN("PRINTK_WITHOUT_KERN_LEVEL",
  4169. "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
  4170. }
  4171. # prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
  4172. if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
  4173. my $printk = $1;
  4174. my $modifier = $2;
  4175. my $orig = $3;
  4176. $modifier = "" if (!defined($modifier));
  4177. my $level = lc($orig);
  4178. $level = "warn" if ($level eq "warning");
  4179. my $level2 = $level;
  4180. $level2 = "dbg" if ($level eq "debug");
  4181. $level .= $modifier;
  4182. $level2 .= $modifier;
  4183. WARN("PREFER_PR_LEVEL",
  4184. "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr);
  4185. }
  4186. # prefer dev_<level> to dev_printk(KERN_<LEVEL>
  4187. if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
  4188. my $orig = $1;
  4189. my $level = lc($orig);
  4190. $level = "warn" if ($level eq "warning");
  4191. $level = "dbg" if ($level eq "debug");
  4192. WARN("PREFER_DEV_LEVEL",
  4193. "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
  4194. }
  4195. # trace_printk should not be used in production code.
  4196. if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
  4197. WARN("TRACE_PRINTK",
  4198. "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
  4199. }
  4200. # ENOSYS means "bad syscall nr" and nothing else. This will have a small
  4201. # number of false positives, but assembly files are not checked, so at
  4202. # least the arch entry code will not trigger this warning.
  4203. if ($line =~ /\bENOSYS\b/) {
  4204. WARN("ENOSYS",
  4205. "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
  4206. }
  4207. # ENOTSUPP is not a standard error code and should be avoided in new patches.
  4208. # Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
  4209. # Similarly to ENOSYS warning a small number of false positives is expected.
  4210. if (!$file && $line =~ /\bENOTSUPP\b/) {
  4211. if (WARN("ENOTSUPP",
  4212. "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
  4213. $fix) {
  4214. $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
  4215. }
  4216. }
  4217. # function brace can't be on same line, except for #defines of do while,
  4218. # or if closed on same line
  4219. if ($perl_version_ok &&
  4220. $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
  4221. $sline !~ /\#\s*define\b.*do\s*\{/ &&
  4222. $sline !~ /}/) {
  4223. if (ERROR("OPEN_BRACE",
  4224. "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
  4225. $fix) {
  4226. fix_delete_line($fixlinenr, $rawline);
  4227. my $fixed_line = $rawline;
  4228. $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
  4229. my $line1 = $1;
  4230. my $line2 = $2;
  4231. fix_insert_line($fixlinenr, ltrim($line1));
  4232. fix_insert_line($fixlinenr, "\+{");
  4233. if ($line2 !~ /^\s*$/) {
  4234. fix_insert_line($fixlinenr, "\+\t" . trim($line2));
  4235. }
  4236. }
  4237. }
  4238. # open braces for enum, union and struct go on the same line.
  4239. if ($line =~ /^.\s*{/ &&
  4240. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  4241. if (ERROR("OPEN_BRACE",
  4242. "open brace '{' following $1 go on the same line\n" . $hereprev) &&
  4243. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  4244. fix_delete_line($fixlinenr - 1, $prevrawline);
  4245. fix_delete_line($fixlinenr, $rawline);
  4246. my $fixedline = rtrim($prevrawline) . " {";
  4247. fix_insert_line($fixlinenr, $fixedline);
  4248. $fixedline = $rawline;
  4249. $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
  4250. if ($fixedline !~ /^\+\s*$/) {
  4251. fix_insert_line($fixlinenr, $fixedline);
  4252. }
  4253. }
  4254. }
  4255. # missing space after union, struct or enum definition
  4256. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
  4257. if (WARN("SPACING",
  4258. "missing space after $1 definition\n" . $herecurr) &&
  4259. $fix) {
  4260. $fixed[$fixlinenr] =~
  4261. s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
  4262. }
  4263. }
  4264. # Function pointer declarations
  4265. # check spacing between type, funcptr, and args
  4266. # canonical declaration is "type (*funcptr)(args...)"
  4267. if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
  4268. my $declare = $1;
  4269. my $pre_pointer_space = $2;
  4270. my $post_pointer_space = $3;
  4271. my $funcname = $4;
  4272. my $post_funcname_space = $5;
  4273. my $pre_args_space = $6;
  4274. # the $Declare variable will capture all spaces after the type
  4275. # so check it for a missing trailing missing space but pointer return types
  4276. # don't need a space so don't warn for those.
  4277. my $post_declare_space = "";
  4278. if ($declare =~ /(\s+)$/) {
  4279. $post_declare_space = $1;
  4280. $declare = rtrim($declare);
  4281. }
  4282. if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
  4283. WARN("SPACING",
  4284. "missing space after return type\n" . $herecurr);
  4285. $post_declare_space = " ";
  4286. }
  4287. # unnecessary space "type (*funcptr)(args...)"
  4288. # This test is not currently implemented because these declarations are
  4289. # equivalent to
  4290. # int foo(int bar, ...)
  4291. # and this is form shouldn't/doesn't generate a checkpatch warning.
  4292. #
  4293. # elsif ($declare =~ /\s{2,}$/) {
  4294. # WARN("SPACING",
  4295. # "Multiple spaces after return type\n" . $herecurr);
  4296. # }
  4297. # unnecessary space "type ( *funcptr)(args...)"
  4298. if (defined $pre_pointer_space &&
  4299. $pre_pointer_space =~ /^\s/) {
  4300. WARN("SPACING",
  4301. "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
  4302. }
  4303. # unnecessary space "type (* funcptr)(args...)"
  4304. if (defined $post_pointer_space &&
  4305. $post_pointer_space =~ /^\s/) {
  4306. WARN("SPACING",
  4307. "Unnecessary space before function pointer name\n" . $herecurr);
  4308. }
  4309. # unnecessary space "type (*funcptr )(args...)"
  4310. if (defined $post_funcname_space &&
  4311. $post_funcname_space =~ /^\s/) {
  4312. WARN("SPACING",
  4313. "Unnecessary space after function pointer name\n" . $herecurr);
  4314. }
  4315. # unnecessary space "type (*funcptr) (args...)"
  4316. if (defined $pre_args_space &&
  4317. $pre_args_space =~ /^\s/) {
  4318. WARN("SPACING",
  4319. "Unnecessary space before function pointer arguments\n" . $herecurr);
  4320. }
  4321. if (show_type("SPACING") && $fix) {
  4322. $fixed[$fixlinenr] =~
  4323. s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
  4324. }
  4325. }
  4326. # check for spacing round square brackets; allowed:
  4327. # 1. with a type on the left -- int [] a;
  4328. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  4329. # 3. inside a curly brace -- = { [0...10] = 5 }
  4330. while ($line =~ /(.*?\s)\[/g) {
  4331. my ($where, $prefix) = ($-[1], $1);
  4332. if ($prefix !~ /$Type\s+$/ &&
  4333. ($where != 0 || $prefix !~ /^.\s+$/) &&
  4334. $prefix !~ /[{,:]\s+$/) {
  4335. if (ERROR("BRACKET_SPACE",
  4336. "space prohibited before open square bracket '['\n" . $herecurr) &&
  4337. $fix) {
  4338. $fixed[$fixlinenr] =~
  4339. s/^(\+.*?)\s+\[/$1\[/;
  4340. }
  4341. }
  4342. }
  4343. # check for spaces between functions and their parentheses.
  4344. while ($line =~ /($Ident)\s+\(/g) {
  4345. my $name = $1;
  4346. my $ctx_before = substr($line, 0, $-[1]);
  4347. my $ctx = "$ctx_before$name";
  4348. # Ignore those directives where spaces _are_ permitted.
  4349. if ($name =~ /^(?:
  4350. if|for|while|switch|return|case|
  4351. volatile|__volatile__|
  4352. __attribute__|format|__extension__|
  4353. asm|__asm__)$/x)
  4354. {
  4355. # cpp #define statements have non-optional spaces, ie
  4356. # if there is a space between the name and the open
  4357. # parenthesis it is simply not a parameter group.
  4358. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  4359. # cpp #elif statement condition may start with a (
  4360. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  4361. # If this whole things ends with a type its most
  4362. # likely a typedef for a function.
  4363. } elsif ($ctx =~ /$Type$/) {
  4364. } else {
  4365. if (WARN("SPACING",
  4366. "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
  4367. $fix) {
  4368. $fixed[$fixlinenr] =~
  4369. s/\b$name\s+\(/$name\(/;
  4370. }
  4371. }
  4372. }
  4373. # Check operator spacing.
  4374. if (!($line=~/\#\s*include/)) {
  4375. my $fixed_line = "";
  4376. my $line_fixed = 0;
  4377. my $ops = qr{
  4378. <<=|>>=|<=|>=|==|!=|
  4379. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  4380. =>|->|<<|>>|<|>|=|!|~|
  4381. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  4382. \?:|\?|:
  4383. }x;
  4384. my @elements = split(/($ops|;)/, $opline);
  4385. ## print("element count: <" . $#elements . ">\n");
  4386. ## foreach my $el (@elements) {
  4387. ## print("el: <$el>\n");
  4388. ## }
  4389. my @fix_elements = ();
  4390. my $off = 0;
  4391. foreach my $el (@elements) {
  4392. push(@fix_elements, substr($rawline, $off, length($el)));
  4393. $off += length($el);
  4394. }
  4395. $off = 0;
  4396. my $blank = copy_spacing($opline);
  4397. my $last_after = -1;
  4398. for (my $n = 0; $n < $#elements; $n += 2) {
  4399. my $good = $fix_elements[$n] . $fix_elements[$n + 1];
  4400. ## print("n: <$n> good: <$good>\n");
  4401. $off += length($elements[$n]);
  4402. # Pick up the preceding and succeeding characters.
  4403. my $ca = substr($opline, 0, $off);
  4404. my $cc = '';
  4405. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  4406. $cc = substr($opline, $off + length($elements[$n + 1]));
  4407. }
  4408. my $cb = "$ca$;$cc";
  4409. my $a = '';
  4410. $a = 'V' if ($elements[$n] ne '');
  4411. $a = 'W' if ($elements[$n] =~ /\s$/);
  4412. $a = 'C' if ($elements[$n] =~ /$;$/);
  4413. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  4414. $a = 'O' if ($elements[$n] eq '');
  4415. $a = 'E' if ($ca =~ /^\s*$/);
  4416. my $op = $elements[$n + 1];
  4417. my $c = '';
  4418. if (defined $elements[$n + 2]) {
  4419. $c = 'V' if ($elements[$n + 2] ne '');
  4420. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  4421. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  4422. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  4423. $c = 'O' if ($elements[$n + 2] eq '');
  4424. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  4425. } else {
  4426. $c = 'E';
  4427. }
  4428. my $ctx = "${a}x${c}";
  4429. my $at = "(ctx:$ctx)";
  4430. my $ptr = substr($blank, 0, $off) . "^";
  4431. my $hereptr = "$hereline$ptr\n";
  4432. # Pull out the value of this operator.
  4433. my $op_type = substr($curr_values, $off + 1, 1);
  4434. # Get the full operator variant.
  4435. my $opv = $op . substr($curr_vars, $off, 1);
  4436. # Ignore operators passed as parameters.
  4437. if ($op_type ne 'V' &&
  4438. $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
  4439. # # Ignore comments
  4440. # } elsif ($op =~ /^$;+$/) {
  4441. # ; should have either the end of line or a space or \ after it
  4442. } elsif ($op eq ';') {
  4443. if ($ctx !~ /.x[WEBC]/ &&
  4444. $cc !~ /^\\/ && $cc !~ /^;/) {
  4445. if (ERROR("SPACING",
  4446. "space required after that '$op' $at\n" . $hereptr)) {
  4447. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  4448. $line_fixed = 1;
  4449. }
  4450. }
  4451. # // is a comment
  4452. } elsif ($op eq '//') {
  4453. # : when part of a bitfield
  4454. } elsif ($opv eq ':B') {
  4455. # skip the bitfield test for now
  4456. # No spaces for:
  4457. # ->
  4458. } elsif ($op eq '->') {
  4459. if ($ctx =~ /Wx.|.xW/) {
  4460. if (ERROR("SPACING",
  4461. "spaces prohibited around that '$op' $at\n" . $hereptr)) {
  4462. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  4463. if (defined $fix_elements[$n + 2]) {
  4464. $fix_elements[$n + 2] =~ s/^\s+//;
  4465. }
  4466. $line_fixed = 1;
  4467. }
  4468. }
  4469. # , must not have a space before and must have a space on the right.
  4470. } elsif ($op eq ',') {
  4471. my $rtrim_before = 0;
  4472. my $space_after = 0;
  4473. if ($ctx =~ /Wx./) {
  4474. if (ERROR("SPACING",
  4475. "space prohibited before that '$op' $at\n" . $hereptr)) {
  4476. $line_fixed = 1;
  4477. $rtrim_before = 1;
  4478. }
  4479. }
  4480. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  4481. if (ERROR("SPACING",
  4482. "space required after that '$op' $at\n" . $hereptr)) {
  4483. $line_fixed = 1;
  4484. $last_after = $n;
  4485. $space_after = 1;
  4486. }
  4487. }
  4488. if ($rtrim_before || $space_after) {
  4489. if ($rtrim_before) {
  4490. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  4491. } else {
  4492. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
  4493. }
  4494. if ($space_after) {
  4495. $good .= " ";
  4496. }
  4497. }
  4498. # '*' as part of a type definition -- reported already.
  4499. } elsif ($opv eq '*_') {
  4500. #warn "'*' is part of type\n";
  4501. # unary operators should have a space before and
  4502. # none after. May be left adjacent to another
  4503. # unary operator, or a cast
  4504. } elsif ($op eq '!' || $op eq '~' ||
  4505. $opv eq '*U' || $opv eq '-U' ||
  4506. $opv eq '&U' || $opv eq '&&U') {
  4507. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  4508. if (ERROR("SPACING",
  4509. "space required before that '$op' $at\n" . $hereptr)) {
  4510. if ($n != $last_after + 2) {
  4511. $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
  4512. $line_fixed = 1;
  4513. }
  4514. }
  4515. }
  4516. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  4517. # A unary '*' may be const
  4518. } elsif ($ctx =~ /.xW/) {
  4519. if (ERROR("SPACING",
  4520. "space prohibited after that '$op' $at\n" . $hereptr)) {
  4521. $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
  4522. if (defined $fix_elements[$n + 2]) {
  4523. $fix_elements[$n + 2] =~ s/^\s+//;
  4524. }
  4525. $line_fixed = 1;
  4526. }
  4527. }
  4528. # unary ++ and unary -- are allowed no space on one side.
  4529. } elsif ($op eq '++' or $op eq '--') {
  4530. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  4531. if (ERROR("SPACING",
  4532. "space required one side of that '$op' $at\n" . $hereptr)) {
  4533. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  4534. $line_fixed = 1;
  4535. }
  4536. }
  4537. if ($ctx =~ /Wx[BE]/ ||
  4538. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  4539. if (ERROR("SPACING",
  4540. "space prohibited before that '$op' $at\n" . $hereptr)) {
  4541. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  4542. $line_fixed = 1;
  4543. }
  4544. }
  4545. if ($ctx =~ /ExW/) {
  4546. if (ERROR("SPACING",
  4547. "space prohibited after that '$op' $at\n" . $hereptr)) {
  4548. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
  4549. if (defined $fix_elements[$n + 2]) {
  4550. $fix_elements[$n + 2] =~ s/^\s+//;
  4551. }
  4552. $line_fixed = 1;
  4553. }
  4554. }
  4555. # << and >> may either have or not have spaces both sides
  4556. } elsif ($op eq '<<' or $op eq '>>' or
  4557. $op eq '&' or $op eq '^' or $op eq '|' or
  4558. $op eq '+' or $op eq '-' or
  4559. $op eq '*' or $op eq '/' or
  4560. $op eq '%')
  4561. {
  4562. if ($check) {
  4563. if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
  4564. if (CHK("SPACING",
  4565. "spaces preferred around that '$op' $at\n" . $hereptr)) {
  4566. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  4567. $fix_elements[$n + 2] =~ s/^\s+//;
  4568. $line_fixed = 1;
  4569. }
  4570. } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
  4571. if (CHK("SPACING",
  4572. "space preferred before that '$op' $at\n" . $hereptr)) {
  4573. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
  4574. $line_fixed = 1;
  4575. }
  4576. }
  4577. } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  4578. if (ERROR("SPACING",
  4579. "need consistent spacing around '$op' $at\n" . $hereptr)) {
  4580. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  4581. if (defined $fix_elements[$n + 2]) {
  4582. $fix_elements[$n + 2] =~ s/^\s+//;
  4583. }
  4584. $line_fixed = 1;
  4585. }
  4586. }
  4587. # A colon needs no spaces before when it is
  4588. # terminating a case value or a label.
  4589. } elsif ($opv eq ':C' || $opv eq ':L') {
  4590. if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
  4591. if (ERROR("SPACING",
  4592. "space prohibited before that '$op' $at\n" . $hereptr)) {
  4593. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  4594. $line_fixed = 1;
  4595. }
  4596. }
  4597. # All the others need spaces both sides.
  4598. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  4599. my $ok = 0;
  4600. # Ignore email addresses <foo@bar>
  4601. if (($op eq '<' &&
  4602. $cc =~ /^\S+\@\S+>/) ||
  4603. ($op eq '>' &&
  4604. $ca =~ /<\S+\@\S+$/))
  4605. {
  4606. $ok = 1;
  4607. }
  4608. # for asm volatile statements
  4609. # ignore a colon with another
  4610. # colon immediately before or after
  4611. if (($op eq ':') &&
  4612. ($ca =~ /:$/ || $cc =~ /^:/)) {
  4613. $ok = 1;
  4614. }
  4615. # messages are ERROR, but ?: are CHK
  4616. if ($ok == 0) {
  4617. my $msg_level = \&ERROR;
  4618. $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
  4619. if (&{$msg_level}("SPACING",
  4620. "spaces required around that '$op' $at\n" . $hereptr)) {
  4621. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  4622. if (defined $fix_elements[$n + 2]) {
  4623. $fix_elements[$n + 2] =~ s/^\s+//;
  4624. }
  4625. $line_fixed = 1;
  4626. }
  4627. }
  4628. }
  4629. $off += length($elements[$n + 1]);
  4630. ## print("n: <$n> GOOD: <$good>\n");
  4631. $fixed_line = $fixed_line . $good;
  4632. }
  4633. if (($#elements % 2) == 0) {
  4634. $fixed_line = $fixed_line . $fix_elements[$#elements];
  4635. }
  4636. if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
  4637. $fixed[$fixlinenr] = $fixed_line;
  4638. }
  4639. }
  4640. # check for whitespace before a non-naked semicolon
  4641. if ($line =~ /^\+.*\S\s+;\s*$/) {
  4642. if (WARN("SPACING",
  4643. "space prohibited before semicolon\n" . $herecurr) &&
  4644. $fix) {
  4645. 1 while $fixed[$fixlinenr] =~
  4646. s/^(\+.*\S)\s+;/$1;/;
  4647. }
  4648. }
  4649. # check for multiple assignments
  4650. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  4651. CHK("MULTIPLE_ASSIGNMENTS",
  4652. "multiple assignments should be avoided\n" . $herecurr);
  4653. }
  4654. ## # check for multiple declarations, allowing for a function declaration
  4655. ## # continuation.
  4656. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  4657. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  4658. ##
  4659. ## # Remove any bracketed sections to ensure we do not
  4660. ## # falsely report the parameters of functions.
  4661. ## my $ln = $line;
  4662. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  4663. ## }
  4664. ## if ($ln =~ /,/) {
  4665. ## WARN("MULTIPLE_DECLARATION",
  4666. ## "declaring multiple variables together should be avoided\n" . $herecurr);
  4667. ## }
  4668. ## }
  4669. #need space before brace following if, while, etc
  4670. if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
  4671. $line =~ /\b(?:else|do)\{/) {
  4672. if (ERROR("SPACING",
  4673. "space required before the open brace '{'\n" . $herecurr) &&
  4674. $fix) {
  4675. $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
  4676. }
  4677. }
  4678. ## # check for blank lines before declarations
  4679. ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
  4680. ## $prevrawline =~ /^.\s*$/) {
  4681. ## WARN("SPACING",
  4682. ## "No blank lines before declarations\n" . $hereprev);
  4683. ## }
  4684. ##
  4685. # closing brace should have a space following it when it has anything
  4686. # on the line
  4687. if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
  4688. if (ERROR("SPACING",
  4689. "space required after that close brace '}'\n" . $herecurr) &&
  4690. $fix) {
  4691. $fixed[$fixlinenr] =~
  4692. s/}((?!(?:,|;|\)))\S)/} $1/;
  4693. }
  4694. }
  4695. # check spacing on square brackets
  4696. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  4697. if (ERROR("SPACING",
  4698. "space prohibited after that open square bracket '['\n" . $herecurr) &&
  4699. $fix) {
  4700. $fixed[$fixlinenr] =~
  4701. s/\[\s+/\[/;
  4702. }
  4703. }
  4704. if ($line =~ /\s\]/) {
  4705. if (ERROR("SPACING",
  4706. "space prohibited before that close square bracket ']'\n" . $herecurr) &&
  4707. $fix) {
  4708. $fixed[$fixlinenr] =~
  4709. s/\s+\]/\]/;
  4710. }
  4711. }
  4712. # check spacing on parentheses
  4713. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  4714. $line !~ /for\s*\(\s+;/) {
  4715. if (ERROR("SPACING",
  4716. "space prohibited after that open parenthesis '('\n" . $herecurr) &&
  4717. $fix) {
  4718. $fixed[$fixlinenr] =~
  4719. s/\(\s+/\(/;
  4720. }
  4721. }
  4722. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  4723. $line !~ /for\s*\(.*;\s+\)/ &&
  4724. $line !~ /:\s+\)/) {
  4725. if (ERROR("SPACING",
  4726. "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
  4727. $fix) {
  4728. $fixed[$fixlinenr] =~
  4729. s/\s+\)/\)/;
  4730. }
  4731. }
  4732. # check unnecessary parentheses around addressof/dereference single $Lvals
  4733. # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
  4734. while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
  4735. my $var = $1;
  4736. if (CHK("UNNECESSARY_PARENTHESES",
  4737. "Unnecessary parentheses around $var\n" . $herecurr) &&
  4738. $fix) {
  4739. $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
  4740. }
  4741. }
  4742. # check for unnecessary parentheses around function pointer uses
  4743. # ie: (foo->bar)(); should be foo->bar();
  4744. # but not "if (foo->bar) (" to avoid some false positives
  4745. if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
  4746. my $var = $2;
  4747. if (CHK("UNNECESSARY_PARENTHESES",
  4748. "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
  4749. $fix) {
  4750. my $var2 = deparenthesize($var);
  4751. $var2 =~ s/\s//g;
  4752. $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
  4753. }
  4754. }
  4755. # check for unnecessary parentheses around comparisons in if uses
  4756. # when !drivers/staging or command-line uses --strict
  4757. if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
  4758. $perl_version_ok && defined($stat) &&
  4759. $stat =~ /(^.\s*if\s*($balanced_parens))/) {
  4760. my $if_stat = $1;
  4761. my $test = substr($2, 1, -1);
  4762. my $herectx;
  4763. while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
  4764. my $match = $1;
  4765. # avoid parentheses around potential macro args
  4766. next if ($match =~ /^\s*\w+\s*$/);
  4767. if (!defined($herectx)) {
  4768. $herectx = $here . "\n";
  4769. my $cnt = statement_rawlines($if_stat);
  4770. for (my $n = 0; $n < $cnt; $n++) {
  4771. my $rl = raw_line($linenr, $n);
  4772. $herectx .= $rl . "\n";
  4773. last if $rl =~ /^[ \+].*\{/;
  4774. }
  4775. }
  4776. CHK("UNNECESSARY_PARENTHESES",
  4777. "Unnecessary parentheses around '$match'\n" . $herectx);
  4778. }
  4779. }
  4780. # check that goto labels aren't indented (allow a single space indentation)
  4781. # and ignore bitfield definitions like foo:1
  4782. # Strictly, labels can have whitespace after the identifier and before the :
  4783. # but this is not allowed here as many ?: uses would appear to be labels
  4784. if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
  4785. $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
  4786. $sline !~ /^.\s+default:/) {
  4787. if (WARN("INDENTED_LABEL",
  4788. "labels should not be indented\n" . $herecurr) &&
  4789. $fix) {
  4790. $fixed[$fixlinenr] =~
  4791. s/^(.)\s+/$1/;
  4792. }
  4793. }
  4794. # check if a statement with a comma should be two statements like:
  4795. # foo = bar(), /* comma should be semicolon */
  4796. # bar = baz();
  4797. if (defined($stat) &&
  4798. $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
  4799. my $cnt = statement_rawlines($stat);
  4800. my $herectx = get_stat_here($linenr, $cnt, $here);
  4801. WARN("SUSPECT_COMMA_SEMICOLON",
  4802. "Possible comma where semicolon could be used\n" . $herectx);
  4803. }
  4804. # return is not a function
  4805. if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
  4806. my $spacing = $1;
  4807. if ($perl_version_ok &&
  4808. $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
  4809. my $value = $1;
  4810. $value = deparenthesize($value);
  4811. if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
  4812. ERROR("RETURN_PARENTHESES",
  4813. "return is not a function, parentheses are not required\n" . $herecurr);
  4814. }
  4815. } elsif ($spacing !~ /\s+/) {
  4816. ERROR("SPACING",
  4817. "space required before the open parenthesis '('\n" . $herecurr);
  4818. }
  4819. }
  4820. # unnecessary return in a void function
  4821. # at end-of-function, with the previous line a single leading tab, then return;
  4822. # and the line before that not a goto label target like "out:"
  4823. if ($sline =~ /^[ \+]}\s*$/ &&
  4824. $prevline =~ /^\+\treturn\s*;\s*$/ &&
  4825. $linenr >= 3 &&
  4826. $lines[$linenr - 3] =~ /^[ +]/ &&
  4827. $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
  4828. WARN("RETURN_VOID",
  4829. "void function return statements are not generally useful\n" . $hereprev);
  4830. }
  4831. # if statements using unnecessary parentheses - ie: if ((foo == bar))
  4832. if ($perl_version_ok &&
  4833. $line =~ /\bif\s*((?:\(\s*){2,})/) {
  4834. my $openparens = $1;
  4835. my $count = $openparens =~ tr@\(@\(@;
  4836. my $msg = "";
  4837. if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
  4838. my $comp = $4; #Not $1 because of $LvalOrFunc
  4839. $msg = " - maybe == should be = ?" if ($comp eq "==");
  4840. WARN("UNNECESSARY_PARENTHESES",
  4841. "Unnecessary parentheses$msg\n" . $herecurr);
  4842. }
  4843. }
  4844. # comparisons with a constant or upper case identifier on the left
  4845. # avoid cases like "foo + BAR < baz"
  4846. # only fix matches surrounded by parentheses to avoid incorrect
  4847. # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
  4848. if ($perl_version_ok &&
  4849. $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
  4850. my $lead = $1;
  4851. my $const = $2;
  4852. my $comp = $3;
  4853. my $to = $4;
  4854. my $newcomp = $comp;
  4855. if ($lead !~ /(?:$Operators|\.)\s*$/ &&
  4856. $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
  4857. WARN("CONSTANT_COMPARISON",
  4858. "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
  4859. $fix) {
  4860. if ($comp eq "<") {
  4861. $newcomp = ">";
  4862. } elsif ($comp eq "<=") {
  4863. $newcomp = ">=";
  4864. } elsif ($comp eq ">") {
  4865. $newcomp = "<";
  4866. } elsif ($comp eq ">=") {
  4867. $newcomp = "<=";
  4868. }
  4869. $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
  4870. }
  4871. }
  4872. # Return of what appears to be an errno should normally be negative
  4873. if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
  4874. my $name = $1;
  4875. if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
  4876. WARN("USE_NEGATIVE_ERRNO",
  4877. "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
  4878. }
  4879. }
  4880. # Need a space before open parenthesis after if, while etc
  4881. if ($line =~ /\b(if|while|for|switch)\(/) {
  4882. if (ERROR("SPACING",
  4883. "space required before the open parenthesis '('\n" . $herecurr) &&
  4884. $fix) {
  4885. $fixed[$fixlinenr] =~
  4886. s/\b(if|while|for|switch)\(/$1 \(/;
  4887. }
  4888. }
  4889. # Check for illegal assignment in if conditional -- and check for trailing
  4890. # statements after the conditional.
  4891. if ($line =~ /do\s*(?!{)/) {
  4892. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  4893. ctx_statement_block($linenr, $realcnt, 0)
  4894. if (!defined $stat);
  4895. my ($stat_next) = ctx_statement_block($line_nr_next,
  4896. $remain_next, $off_next);
  4897. $stat_next =~ s/\n./\n /g;
  4898. ##print "stat<$stat> stat_next<$stat_next>\n";
  4899. if ($stat_next =~ /^\s*while\b/) {
  4900. # If the statement carries leading newlines,
  4901. # then count those as offsets.
  4902. my ($whitespace) =
  4903. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  4904. my $offset =
  4905. statement_rawlines($whitespace) - 1;
  4906. $suppress_whiletrailers{$line_nr_next +
  4907. $offset} = 1;
  4908. }
  4909. }
  4910. if (!defined $suppress_whiletrailers{$linenr} &&
  4911. defined($stat) && defined($cond) &&
  4912. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  4913. my ($s, $c) = ($stat, $cond);
  4914. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  4915. if (ERROR("ASSIGN_IN_IF",
  4916. "do not use assignment in if condition\n" . $herecurr) &&
  4917. $fix && $perl_version_ok) {
  4918. if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
  4919. my $space = $1;
  4920. my $not = $2;
  4921. my $statement = $3;
  4922. my $assigned = $4;
  4923. my $test = $8;
  4924. my $against = $9;
  4925. my $brace = $15;
  4926. fix_delete_line($fixlinenr, $rawline);
  4927. fix_insert_line($fixlinenr, "$space$statement;");
  4928. my $newline = "${space}if (";
  4929. $newline .= '!' if defined($not);
  4930. $newline .= '(' if (defined $not && defined($test) && defined($against));
  4931. $newline .= "$assigned";
  4932. $newline .= " $test $against" if (defined($test) && defined($against));
  4933. $newline .= ')' if (defined $not && defined($test) && defined($against));
  4934. $newline .= ')';
  4935. $newline .= " {" if (defined($brace));
  4936. fix_insert_line($fixlinenr + 1, $newline);
  4937. }
  4938. }
  4939. }
  4940. # Find out what is on the end of the line after the
  4941. # conditional.
  4942. substr($s, 0, length($c), '');
  4943. $s =~ s/\n.*//g;
  4944. $s =~ s/$;//g; # Remove any comments
  4945. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  4946. $c !~ /}\s*while\s*/)
  4947. {
  4948. # Find out how long the conditional actually is.
  4949. my @newlines = ($c =~ /\n/gs);
  4950. my $cond_lines = 1 + $#newlines;
  4951. my $stat_real = '';
  4952. $stat_real = raw_line($linenr, $cond_lines)
  4953. . "\n" if ($cond_lines);
  4954. if (defined($stat_real) && $cond_lines > 1) {
  4955. $stat_real = "[...]\n$stat_real";
  4956. }
  4957. ERROR("TRAILING_STATEMENTS",
  4958. "trailing statements should be on next line\n" . $herecurr . $stat_real);
  4959. }
  4960. }
  4961. # Check for bitwise tests written as boolean
  4962. if ($line =~ /
  4963. (?:
  4964. (?:\[|\(|\&\&|\|\|)
  4965. \s*0[xX][0-9]+\s*
  4966. (?:\&\&|\|\|)
  4967. |
  4968. (?:\&\&|\|\|)
  4969. \s*0[xX][0-9]+\s*
  4970. (?:\&\&|\|\||\)|\])
  4971. )/x)
  4972. {
  4973. WARN("HEXADECIMAL_BOOLEAN_TEST",
  4974. "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  4975. }
  4976. # if and else should not have general statements after it
  4977. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  4978. my $s = $1;
  4979. $s =~ s/$;//g; # Remove any comments
  4980. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  4981. ERROR("TRAILING_STATEMENTS",
  4982. "trailing statements should be on next line\n" . $herecurr);
  4983. }
  4984. }
  4985. # if should not continue a brace
  4986. if ($line =~ /}\s*if\b/) {
  4987. ERROR("TRAILING_STATEMENTS",
  4988. "trailing statements should be on next line (or did you mean 'else if'?)\n" .
  4989. $herecurr);
  4990. }
  4991. # case and default should not have general statements after them
  4992. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  4993. $line !~ /\G(?:
  4994. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  4995. \s*return\s+
  4996. )/xg)
  4997. {
  4998. ERROR("TRAILING_STATEMENTS",
  4999. "trailing statements should be on next line\n" . $herecurr);
  5000. }
  5001. # Check for }<nl>else {, these must be at the same
  5002. # indent level to be relevant to each other.
  5003. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
  5004. $previndent == $indent) {
  5005. if (ERROR("ELSE_AFTER_BRACE",
  5006. "else should follow close brace '}'\n" . $hereprev) &&
  5007. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  5008. fix_delete_line($fixlinenr - 1, $prevrawline);
  5009. fix_delete_line($fixlinenr, $rawline);
  5010. my $fixedline = $prevrawline;
  5011. $fixedline =~ s/}\s*$//;
  5012. if ($fixedline !~ /^\+\s*$/) {
  5013. fix_insert_line($fixlinenr, $fixedline);
  5014. }
  5015. $fixedline = $rawline;
  5016. $fixedline =~ s/^(.\s*)else/$1} else/;
  5017. fix_insert_line($fixlinenr, $fixedline);
  5018. }
  5019. }
  5020. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
  5021. $previndent == $indent) {
  5022. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  5023. # Find out what is on the end of the line after the
  5024. # conditional.
  5025. substr($s, 0, length($c), '');
  5026. $s =~ s/\n.*//g;
  5027. if ($s =~ /^\s*;/) {
  5028. if (ERROR("WHILE_AFTER_BRACE",
  5029. "while should follow close brace '}'\n" . $hereprev) &&
  5030. $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
  5031. fix_delete_line($fixlinenr - 1, $prevrawline);
  5032. fix_delete_line($fixlinenr, $rawline);
  5033. my $fixedline = $prevrawline;
  5034. my $trailing = $rawline;
  5035. $trailing =~ s/^\+//;
  5036. $trailing = trim($trailing);
  5037. $fixedline =~ s/}\s*$/} $trailing/;
  5038. fix_insert_line($fixlinenr, $fixedline);
  5039. }
  5040. }
  5041. }
  5042. #Specific variable tests
  5043. while ($line =~ m{($Constant|$Lval)}g) {
  5044. my $var = $1;
  5045. #CamelCase
  5046. if ($var !~ /^$Constant$/ &&
  5047. $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
  5048. #Ignore some autogenerated defines and enum values
  5049. $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
  5050. #Ignore Page<foo> variants
  5051. $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
  5052. #Ignore SI style variants like nS, mV and dB
  5053. #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
  5054. $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
  5055. #Ignore some three character SI units explicitly, like MiB and KHz
  5056. $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
  5057. while ($var =~ m{($Ident)}g) {
  5058. my $word = $1;
  5059. next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
  5060. if ($check) {
  5061. seed_camelcase_includes();
  5062. if (!$file && !$camelcase_file_seeded) {
  5063. seed_camelcase_file($realfile);
  5064. $camelcase_file_seeded = 1;
  5065. }
  5066. }
  5067. if (!defined $camelcase{$word}) {
  5068. $camelcase{$word} = 1;
  5069. CHK("CAMELCASE",
  5070. "Avoid CamelCase: <$word>\n" . $herecurr);
  5071. }
  5072. }
  5073. }
  5074. }
  5075. #no spaces allowed after \ in define
  5076. if ($line =~ /\#\s*define.*\\\s+$/) {
  5077. if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
  5078. "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
  5079. $fix) {
  5080. $fixed[$fixlinenr] =~ s/\s+$//;
  5081. }
  5082. }
  5083. # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
  5084. # itself <asm/foo.h> (uses RAW line)
  5085. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  5086. my $file = "$1.h";
  5087. my $checkfile = "include/linux/$file";
  5088. if (-f "$root/$checkfile" &&
  5089. $realfile ne $checkfile &&
  5090. $1 !~ /$allowed_asm_includes/)
  5091. {
  5092. my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
  5093. if ($asminclude > 0) {
  5094. if ($realfile =~ m{^arch/}) {
  5095. CHK("ARCH_INCLUDE_LINUX",
  5096. "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  5097. } else {
  5098. WARN("INCLUDE_LINUX",
  5099. "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  5100. }
  5101. }
  5102. }
  5103. }
  5104. # multi-statement macros should be enclosed in a do while loop, grab the
  5105. # first statement and ensure its the whole macro if its not enclosed
  5106. # in a known good container
  5107. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  5108. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  5109. my $ln = $linenr;
  5110. my $cnt = $realcnt;
  5111. my ($off, $dstat, $dcond, $rest);
  5112. my $ctx = '';
  5113. my $has_flow_statement = 0;
  5114. my $has_arg_concat = 0;
  5115. ($dstat, $dcond, $ln, $cnt, $off) =
  5116. ctx_statement_block($linenr, $realcnt, 0);
  5117. $ctx = $dstat;
  5118. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  5119. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  5120. $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
  5121. $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
  5122. $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
  5123. my $define_args = $1;
  5124. my $define_stmt = $dstat;
  5125. my @def_args = ();
  5126. if (defined $define_args && $define_args ne "") {
  5127. $define_args = substr($define_args, 1, length($define_args) - 2);
  5128. $define_args =~ s/\s*//g;
  5129. $define_args =~ s/\\\+?//g;
  5130. @def_args = split(",", $define_args);
  5131. }
  5132. $dstat =~ s/$;//g;
  5133. $dstat =~ s/\\\n.//g;
  5134. $dstat =~ s/^\s*//s;
  5135. $dstat =~ s/\s*$//s;
  5136. # Flatten any parentheses and braces
  5137. while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
  5138. $dstat =~ s/\{[^\{\}]*\}/1u/ ||
  5139. $dstat =~ s/.\[[^\[\]]*\]/1u/)
  5140. {
  5141. }
  5142. # Flatten any obvious string concatenation.
  5143. while ($dstat =~ s/($String)\s*$Ident/$1/ ||
  5144. $dstat =~ s/$Ident\s*($String)/$1/)
  5145. {
  5146. }
  5147. # Make asm volatile uses seem like a generic function
  5148. $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
  5149. my $exceptions = qr{
  5150. $Declare|
  5151. module_param_named|
  5152. MODULE_PARM_DESC|
  5153. DECLARE_PER_CPU|
  5154. DEFINE_PER_CPU|
  5155. __typeof__\(|
  5156. union|
  5157. struct|
  5158. \.$Ident\s*=\s*|
  5159. ^\"|\"$|
  5160. ^\[
  5161. }x;
  5162. #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
  5163. $ctx =~ s/\n*$//;
  5164. my $stmt_cnt = statement_rawlines($ctx);
  5165. my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
  5166. if ($dstat ne '' &&
  5167. $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
  5168. $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
  5169. $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
  5170. $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
  5171. $dstat !~ /$exceptions/ &&
  5172. $dstat !~ /^\.$Ident\s*=/ && # .foo =
  5173. $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
  5174. $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
  5175. $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
  5176. $dstat !~ /^for\s*$Constant$/ && # for (...)
  5177. $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
  5178. $dstat !~ /^do\s*{/ && # do {...
  5179. $dstat !~ /^\(\{/ && # ({...
  5180. $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
  5181. {
  5182. if ($dstat =~ /^\s*if\b/) {
  5183. ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
  5184. "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
  5185. } elsif ($dstat =~ /;/) {
  5186. ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
  5187. "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
  5188. } else {
  5189. ERROR("COMPLEX_MACRO",
  5190. "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
  5191. }
  5192. }
  5193. # Make $define_stmt single line, comment-free, etc
  5194. my @stmt_array = split('\n', $define_stmt);
  5195. my $first = 1;
  5196. $define_stmt = "";
  5197. foreach my $l (@stmt_array) {
  5198. $l =~ s/\\$//;
  5199. if ($first) {
  5200. $define_stmt = $l;
  5201. $first = 0;
  5202. } elsif ($l =~ /^[\+ ]/) {
  5203. $define_stmt .= substr($l, 1);
  5204. }
  5205. }
  5206. $define_stmt =~ s/$;//g;
  5207. $define_stmt =~ s/\s+/ /g;
  5208. $define_stmt = trim($define_stmt);
  5209. # check if any macro arguments are reused (ignore '...' and 'type')
  5210. foreach my $arg (@def_args) {
  5211. next if ($arg =~ /\.\.\./);
  5212. next if ($arg =~ /^type$/i);
  5213. my $tmp_stmt = $define_stmt;
  5214. $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;
  5215. $tmp_stmt =~ s/\#+\s*$arg\b//g;
  5216. $tmp_stmt =~ s/\b$arg\s*\#\#//g;
  5217. my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
  5218. if ($use_cnt > 1) {
  5219. CHK("MACRO_ARG_REUSE",
  5220. "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
  5221. }
  5222. # check if any macro arguments may have other precedence issues
  5223. if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
  5224. ((defined($1) && $1 ne ',') ||
  5225. (defined($2) && $2 ne ','))) {
  5226. CHK("MACRO_ARG_PRECEDENCE",
  5227. "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
  5228. }
  5229. }
  5230. # check for macros with flow control, but without ## concatenation
  5231. # ## concatenation is commonly a macro that defines a function so ignore those
  5232. if ($has_flow_statement && !$has_arg_concat) {
  5233. my $cnt = statement_rawlines($ctx);
  5234. my $herectx = get_stat_here($linenr, $cnt, $here);
  5235. WARN("MACRO_WITH_FLOW_CONTROL",
  5236. "Macros with flow control statements should be avoided\n" . "$herectx");
  5237. }
  5238. # check for line continuations outside of #defines, preprocessor #, and asm
  5239. } else {
  5240. if ($prevline !~ /^..*\\$/ &&
  5241. $line !~ /^\+\s*\#.*\\$/ && # preprocessor
  5242. $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
  5243. $line =~ /^\+.*\\$/) {
  5244. WARN("LINE_CONTINUATIONS",
  5245. "Avoid unnecessary line continuations\n" . $herecurr);
  5246. }
  5247. }
  5248. # do {} while (0) macro tests:
  5249. # single-statement macros do not need to be enclosed in do while (0) loop,
  5250. # macro should not end with a semicolon
  5251. if ($perl_version_ok &&
  5252. $realfile !~ m@/vmlinux.lds.h$@ &&
  5253. $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
  5254. my $ln = $linenr;
  5255. my $cnt = $realcnt;
  5256. my ($off, $dstat, $dcond, $rest);
  5257. my $ctx = '';
  5258. ($dstat, $dcond, $ln, $cnt, $off) =
  5259. ctx_statement_block($linenr, $realcnt, 0);
  5260. $ctx = $dstat;
  5261. $dstat =~ s/\\\n.//g;
  5262. $dstat =~ s/$;/ /g;
  5263. if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
  5264. my $stmts = $2;
  5265. my $semis = $3;
  5266. $ctx =~ s/\n*$//;
  5267. my $cnt = statement_rawlines($ctx);
  5268. my $herectx = get_stat_here($linenr, $cnt, $here);
  5269. if (($stmts =~ tr/;/;/) == 1 &&
  5270. $stmts !~ /^\s*(if|while|for|switch)\b/) {
  5271. WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
  5272. "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
  5273. }
  5274. if (defined $semis && $semis ne "") {
  5275. WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
  5276. "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
  5277. }
  5278. } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
  5279. $ctx =~ s/\n*$//;
  5280. my $cnt = statement_rawlines($ctx);
  5281. my $herectx = get_stat_here($linenr, $cnt, $here);
  5282. WARN("TRAILING_SEMICOLON",
  5283. "macros should not use a trailing semicolon\n" . "$herectx");
  5284. }
  5285. }
  5286. # check for redundant bracing round if etc
  5287. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  5288. my ($level, $endln, @chunks) =
  5289. ctx_statement_full($linenr, $realcnt, 1);
  5290. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  5291. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  5292. if ($#chunks > 0 && $level == 0) {
  5293. my @allowed = ();
  5294. my $allow = 0;
  5295. my $seen = 0;
  5296. my $herectx = $here . "\n";
  5297. my $ln = $linenr - 1;
  5298. for my $chunk (@chunks) {
  5299. my ($cond, $block) = @{$chunk};
  5300. # If the condition carries leading newlines, then count those as offsets.
  5301. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  5302. my $offset = statement_rawlines($whitespace) - 1;
  5303. $allowed[$allow] = 0;
  5304. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  5305. # We have looked at and allowed this specific line.
  5306. $suppress_ifbraces{$ln + $offset} = 1;
  5307. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  5308. $ln += statement_rawlines($block) - 1;
  5309. substr($block, 0, length($cond), '');
  5310. $seen++ if ($block =~ /^\s*{/);
  5311. #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
  5312. if (statement_lines($cond) > 1) {
  5313. #print "APW: ALLOWED: cond<$cond>\n";
  5314. $allowed[$allow] = 1;
  5315. }
  5316. if ($block =~/\b(?:if|for|while)\b/) {
  5317. #print "APW: ALLOWED: block<$block>\n";
  5318. $allowed[$allow] = 1;
  5319. }
  5320. if (statement_block_size($block) > 1) {
  5321. #print "APW: ALLOWED: lines block<$block>\n";
  5322. $allowed[$allow] = 1;
  5323. }
  5324. $allow++;
  5325. }
  5326. if ($seen) {
  5327. my $sum_allowed = 0;
  5328. foreach (@allowed) {
  5329. $sum_allowed += $_;
  5330. }
  5331. if ($sum_allowed == 0) {
  5332. WARN("BRACES",
  5333. "braces {} are not necessary for any arm of this statement\n" . $herectx);
  5334. } elsif ($sum_allowed != $allow &&
  5335. $seen != $allow) {
  5336. CHK("BRACES",
  5337. "braces {} should be used on all arms of this statement\n" . $herectx);
  5338. }
  5339. }
  5340. }
  5341. }
  5342. if (!defined $suppress_ifbraces{$linenr - 1} &&
  5343. $line =~ /\b(if|while|for|else)\b/) {
  5344. my $allowed = 0;
  5345. # Check the pre-context.
  5346. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  5347. #print "APW: ALLOWED: pre<$1>\n";
  5348. $allowed = 1;
  5349. }
  5350. my ($level, $endln, @chunks) =
  5351. ctx_statement_full($linenr, $realcnt, $-[0]);
  5352. # Check the condition.
  5353. my ($cond, $block) = @{$chunks[0]};
  5354. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  5355. if (defined $cond) {
  5356. substr($block, 0, length($cond), '');
  5357. }
  5358. if (statement_lines($cond) > 1) {
  5359. #print "APW: ALLOWED: cond<$cond>\n";
  5360. $allowed = 1;
  5361. }
  5362. if ($block =~/\b(?:if|for|while)\b/) {
  5363. #print "APW: ALLOWED: block<$block>\n";
  5364. $allowed = 1;
  5365. }
  5366. if (statement_block_size($block) > 1) {
  5367. #print "APW: ALLOWED: lines block<$block>\n";
  5368. $allowed = 1;
  5369. }
  5370. # Check the post-context.
  5371. if (defined $chunks[1]) {
  5372. my ($cond, $block) = @{$chunks[1]};
  5373. if (defined $cond) {
  5374. substr($block, 0, length($cond), '');
  5375. }
  5376. if ($block =~ /^\s*\{/) {
  5377. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  5378. $allowed = 1;
  5379. }
  5380. }
  5381. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  5382. my $cnt = statement_rawlines($block);
  5383. my $herectx = get_stat_here($linenr, $cnt, $here);
  5384. WARN("BRACES",
  5385. "braces {} are not necessary for single statement blocks\n" . $herectx);
  5386. }
  5387. }
  5388. # check for single line unbalanced braces
  5389. if ($sline =~ /^.\s*\}\s*else\s*$/ ||
  5390. $sline =~ /^.\s*else\s*\{\s*$/) {
  5391. CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
  5392. }
  5393. # check for unnecessary blank lines around braces
  5394. if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
  5395. if (CHK("BRACES",
  5396. "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
  5397. $fix && $prevrawline =~ /^\+/) {
  5398. fix_delete_line($fixlinenr - 1, $prevrawline);
  5399. }
  5400. }
  5401. if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
  5402. if (CHK("BRACES",
  5403. "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
  5404. $fix) {
  5405. fix_delete_line($fixlinenr, $rawline);
  5406. }
  5407. }
  5408. # no volatiles please
  5409. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  5410. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  5411. WARN("VOLATILE",
  5412. "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
  5413. }
  5414. # Check for user-visible strings broken across lines, which breaks the ability
  5415. # to grep for the string. Make exceptions when the previous string ends in a
  5416. # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
  5417. # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
  5418. if ($line =~ /^\+\s*$String/ &&
  5419. $prevline =~ /"\s*$/ &&
  5420. $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
  5421. if (WARN("SPLIT_STRING",
  5422. "quoted string split across lines\n" . $hereprev) &&
  5423. $fix &&
  5424. $prevrawline =~ /^\+.*"\s*$/ &&
  5425. $last_coalesced_string_linenr != $linenr - 1) {
  5426. my $extracted_string = get_quoted_string($line, $rawline);
  5427. my $comma_close = "";
  5428. if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
  5429. $comma_close = $1;
  5430. }
  5431. fix_delete_line($fixlinenr - 1, $prevrawline);
  5432. fix_delete_line($fixlinenr, $rawline);
  5433. my $fixedline = $prevrawline;
  5434. $fixedline =~ s/"\s*$//;
  5435. $fixedline .= substr($extracted_string, 1) . trim($comma_close);
  5436. fix_insert_line($fixlinenr - 1, $fixedline);
  5437. $fixedline = $rawline;
  5438. $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
  5439. if ($fixedline !~ /\+\s*$/) {
  5440. fix_insert_line($fixlinenr, $fixedline);
  5441. }
  5442. $last_coalesced_string_linenr = $linenr;
  5443. }
  5444. }
  5445. # check for missing a space in a string concatenation
  5446. if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
  5447. WARN('MISSING_SPACE',
  5448. "break quoted strings at a space character\n" . $hereprev);
  5449. }
  5450. # check for an embedded function name in a string when the function is known
  5451. # This does not work very well for -f --file checking as it depends on patch
  5452. # context providing the function name or a single line form for in-file
  5453. # function declarations
  5454. if ($line =~ /^\+.*$String/ &&
  5455. defined($context_function) &&
  5456. get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
  5457. length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
  5458. WARN("EMBEDDED_FUNCTION_NAME",
  5459. "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
  5460. }
  5461. # check for unnecessary function tracing like uses
  5462. # This does not use $logFunctions because there are many instances like
  5463. # 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
  5464. if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
  5465. if (WARN("TRACING_LOGGING",
  5466. "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
  5467. $fix) {
  5468. fix_delete_line($fixlinenr, $rawline);
  5469. }
  5470. }
  5471. # check for spaces before a quoted newline
  5472. if ($rawline =~ /^.*\".*\s\\n/) {
  5473. if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
  5474. "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
  5475. $fix) {
  5476. $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
  5477. }
  5478. }
  5479. # concatenated string without spaces between elements
  5480. if ($line =~ /$String[A-Z_]/ ||
  5481. ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
  5482. if (CHK("CONCATENATED_STRING",
  5483. "Concatenated strings should use spaces between elements\n" . $herecurr) &&
  5484. $fix) {
  5485. while ($line =~ /($String)/g) {
  5486. my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
  5487. $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
  5488. $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
  5489. }
  5490. }
  5491. }
  5492. # uncoalesced string fragments
  5493. if ($line =~ /$String\s*[Lu]?"/) {
  5494. if (WARN("STRING_FRAGMENTS",
  5495. "Consecutive strings are generally better as a single string\n" . $herecurr) &&
  5496. $fix) {
  5497. while ($line =~ /($String)(?=\s*")/g) {
  5498. my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
  5499. $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
  5500. }
  5501. }
  5502. }
  5503. # check for non-standard and hex prefixed decimal printf formats
  5504. my $show_L = 1; #don't show the same defect twice
  5505. my $show_Z = 1;
  5506. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  5507. my $string = substr($rawline, $-[1], $+[1] - $-[1]);
  5508. $string =~ s/%%/__/g;
  5509. # check for %L
  5510. if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
  5511. WARN("PRINTF_L",
  5512. "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
  5513. $show_L = 0;
  5514. }
  5515. # check for %Z
  5516. if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
  5517. WARN("PRINTF_Z",
  5518. "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
  5519. $show_Z = 0;
  5520. }
  5521. # check for 0x<decimal>
  5522. if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
  5523. ERROR("PRINTF_0XDECIMAL",
  5524. "Prefixing 0x with decimal output is defective\n" . $herecurr);
  5525. }
  5526. }
  5527. # check for line continuations in quoted strings with odd counts of "
  5528. if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
  5529. WARN("LINE_CONTINUATIONS",
  5530. "Avoid line continuations in quoted strings\n" . $herecurr);
  5531. }
  5532. # warn about #if 0
  5533. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  5534. WARN("IF_0",
  5535. "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
  5536. }
  5537. # warn about #if 1
  5538. if ($line =~ /^.\s*\#\s*if\s+1\b/) {
  5539. WARN("IF_1",
  5540. "Consider removing the #if 1 and its #endif\n" . $herecurr);
  5541. }
  5542. # check for needless "if (<foo>) fn(<foo>)" uses
  5543. if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
  5544. my $tested = quotemeta($1);
  5545. my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
  5546. if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
  5547. my $func = $1;
  5548. if (WARN('NEEDLESS_IF',
  5549. "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
  5550. $fix) {
  5551. my $do_fix = 1;
  5552. my $leading_tabs = "";
  5553. my $new_leading_tabs = "";
  5554. if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
  5555. $leading_tabs = $1;
  5556. } else {
  5557. $do_fix = 0;
  5558. }
  5559. if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
  5560. $new_leading_tabs = $1;
  5561. if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
  5562. $do_fix = 0;
  5563. }
  5564. } else {
  5565. $do_fix = 0;
  5566. }
  5567. if ($do_fix) {
  5568. fix_delete_line($fixlinenr - 1, $prevrawline);
  5569. $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
  5570. }
  5571. }
  5572. }
  5573. }
  5574. # check for unnecessary "Out of Memory" messages
  5575. if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
  5576. $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
  5577. (defined $1 || defined $3) &&
  5578. $linenr > 3) {
  5579. my $testval = $2;
  5580. my $testline = $lines[$linenr - 3];
  5581. my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
  5582. # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
  5583. if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
  5584. $s !~ /\b__GFP_NOWARN\b/ ) {
  5585. WARN("OOM_MESSAGE",
  5586. "Possible unnecessary 'out of memory' message\n" . $hereprev);
  5587. }
  5588. }
  5589. # check for logging functions with KERN_<LEVEL>
  5590. if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
  5591. $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
  5592. my $level = $1;
  5593. if (WARN("UNNECESSARY_KERN_LEVEL",
  5594. "Possible unnecessary $level\n" . $herecurr) &&
  5595. $fix) {
  5596. $fixed[$fixlinenr] =~ s/\s*$level\s*//;
  5597. }
  5598. }
  5599. # check for logging continuations
  5600. if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
  5601. WARN("LOGGING_CONTINUATION",
  5602. "Avoid logging continuation uses where feasible\n" . $herecurr);
  5603. }
  5604. # check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
  5605. if (defined $stat &&
  5606. $line =~ /\b$logFunctions\s*\(/ &&
  5607. index($stat, '"') >= 0) {
  5608. my $lc = $stat =~ tr@\n@@;
  5609. $lc = $lc + $linenr;
  5610. my $stat_real = get_stat_real($linenr, $lc);
  5611. pos($stat_real) = index($stat_real, '"');
  5612. while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
  5613. my $pspec = $1;
  5614. my $h = $2;
  5615. my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
  5616. if (WARN("UNNECESSARY_MODIFIER",
  5617. "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
  5618. $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
  5619. my $nspec = $pspec;
  5620. $nspec =~ s/h//g;
  5621. $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
  5622. }
  5623. }
  5624. }
  5625. # check for mask then right shift without a parentheses
  5626. if ($perl_version_ok &&
  5627. $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
  5628. $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
  5629. WARN("MASK_THEN_SHIFT",
  5630. "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
  5631. }
  5632. # check for pointer comparisons to NULL
  5633. if ($perl_version_ok) {
  5634. while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
  5635. my $val = $1;
  5636. my $equal = "!";
  5637. $equal = "" if ($4 eq "!=");
  5638. if (CHK("COMPARISON_TO_NULL",
  5639. "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
  5640. $fix) {
  5641. $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
  5642. }
  5643. }
  5644. }
  5645. # check for bad placement of section $InitAttribute (e.g.: __initdata)
  5646. if ($line =~ /(\b$InitAttribute\b)/) {
  5647. my $attr = $1;
  5648. if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
  5649. my $ptr = $1;
  5650. my $var = $2;
  5651. if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
  5652. ERROR("MISPLACED_INIT",
  5653. "$attr should be placed after $var\n" . $herecurr)) ||
  5654. ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
  5655. WARN("MISPLACED_INIT",
  5656. "$attr should be placed after $var\n" . $herecurr))) &&
  5657. $fix) {
  5658. $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;
  5659. }
  5660. }
  5661. }
  5662. # check for $InitAttributeData (ie: __initdata) with const
  5663. if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
  5664. my $attr = $1;
  5665. $attr =~ /($InitAttributePrefix)(.*)/;
  5666. my $attr_prefix = $1;
  5667. my $attr_type = $2;
  5668. if (ERROR("INIT_ATTRIBUTE",
  5669. "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
  5670. $fix) {
  5671. $fixed[$fixlinenr] =~
  5672. s/$InitAttributeData/${attr_prefix}initconst/;
  5673. }
  5674. }
  5675. # check for $InitAttributeConst (ie: __initconst) without const
  5676. if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
  5677. my $attr = $1;
  5678. if (ERROR("INIT_ATTRIBUTE",
  5679. "Use of $attr requires a separate use of const\n" . $herecurr) &&
  5680. $fix) {
  5681. my $lead = $fixed[$fixlinenr] =~
  5682. /(^\+\s*(?:static\s+))/;
  5683. $lead = rtrim($1);
  5684. $lead = "$lead " if ($lead !~ /^\+$/);
  5685. $lead = "${lead}const ";
  5686. $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
  5687. }
  5688. }
  5689. # check for __read_mostly with const non-pointer (should just be const)
  5690. if ($line =~ /\b__read_mostly\b/ &&
  5691. $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
  5692. if (ERROR("CONST_READ_MOSTLY",
  5693. "Invalid use of __read_mostly with const type\n" . $herecurr) &&
  5694. $fix) {
  5695. $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
  5696. }
  5697. }
  5698. # don't use __constant_<foo> functions outside of include/uapi/
  5699. if ($realfile !~ m@^include/uapi/@ &&
  5700. $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
  5701. my $constant_func = $1;
  5702. my $func = $constant_func;
  5703. $func =~ s/^__constant_//;
  5704. if (WARN("CONSTANT_CONVERSION",
  5705. "$constant_func should be $func\n" . $herecurr) &&
  5706. $fix) {
  5707. $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
  5708. }
  5709. }
  5710. # prefer usleep_range over udelay
  5711. if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
  5712. my $delay = $1;
  5713. # ignore udelay's < 10, however
  5714. if (! ($delay < 10) ) {
  5715. CHK("USLEEP_RANGE",
  5716. "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
  5717. }
  5718. if ($delay > 2000) {
  5719. WARN("LONG_UDELAY",
  5720. "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
  5721. }
  5722. }
  5723. # warn about unexpectedly long msleep's
  5724. if ($line =~ /\bmsleep\s*\((\d+)\);/) {
  5725. if ($1 < 20) {
  5726. WARN("MSLEEP",
  5727. "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
  5728. }
  5729. }
  5730. # check for comparisons of jiffies
  5731. if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
  5732. WARN("JIFFIES_COMPARISON",
  5733. "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
  5734. }
  5735. # check for comparisons of get_jiffies_64()
  5736. if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
  5737. WARN("JIFFIES_COMPARISON",
  5738. "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
  5739. }
  5740. # warn about #ifdefs in C files
  5741. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  5742. # print "#ifdef in C files should be avoided\n";
  5743. # print "$herecurr";
  5744. # $clean = 0;
  5745. # }
  5746. # warn about spacing in #ifdefs
  5747. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  5748. if (ERROR("SPACING",
  5749. "exactly one space required after that #$1\n" . $herecurr) &&
  5750. $fix) {
  5751. $fixed[$fixlinenr] =~
  5752. s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
  5753. }
  5754. }
  5755. # check for spinlock_t definitions without a comment.
  5756. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  5757. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  5758. my $which = $1;
  5759. if (!ctx_has_comment($first_line, $linenr)) {
  5760. CHK("UNCOMMENTED_DEFINITION",
  5761. "$1 definition without comment\n" . $herecurr);
  5762. }
  5763. }
  5764. # check for memory barriers without a comment.
  5765. my $barriers = qr{
  5766. mb|
  5767. rmb|
  5768. wmb
  5769. }x;
  5770. my $barrier_stems = qr{
  5771. mb__before_atomic|
  5772. mb__after_atomic|
  5773. store_release|
  5774. load_acquire|
  5775. store_mb|
  5776. (?:$barriers)
  5777. }x;
  5778. my $all_barriers = qr{
  5779. (?:$barriers)|
  5780. smp_(?:$barrier_stems)|
  5781. virt_(?:$barrier_stems)
  5782. }x;
  5783. if ($line =~ /\b(?:$all_barriers)\s*\(/) {
  5784. if (!ctx_has_comment($first_line, $linenr)) {
  5785. WARN("MEMORY_BARRIER",
  5786. "memory barrier without comment\n" . $herecurr);
  5787. }
  5788. }
  5789. my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
  5790. if ($realfile !~ m@^include/asm-generic/@ &&
  5791. $realfile !~ m@/barrier\.h$@ &&
  5792. $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
  5793. $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
  5794. WARN("MEMORY_BARRIER",
  5795. "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
  5796. }
  5797. # check for waitqueue_active without a comment.
  5798. if ($line =~ /\bwaitqueue_active\s*\(/) {
  5799. if (!ctx_has_comment($first_line, $linenr)) {
  5800. WARN("WAITQUEUE_ACTIVE",
  5801. "waitqueue_active without comment\n" . $herecurr);
  5802. }
  5803. }
  5804. # check for data_race without a comment.
  5805. if ($line =~ /\bdata_race\s*\(/) {
  5806. if (!ctx_has_comment($first_line, $linenr)) {
  5807. WARN("DATA_RACE",
  5808. "data_race without comment\n" . $herecurr);
  5809. }
  5810. }
  5811. # check of hardware specific defines
  5812. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  5813. CHK("ARCH_DEFINES",
  5814. "architecture specific defines should be avoided\n" . $herecurr);
  5815. }
  5816. # check that the storage class is not after a type
  5817. if ($line =~ /\b($Type)\s+($Storage)\b/) {
  5818. WARN("STORAGE_CLASS",
  5819. "storage class '$2' should be located before type '$1'\n" . $herecurr);
  5820. }
  5821. # Check that the storage class is at the beginning of a declaration
  5822. if ($line =~ /\b$Storage\b/ &&
  5823. $line !~ /^.\s*$Storage/ &&
  5824. $line =~ /^.\s*(.+?)\$Storage\s/ &&
  5825. $1 !~ /[\,\)]\s*$/) {
  5826. WARN("STORAGE_CLASS",
  5827. "storage class should be at the beginning of the declaration\n" . $herecurr);
  5828. }
  5829. # check the location of the inline attribute, that it is between
  5830. # storage class and type.
  5831. if ($line =~ /\b$Type\s+$Inline\b/ ||
  5832. $line =~ /\b$Inline\s+$Storage\b/) {
  5833. ERROR("INLINE_LOCATION",
  5834. "inline keyword should sit between storage class and type\n" . $herecurr);
  5835. }
  5836. # Check for __inline__ and __inline, prefer inline
  5837. if ($realfile !~ m@\binclude/uapi/@ &&
  5838. $line =~ /\b(__inline__|__inline)\b/) {
  5839. if (WARN("INLINE",
  5840. "plain inline is preferred over $1\n" . $herecurr) &&
  5841. $fix) {
  5842. $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
  5843. }
  5844. }
  5845. # Check for compiler attributes
  5846. if ($realfile !~ m@\binclude/uapi/@ &&
  5847. $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
  5848. my $attr = $1;
  5849. $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
  5850. my %attr_list = (
  5851. "alias" => "__alias",
  5852. "aligned" => "__aligned",
  5853. "always_inline" => "__always_inline",
  5854. "assume_aligned" => "__assume_aligned",
  5855. "cold" => "__cold",
  5856. "const" => "__attribute_const__",
  5857. "copy" => "__copy",
  5858. "designated_init" => "__designated_init",
  5859. "externally_visible" => "__visible",
  5860. "format" => "printf|scanf",
  5861. "gnu_inline" => "__gnu_inline",
  5862. "malloc" => "__malloc",
  5863. "mode" => "__mode",
  5864. "no_caller_saved_registers" => "__no_caller_saved_registers",
  5865. "noclone" => "__noclone",
  5866. "noinline" => "noinline",
  5867. "nonstring" => "__nonstring",
  5868. "noreturn" => "__noreturn",
  5869. "packed" => "__packed",
  5870. "pure" => "__pure",
  5871. "section" => "__section",
  5872. "used" => "__used",
  5873. "weak" => "__weak"
  5874. );
  5875. while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
  5876. my $orig_attr = $1;
  5877. my $params = '';
  5878. $params = $2 if defined($2);
  5879. my $curr_attr = $orig_attr;
  5880. $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
  5881. if (exists($attr_list{$curr_attr})) {
  5882. my $new = $attr_list{$curr_attr};
  5883. if ($curr_attr eq "format" && $params) {
  5884. $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
  5885. $new = "__$1\($2";
  5886. } else {
  5887. $new = "$new$params";
  5888. }
  5889. if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
  5890. "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
  5891. $fix) {
  5892. my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
  5893. $fixed[$fixlinenr] =~ s/$remove//;
  5894. $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
  5895. $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
  5896. $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
  5897. }
  5898. }
  5899. }
  5900. # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
  5901. if ($attr =~ /^_*unused/) {
  5902. WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
  5903. "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
  5904. }
  5905. }
  5906. # Check for __attribute__ weak, or __weak declarations (may have link issues)
  5907. if ($perl_version_ok &&
  5908. $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
  5909. ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
  5910. $line =~ /\b__weak\b/)) {
  5911. ERROR("WEAK_DECLARATION",
  5912. "Using weak declarations can have unintended link defects\n" . $herecurr);
  5913. }
  5914. # check for c99 types like uint8_t used outside of uapi/ and tools/
  5915. if ($realfile !~ m@\binclude/uapi/@ &&
  5916. $realfile !~ m@\btools/@ &&
  5917. $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
  5918. my $type = $1;
  5919. if ($type =~ /\b($typeC99Typedefs)\b/) {
  5920. $type = $1;
  5921. my $kernel_type = 'u';
  5922. $kernel_type = 's' if ($type =~ /^_*[si]/);
  5923. $type =~ /(\d+)/;
  5924. $kernel_type .= $1;
  5925. if (CHK("PREFER_KERNEL_TYPES",
  5926. "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
  5927. $fix) {
  5928. $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
  5929. }
  5930. }
  5931. }
  5932. # check for cast of C90 native int or longer types constants
  5933. if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
  5934. my $cast = $1;
  5935. my $const = $2;
  5936. my $suffix = "";
  5937. my $newconst = $const;
  5938. $newconst =~ s/${Int_type}$//;
  5939. $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
  5940. if ($cast =~ /\blong\s+long\b/) {
  5941. $suffix .= 'LL';
  5942. } elsif ($cast =~ /\blong\b/) {
  5943. $suffix .= 'L';
  5944. }
  5945. if (WARN("TYPECAST_INT_CONSTANT",
  5946. "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
  5947. $fix) {
  5948. $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
  5949. }
  5950. }
  5951. # check for sizeof(&)
  5952. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  5953. WARN("SIZEOF_ADDRESS",
  5954. "sizeof(& should be avoided\n" . $herecurr);
  5955. }
  5956. # check for sizeof without parenthesis
  5957. if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
  5958. if (WARN("SIZEOF_PARENTHESIS",
  5959. "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
  5960. $fix) {
  5961. $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
  5962. }
  5963. }
  5964. # check for struct spinlock declarations
  5965. if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
  5966. WARN("USE_SPINLOCK_T",
  5967. "struct spinlock should be spinlock_t\n" . $herecurr);
  5968. }
  5969. # check for seq_printf uses that could be seq_puts
  5970. if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
  5971. my $fmt = get_quoted_string($line, $rawline);
  5972. $fmt =~ s/%%//g;
  5973. if ($fmt !~ /%/) {
  5974. if (WARN("PREFER_SEQ_PUTS",
  5975. "Prefer seq_puts to seq_printf\n" . $herecurr) &&
  5976. $fix) {
  5977. $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
  5978. }
  5979. }
  5980. }
  5981. # check for vsprintf extension %p<foo> misuses
  5982. if ($perl_version_ok &&
  5983. defined $stat &&
  5984. $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
  5985. $1 !~ /^_*volatile_*$/) {
  5986. my $stat_real;
  5987. my $lc = $stat =~ tr@\n@@;
  5988. $lc = $lc + $linenr;
  5989. for (my $count = $linenr; $count <= $lc; $count++) {
  5990. my $specifier;
  5991. my $extension;
  5992. my $qualifier;
  5993. my $bad_specifier = "";
  5994. my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
  5995. $fmt =~ s/%%//g;
  5996. while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
  5997. $specifier = $1;
  5998. $extension = $2;
  5999. $qualifier = $3;
  6000. if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
  6001. ($extension eq "f" &&
  6002. defined $qualifier && $qualifier !~ /^w/) ||
  6003. ($extension eq "4" &&
  6004. defined $qualifier && $qualifier !~ /^cc/)) {
  6005. $bad_specifier = $specifier;
  6006. last;
  6007. }
  6008. if ($extension eq "x" && !defined($stat_real)) {
  6009. if (!defined($stat_real)) {
  6010. $stat_real = get_stat_real($linenr, $lc);
  6011. }
  6012. WARN("VSPRINTF_SPECIFIER_PX",
  6013. "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");
  6014. }
  6015. }
  6016. if ($bad_specifier ne "") {
  6017. my $stat_real = get_stat_real($linenr, $lc);
  6018. my $ext_type = "Invalid";
  6019. my $use = "";
  6020. if ($bad_specifier =~ /p[Ff]/) {
  6021. $use = " - use %pS instead";
  6022. $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
  6023. }
  6024. WARN("VSPRINTF_POINTER_EXTENSION",
  6025. "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
  6026. }
  6027. }
  6028. }
  6029. # Check for misused memsets
  6030. if ($perl_version_ok &&
  6031. defined $stat &&
  6032. $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
  6033. my $ms_addr = $2;
  6034. my $ms_val = $7;
  6035. my $ms_size = $12;
  6036. if ($ms_size =~ /^(0x|)0$/i) {
  6037. ERROR("MEMSET",
  6038. "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
  6039. } elsif ($ms_size =~ /^(0x|)1$/i) {
  6040. WARN("MEMSET",
  6041. "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
  6042. }
  6043. }
  6044. # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
  6045. # if ($perl_version_ok &&
  6046. # defined $stat &&
  6047. # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
  6048. # if (WARN("PREFER_ETHER_ADDR_COPY",
  6049. # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
  6050. # $fix) {
  6051. # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
  6052. # }
  6053. # }
  6054. # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
  6055. # if ($perl_version_ok &&
  6056. # defined $stat &&
  6057. # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
  6058. # WARN("PREFER_ETHER_ADDR_EQUAL",
  6059. # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
  6060. # }
  6061. # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
  6062. # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
  6063. # if ($perl_version_ok &&
  6064. # defined $stat &&
  6065. # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
  6066. #
  6067. # my $ms_val = $7;
  6068. #
  6069. # if ($ms_val =~ /^(?:0x|)0+$/i) {
  6070. # if (WARN("PREFER_ETH_ZERO_ADDR",
  6071. # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
  6072. # $fix) {
  6073. # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
  6074. # }
  6075. # } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
  6076. # if (WARN("PREFER_ETH_BROADCAST_ADDR",
  6077. # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
  6078. # $fix) {
  6079. # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
  6080. # }
  6081. # }
  6082. # }
  6083. # typecasts on min/max could be min_t/max_t
  6084. if ($perl_version_ok &&
  6085. defined $stat &&
  6086. $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
  6087. if (defined $2 || defined $7) {
  6088. my $call = $1;
  6089. my $cast1 = deparenthesize($2);
  6090. my $arg1 = $3;
  6091. my $cast2 = deparenthesize($7);
  6092. my $arg2 = $8;
  6093. my $cast;
  6094. if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
  6095. $cast = "$cast1 or $cast2";
  6096. } elsif ($cast1 ne "") {
  6097. $cast = $cast1;
  6098. } else {
  6099. $cast = $cast2;
  6100. }
  6101. WARN("MINMAX",
  6102. "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
  6103. }
  6104. }
  6105. # check usleep_range arguments
  6106. if ($perl_version_ok &&
  6107. defined $stat &&
  6108. $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
  6109. my $min = $1;
  6110. my $max = $7;
  6111. if ($min eq $max) {
  6112. WARN("USLEEP_RANGE",
  6113. "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
  6114. } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
  6115. $min > $max) {
  6116. WARN("USLEEP_RANGE",
  6117. "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
  6118. }
  6119. }
  6120. # check for naked sscanf
  6121. if ($perl_version_ok &&
  6122. defined $stat &&
  6123. $line =~ /\bsscanf\b/ &&
  6124. ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
  6125. $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
  6126. $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
  6127. my $lc = $stat =~ tr@\n@@;
  6128. $lc = $lc + $linenr;
  6129. my $stat_real = get_stat_real($linenr, $lc);
  6130. WARN("NAKED_SSCANF",
  6131. "unchecked sscanf return value\n" . "$here\n$stat_real\n");
  6132. }
  6133. # check for simple sscanf that should be kstrto<foo>
  6134. if ($perl_version_ok &&
  6135. defined $stat &&
  6136. $line =~ /\bsscanf\b/) {
  6137. my $lc = $stat =~ tr@\n@@;
  6138. $lc = $lc + $linenr;
  6139. my $stat_real = get_stat_real($linenr, $lc);
  6140. if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
  6141. my $format = $6;
  6142. my $count = $format =~ tr@%@%@;
  6143. if ($count == 1 &&
  6144. $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
  6145. WARN("SSCANF_TO_KSTRTO",
  6146. "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
  6147. }
  6148. }
  6149. }
  6150. # check for new externs in .h files.
  6151. if ($realfile =~ /\.h$/ &&
  6152. $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
  6153. if (CHK("AVOID_EXTERNS",
  6154. "extern prototypes should be avoided in .h files\n" . $herecurr) &&
  6155. $fix) {
  6156. $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
  6157. }
  6158. }
  6159. # check for new externs in .c files.
  6160. if ($realfile =~ /\.c$/ && defined $stat &&
  6161. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  6162. {
  6163. my $function_name = $1;
  6164. my $paren_space = $2;
  6165. my $s = $stat;
  6166. if (defined $cond) {
  6167. substr($s, 0, length($cond), '');
  6168. }
  6169. if ($s =~ /^\s*;/)
  6170. {
  6171. WARN("AVOID_EXTERNS",
  6172. "externs should be avoided in .c files\n" . $herecurr);
  6173. }
  6174. if ($paren_space =~ /\n/) {
  6175. WARN("FUNCTION_ARGUMENTS",
  6176. "arguments for function declarations should follow identifier\n" . $herecurr);
  6177. }
  6178. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  6179. $stat =~ /^.\s*extern\s+/)
  6180. {
  6181. WARN("AVOID_EXTERNS",
  6182. "externs should be avoided in .c files\n" . $herecurr);
  6183. }
  6184. # check for function declarations that have arguments without identifier names
  6185. if (defined $stat &&
  6186. $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
  6187. $1 ne "void") {
  6188. my $args = trim($1);
  6189. while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
  6190. my $arg = trim($1);
  6191. if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
  6192. WARN("FUNCTION_ARGUMENTS",
  6193. "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
  6194. }
  6195. }
  6196. }
  6197. # check for function definitions
  6198. if ($perl_version_ok &&
  6199. defined $stat &&
  6200. $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
  6201. $context_function = $1;
  6202. # check for multiline function definition with misplaced open brace
  6203. my $ok = 0;
  6204. my $cnt = statement_rawlines($stat);
  6205. my $herectx = $here . "\n";
  6206. for (my $n = 0; $n < $cnt; $n++) {
  6207. my $rl = raw_line($linenr, $n);
  6208. $herectx .= $rl . "\n";
  6209. $ok = 1 if ($rl =~ /^[ \+]\{/);
  6210. $ok = 1 if ($rl =~ /\{/ && $n == 0);
  6211. last if $rl =~ /^[ \+].*\{/;
  6212. }
  6213. if (!$ok) {
  6214. ERROR("OPEN_BRACE",
  6215. "open brace '{' following function definitions go on the next line\n" . $herectx);
  6216. }
  6217. }
  6218. # checks for new __setup's
  6219. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  6220. my $name = $1;
  6221. if (!grep(/$name/, @setup_docs)) {
  6222. CHK("UNDOCUMENTED_SETUP",
  6223. "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
  6224. }
  6225. }
  6226. # check for pointless casting of alloc functions
  6227. if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
  6228. WARN("UNNECESSARY_CASTS",
  6229. "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  6230. }
  6231. # alloc style
  6232. # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
  6233. if ($perl_version_ok &&
  6234. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
  6235. CHK("ALLOC_SIZEOF_STRUCT",
  6236. "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
  6237. }
  6238. # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
  6239. if ($perl_version_ok &&
  6240. defined $stat &&
  6241. $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
  6242. my $oldfunc = $3;
  6243. my $a1 = $4;
  6244. my $a2 = $10;
  6245. my $newfunc = "kmalloc_array";
  6246. $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
  6247. my $r1 = $a1;
  6248. my $r2 = $a2;
  6249. if ($a1 =~ /^sizeof\s*\S/) {
  6250. $r1 = $a2;
  6251. $r2 = $a1;
  6252. }
  6253. if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
  6254. !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
  6255. my $cnt = statement_rawlines($stat);
  6256. my $herectx = get_stat_here($linenr, $cnt, $here);
  6257. if (WARN("ALLOC_WITH_MULTIPLY",
  6258. "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
  6259. $cnt == 1 &&
  6260. $fix) {
  6261. $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;
  6262. }
  6263. }
  6264. }
  6265. # check for krealloc arg reuse
  6266. if ($perl_version_ok &&
  6267. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
  6268. $1 eq $3) {
  6269. WARN("KREALLOC_ARG_REUSE",
  6270. "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
  6271. }
  6272. # check for alloc argument mismatch
  6273. if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
  6274. WARN("ALLOC_ARRAY_ARGS",
  6275. "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
  6276. }
  6277. # check for multiple semicolons
  6278. if ($line =~ /;\s*;\s*$/) {
  6279. if (WARN("ONE_SEMICOLON",
  6280. "Statements terminations use 1 semicolon\n" . $herecurr) &&
  6281. $fix) {
  6282. $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
  6283. }
  6284. }
  6285. # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
  6286. if ($realfile !~ m@^include/uapi/@ &&
  6287. $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
  6288. my $ull = "";
  6289. $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
  6290. if (CHK("BIT_MACRO",
  6291. "Prefer using the BIT$ull macro\n" . $herecurr) &&
  6292. $fix) {
  6293. $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
  6294. }
  6295. }
  6296. # check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
  6297. if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
  6298. WARN("IS_ENABLED_CONFIG",
  6299. "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
  6300. }
  6301. # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
  6302. if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
  6303. my $config = $1;
  6304. if (WARN("PREFER_IS_ENABLED",
  6305. "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
  6306. $fix) {
  6307. $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
  6308. }
  6309. }
  6310. # check for /* fallthrough */ like comment, prefer fallthrough;
  6311. my @fallthroughs = (
  6312. 'fallthrough',
  6313. '@fallthrough@',
  6314. 'lint -fallthrough[ \t]*',
  6315. 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
  6316. '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
  6317. 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
  6318. 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
  6319. );
  6320. if ($raw_comment ne '') {
  6321. foreach my $ft (@fallthroughs) {
  6322. if ($raw_comment =~ /$ft/) {
  6323. my $msg_level = \&WARN;
  6324. $msg_level = \&CHK if ($file);
  6325. &{$msg_level}("PREFER_FALLTHROUGH",
  6326. "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
  6327. last;
  6328. }
  6329. }
  6330. }
  6331. # check for switch/default statements without a break;
  6332. if ($perl_version_ok &&
  6333. defined $stat &&
  6334. $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
  6335. my $cnt = statement_rawlines($stat);
  6336. my $herectx = get_stat_here($linenr, $cnt, $here);
  6337. WARN("DEFAULT_NO_BREAK",
  6338. "switch default: should use break\n" . $herectx);
  6339. }
  6340. # check for gcc specific __FUNCTION__
  6341. if ($line =~ /\b__FUNCTION__\b/) {
  6342. if (WARN("USE_FUNC",
  6343. "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
  6344. $fix) {
  6345. $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
  6346. }
  6347. }
  6348. # check for uses of __DATE__, __TIME__, __TIMESTAMP__
  6349. while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
  6350. ERROR("DATE_TIME",
  6351. "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
  6352. }
  6353. # check for use of yield()
  6354. if ($line =~ /\byield\s*\(\s*\)/) {
  6355. WARN("YIELD",
  6356. "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
  6357. }
  6358. # check for comparisons against true and false
  6359. if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
  6360. my $lead = $1;
  6361. my $arg = $2;
  6362. my $test = $3;
  6363. my $otype = $4;
  6364. my $trail = $5;
  6365. my $op = "!";
  6366. ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
  6367. my $type = lc($otype);
  6368. if ($type =~ /^(?:true|false)$/) {
  6369. if (("$test" eq "==" && "$type" eq "true") ||
  6370. ("$test" eq "!=" && "$type" eq "false")) {
  6371. $op = "";
  6372. }
  6373. CHK("BOOL_COMPARISON",
  6374. "Using comparison to $otype is error prone\n" . $herecurr);
  6375. ## maybe suggesting a correct construct would better
  6376. ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
  6377. }
  6378. }
  6379. # check for semaphores initialized locked
  6380. if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
  6381. WARN("CONSIDER_COMPLETION",
  6382. "consider using a completion\n" . $herecurr);
  6383. }
  6384. # recommend kstrto* over simple_strto* and strict_strto*
  6385. if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
  6386. WARN("CONSIDER_KSTRTO",
  6387. "$1 is obsolete, use k$3 instead\n" . $herecurr);
  6388. }
  6389. # check for __initcall(), use device_initcall() explicitly or more appropriate function please
  6390. if ($line =~ /^.\s*__initcall\s*\(/) {
  6391. WARN("USE_DEVICE_INITCALL",
  6392. "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
  6393. }
  6394. # check for spin_is_locked(), suggest lockdep instead
  6395. if ($line =~ /\bspin_is_locked\(/) {
  6396. WARN("USE_LOCKDEP",
  6397. "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
  6398. }
  6399. # check for deprecated apis
  6400. if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
  6401. my $deprecated_api = $1;
  6402. my $new_api = $deprecated_apis{$deprecated_api};
  6403. WARN("DEPRECATED_API",
  6404. "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
  6405. }
  6406. # check for various structs that are normally const (ops, kgdb, device_tree)
  6407. # and avoid what seem like struct definitions 'struct foo {'
  6408. if (defined($const_structs) &&
  6409. $line !~ /\bconst\b/ &&
  6410. $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
  6411. WARN("CONST_STRUCT",
  6412. "struct $1 should normally be const\n" . $herecurr);
  6413. }
  6414. # use of NR_CPUS is usually wrong
  6415. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  6416. # ignore designated initializers using NR_CPUS
  6417. if ($line =~ /\bNR_CPUS\b/ &&
  6418. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  6419. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  6420. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  6421. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  6422. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
  6423. $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
  6424. {
  6425. WARN("NR_CPUS",
  6426. "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  6427. }
  6428. # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
  6429. if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
  6430. ERROR("DEFINE_ARCH_HAS",
  6431. "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
  6432. }
  6433. # likely/unlikely comparisons similar to "(likely(foo) > 0)"
  6434. if ($perl_version_ok &&
  6435. $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
  6436. WARN("LIKELY_MISUSE",
  6437. "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
  6438. }
  6439. # return sysfs_emit(foo, fmt, ...) fmt without newline
  6440. if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
  6441. substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
  6442. my $offset = $+[6] - 1;
  6443. if (WARN("SYSFS_EMIT",
  6444. "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
  6445. $fix) {
  6446. substr($fixed[$fixlinenr], $offset, 0) = '\\n';
  6447. }
  6448. }
  6449. # nested likely/unlikely calls
  6450. if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
  6451. WARN("LIKELY_MISUSE",
  6452. "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
  6453. }
  6454. # whine mightly about in_atomic
  6455. if ($line =~ /\bin_atomic\s*\(/) {
  6456. if ($realfile =~ m@^drivers/@) {
  6457. ERROR("IN_ATOMIC",
  6458. "do not use in_atomic in drivers\n" . $herecurr);
  6459. } elsif ($realfile !~ m@^kernel/@) {
  6460. WARN("IN_ATOMIC",
  6461. "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  6462. }
  6463. }
  6464. # check for lockdep_set_novalidate_class
  6465. if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
  6466. $line =~ /__lockdep_no_validate__\s*\)/ ) {
  6467. if ($realfile !~ m@^kernel/lockdep@ &&
  6468. $realfile !~ m@^include/linux/lockdep@ &&
  6469. $realfile !~ m@^drivers/base/core@) {
  6470. ERROR("LOCKDEP",
  6471. "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
  6472. }
  6473. }
  6474. if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
  6475. $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
  6476. WARN("EXPORTED_WORLD_WRITABLE",
  6477. "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
  6478. }
  6479. # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
  6480. # and whether or not function naming is typical and if
  6481. # DEVICE_ATTR permissions uses are unusual too
  6482. if ($perl_version_ok &&
  6483. defined $stat &&
  6484. $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*\)/) {
  6485. my $var = $1;
  6486. my $perms = $2;
  6487. my $show = $3;
  6488. my $store = $4;
  6489. my $octal_perms = perms_to_octal($perms);
  6490. if ($show =~ /^${var}_show$/ &&
  6491. $store =~ /^${var}_store$/ &&
  6492. $octal_perms eq "0644") {
  6493. if (WARN("DEVICE_ATTR_RW",
  6494. "Use DEVICE_ATTR_RW\n" . $herecurr) &&
  6495. $fix) {
  6496. $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
  6497. }
  6498. } elsif ($show =~ /^${var}_show$/ &&
  6499. $store =~ /^NULL$/ &&
  6500. $octal_perms eq "0444") {
  6501. if (WARN("DEVICE_ATTR_RO",
  6502. "Use DEVICE_ATTR_RO\n" . $herecurr) &&
  6503. $fix) {
  6504. $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
  6505. }
  6506. } elsif ($show =~ /^NULL$/ &&
  6507. $store =~ /^${var}_store$/ &&
  6508. $octal_perms eq "0200") {
  6509. if (WARN("DEVICE_ATTR_WO",
  6510. "Use DEVICE_ATTR_WO\n" . $herecurr) &&
  6511. $fix) {
  6512. $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
  6513. }
  6514. } elsif ($octal_perms eq "0644" ||
  6515. $octal_perms eq "0444" ||
  6516. $octal_perms eq "0200") {
  6517. my $newshow = "$show";
  6518. $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
  6519. my $newstore = $store;
  6520. $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
  6521. my $rename = "";
  6522. if ($show ne $newshow) {
  6523. $rename .= " '$show' to '$newshow'";
  6524. }
  6525. if ($store ne $newstore) {
  6526. $rename .= " '$store' to '$newstore'";
  6527. }
  6528. WARN("DEVICE_ATTR_FUNCTIONS",
  6529. "Consider renaming function(s)$rename\n" . $herecurr);
  6530. } else {
  6531. WARN("DEVICE_ATTR_PERMS",
  6532. "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
  6533. }
  6534. }
  6535. # Mode permission misuses where it seems decimal should be octal
  6536. # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
  6537. # o Ignore module_param*(...) uses with a decimal 0 permission as that has a
  6538. # specific definition of not visible in sysfs.
  6539. # o Ignore proc_create*(...) uses with a decimal 0 permission as that means
  6540. # use the default permissions
  6541. if ($perl_version_ok &&
  6542. defined $stat &&
  6543. $line =~ /$mode_perms_search/) {
  6544. foreach my $entry (@mode_permission_funcs) {
  6545. my $func = $entry->[0];
  6546. my $arg_pos = $entry->[1];
  6547. my $lc = $stat =~ tr@\n@@;
  6548. $lc = $lc + $linenr;
  6549. my $stat_real = get_stat_real($linenr, $lc);
  6550. my $skip_args = "";
  6551. if ($arg_pos > 1) {
  6552. $arg_pos--;
  6553. $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
  6554. }
  6555. my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
  6556. if ($stat =~ /$test/) {
  6557. my $val = $1;
  6558. $val = $6 if ($skip_args ne "");
  6559. if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
  6560. (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
  6561. ($val =~ /^$Octal$/ && length($val) ne 4))) {
  6562. ERROR("NON_OCTAL_PERMISSIONS",
  6563. "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
  6564. }
  6565. if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
  6566. ERROR("EXPORTED_WORLD_WRITABLE",
  6567. "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
  6568. }
  6569. }
  6570. }
  6571. }
  6572. # check for uses of S_<PERMS> that could be octal for readability
  6573. while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
  6574. my $oval = $1;
  6575. my $octal = perms_to_octal($oval);
  6576. if (WARN("SYMBOLIC_PERMS",
  6577. "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
  6578. $fix) {
  6579. $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
  6580. }
  6581. }
  6582. # validate content of MODULE_LICENSE against list from include/linux/module.h
  6583. if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
  6584. my $extracted_string = get_quoted_string($line, $rawline);
  6585. my $valid_licenses = qr{
  6586. GPL|
  6587. GPL\ v2|
  6588. GPL\ and\ additional\ rights|
  6589. Dual\ BSD/GPL|
  6590. Dual\ MIT/GPL|
  6591. Dual\ MPL/GPL|
  6592. Proprietary
  6593. }x;
  6594. if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
  6595. WARN("MODULE_LICENSE",
  6596. "unknown module license " . $extracted_string . "\n" . $herecurr);
  6597. }
  6598. }
  6599. # check for sysctl duplicate constants
  6600. if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
  6601. WARN("DUPLICATED_SYSCTL_CONST",
  6602. "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
  6603. }
  6604. }
  6605. # If we have no input at all, then there is nothing to report on
  6606. # so just keep quiet.
  6607. if ($#rawlines == -1) {
  6608. exit(0);
  6609. }
  6610. # In mailback mode only produce a report in the negative, for
  6611. # things that appear to be patches.
  6612. if ($mailback && ($clean == 1 || !$is_patch)) {
  6613. exit(0);
  6614. }
  6615. # This is not a patch, and we are in 'no-patch' mode so
  6616. # just keep quiet.
  6617. if (!$chk_patch && !$is_patch) {
  6618. exit(0);
  6619. }
  6620. if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
  6621. ERROR("NOT_UNIFIED_DIFF",
  6622. "Does not appear to be a unified-diff format patch\n");
  6623. }
  6624. if ($is_patch && $has_commit_log && $chk_signoff) {
  6625. if ($signoff == 0) {
  6626. ERROR("MISSING_SIGN_OFF",
  6627. "Missing Signed-off-by: line(s)\n");
  6628. } elsif ($authorsignoff != 1) {
  6629. # authorsignoff values:
  6630. # 0 -> missing sign off
  6631. # 1 -> sign off identical
  6632. # 2 -> names and addresses match, comments mismatch
  6633. # 3 -> addresses match, names different
  6634. # 4 -> names match, addresses different
  6635. # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
  6636. my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
  6637. if ($authorsignoff == 0) {
  6638. ERROR("NO_AUTHOR_SIGN_OFF",
  6639. "Missing Signed-off-by: line by nominal patch author '$author'\n");
  6640. } elsif ($authorsignoff == 2) {
  6641. CHK("FROM_SIGN_OFF_MISMATCH",
  6642. "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
  6643. } elsif ($authorsignoff == 3) {
  6644. WARN("FROM_SIGN_OFF_MISMATCH",
  6645. "From:/Signed-off-by: email name mismatch: $sob_msg\n");
  6646. } elsif ($authorsignoff == 4) {
  6647. WARN("FROM_SIGN_OFF_MISMATCH",
  6648. "From:/Signed-off-by: email address mismatch: $sob_msg\n");
  6649. } elsif ($authorsignoff == 5) {
  6650. WARN("FROM_SIGN_OFF_MISMATCH",
  6651. "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
  6652. }
  6653. }
  6654. }
  6655. print report_dump();
  6656. if ($summary && !($clean == 1 && $quiet == 1)) {
  6657. print "$filename " if ($summary_file);
  6658. print "total: $cnt_error errors, $cnt_warn warnings, " .
  6659. (($check)? "$cnt_chk checks, " : "") .
  6660. "$cnt_lines lines checked\n";
  6661. }
  6662. if ($quiet == 0) {
  6663. # If there were any defects found and not already fixing them
  6664. if (!$clean and !$fix) {
  6665. print << "EOM"
  6666. NOTE: For some of the reported defects, checkpatch may be able to
  6667. mechanically convert to the typical style using --fix or --fix-inplace.
  6668. EOM
  6669. }
  6670. # If there were whitespace errors which cleanpatch can fix
  6671. # then suggest that.
  6672. if ($rpt_cleaners) {
  6673. $rpt_cleaners = 0;
  6674. print << "EOM"
  6675. NOTE: Whitespace errors detected.
  6676. You may wish to use scripts/cleanpatch or scripts/cleanfile
  6677. EOM
  6678. }
  6679. }
  6680. if ($clean == 0 && $fix &&
  6681. ("@rawlines" ne "@fixed" ||
  6682. $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
  6683. my $newfile = $filename;
  6684. $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
  6685. my $linecount = 0;
  6686. my $f;
  6687. @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
  6688. open($f, '>', $newfile)
  6689. or die "$P: Can't open $newfile for write\n";
  6690. foreach my $fixed_line (@fixed) {
  6691. $linecount++;
  6692. if ($file) {
  6693. if ($linecount > 3) {
  6694. $fixed_line =~ s/^\+//;
  6695. print $f $fixed_line . "\n";
  6696. }
  6697. } else {
  6698. print $f $fixed_line . "\n";
  6699. }
  6700. }
  6701. close($f);
  6702. if (!$quiet) {
  6703. print << "EOM";
  6704. Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
  6705. Do _NOT_ trust the results written to this file.
  6706. Do _NOT_ submit these changes without inspecting them for correctness.
  6707. This EXPERIMENTAL file is simply a convenience to help rewrite patches.
  6708. No warranties, expressed or implied...
  6709. EOM
  6710. }
  6711. }
  6712. if ($quiet == 0) {
  6713. print "\n";
  6714. if ($clean == 1) {
  6715. print "$vname has no obvious style problems and is ready for submission.\n";
  6716. } else {
  6717. print "$vname has style problems, please review.\n";
  6718. }
  6719. }
  6720. return $clean;
  6721. }