checkpatch.pl 156 KB

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