checkpatch.pl 120 KB

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