checkpatch.pl 116 KB

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