checkpatch.pl 114 KB

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