checkpatch.pl 188 KB

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