checkpatch.pl 118 KB

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