checkpatch.pl 206 KB

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