checkpatch.pl 153 KB

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