checkpatch.pl 190 KB

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