checkpatch.pl 232 KB

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