checkpatch.pl 160 KB

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