checkpatch.pl 118 KB

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