checkpatch.pl 182 KB

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