checkpatch.pl 115 KB

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