checkpatch.pl 98 KB

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