checkpatch.pl 101 KB

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