checkpatch.pl 126 KB

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