checkpatch.pl 177 KB

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