checkpatch.pl 187 KB

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