checkpatch.pl 118 KB

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