checkpatch.pl 101 KB

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