checkpatch.pl 135 KB

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