checkpatch.pl 139 KB

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