checkpatch.pl 99 KB

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