checkpatch.pl 164 KB

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