checkpatch.pl 145 KB

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