checkpatch.pl 226 KB

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