checkpatch.pl 129 KB

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