checkpatch.pl 204 KB

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