checkpatch.pl 102 KB

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