checkpatch.pl 205 KB

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