checkpatch.pl 165 KB

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