checkpatch.pl 204 KB

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