checkpatch.pl 116 KB

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