checkpatch.pl 148 KB

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