checkpatch.pl 104 KB

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