checkpatch.pl 238 KB

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