checkpatch.pl 157 KB

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