checkpatch.pl 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801
  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. # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
  1977. if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
  1978. WARN("CONFIG_EXPERIMENTAL",
  1979. "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
  1980. }
  1981. # check for RCS/CVS revision markers
  1982. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  1983. WARN("CVS_KEYWORD",
  1984. "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  1985. }
  1986. # Blackfin: don't use __builtin_bfin_[cs]sync
  1987. if ($line =~ /__builtin_bfin_csync/) {
  1988. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1989. ERROR("CSYNC",
  1990. "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
  1991. }
  1992. if ($line =~ /__builtin_bfin_ssync/) {
  1993. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1994. ERROR("SSYNC",
  1995. "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
  1996. }
  1997. # check for old HOTPLUG __dev<foo> section markings
  1998. if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
  1999. WARN("HOTPLUG_SECTION",
  2000. "Using $1 is unnecessary\n" . $herecurr);
  2001. }
  2002. # Check for potential 'bare' types
  2003. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  2004. $realline_next);
  2005. #print "LINE<$line>\n";
  2006. if ($linenr >= $suppress_statement &&
  2007. $realcnt && $sline =~ /.\s*\S/) {
  2008. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  2009. ctx_statement_block($linenr, $realcnt, 0);
  2010. $stat =~ s/\n./\n /g;
  2011. $cond =~ s/\n./\n /g;
  2012. #print "linenr<$linenr> <$stat>\n";
  2013. # If this statement has no statement boundaries within
  2014. # it there is no point in retrying a statement scan
  2015. # until we hit end of it.
  2016. my $frag = $stat; $frag =~ s/;+\s*$//;
  2017. if ($frag !~ /(?:{|;)/) {
  2018. #print "skip<$line_nr_next>\n";
  2019. $suppress_statement = $line_nr_next;
  2020. }
  2021. # Find the real next line.
  2022. $realline_next = $line_nr_next;
  2023. if (defined $realline_next &&
  2024. (!defined $lines[$realline_next - 1] ||
  2025. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  2026. $realline_next++;
  2027. }
  2028. my $s = $stat;
  2029. $s =~ s/{.*$//s;
  2030. # Ignore goto labels.
  2031. if ($s =~ /$Ident:\*$/s) {
  2032. # Ignore functions being called
  2033. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  2034. } elsif ($s =~ /^.\s*else\b/s) {
  2035. # declarations always start with types
  2036. } 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) {
  2037. my $type = $1;
  2038. $type =~ s/\s+/ /g;
  2039. possible($type, "A:" . $s);
  2040. # definitions in global scope can only start with types
  2041. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  2042. possible($1, "B:" . $s);
  2043. }
  2044. # any (foo ... *) is a pointer cast, and foo is a type
  2045. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  2046. possible($1, "C:" . $s);
  2047. }
  2048. # Check for any sort of function declaration.
  2049. # int foo(something bar, other baz);
  2050. # void (*store_gdt)(x86_descr_ptr *);
  2051. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  2052. my ($name_len) = length($1);
  2053. my $ctx = $s;
  2054. substr($ctx, 0, $name_len + 1, '');
  2055. $ctx =~ s/\)[^\)]*$//;
  2056. for my $arg (split(/\s*,\s*/, $ctx)) {
  2057. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  2058. possible($1, "D:" . $s);
  2059. }
  2060. }
  2061. }
  2062. }
  2063. #
  2064. # Checks which may be anchored in the context.
  2065. #
  2066. # Check for switch () and associated case and default
  2067. # statements should be at the same indent.
  2068. if ($line=~/\bswitch\s*\(.*\)/) {
  2069. my $err = '';
  2070. my $sep = '';
  2071. my @ctx = ctx_block_outer($linenr, $realcnt);
  2072. shift(@ctx);
  2073. for my $ctx (@ctx) {
  2074. my ($clen, $cindent) = line_stats($ctx);
  2075. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  2076. $indent != $cindent) {
  2077. $err .= "$sep$ctx\n";
  2078. $sep = '';
  2079. } else {
  2080. $sep = "[...]\n";
  2081. }
  2082. }
  2083. if ($err ne '') {
  2084. ERROR("SWITCH_CASE_INDENT_LEVEL",
  2085. "switch and case should be at the same indent\n$hereline$err");
  2086. }
  2087. }
  2088. # if/while/etc brace do not go on next line, unless defining a do while loop,
  2089. # or if that brace on the next line is for something else
  2090. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  2091. my $pre_ctx = "$1$2";
  2092. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  2093. if ($line =~ /^\+\t{6,}/) {
  2094. WARN("DEEP_INDENTATION",
  2095. "Too many leading tabs - consider code refactoring\n" . $herecurr);
  2096. }
  2097. my $ctx_cnt = $realcnt - $#ctx - 1;
  2098. my $ctx = join("\n", @ctx);
  2099. my $ctx_ln = $linenr;
  2100. my $ctx_skip = $realcnt;
  2101. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  2102. defined $lines[$ctx_ln - 1] &&
  2103. $lines[$ctx_ln - 1] =~ /^-/)) {
  2104. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  2105. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  2106. $ctx_ln++;
  2107. }
  2108. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  2109. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  2110. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  2111. ERROR("OPEN_BRACE",
  2112. "that open brace { should be on the previous line\n" .
  2113. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  2114. }
  2115. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  2116. $ctx =~ /\)\s*\;\s*$/ &&
  2117. defined $lines[$ctx_ln - 1])
  2118. {
  2119. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  2120. if ($nindent > $indent) {
  2121. WARN("TRAILING_SEMICOLON",
  2122. "trailing semicolon indicates no statements, indent implies otherwise\n" .
  2123. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  2124. }
  2125. }
  2126. }
  2127. # Check relative indent for conditionals and blocks.
  2128. if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  2129. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  2130. ctx_statement_block($linenr, $realcnt, 0)
  2131. if (!defined $stat);
  2132. my ($s, $c) = ($stat, $cond);
  2133. substr($s, 0, length($c), '');
  2134. # Make sure we remove the line prefixes as we have
  2135. # none on the first line, and are going to readd them
  2136. # where necessary.
  2137. $s =~ s/\n./\n/gs;
  2138. # Find out how long the conditional actually is.
  2139. my @newlines = ($c =~ /\n/gs);
  2140. my $cond_lines = 1 + $#newlines;
  2141. # We want to check the first line inside the block
  2142. # starting at the end of the conditional, so remove:
  2143. # 1) any blank line termination
  2144. # 2) any opening brace { on end of the line
  2145. # 3) any do (...) {
  2146. my $continuation = 0;
  2147. my $check = 0;
  2148. $s =~ s/^.*\bdo\b//;
  2149. $s =~ s/^\s*{//;
  2150. if ($s =~ s/^\s*\\//) {
  2151. $continuation = 1;
  2152. }
  2153. if ($s =~ s/^\s*?\n//) {
  2154. $check = 1;
  2155. $cond_lines++;
  2156. }
  2157. # Also ignore a loop construct at the end of a
  2158. # preprocessor statement.
  2159. if (($prevline =~ /^.\s*#\s*define\s/ ||
  2160. $prevline =~ /\\\s*$/) && $continuation == 0) {
  2161. $check = 0;
  2162. }
  2163. my $cond_ptr = -1;
  2164. $continuation = 0;
  2165. while ($cond_ptr != $cond_lines) {
  2166. $cond_ptr = $cond_lines;
  2167. # If we see an #else/#elif then the code
  2168. # is not linear.
  2169. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  2170. $check = 0;
  2171. }
  2172. # Ignore:
  2173. # 1) blank lines, they should be at 0,
  2174. # 2) preprocessor lines, and
  2175. # 3) labels.
  2176. if ($continuation ||
  2177. $s =~ /^\s*?\n/ ||
  2178. $s =~ /^\s*#\s*?/ ||
  2179. $s =~ /^\s*$Ident\s*:/) {
  2180. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  2181. if ($s =~ s/^.*?\n//) {
  2182. $cond_lines++;
  2183. }
  2184. }
  2185. }
  2186. my (undef, $sindent) = line_stats("+" . $s);
  2187. my $stat_real = raw_line($linenr, $cond_lines);
  2188. # Check if either of these lines are modified, else
  2189. # this is not this patch's fault.
  2190. if (!defined($stat_real) ||
  2191. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  2192. $check = 0;
  2193. }
  2194. if (defined($stat_real) && $cond_lines > 1) {
  2195. $stat_real = "[...]\n$stat_real";
  2196. }
  2197. #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";
  2198. if ($check && (($sindent % 8) != 0 ||
  2199. ($sindent <= $indent && $s ne ''))) {
  2200. WARN("SUSPECT_CODE_INDENT",
  2201. "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  2202. }
  2203. }
  2204. # Track the 'values' across context and added lines.
  2205. my $opline = $line; $opline =~ s/^./ /;
  2206. my ($curr_values, $curr_vars) =
  2207. annotate_values($opline . "\n", $prev_values);
  2208. $curr_values = $prev_values . $curr_values;
  2209. if ($dbg_values) {
  2210. my $outline = $opline; $outline =~ s/\t/ /g;
  2211. print "$linenr > .$outline\n";
  2212. print "$linenr > $curr_values\n";
  2213. print "$linenr > $curr_vars\n";
  2214. }
  2215. $prev_values = substr($curr_values, -1);
  2216. #ignore lines not being added
  2217. next if ($line =~ /^[^\+]/);
  2218. # TEST: allow direct testing of the type matcher.
  2219. if ($dbg_type) {
  2220. if ($line =~ /^.\s*$Declare\s*$/) {
  2221. ERROR("TEST_TYPE",
  2222. "TEST: is type\n" . $herecurr);
  2223. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  2224. ERROR("TEST_NOT_TYPE",
  2225. "TEST: is not type ($1 is)\n". $herecurr);
  2226. }
  2227. next;
  2228. }
  2229. # TEST: allow direct testing of the attribute matcher.
  2230. if ($dbg_attr) {
  2231. if ($line =~ /^.\s*$Modifier\s*$/) {
  2232. ERROR("TEST_ATTR",
  2233. "TEST: is attr\n" . $herecurr);
  2234. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  2235. ERROR("TEST_NOT_ATTR",
  2236. "TEST: is not attr ($1 is)\n". $herecurr);
  2237. }
  2238. next;
  2239. }
  2240. # check for initialisation to aggregates open brace on the next line
  2241. if ($line =~ /^.\s*{/ &&
  2242. $prevline =~ /(?:^|[^=])=\s*$/) {
  2243. ERROR("OPEN_BRACE",
  2244. "that open brace { should be on the previous line\n" . $hereprev);
  2245. }
  2246. #
  2247. # Checks which are anchored on the added line.
  2248. #
  2249. # check for malformed paths in #include statements (uses RAW line)
  2250. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  2251. my $path = $1;
  2252. if ($path =~ m{//}) {
  2253. ERROR("MALFORMED_INCLUDE",
  2254. "malformed #include filename\n" . $herecurr);
  2255. }
  2256. if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
  2257. ERROR("UAPI_INCLUDE",
  2258. "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
  2259. }
  2260. }
  2261. # no C99 // comments
  2262. if ($line =~ m{//}) {
  2263. if (ERROR("C99_COMMENTS",
  2264. "do not use C99 // comments\n" . $herecurr) &&
  2265. $fix) {
  2266. my $line = $fixed[$linenr - 1];
  2267. if ($line =~ /\/\/(.*)$/) {
  2268. my $comment = trim($1);
  2269. $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
  2270. }
  2271. }
  2272. }
  2273. # Remove C99 comments.
  2274. $line =~ s@//.*@@;
  2275. $opline =~ s@//.*@@;
  2276. # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
  2277. # the whole statement.
  2278. #print "APW <$lines[$realline_next - 1]>\n";
  2279. if (defined $realline_next &&
  2280. exists $lines[$realline_next - 1] &&
  2281. !defined $suppress_export{$realline_next} &&
  2282. ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  2283. $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  2284. # Handle definitions which produce identifiers with
  2285. # a prefix:
  2286. # XXX(foo);
  2287. # EXPORT_SYMBOL(something_foo);
  2288. my $name = $1;
  2289. if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
  2290. $name =~ /^${Ident}_$2/) {
  2291. #print "FOO C name<$name>\n";
  2292. $suppress_export{$realline_next} = 1;
  2293. } elsif ($stat !~ /(?:
  2294. \n.}\s*$|
  2295. ^.DEFINE_$Ident\(\Q$name\E\)|
  2296. ^.DECLARE_$Ident\(\Q$name\E\)|
  2297. ^.LIST_HEAD\(\Q$name\E\)|
  2298. ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
  2299. \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
  2300. )/x) {
  2301. #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
  2302. $suppress_export{$realline_next} = 2;
  2303. } else {
  2304. $suppress_export{$realline_next} = 1;
  2305. }
  2306. }
  2307. if (!defined $suppress_export{$linenr} &&
  2308. $prevline =~ /^.\s*$/ &&
  2309. ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  2310. $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  2311. #print "FOO B <$lines[$linenr - 1]>\n";
  2312. $suppress_export{$linenr} = 2;
  2313. }
  2314. if (defined $suppress_export{$linenr} &&
  2315. $suppress_export{$linenr} == 2) {
  2316. WARN("EXPORT_SYMBOL",
  2317. "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  2318. }
  2319. # check for global initialisers.
  2320. if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
  2321. if (ERROR("GLOBAL_INITIALISERS",
  2322. "do not initialise globals to 0 or NULL\n" .
  2323. $herecurr) &&
  2324. $fix) {
  2325. $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
  2326. }
  2327. }
  2328. # check for static initialisers.
  2329. if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
  2330. if (ERROR("INITIALISED_STATIC",
  2331. "do not initialise statics to 0 or NULL\n" .
  2332. $herecurr) &&
  2333. $fix) {
  2334. $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
  2335. }
  2336. }
  2337. # check for static const char * arrays.
  2338. if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
  2339. WARN("STATIC_CONST_CHAR_ARRAY",
  2340. "static const char * array should probably be static const char * const\n" .
  2341. $herecurr);
  2342. }
  2343. # check for static char foo[] = "bar" declarations.
  2344. if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
  2345. WARN("STATIC_CONST_CHAR_ARRAY",
  2346. "static char array declaration should probably be static const char\n" .
  2347. $herecurr);
  2348. }
  2349. # check for non-global char *foo[] = {"bar", ...} declarations.
  2350. if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
  2351. WARN("STATIC_CONST_CHAR_ARRAY",
  2352. "char * array declaration might be better as static const\n" .
  2353. $herecurr);
  2354. }
  2355. # check for function declarations without arguments like "int foo()"
  2356. if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
  2357. if (ERROR("FUNCTION_WITHOUT_ARGS",
  2358. "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
  2359. $fix) {
  2360. $fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
  2361. }
  2362. }
  2363. # check for uses of DEFINE_PCI_DEVICE_TABLE
  2364. if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
  2365. if (WARN("DEFINE_PCI_DEVICE_TABLE",
  2366. "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
  2367. $fix) {
  2368. $fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
  2369. }
  2370. }
  2371. # check for new typedefs, only function parameters and sparse annotations
  2372. # make sense.
  2373. if ($line =~ /\btypedef\s/ &&
  2374. $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
  2375. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  2376. $line !~ /\b$typeTypedefs\b/ &&
  2377. $line !~ /\b__bitwise(?:__|)\b/) {
  2378. WARN("NEW_TYPEDEFS",
  2379. "do not add new typedefs\n" . $herecurr);
  2380. }
  2381. # * goes on variable not on type
  2382. # (char*[ const])
  2383. while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
  2384. #print "AA<$1>\n";
  2385. my ($ident, $from, $to) = ($1, $2, $2);
  2386. # Should start with a space.
  2387. $to =~ s/^(\S)/ $1/;
  2388. # Should not end with a space.
  2389. $to =~ s/\s+$//;
  2390. # '*'s should not have spaces between.
  2391. while ($to =~ s/\*\s+\*/\*\*/) {
  2392. }
  2393. ## print "1: from<$from> to<$to> ident<$ident>\n";
  2394. if ($from ne $to) {
  2395. if (ERROR("POINTER_LOCATION",
  2396. "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
  2397. $fix) {
  2398. my $sub_from = $ident;
  2399. my $sub_to = $ident;
  2400. $sub_to =~ s/\Q$from\E/$to/;
  2401. $fixed[$linenr - 1] =~
  2402. s@\Q$sub_from\E@$sub_to@;
  2403. }
  2404. }
  2405. }
  2406. while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
  2407. #print "BB<$1>\n";
  2408. my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
  2409. # Should start with a space.
  2410. $to =~ s/^(\S)/ $1/;
  2411. # Should not end with a space.
  2412. $to =~ s/\s+$//;
  2413. # '*'s should not have spaces between.
  2414. while ($to =~ s/\*\s+\*/\*\*/) {
  2415. }
  2416. # Modifiers should have spaces.
  2417. $to =~ s/(\b$Modifier$)/$1 /;
  2418. ## print "2: from<$from> to<$to> ident<$ident>\n";
  2419. if ($from ne $to && $ident !~ /^$Modifier$/) {
  2420. if (ERROR("POINTER_LOCATION",
  2421. "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
  2422. $fix) {
  2423. my $sub_from = $match;
  2424. my $sub_to = $match;
  2425. $sub_to =~ s/\Q$from\E/$to/;
  2426. $fixed[$linenr - 1] =~
  2427. s@\Q$sub_from\E@$sub_to@;
  2428. }
  2429. }
  2430. }
  2431. # # no BUG() or BUG_ON()
  2432. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  2433. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  2434. # print "$herecurr";
  2435. # $clean = 0;
  2436. # }
  2437. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  2438. WARN("LINUX_VERSION_CODE",
  2439. "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  2440. }
  2441. # check for uses of printk_ratelimit
  2442. if ($line =~ /\bprintk_ratelimit\s*\(/) {
  2443. WARN("PRINTK_RATELIMITED",
  2444. "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
  2445. }
  2446. # printk should use KERN_* levels. Note that follow on printk's on the
  2447. # same line do not need a level, so we use the current block context
  2448. # to try and find and validate the current printk. In summary the current
  2449. # printk includes all preceding printk's which have no newline on the end.
  2450. # we assume the first bad printk is the one to report.
  2451. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  2452. my $ok = 0;
  2453. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  2454. #print "CHECK<$lines[$ln - 1]\n";
  2455. # we have a preceding printk if it ends
  2456. # with "\n" ignore it, else it is to blame
  2457. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  2458. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  2459. $ok = 1;
  2460. }
  2461. last;
  2462. }
  2463. }
  2464. if ($ok == 0) {
  2465. WARN("PRINTK_WITHOUT_KERN_LEVEL",
  2466. "printk() should include KERN_ facility level\n" . $herecurr);
  2467. }
  2468. }
  2469. if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
  2470. my $orig = $1;
  2471. my $level = lc($orig);
  2472. $level = "warn" if ($level eq "warning");
  2473. my $level2 = $level;
  2474. $level2 = "dbg" if ($level eq "debug");
  2475. WARN("PREFER_PR_LEVEL",
  2476. "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
  2477. }
  2478. if ($line =~ /\bpr_warning\s*\(/) {
  2479. if (WARN("PREFER_PR_LEVEL",
  2480. "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
  2481. $fix) {
  2482. $fixed[$linenr - 1] =~
  2483. s/\bpr_warning\b/pr_warn/;
  2484. }
  2485. }
  2486. if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
  2487. my $orig = $1;
  2488. my $level = lc($orig);
  2489. $level = "warn" if ($level eq "warning");
  2490. $level = "dbg" if ($level eq "debug");
  2491. WARN("PREFER_DEV_LEVEL",
  2492. "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
  2493. }
  2494. # function brace can't be on same line, except for #defines of do while,
  2495. # or if closed on same line
  2496. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  2497. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  2498. ERROR("OPEN_BRACE",
  2499. "open brace '{' following function declarations go on the next line\n" . $herecurr);
  2500. }
  2501. # open braces for enum, union and struct go on the same line.
  2502. if ($line =~ /^.\s*{/ &&
  2503. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  2504. ERROR("OPEN_BRACE",
  2505. "open brace '{' following $1 go on the same line\n" . $hereprev);
  2506. }
  2507. # missing space after union, struct or enum definition
  2508. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
  2509. if (WARN("SPACING",
  2510. "missing space after $1 definition\n" . $herecurr) &&
  2511. $fix) {
  2512. $fixed[$linenr - 1] =~
  2513. s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
  2514. }
  2515. }
  2516. # Function pointer declarations
  2517. # check spacing between type, funcptr, and args
  2518. # canonical declaration is "type (*funcptr)(args...)"
  2519. if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
  2520. my $declare = $1;
  2521. my $pre_pointer_space = $2;
  2522. my $post_pointer_space = $3;
  2523. my $funcname = $4;
  2524. my $post_funcname_space = $5;
  2525. my $pre_args_space = $6;
  2526. # the $Declare variable will capture all spaces after the type
  2527. # so check it for a missing trailing missing space but pointer return types
  2528. # don't need a space so don't warn for those.
  2529. my $post_declare_space = "";
  2530. if ($declare =~ /(\s+)$/) {
  2531. $post_declare_space = $1;
  2532. $declare = rtrim($declare);
  2533. }
  2534. if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
  2535. WARN("SPACING",
  2536. "missing space after return type\n" . $herecurr);
  2537. $post_declare_space = " ";
  2538. }
  2539. # unnecessary space "type (*funcptr)(args...)"
  2540. # This test is not currently implemented because these declarations are
  2541. # equivalent to
  2542. # int foo(int bar, ...)
  2543. # and this is form shouldn't/doesn't generate a checkpatch warning.
  2544. #
  2545. # elsif ($declare =~ /\s{2,}$/) {
  2546. # WARN("SPACING",
  2547. # "Multiple spaces after return type\n" . $herecurr);
  2548. # }
  2549. # unnecessary space "type ( *funcptr)(args...)"
  2550. if (defined $pre_pointer_space &&
  2551. $pre_pointer_space =~ /^\s/) {
  2552. WARN("SPACING",
  2553. "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
  2554. }
  2555. # unnecessary space "type (* funcptr)(args...)"
  2556. if (defined $post_pointer_space &&
  2557. $post_pointer_space =~ /^\s/) {
  2558. WARN("SPACING",
  2559. "Unnecessary space before function pointer name\n" . $herecurr);
  2560. }
  2561. # unnecessary space "type (*funcptr )(args...)"
  2562. if (defined $post_funcname_space &&
  2563. $post_funcname_space =~ /^\s/) {
  2564. WARN("SPACING",
  2565. "Unnecessary space after function pointer name\n" . $herecurr);
  2566. }
  2567. # unnecessary space "type (*funcptr) (args...)"
  2568. if (defined $pre_args_space &&
  2569. $pre_args_space =~ /^\s/) {
  2570. WARN("SPACING",
  2571. "Unnecessary space before function pointer arguments\n" . $herecurr);
  2572. }
  2573. if (show_type("SPACING") && $fix) {
  2574. $fixed[$linenr - 1] =~
  2575. s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
  2576. }
  2577. }
  2578. # check for spacing round square brackets; allowed:
  2579. # 1. with a type on the left -- int [] a;
  2580. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  2581. # 3. inside a curly brace -- = { [0...10] = 5 }
  2582. while ($line =~ /(.*?\s)\[/g) {
  2583. my ($where, $prefix) = ($-[1], $1);
  2584. if ($prefix !~ /$Type\s+$/ &&
  2585. ($where != 0 || $prefix !~ /^.\s+$/) &&
  2586. $prefix !~ /[{,]\s+$/) {
  2587. if (ERROR("BRACKET_SPACE",
  2588. "space prohibited before open square bracket '['\n" . $herecurr) &&
  2589. $fix) {
  2590. $fixed[$linenr - 1] =~
  2591. s/^(\+.*?)\s+\[/$1\[/;
  2592. }
  2593. }
  2594. }
  2595. # check for spaces between functions and their parentheses.
  2596. while ($line =~ /($Ident)\s+\(/g) {
  2597. my $name = $1;
  2598. my $ctx_before = substr($line, 0, $-[1]);
  2599. my $ctx = "$ctx_before$name";
  2600. # Ignore those directives where spaces _are_ permitted.
  2601. if ($name =~ /^(?:
  2602. if|for|while|switch|return|case|
  2603. volatile|__volatile__|
  2604. __attribute__|format|__extension__|
  2605. asm|__asm__)$/x)
  2606. {
  2607. # cpp #define statements have non-optional spaces, ie
  2608. # if there is a space between the name and the open
  2609. # parenthesis it is simply not a parameter group.
  2610. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  2611. # cpp #elif statement condition may start with a (
  2612. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  2613. # If this whole things ends with a type its most
  2614. # likely a typedef for a function.
  2615. } elsif ($ctx =~ /$Type$/) {
  2616. } else {
  2617. if (WARN("SPACING",
  2618. "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
  2619. $fix) {
  2620. $fixed[$linenr - 1] =~
  2621. s/\b$name\s+\(/$name\(/;
  2622. }
  2623. }
  2624. }
  2625. # Check operator spacing.
  2626. if (!($line=~/\#\s*include/)) {
  2627. my $fixed_line = "";
  2628. my $line_fixed = 0;
  2629. my $ops = qr{
  2630. <<=|>>=|<=|>=|==|!=|
  2631. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  2632. =>|->|<<|>>|<|>|=|!|~|
  2633. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  2634. \?:|\?|:
  2635. }x;
  2636. my @elements = split(/($ops|;)/, $opline);
  2637. ## print("element count: <" . $#elements . ">\n");
  2638. ## foreach my $el (@elements) {
  2639. ## print("el: <$el>\n");
  2640. ## }
  2641. my @fix_elements = ();
  2642. my $off = 0;
  2643. foreach my $el (@elements) {
  2644. push(@fix_elements, substr($rawline, $off, length($el)));
  2645. $off += length($el);
  2646. }
  2647. $off = 0;
  2648. my $blank = copy_spacing($opline);
  2649. my $last_after = -1;
  2650. for (my $n = 0; $n < $#elements; $n += 2) {
  2651. my $good = $fix_elements[$n] . $fix_elements[$n + 1];
  2652. ## print("n: <$n> good: <$good>\n");
  2653. $off += length($elements[$n]);
  2654. # Pick up the preceding and succeeding characters.
  2655. my $ca = substr($opline, 0, $off);
  2656. my $cc = '';
  2657. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  2658. $cc = substr($opline, $off + length($elements[$n + 1]));
  2659. }
  2660. my $cb = "$ca$;$cc";
  2661. my $a = '';
  2662. $a = 'V' if ($elements[$n] ne '');
  2663. $a = 'W' if ($elements[$n] =~ /\s$/);
  2664. $a = 'C' if ($elements[$n] =~ /$;$/);
  2665. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  2666. $a = 'O' if ($elements[$n] eq '');
  2667. $a = 'E' if ($ca =~ /^\s*$/);
  2668. my $op = $elements[$n + 1];
  2669. my $c = '';
  2670. if (defined $elements[$n + 2]) {
  2671. $c = 'V' if ($elements[$n + 2] ne '');
  2672. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  2673. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  2674. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  2675. $c = 'O' if ($elements[$n + 2] eq '');
  2676. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  2677. } else {
  2678. $c = 'E';
  2679. }
  2680. my $ctx = "${a}x${c}";
  2681. my $at = "(ctx:$ctx)";
  2682. my $ptr = substr($blank, 0, $off) . "^";
  2683. my $hereptr = "$hereline$ptr\n";
  2684. # Pull out the value of this operator.
  2685. my $op_type = substr($curr_values, $off + 1, 1);
  2686. # Get the full operator variant.
  2687. my $opv = $op . substr($curr_vars, $off, 1);
  2688. # Ignore operators passed as parameters.
  2689. if ($op_type ne 'V' &&
  2690. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  2691. # # Ignore comments
  2692. # } elsif ($op =~ /^$;+$/) {
  2693. # ; should have either the end of line or a space or \ after it
  2694. } elsif ($op eq ';') {
  2695. if ($ctx !~ /.x[WEBC]/ &&
  2696. $cc !~ /^\\/ && $cc !~ /^;/) {
  2697. if (ERROR("SPACING",
  2698. "space required after that '$op' $at\n" . $hereptr)) {
  2699. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  2700. $line_fixed = 1;
  2701. }
  2702. }
  2703. # // is a comment
  2704. } elsif ($op eq '//') {
  2705. # : when part of a bitfield
  2706. } elsif ($opv eq ':B') {
  2707. # skip the bitfield test for now
  2708. # No spaces for:
  2709. # ->
  2710. } elsif ($op eq '->') {
  2711. if ($ctx =~ /Wx.|.xW/) {
  2712. if (ERROR("SPACING",
  2713. "spaces prohibited around that '$op' $at\n" . $hereptr)) {
  2714. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  2715. if (defined $fix_elements[$n + 2]) {
  2716. $fix_elements[$n + 2] =~ s/^\s+//;
  2717. }
  2718. $line_fixed = 1;
  2719. }
  2720. }
  2721. # , must have a space on the right.
  2722. } elsif ($op eq ',') {
  2723. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  2724. if (ERROR("SPACING",
  2725. "space required after that '$op' $at\n" . $hereptr)) {
  2726. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  2727. $line_fixed = 1;
  2728. $last_after = $n;
  2729. }
  2730. }
  2731. # '*' as part of a type definition -- reported already.
  2732. } elsif ($opv eq '*_') {
  2733. #warn "'*' is part of type\n";
  2734. # unary operators should have a space before and
  2735. # none after. May be left adjacent to another
  2736. # unary operator, or a cast
  2737. } elsif ($op eq '!' || $op eq '~' ||
  2738. $opv eq '*U' || $opv eq '-U' ||
  2739. $opv eq '&U' || $opv eq '&&U') {
  2740. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  2741. if (ERROR("SPACING",
  2742. "space required before that '$op' $at\n" . $hereptr)) {
  2743. if ($n != $last_after + 2) {
  2744. $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
  2745. $line_fixed = 1;
  2746. }
  2747. }
  2748. }
  2749. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  2750. # A unary '*' may be const
  2751. } elsif ($ctx =~ /.xW/) {
  2752. if (ERROR("SPACING",
  2753. "space prohibited after that '$op' $at\n" . $hereptr)) {
  2754. $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
  2755. if (defined $fix_elements[$n + 2]) {
  2756. $fix_elements[$n + 2] =~ s/^\s+//;
  2757. }
  2758. $line_fixed = 1;
  2759. }
  2760. }
  2761. # unary ++ and unary -- are allowed no space on one side.
  2762. } elsif ($op eq '++' or $op eq '--') {
  2763. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  2764. if (ERROR("SPACING",
  2765. "space required one side of that '$op' $at\n" . $hereptr)) {
  2766. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
  2767. $line_fixed = 1;
  2768. }
  2769. }
  2770. if ($ctx =~ /Wx[BE]/ ||
  2771. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  2772. if (ERROR("SPACING",
  2773. "space prohibited before that '$op' $at\n" . $hereptr)) {
  2774. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  2775. $line_fixed = 1;
  2776. }
  2777. }
  2778. if ($ctx =~ /ExW/) {
  2779. if (ERROR("SPACING",
  2780. "space prohibited after that '$op' $at\n" . $hereptr)) {
  2781. $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
  2782. if (defined $fix_elements[$n + 2]) {
  2783. $fix_elements[$n + 2] =~ s/^\s+//;
  2784. }
  2785. $line_fixed = 1;
  2786. }
  2787. }
  2788. # << and >> may either have or not have spaces both sides
  2789. } elsif ($op eq '<<' or $op eq '>>' or
  2790. $op eq '&' or $op eq '^' or $op eq '|' or
  2791. $op eq '+' or $op eq '-' or
  2792. $op eq '*' or $op eq '/' or
  2793. $op eq '%')
  2794. {
  2795. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  2796. if (ERROR("SPACING",
  2797. "need consistent spacing around '$op' $at\n" . $hereptr)) {
  2798. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  2799. if (defined $fix_elements[$n + 2]) {
  2800. $fix_elements[$n + 2] =~ s/^\s+//;
  2801. }
  2802. $line_fixed = 1;
  2803. }
  2804. }
  2805. # A colon needs no spaces before when it is
  2806. # terminating a case value or a label.
  2807. } elsif ($opv eq ':C' || $opv eq ':L') {
  2808. if ($ctx =~ /Wx./) {
  2809. if (ERROR("SPACING",
  2810. "space prohibited before that '$op' $at\n" . $hereptr)) {
  2811. $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
  2812. $line_fixed = 1;
  2813. }
  2814. }
  2815. # All the others need spaces both sides.
  2816. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  2817. my $ok = 0;
  2818. # Ignore email addresses <foo@bar>
  2819. if (($op eq '<' &&
  2820. $cc =~ /^\S+\@\S+>/) ||
  2821. ($op eq '>' &&
  2822. $ca =~ /<\S+\@\S+$/))
  2823. {
  2824. $ok = 1;
  2825. }
  2826. # messages are ERROR, but ?: are CHK
  2827. if ($ok == 0) {
  2828. my $msg_type = \&ERROR;
  2829. $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
  2830. if (&{$msg_type}("SPACING",
  2831. "spaces required around that '$op' $at\n" . $hereptr)) {
  2832. $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
  2833. if (defined $fix_elements[$n + 2]) {
  2834. $fix_elements[$n + 2] =~ s/^\s+//;
  2835. }
  2836. $line_fixed = 1;
  2837. }
  2838. }
  2839. }
  2840. $off += length($elements[$n + 1]);
  2841. ## print("n: <$n> GOOD: <$good>\n");
  2842. $fixed_line = $fixed_line . $good;
  2843. }
  2844. if (($#elements % 2) == 0) {
  2845. $fixed_line = $fixed_line . $fix_elements[$#elements];
  2846. }
  2847. if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
  2848. $fixed[$linenr - 1] = $fixed_line;
  2849. }
  2850. }
  2851. # check for whitespace before a non-naked semicolon
  2852. if ($line =~ /^\+.*\S\s+;\s*$/) {
  2853. if (WARN("SPACING",
  2854. "space prohibited before semicolon\n" . $herecurr) &&
  2855. $fix) {
  2856. 1 while $fixed[$linenr - 1] =~
  2857. s/^(\+.*\S)\s+;/$1;/;
  2858. }
  2859. }
  2860. # check for multiple assignments
  2861. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  2862. CHK("MULTIPLE_ASSIGNMENTS",
  2863. "multiple assignments should be avoided\n" . $herecurr);
  2864. }
  2865. ## # check for multiple declarations, allowing for a function declaration
  2866. ## # continuation.
  2867. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  2868. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  2869. ##
  2870. ## # Remove any bracketed sections to ensure we do not
  2871. ## # falsly report the parameters of functions.
  2872. ## my $ln = $line;
  2873. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  2874. ## }
  2875. ## if ($ln =~ /,/) {
  2876. ## WARN("MULTIPLE_DECLARATION",
  2877. ## "declaring multiple variables together should be avoided\n" . $herecurr);
  2878. ## }
  2879. ## }
  2880. #need space before brace following if, while, etc
  2881. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  2882. $line =~ /do{/) {
  2883. if (ERROR("SPACING",
  2884. "space required before the open brace '{'\n" . $herecurr) &&
  2885. $fix) {
  2886. $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
  2887. }
  2888. }
  2889. ## # check for blank lines before declarations
  2890. ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
  2891. ## $prevrawline =~ /^.\s*$/) {
  2892. ## WARN("SPACING",
  2893. ## "No blank lines before declarations\n" . $hereprev);
  2894. ## }
  2895. ##
  2896. # closing brace should have a space following it when it has anything
  2897. # on the line
  2898. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  2899. if (ERROR("SPACING",
  2900. "space required after that close brace '}'\n" . $herecurr) &&
  2901. $fix) {
  2902. $fixed[$linenr - 1] =~
  2903. s/}((?!(?:,|;|\)))\S)/} $1/;
  2904. }
  2905. }
  2906. # check spacing on square brackets
  2907. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  2908. if (ERROR("SPACING",
  2909. "space prohibited after that open square bracket '['\n" . $herecurr) &&
  2910. $fix) {
  2911. $fixed[$linenr - 1] =~
  2912. s/\[\s+/\[/;
  2913. }
  2914. }
  2915. if ($line =~ /\s\]/) {
  2916. if (ERROR("SPACING",
  2917. "space prohibited before that close square bracket ']'\n" . $herecurr) &&
  2918. $fix) {
  2919. $fixed[$linenr - 1] =~
  2920. s/\s+\]/\]/;
  2921. }
  2922. }
  2923. # check spacing on parentheses
  2924. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  2925. $line !~ /for\s*\(\s+;/) {
  2926. if (ERROR("SPACING",
  2927. "space prohibited after that open parenthesis '('\n" . $herecurr) &&
  2928. $fix) {
  2929. $fixed[$linenr - 1] =~
  2930. s/\(\s+/\(/;
  2931. }
  2932. }
  2933. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  2934. $line !~ /for\s*\(.*;\s+\)/ &&
  2935. $line !~ /:\s+\)/) {
  2936. if (ERROR("SPACING",
  2937. "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
  2938. $fix) {
  2939. $fixed[$linenr - 1] =~
  2940. s/\s+\)/\)/;
  2941. }
  2942. }
  2943. #goto labels aren't indented, allow a single space however
  2944. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  2945. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  2946. if (WARN("INDENTED_LABEL",
  2947. "labels should not be indented\n" . $herecurr) &&
  2948. $fix) {
  2949. $fixed[$linenr - 1] =~
  2950. s/^(.)\s+/$1/;
  2951. }
  2952. }
  2953. # return is not a function
  2954. if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
  2955. my $spacing = $1;
  2956. if ($^V && $^V ge 5.10.0 &&
  2957. $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
  2958. my $value = $1;
  2959. $value = deparenthesize($value);
  2960. if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
  2961. ERROR("RETURN_PARENTHESES",
  2962. "return is not a function, parentheses are not required\n" . $herecurr);
  2963. }
  2964. } elsif ($spacing !~ /\s+/) {
  2965. ERROR("SPACING",
  2966. "space required before the open parenthesis '('\n" . $herecurr);
  2967. }
  2968. }
  2969. # unnecessary return in a void function
  2970. # at end-of-function, with the previous line a single leading tab, then return;
  2971. # and the line before that not a goto label target like "out:"
  2972. if ($sline =~ /^[ \+]}\s*$/ &&
  2973. $prevline =~ /^\+\treturn\s*;\s*$/ &&
  2974. $linenr >= 3 &&
  2975. $lines[$linenr - 3] =~ /^[ +]/ &&
  2976. $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
  2977. WARN("RETURN_VOID",
  2978. "void function return statements are not generally useful\n" . $hereprev);
  2979. }
  2980. # if statements using unnecessary parentheses - ie: if ((foo == bar))
  2981. if ($^V && $^V ge 5.10.0 &&
  2982. $line =~ /\bif\s*((?:\(\s*){2,})/) {
  2983. my $openparens = $1;
  2984. my $count = $openparens =~ tr@\(@\(@;
  2985. my $msg = "";
  2986. if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
  2987. my $comp = $4; #Not $1 because of $LvalOrFunc
  2988. $msg = " - maybe == should be = ?" if ($comp eq "==");
  2989. WARN("UNNECESSARY_PARENTHESES",
  2990. "Unnecessary parentheses$msg\n" . $herecurr);
  2991. }
  2992. }
  2993. # Return of what appears to be an errno should normally be -'ve
  2994. if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
  2995. my $name = $1;
  2996. if ($name ne 'EOF' && $name ne 'ERROR') {
  2997. WARN("USE_NEGATIVE_ERRNO",
  2998. "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
  2999. }
  3000. }
  3001. # Need a space before open parenthesis after if, while etc
  3002. if ($line =~ /\b(if|while|for|switch)\(/) {
  3003. if (ERROR("SPACING",
  3004. "space required before the open parenthesis '('\n" . $herecurr) &&
  3005. $fix) {
  3006. $fixed[$linenr - 1] =~
  3007. s/\b(if|while|for|switch)\(/$1 \(/;
  3008. }
  3009. }
  3010. # Check for illegal assignment in if conditional -- and check for trailing
  3011. # statements after the conditional.
  3012. if ($line =~ /do\s*(?!{)/) {
  3013. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  3014. ctx_statement_block($linenr, $realcnt, 0)
  3015. if (!defined $stat);
  3016. my ($stat_next) = ctx_statement_block($line_nr_next,
  3017. $remain_next, $off_next);
  3018. $stat_next =~ s/\n./\n /g;
  3019. ##print "stat<$stat> stat_next<$stat_next>\n";
  3020. if ($stat_next =~ /^\s*while\b/) {
  3021. # If the statement carries leading newlines,
  3022. # then count those as offsets.
  3023. my ($whitespace) =
  3024. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  3025. my $offset =
  3026. statement_rawlines($whitespace) - 1;
  3027. $suppress_whiletrailers{$line_nr_next +
  3028. $offset} = 1;
  3029. }
  3030. }
  3031. if (!defined $suppress_whiletrailers{$linenr} &&
  3032. defined($stat) && defined($cond) &&
  3033. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  3034. my ($s, $c) = ($stat, $cond);
  3035. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  3036. ERROR("ASSIGN_IN_IF",
  3037. "do not use assignment in if condition\n" . $herecurr);
  3038. }
  3039. # Find out what is on the end of the line after the
  3040. # conditional.
  3041. substr($s, 0, length($c), '');
  3042. $s =~ s/\n.*//g;
  3043. $s =~ s/$;//g; # Remove any comments
  3044. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  3045. $c !~ /}\s*while\s*/)
  3046. {
  3047. # Find out how long the conditional actually is.
  3048. my @newlines = ($c =~ /\n/gs);
  3049. my $cond_lines = 1 + $#newlines;
  3050. my $stat_real = '';
  3051. $stat_real = raw_line($linenr, $cond_lines)
  3052. . "\n" if ($cond_lines);
  3053. if (defined($stat_real) && $cond_lines > 1) {
  3054. $stat_real = "[...]\n$stat_real";
  3055. }
  3056. ERROR("TRAILING_STATEMENTS",
  3057. "trailing statements should be on next line\n" . $herecurr . $stat_real);
  3058. }
  3059. }
  3060. # Check for bitwise tests written as boolean
  3061. if ($line =~ /
  3062. (?:
  3063. (?:\[|\(|\&\&|\|\|)
  3064. \s*0[xX][0-9]+\s*
  3065. (?:\&\&|\|\|)
  3066. |
  3067. (?:\&\&|\|\|)
  3068. \s*0[xX][0-9]+\s*
  3069. (?:\&\&|\|\||\)|\])
  3070. )/x)
  3071. {
  3072. WARN("HEXADECIMAL_BOOLEAN_TEST",
  3073. "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  3074. }
  3075. # if and else should not have general statements after it
  3076. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  3077. my $s = $1;
  3078. $s =~ s/$;//g; # Remove any comments
  3079. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  3080. ERROR("TRAILING_STATEMENTS",
  3081. "trailing statements should be on next line\n" . $herecurr);
  3082. }
  3083. }
  3084. # if should not continue a brace
  3085. if ($line =~ /}\s*if\b/) {
  3086. ERROR("TRAILING_STATEMENTS",
  3087. "trailing statements should be on next line\n" .
  3088. $herecurr);
  3089. }
  3090. # case and default should not have general statements after them
  3091. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  3092. $line !~ /\G(?:
  3093. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  3094. \s*return\s+
  3095. )/xg)
  3096. {
  3097. ERROR("TRAILING_STATEMENTS",
  3098. "trailing statements should be on next line\n" . $herecurr);
  3099. }
  3100. # Check for }<nl>else {, these must be at the same
  3101. # indent level to be relevant to each other.
  3102. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  3103. $previndent == $indent) {
  3104. ERROR("ELSE_AFTER_BRACE",
  3105. "else should follow close brace '}'\n" . $hereprev);
  3106. }
  3107. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  3108. $previndent == $indent) {
  3109. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  3110. # Find out what is on the end of the line after the
  3111. # conditional.
  3112. substr($s, 0, length($c), '');
  3113. $s =~ s/\n.*//g;
  3114. if ($s =~ /^\s*;/) {
  3115. ERROR("WHILE_AFTER_BRACE",
  3116. "while should follow close brace '}'\n" . $hereprev);
  3117. }
  3118. }
  3119. #Specific variable tests
  3120. while ($line =~ m{($Constant|$Lval)}g) {
  3121. my $var = $1;
  3122. #gcc binary extension
  3123. if ($var =~ /^$Binary$/) {
  3124. if (WARN("GCC_BINARY_CONSTANT",
  3125. "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
  3126. $fix) {
  3127. my $hexval = sprintf("0x%x", oct($var));
  3128. $fixed[$linenr - 1] =~
  3129. s/\b$var\b/$hexval/;
  3130. }
  3131. }
  3132. #CamelCase
  3133. if ($var !~ /^$Constant$/ &&
  3134. $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
  3135. #Ignore Page<foo> variants
  3136. $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
  3137. #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
  3138. $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
  3139. while ($var =~ m{($Ident)}g) {
  3140. my $word = $1;
  3141. next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
  3142. if ($check) {
  3143. seed_camelcase_includes();
  3144. if (!$file && !$camelcase_file_seeded) {
  3145. seed_camelcase_file($realfile);
  3146. $camelcase_file_seeded = 1;
  3147. }
  3148. }
  3149. if (!defined $camelcase{$word}) {
  3150. $camelcase{$word} = 1;
  3151. CHK("CAMELCASE",
  3152. "Avoid CamelCase: <$word>\n" . $herecurr);
  3153. }
  3154. }
  3155. }
  3156. }
  3157. #no spaces allowed after \ in define
  3158. if ($line =~ /\#\s*define.*\\\s+$/) {
  3159. if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
  3160. "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
  3161. $fix) {
  3162. $fixed[$linenr - 1] =~ s/\s+$//;
  3163. }
  3164. }
  3165. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  3166. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  3167. my $file = "$1.h";
  3168. my $checkfile = "include/linux/$file";
  3169. if (-f "$root/$checkfile" &&
  3170. $realfile ne $checkfile &&
  3171. $1 !~ /$allowed_asm_includes/)
  3172. {
  3173. if ($realfile =~ m{^arch/}) {
  3174. CHK("ARCH_INCLUDE_LINUX",
  3175. "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  3176. } else {
  3177. WARN("INCLUDE_LINUX",
  3178. "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  3179. }
  3180. }
  3181. }
  3182. # multi-statement macros should be enclosed in a do while loop, grab the
  3183. # first statement and ensure its the whole macro if its not enclosed
  3184. # in a known good container
  3185. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  3186. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  3187. my $ln = $linenr;
  3188. my $cnt = $realcnt;
  3189. my ($off, $dstat, $dcond, $rest);
  3190. my $ctx = '';
  3191. ($dstat, $dcond, $ln, $cnt, $off) =
  3192. ctx_statement_block($linenr, $realcnt, 0);
  3193. $ctx = $dstat;
  3194. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  3195. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  3196. $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
  3197. $dstat =~ s/$;//g;
  3198. $dstat =~ s/\\\n.//g;
  3199. $dstat =~ s/^\s*//s;
  3200. $dstat =~ s/\s*$//s;
  3201. # Flatten any parentheses and braces
  3202. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  3203. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  3204. $dstat =~ s/\[[^\[\]]*\]/1/)
  3205. {
  3206. }
  3207. # Flatten any obvious string concatentation.
  3208. while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
  3209. $dstat =~ s/$Ident\s*("X*")/$1/)
  3210. {
  3211. }
  3212. my $exceptions = qr{
  3213. $Declare|
  3214. module_param_named|
  3215. MODULE_PARM_DESC|
  3216. DECLARE_PER_CPU|
  3217. DEFINE_PER_CPU|
  3218. __typeof__\(|
  3219. union|
  3220. struct|
  3221. \.$Ident\s*=\s*|
  3222. ^\"|\"$
  3223. }x;
  3224. #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
  3225. if ($dstat ne '' &&
  3226. $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
  3227. $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
  3228. $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
  3229. $dstat !~ /^'X'$/ && # character constants
  3230. $dstat !~ /$exceptions/ &&
  3231. $dstat !~ /^\.$Ident\s*=/ && # .foo =
  3232. $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
  3233. $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
  3234. $dstat !~ /^for\s*$Constant$/ && # for (...)
  3235. $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
  3236. $dstat !~ /^do\s*{/ && # do {...
  3237. $dstat !~ /^\({/ && # ({...
  3238. $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
  3239. {
  3240. $ctx =~ s/\n*$//;
  3241. my $herectx = $here . "\n";
  3242. my $cnt = statement_rawlines($ctx);
  3243. for (my $n = 0; $n < $cnt; $n++) {
  3244. $herectx .= raw_line($linenr, $n) . "\n";
  3245. }
  3246. if ($dstat =~ /;/) {
  3247. ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
  3248. "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
  3249. } else {
  3250. ERROR("COMPLEX_MACRO",
  3251. "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
  3252. }
  3253. }
  3254. # check for line continuations outside of #defines, preprocessor #, and asm
  3255. } else {
  3256. if ($prevline !~ /^..*\\$/ &&
  3257. $line !~ /^\+\s*\#.*\\$/ && # preprocessor
  3258. $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
  3259. $line =~ /^\+.*\\$/) {
  3260. WARN("LINE_CONTINUATIONS",
  3261. "Avoid unnecessary line continuations\n" . $herecurr);
  3262. }
  3263. }
  3264. # do {} while (0) macro tests:
  3265. # single-statement macros do not need to be enclosed in do while (0) loop,
  3266. # macro should not end with a semicolon
  3267. if ($^V && $^V ge 5.10.0 &&
  3268. $realfile !~ m@/vmlinux.lds.h$@ &&
  3269. $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
  3270. my $ln = $linenr;
  3271. my $cnt = $realcnt;
  3272. my ($off, $dstat, $dcond, $rest);
  3273. my $ctx = '';
  3274. ($dstat, $dcond, $ln, $cnt, $off) =
  3275. ctx_statement_block($linenr, $realcnt, 0);
  3276. $ctx = $dstat;
  3277. $dstat =~ s/\\\n.//g;
  3278. if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
  3279. my $stmts = $2;
  3280. my $semis = $3;
  3281. $ctx =~ s/\n*$//;
  3282. my $cnt = statement_rawlines($ctx);
  3283. my $herectx = $here . "\n";
  3284. for (my $n = 0; $n < $cnt; $n++) {
  3285. $herectx .= raw_line($linenr, $n) . "\n";
  3286. }
  3287. if (($stmts =~ tr/;/;/) == 1 &&
  3288. $stmts !~ /^\s*(if|while|for|switch)\b/) {
  3289. WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
  3290. "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
  3291. }
  3292. if (defined $semis && $semis ne "") {
  3293. WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
  3294. "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
  3295. }
  3296. } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
  3297. $ctx =~ s/\n*$//;
  3298. my $cnt = statement_rawlines($ctx);
  3299. my $herectx = $here . "\n";
  3300. for (my $n = 0; $n < $cnt; $n++) {
  3301. $herectx .= raw_line($linenr, $n) . "\n";
  3302. }
  3303. WARN("TRAILING_SEMICOLON",
  3304. "macros should not use a trailing semicolon\n" . "$herectx");
  3305. }
  3306. }
  3307. # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
  3308. # all assignments may have only one of the following with an assignment:
  3309. # .
  3310. # ALIGN(...)
  3311. # VMLINUX_SYMBOL(...)
  3312. if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
  3313. WARN("MISSING_VMLINUX_SYMBOL",
  3314. "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
  3315. }
  3316. # check for redundant bracing round if etc
  3317. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  3318. my ($level, $endln, @chunks) =
  3319. ctx_statement_full($linenr, $realcnt, 1);
  3320. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  3321. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  3322. if ($#chunks > 0 && $level == 0) {
  3323. my @allowed = ();
  3324. my $allow = 0;
  3325. my $seen = 0;
  3326. my $herectx = $here . "\n";
  3327. my $ln = $linenr - 1;
  3328. for my $chunk (@chunks) {
  3329. my ($cond, $block) = @{$chunk};
  3330. # If the condition carries leading newlines, then count those as offsets.
  3331. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  3332. my $offset = statement_rawlines($whitespace) - 1;
  3333. $allowed[$allow] = 0;
  3334. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  3335. # We have looked at and allowed this specific line.
  3336. $suppress_ifbraces{$ln + $offset} = 1;
  3337. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  3338. $ln += statement_rawlines($block) - 1;
  3339. substr($block, 0, length($cond), '');
  3340. $seen++ if ($block =~ /^\s*{/);
  3341. #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
  3342. if (statement_lines($cond) > 1) {
  3343. #print "APW: ALLOWED: cond<$cond>\n";
  3344. $allowed[$allow] = 1;
  3345. }
  3346. if ($block =~/\b(?:if|for|while)\b/) {
  3347. #print "APW: ALLOWED: block<$block>\n";
  3348. $allowed[$allow] = 1;
  3349. }
  3350. if (statement_block_size($block) > 1) {
  3351. #print "APW: ALLOWED: lines block<$block>\n";
  3352. $allowed[$allow] = 1;
  3353. }
  3354. $allow++;
  3355. }
  3356. if ($seen) {
  3357. my $sum_allowed = 0;
  3358. foreach (@allowed) {
  3359. $sum_allowed += $_;
  3360. }
  3361. if ($sum_allowed == 0) {
  3362. WARN("BRACES",
  3363. "braces {} are not necessary for any arm of this statement\n" . $herectx);
  3364. } elsif ($sum_allowed != $allow &&
  3365. $seen != $allow) {
  3366. CHK("BRACES",
  3367. "braces {} should be used on all arms of this statement\n" . $herectx);
  3368. }
  3369. }
  3370. }
  3371. }
  3372. if (!defined $suppress_ifbraces{$linenr - 1} &&
  3373. $line =~ /\b(if|while|for|else)\b/) {
  3374. my $allowed = 0;
  3375. # Check the pre-context.
  3376. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  3377. #print "APW: ALLOWED: pre<$1>\n";
  3378. $allowed = 1;
  3379. }
  3380. my ($level, $endln, @chunks) =
  3381. ctx_statement_full($linenr, $realcnt, $-[0]);
  3382. # Check the condition.
  3383. my ($cond, $block) = @{$chunks[0]};
  3384. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  3385. if (defined $cond) {
  3386. substr($block, 0, length($cond), '');
  3387. }
  3388. if (statement_lines($cond) > 1) {
  3389. #print "APW: ALLOWED: cond<$cond>\n";
  3390. $allowed = 1;
  3391. }
  3392. if ($block =~/\b(?:if|for|while)\b/) {
  3393. #print "APW: ALLOWED: block<$block>\n";
  3394. $allowed = 1;
  3395. }
  3396. if (statement_block_size($block) > 1) {
  3397. #print "APW: ALLOWED: lines block<$block>\n";
  3398. $allowed = 1;
  3399. }
  3400. # Check the post-context.
  3401. if (defined $chunks[1]) {
  3402. my ($cond, $block) = @{$chunks[1]};
  3403. if (defined $cond) {
  3404. substr($block, 0, length($cond), '');
  3405. }
  3406. if ($block =~ /^\s*\{/) {
  3407. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  3408. $allowed = 1;
  3409. }
  3410. }
  3411. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  3412. my $herectx = $here . "\n";
  3413. my $cnt = statement_rawlines($block);
  3414. for (my $n = 0; $n < $cnt; $n++) {
  3415. $herectx .= raw_line($linenr, $n) . "\n";
  3416. }
  3417. WARN("BRACES",
  3418. "braces {} are not necessary for single statement blocks\n" . $herectx);
  3419. }
  3420. }
  3421. # check for unnecessary blank lines around braces
  3422. if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
  3423. CHK("BRACES",
  3424. "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
  3425. }
  3426. if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
  3427. CHK("BRACES",
  3428. "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
  3429. }
  3430. # no volatiles please
  3431. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  3432. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  3433. WARN("VOLATILE",
  3434. "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  3435. }
  3436. # warn about #if 0
  3437. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  3438. CHK("REDUNDANT_CODE",
  3439. "if this code is redundant consider removing it\n" .
  3440. $herecurr);
  3441. }
  3442. # check for needless "if (<foo>) fn(<foo>)" uses
  3443. if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
  3444. my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
  3445. if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
  3446. WARN('NEEDLESS_IF',
  3447. "$1(NULL) is safe this check is probably not required\n" . $hereprev);
  3448. }
  3449. }
  3450. # check for bad placement of section $InitAttribute (e.g.: __initdata)
  3451. if ($line =~ /(\b$InitAttribute\b)/) {
  3452. my $attr = $1;
  3453. if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
  3454. my $ptr = $1;
  3455. my $var = $2;
  3456. if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
  3457. ERROR("MISPLACED_INIT",
  3458. "$attr should be placed after $var\n" . $herecurr)) ||
  3459. ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
  3460. WARN("MISPLACED_INIT",
  3461. "$attr should be placed after $var\n" . $herecurr))) &&
  3462. $fix) {
  3463. $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;
  3464. }
  3465. }
  3466. }
  3467. # check for $InitAttributeData (ie: __initdata) with const
  3468. if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
  3469. my $attr = $1;
  3470. $attr =~ /($InitAttributePrefix)(.*)/;
  3471. my $attr_prefix = $1;
  3472. my $attr_type = $2;
  3473. if (ERROR("INIT_ATTRIBUTE",
  3474. "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
  3475. $fix) {
  3476. $fixed[$linenr - 1] =~
  3477. s/$InitAttributeData/${attr_prefix}initconst/;
  3478. }
  3479. }
  3480. # check for $InitAttributeConst (ie: __initconst) without const
  3481. if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
  3482. my $attr = $1;
  3483. if (ERROR("INIT_ATTRIBUTE",
  3484. "Use of $attr requires a separate use of const\n" . $herecurr) &&
  3485. $fix) {
  3486. my $lead = $fixed[$linenr - 1] =~
  3487. /(^\+\s*(?:static\s+))/;
  3488. $lead = rtrim($1);
  3489. $lead = "$lead " if ($lead !~ /^\+$/);
  3490. $lead = "${lead}const ";
  3491. $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
  3492. }
  3493. }
  3494. # don't use __constant_<foo> functions outside of include/uapi/
  3495. if ($realfile !~ m@^include/uapi/@ &&
  3496. $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
  3497. my $constant_func = $1;
  3498. my $func = $constant_func;
  3499. $func =~ s/^__constant_//;
  3500. if (WARN("CONSTANT_CONVERSION",
  3501. "$constant_func should be $func\n" . $herecurr) &&
  3502. $fix) {
  3503. $fixed[$linenr - 1] =~ s/\b$constant_func\b/$func/g;
  3504. }
  3505. }
  3506. # prefer usleep_range over udelay
  3507. if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
  3508. my $delay = $1;
  3509. # ignore udelay's < 10, however
  3510. if (! ($delay < 10) ) {
  3511. CHK("USLEEP_RANGE",
  3512. "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
  3513. }
  3514. if ($delay > 2000) {
  3515. WARN("LONG_UDELAY",
  3516. "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
  3517. }
  3518. }
  3519. # warn about unexpectedly long msleep's
  3520. if ($line =~ /\bmsleep\s*\((\d+)\);/) {
  3521. if ($1 < 20) {
  3522. WARN("MSLEEP",
  3523. "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
  3524. }
  3525. }
  3526. # check for comparisons of jiffies
  3527. if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
  3528. WARN("JIFFIES_COMPARISON",
  3529. "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
  3530. }
  3531. # check for comparisons of get_jiffies_64()
  3532. if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
  3533. WARN("JIFFIES_COMPARISON",
  3534. "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
  3535. }
  3536. # warn about #ifdefs in C files
  3537. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  3538. # print "#ifdef in C files should be avoided\n";
  3539. # print "$herecurr";
  3540. # $clean = 0;
  3541. # }
  3542. # warn about spacing in #ifdefs
  3543. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  3544. if (ERROR("SPACING",
  3545. "exactly one space required after that #$1\n" . $herecurr) &&
  3546. $fix) {
  3547. $fixed[$linenr - 1] =~
  3548. s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
  3549. }
  3550. }
  3551. # check for spinlock_t definitions without a comment.
  3552. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  3553. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  3554. my $which = $1;
  3555. if (!ctx_has_comment($first_line, $linenr)) {
  3556. CHK("UNCOMMENTED_DEFINITION",
  3557. "$1 definition without comment\n" . $herecurr);
  3558. }
  3559. }
  3560. # check for memory barriers without a comment.
  3561. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  3562. if (!ctx_has_comment($first_line, $linenr)) {
  3563. WARN("MEMORY_BARRIER",
  3564. "memory barrier without comment\n" . $herecurr);
  3565. }
  3566. }
  3567. # check of hardware specific defines
  3568. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  3569. CHK("ARCH_DEFINES",
  3570. "architecture specific defines should be avoided\n" . $herecurr);
  3571. }
  3572. # Check that the storage class is at the beginning of a declaration
  3573. if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
  3574. WARN("STORAGE_CLASS",
  3575. "storage class should be at the beginning of the declaration\n" . $herecurr)
  3576. }
  3577. # check the location of the inline attribute, that it is between
  3578. # storage class and type.
  3579. if ($line =~ /\b$Type\s+$Inline\b/ ||
  3580. $line =~ /\b$Inline\s+$Storage\b/) {
  3581. ERROR("INLINE_LOCATION",
  3582. "inline keyword should sit between storage class and type\n" . $herecurr);
  3583. }
  3584. # Check for __inline__ and __inline, prefer inline
  3585. if ($realfile !~ m@\binclude/uapi/@ &&
  3586. $line =~ /\b(__inline__|__inline)\b/) {
  3587. if (WARN("INLINE",
  3588. "plain inline is preferred over $1\n" . $herecurr) &&
  3589. $fix) {
  3590. $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
  3591. }
  3592. }
  3593. # Check for __attribute__ packed, prefer __packed
  3594. if ($realfile !~ m@\binclude/uapi/@ &&
  3595. $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
  3596. WARN("PREFER_PACKED",
  3597. "__packed is preferred over __attribute__((packed))\n" . $herecurr);
  3598. }
  3599. # Check for __attribute__ aligned, prefer __aligned
  3600. if ($realfile !~ m@\binclude/uapi/@ &&
  3601. $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
  3602. WARN("PREFER_ALIGNED",
  3603. "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
  3604. }
  3605. # Check for __attribute__ format(printf, prefer __printf
  3606. if ($realfile !~ m@\binclude/uapi/@ &&
  3607. $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
  3608. if (WARN("PREFER_PRINTF",
  3609. "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
  3610. $fix) {
  3611. $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
  3612. }
  3613. }
  3614. # Check for __attribute__ format(scanf, prefer __scanf
  3615. if ($realfile !~ m@\binclude/uapi/@ &&
  3616. $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
  3617. if (WARN("PREFER_SCANF",
  3618. "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
  3619. $fix) {
  3620. $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
  3621. }
  3622. }
  3623. # check for sizeof(&)
  3624. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  3625. WARN("SIZEOF_ADDRESS",
  3626. "sizeof(& should be avoided\n" . $herecurr);
  3627. }
  3628. # check for sizeof without parenthesis
  3629. if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
  3630. if (WARN("SIZEOF_PARENTHESIS",
  3631. "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
  3632. $fix) {
  3633. $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
  3634. }
  3635. }
  3636. # check for line continuations in quoted strings with odd counts of "
  3637. if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
  3638. WARN("LINE_CONTINUATIONS",
  3639. "Avoid line continuations in quoted strings\n" . $herecurr);
  3640. }
  3641. # check for struct spinlock declarations
  3642. if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
  3643. WARN("USE_SPINLOCK_T",
  3644. "struct spinlock should be spinlock_t\n" . $herecurr);
  3645. }
  3646. # check for seq_printf uses that could be seq_puts
  3647. if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
  3648. my $fmt = get_quoted_string($line, $rawline);
  3649. if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
  3650. if (WARN("PREFER_SEQ_PUTS",
  3651. "Prefer seq_puts to seq_printf\n" . $herecurr) &&
  3652. $fix) {
  3653. $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
  3654. }
  3655. }
  3656. }
  3657. # Check for misused memsets
  3658. if ($^V && $^V ge 5.10.0 &&
  3659. defined $stat &&
  3660. $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
  3661. my $ms_addr = $2;
  3662. my $ms_val = $7;
  3663. my $ms_size = $12;
  3664. if ($ms_size =~ /^(0x|)0$/i) {
  3665. ERROR("MEMSET",
  3666. "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
  3667. } elsif ($ms_size =~ /^(0x|)1$/i) {
  3668. WARN("MEMSET",
  3669. "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
  3670. }
  3671. }
  3672. # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
  3673. if ($^V && $^V ge 5.10.0 &&
  3674. $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
  3675. if (WARN("PREFER_ETHER_ADDR_COPY",
  3676. "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
  3677. $fix) {
  3678. $fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
  3679. }
  3680. }
  3681. # typecasts on min/max could be min_t/max_t
  3682. if ($^V && $^V ge 5.10.0 &&
  3683. defined $stat &&
  3684. $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
  3685. if (defined $2 || defined $7) {
  3686. my $call = $1;
  3687. my $cast1 = deparenthesize($2);
  3688. my $arg1 = $3;
  3689. my $cast2 = deparenthesize($7);
  3690. my $arg2 = $8;
  3691. my $cast;
  3692. if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
  3693. $cast = "$cast1 or $cast2";
  3694. } elsif ($cast1 ne "") {
  3695. $cast = $cast1;
  3696. } else {
  3697. $cast = $cast2;
  3698. }
  3699. WARN("MINMAX",
  3700. "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
  3701. }
  3702. }
  3703. # check usleep_range arguments
  3704. if ($^V && $^V ge 5.10.0 &&
  3705. defined $stat &&
  3706. $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
  3707. my $min = $1;
  3708. my $max = $7;
  3709. if ($min eq $max) {
  3710. WARN("USLEEP_RANGE",
  3711. "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
  3712. } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
  3713. $min > $max) {
  3714. WARN("USLEEP_RANGE",
  3715. "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
  3716. }
  3717. }
  3718. # check for naked sscanf
  3719. if ($^V && $^V ge 5.10.0 &&
  3720. defined $stat &&
  3721. $line =~ /\bsscanf\b/ &&
  3722. ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
  3723. $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
  3724. $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
  3725. my $lc = $stat =~ tr@\n@@;
  3726. $lc = $lc + $linenr;
  3727. my $stat_real = raw_line($linenr, 0);
  3728. for (my $count = $linenr + 1; $count <= $lc; $count++) {
  3729. $stat_real = $stat_real . "\n" . raw_line($count, 0);
  3730. }
  3731. WARN("NAKED_SSCANF",
  3732. "unchecked sscanf return value\n" . "$here\n$stat_real\n");
  3733. }
  3734. # check for simple sscanf that should be kstrto<foo>
  3735. if ($^V && $^V ge 5.10.0 &&
  3736. defined $stat &&
  3737. $line =~ /\bsscanf\b/) {
  3738. my $lc = $stat =~ tr@\n@@;
  3739. $lc = $lc + $linenr;
  3740. my $stat_real = raw_line($linenr, 0);
  3741. for (my $count = $linenr + 1; $count <= $lc; $count++) {
  3742. $stat_real = $stat_real . "\n" . raw_line($count, 0);
  3743. }
  3744. if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
  3745. my $format = $6;
  3746. my $count = $format =~ tr@%@%@;
  3747. if ($count == 1 &&
  3748. $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
  3749. WARN("SSCANF_TO_KSTRTO",
  3750. "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
  3751. }
  3752. }
  3753. }
  3754. # check for new externs in .h files.
  3755. if ($realfile =~ /\.h$/ &&
  3756. $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
  3757. if (CHK("AVOID_EXTERNS",
  3758. "extern prototypes should be avoided in .h files\n" . $herecurr) &&
  3759. $fix) {
  3760. $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
  3761. }
  3762. }
  3763. # check for new externs in .c files.
  3764. if ($realfile =~ /\.c$/ && defined $stat &&
  3765. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  3766. {
  3767. my $function_name = $1;
  3768. my $paren_space = $2;
  3769. my $s = $stat;
  3770. if (defined $cond) {
  3771. substr($s, 0, length($cond), '');
  3772. }
  3773. if ($s =~ /^\s*;/ &&
  3774. $function_name ne 'uninitialized_var')
  3775. {
  3776. WARN("AVOID_EXTERNS",
  3777. "externs should be avoided in .c files\n" . $herecurr);
  3778. }
  3779. if ($paren_space =~ /\n/) {
  3780. WARN("FUNCTION_ARGUMENTS",
  3781. "arguments for function declarations should follow identifier\n" . $herecurr);
  3782. }
  3783. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  3784. $stat =~ /^.\s*extern\s+/)
  3785. {
  3786. WARN("AVOID_EXTERNS",
  3787. "externs should be avoided in .c files\n" . $herecurr);
  3788. }
  3789. # checks for new __setup's
  3790. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  3791. my $name = $1;
  3792. if (!grep(/$name/, @setup_docs)) {
  3793. CHK("UNDOCUMENTED_SETUP",
  3794. "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  3795. }
  3796. }
  3797. # check for pointless casting of kmalloc return
  3798. if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
  3799. WARN("UNNECESSARY_CASTS",
  3800. "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  3801. }
  3802. # alloc style
  3803. # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
  3804. if ($^V && $^V ge 5.10.0 &&
  3805. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
  3806. CHK("ALLOC_SIZEOF_STRUCT",
  3807. "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
  3808. }
  3809. # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
  3810. if ($^V && $^V ge 5.10.0 &&
  3811. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/) {
  3812. my $oldfunc = $3;
  3813. my $a1 = $4;
  3814. my $a2 = $10;
  3815. my $newfunc = "kmalloc_array";
  3816. $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
  3817. if ($a1 =~ /^sizeof\s*\S/ || $a2 =~ /^sizeof\s*\S/) {
  3818. if (WARN("ALLOC_WITH_MULTIPLY",
  3819. "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
  3820. $fix) {
  3821. my $r1 = $a1;
  3822. my $r2 = $a2;
  3823. if ($a1 =~ /^sizeof\s*\S/) {
  3824. $r1 = $a2;
  3825. $r2 = $a1;
  3826. }
  3827. $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;
  3828. }
  3829. }
  3830. }
  3831. # check for krealloc arg reuse
  3832. if ($^V && $^V ge 5.10.0 &&
  3833. $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
  3834. WARN("KREALLOC_ARG_REUSE",
  3835. "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
  3836. }
  3837. # check for alloc argument mismatch
  3838. if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
  3839. WARN("ALLOC_ARRAY_ARGS",
  3840. "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
  3841. }
  3842. # check for multiple semicolons
  3843. if ($line =~ /;\s*;\s*$/) {
  3844. if (WARN("ONE_SEMICOLON",
  3845. "Statements terminations use 1 semicolon\n" . $herecurr) &&
  3846. $fix) {
  3847. $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
  3848. }
  3849. }
  3850. # check for case / default statements not preceeded by break/fallthrough/switch
  3851. if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
  3852. my $has_break = 0;
  3853. my $has_statement = 0;
  3854. my $count = 0;
  3855. my $prevline = $linenr;
  3856. while ($prevline > 1 && $count < 3 && !$has_break) {
  3857. $prevline--;
  3858. my $rline = $rawlines[$prevline - 1];
  3859. my $fline = $lines[$prevline - 1];
  3860. last if ($fline =~ /^\@\@/);
  3861. next if ($fline =~ /^\-/);
  3862. next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
  3863. $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
  3864. next if ($fline =~ /^.[\s$;]*$/);
  3865. $has_statement = 1;
  3866. $count++;
  3867. $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
  3868. }
  3869. if (!$has_break && $has_statement) {
  3870. WARN("MISSING_BREAK",
  3871. "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
  3872. }
  3873. }
  3874. # check for switch/default statements without a break;
  3875. if ($^V && $^V ge 5.10.0 &&
  3876. defined $stat &&
  3877. $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
  3878. my $ctx = '';
  3879. my $herectx = $here . "\n";
  3880. my $cnt = statement_rawlines($stat);
  3881. for (my $n = 0; $n < $cnt; $n++) {
  3882. $herectx .= raw_line($linenr, $n) . "\n";
  3883. }
  3884. WARN("DEFAULT_NO_BREAK",
  3885. "switch default: should use break\n" . $herectx);
  3886. }
  3887. # check for gcc specific __FUNCTION__
  3888. if ($line =~ /\b__FUNCTION__\b/) {
  3889. if (WARN("USE_FUNC",
  3890. "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
  3891. $fix) {
  3892. $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
  3893. }
  3894. }
  3895. # check for use of yield()
  3896. if ($line =~ /\byield\s*\(\s*\)/) {
  3897. WARN("YIELD",
  3898. "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
  3899. }
  3900. # check for comparisons against true and false
  3901. if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
  3902. my $lead = $1;
  3903. my $arg = $2;
  3904. my $test = $3;
  3905. my $otype = $4;
  3906. my $trail = $5;
  3907. my $op = "!";
  3908. ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
  3909. my $type = lc($otype);
  3910. if ($type =~ /^(?:true|false)$/) {
  3911. if (("$test" eq "==" && "$type" eq "true") ||
  3912. ("$test" eq "!=" && "$type" eq "false")) {
  3913. $op = "";
  3914. }
  3915. CHK("BOOL_COMPARISON",
  3916. "Using comparison to $otype is error prone\n" . $herecurr);
  3917. ## maybe suggesting a correct construct would better
  3918. ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
  3919. }
  3920. }
  3921. # check for semaphores initialized locked
  3922. if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
  3923. WARN("CONSIDER_COMPLETION",
  3924. "consider using a completion\n" . $herecurr);
  3925. }
  3926. # recommend kstrto* over simple_strto* and strict_strto*
  3927. if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
  3928. WARN("CONSIDER_KSTRTO",
  3929. "$1 is obsolete, use k$3 instead\n" . $herecurr);
  3930. }
  3931. # check for __initcall(), use device_initcall() explicitly or more appropriate function please
  3932. if ($line =~ /^.\s*__initcall\s*\(/) {
  3933. WARN("USE_DEVICE_INITCALL",
  3934. "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
  3935. }
  3936. # check for various ops structs, ensure they are const.
  3937. my $struct_ops = qr{acpi_dock_ops|
  3938. address_space_operations|
  3939. backlight_ops|
  3940. block_device_operations|
  3941. dentry_operations|
  3942. dev_pm_ops|
  3943. dma_map_ops|
  3944. extent_io_ops|
  3945. file_lock_operations|
  3946. file_operations|
  3947. hv_ops|
  3948. ide_dma_ops|
  3949. intel_dvo_dev_ops|
  3950. item_operations|
  3951. iwl_ops|
  3952. kgdb_arch|
  3953. kgdb_io|
  3954. kset_uevent_ops|
  3955. lock_manager_operations|
  3956. microcode_ops|
  3957. mtrr_ops|
  3958. neigh_ops|
  3959. nlmsvc_binding|
  3960. pci_raw_ops|
  3961. pipe_buf_operations|
  3962. platform_hibernation_ops|
  3963. platform_suspend_ops|
  3964. proto_ops|
  3965. rpc_pipe_ops|
  3966. seq_operations|
  3967. snd_ac97_build_ops|
  3968. soc_pcmcia_socket_ops|
  3969. stacktrace_ops|
  3970. sysfs_ops|
  3971. tty_operations|
  3972. usb_mon_operations|
  3973. wd_ops}x;
  3974. if ($line !~ /\bconst\b/ &&
  3975. $line =~ /\bstruct\s+($struct_ops)\b/) {
  3976. WARN("CONST_STRUCT",
  3977. "struct $1 should normally be const\n" .
  3978. $herecurr);
  3979. }
  3980. # use of NR_CPUS is usually wrong
  3981. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  3982. if ($line =~ /\bNR_CPUS\b/ &&
  3983. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  3984. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  3985. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  3986. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  3987. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  3988. {
  3989. WARN("NR_CPUS",
  3990. "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  3991. }
  3992. # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
  3993. if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
  3994. ERROR("DEFINE_ARCH_HAS",
  3995. "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
  3996. }
  3997. # check for %L{u,d,i} in strings
  3998. my $string;
  3999. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  4000. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  4001. $string =~ s/%%/__/g;
  4002. if ($string =~ /(?<!%)%L[udi]/) {
  4003. WARN("PRINTF_L",
  4004. "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  4005. last;
  4006. }
  4007. }
  4008. # whine mightly about in_atomic
  4009. if ($line =~ /\bin_atomic\s*\(/) {
  4010. if ($realfile =~ m@^drivers/@) {
  4011. ERROR("IN_ATOMIC",
  4012. "do not use in_atomic in drivers\n" . $herecurr);
  4013. } elsif ($realfile !~ m@^kernel/@) {
  4014. WARN("IN_ATOMIC",
  4015. "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  4016. }
  4017. }
  4018. # check for lockdep_set_novalidate_class
  4019. if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
  4020. $line =~ /__lockdep_no_validate__\s*\)/ ) {
  4021. if ($realfile !~ m@^kernel/lockdep@ &&
  4022. $realfile !~ m@^include/linux/lockdep@ &&
  4023. $realfile !~ m@^drivers/base/core@) {
  4024. ERROR("LOCKDEP",
  4025. "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
  4026. }
  4027. }
  4028. if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
  4029. $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
  4030. WARN("EXPORTED_WORLD_WRITABLE",
  4031. "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
  4032. }
  4033. # Mode permission misuses where it seems decimal should be octal
  4034. # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
  4035. if ($^V && $^V ge 5.10.0 &&
  4036. $line =~ /$mode_perms_search/) {
  4037. foreach my $entry (@mode_permission_funcs) {
  4038. my $func = $entry->[0];
  4039. my $arg_pos = $entry->[1];
  4040. my $skip_args = "";
  4041. if ($arg_pos > 1) {
  4042. $arg_pos--;
  4043. $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
  4044. }
  4045. my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
  4046. if ($line =~ /$test/) {
  4047. my $val = $1;
  4048. $val = $6 if ($skip_args ne "");
  4049. if ($val !~ /^0$/ &&
  4050. (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
  4051. length($val) ne 4)) {
  4052. ERROR("NON_OCTAL_PERMISSIONS",
  4053. "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
  4054. }
  4055. }
  4056. }
  4057. }
  4058. }
  4059. # If we have no input at all, then there is nothing to report on
  4060. # so just keep quiet.
  4061. if ($#rawlines == -1) {
  4062. exit(0);
  4063. }
  4064. # In mailback mode only produce a report in the negative, for
  4065. # things that appear to be patches.
  4066. if ($mailback && ($clean == 1 || !$is_patch)) {
  4067. exit(0);
  4068. }
  4069. # This is not a patch, and we are are in 'no-patch' mode so
  4070. # just keep quiet.
  4071. if (!$chk_patch && !$is_patch) {
  4072. exit(0);
  4073. }
  4074. if (!$is_patch) {
  4075. ERROR("NOT_UNIFIED_DIFF",
  4076. "Does not appear to be a unified-diff format patch\n");
  4077. }
  4078. if ($is_patch && $chk_signoff && $signoff == 0) {
  4079. ERROR("MISSING_SIGN_OFF",
  4080. "Missing Signed-off-by: line(s)\n");
  4081. }
  4082. print report_dump();
  4083. if ($summary && !($clean == 1 && $quiet == 1)) {
  4084. print "$filename " if ($summary_file);
  4085. print "total: $cnt_error errors, $cnt_warn warnings, " .
  4086. (($check)? "$cnt_chk checks, " : "") .
  4087. "$cnt_lines lines checked\n";
  4088. print "\n" if ($quiet == 0);
  4089. }
  4090. if ($quiet == 0) {
  4091. if ($^V lt 5.10.0) {
  4092. print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
  4093. print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
  4094. }
  4095. # If there were whitespace errors which cleanpatch can fix
  4096. # then suggest that.
  4097. if ($rpt_cleaners) {
  4098. print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
  4099. print " scripts/cleanfile\n\n";
  4100. $rpt_cleaners = 0;
  4101. }
  4102. }
  4103. hash_show_words(\%use_type, "Used");
  4104. hash_show_words(\%ignore_type, "Ignored");
  4105. if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
  4106. my $newfile = $filename;
  4107. $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
  4108. my $linecount = 0;
  4109. my $f;
  4110. open($f, '>', $newfile)
  4111. or die "$P: Can't open $newfile for write\n";
  4112. foreach my $fixed_line (@fixed) {
  4113. $linecount++;
  4114. if ($file) {
  4115. if ($linecount > 3) {
  4116. $fixed_line =~ s/^\+//;
  4117. print $f $fixed_line. "\n";
  4118. }
  4119. } else {
  4120. print $f $fixed_line . "\n";
  4121. }
  4122. }
  4123. close($f);
  4124. if (!$quiet) {
  4125. print << "EOM";
  4126. Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
  4127. Do _NOT_ trust the results written to this file.
  4128. Do _NOT_ submit these changes without inspecting them for correctness.
  4129. This EXPERIMENTAL file is simply a convenience to help rewrite patches.
  4130. No warranties, expressed or implied...
  4131. EOM
  4132. }
  4133. }
  4134. if ($clean == 1 && $quiet == 0) {
  4135. print "$vname has no obvious style problems and is ready for submission.\n"
  4136. }
  4137. if ($clean == 0 && $quiet == 0) {
  4138. print << "EOM";
  4139. $vname has style problems, please review.
  4140. If any of these errors are false positives, please report
  4141. them to the maintainer, see CHECKPATCH in MAINTAINERS.
  4142. EOM
  4143. }
  4144. return $clean;
  4145. }