checkpatch.pl 153 KB

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