checkpatch.pl 158 KB

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