checkpatch.pl 210 KB

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