checkpatch.pl 98 KB

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