checkpatch.pl 145 KB

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