checkpatch.pl 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  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,2009, Andy Whitcroft <apw@canonical.com>
  6. # Licensed under the terms of the GNU GPL License version 2
  7. use strict;
  8. my $P = $0;
  9. $P =~ s@.*/@@g;
  10. my $V = '0.30';
  11. use Getopt::Long qw(:config no_auto_abbrev);
  12. my $quiet = 0;
  13. my $tree = 1;
  14. my $chk_signoff = 1;
  15. my $chk_patch = 1;
  16. my $tst_only;
  17. my $emacs = 0;
  18. my $terse = 0;
  19. my $file = 0;
  20. my $check = 0;
  21. my $summary = 1;
  22. my $mailback = 0;
  23. my $summary_file = 0;
  24. my $root;
  25. my %debug;
  26. my $help = 0;
  27. sub help {
  28. my ($exitcode) = @_;
  29. print << "EOM";
  30. Usage: $P [OPTION]... [FILE]...
  31. Version: $V
  32. Options:
  33. -q, --quiet quiet
  34. --no-tree run without a kernel tree
  35. --no-signoff do not check for 'Signed-off-by' line
  36. --patch treat FILE as patchfile (default)
  37. --emacs emacs compile window format
  38. --terse one line per report
  39. -f, --file treat FILE as regular source file
  40. --subjective, --strict enable more subjective tests
  41. --root=PATH PATH to the kernel tree root
  42. --no-summary suppress the per-file summary
  43. --mailback only produce a report in case of warnings/errors
  44. --summary-file include the filename in summary
  45. --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
  46. 'values', 'possible', 'type', and 'attr' (default
  47. is all off)
  48. --test-only=WORD report only warnings/errors containing WORD
  49. literally
  50. -h, --help, --version display this help and exit
  51. When FILE is - read standard input.
  52. EOM
  53. exit($exitcode);
  54. }
  55. GetOptions(
  56. 'q|quiet+' => \$quiet,
  57. 'tree!' => \$tree,
  58. 'signoff!' => \$chk_signoff,
  59. 'patch!' => \$chk_patch,
  60. 'emacs!' => \$emacs,
  61. 'terse!' => \$terse,
  62. 'f|file!' => \$file,
  63. 'subjective!' => \$check,
  64. 'strict!' => \$check,
  65. 'root=s' => \$root,
  66. 'summary!' => \$summary,
  67. 'mailback!' => \$mailback,
  68. 'summary-file!' => \$summary_file,
  69. 'debug=s' => \%debug,
  70. 'test-only=s' => \$tst_only,
  71. 'h|help' => \$help,
  72. 'version' => \$help
  73. ) or help(1);
  74. help(0) if ($help);
  75. my $exit = 0;
  76. if ($#ARGV < 0) {
  77. print "$P: no input files\n";
  78. exit(1);
  79. }
  80. my $dbg_values = 0;
  81. my $dbg_possible = 0;
  82. my $dbg_type = 0;
  83. my $dbg_attr = 0;
  84. for my $key (keys %debug) {
  85. ## no critic
  86. eval "\${dbg_$key} = '$debug{$key}';";
  87. die "$@" if ($@);
  88. }
  89. my $rpt_cleaners = 0;
  90. if ($terse) {
  91. $emacs = 1;
  92. $quiet++;
  93. }
  94. if ($tree) {
  95. if (defined $root) {
  96. if (!top_of_kernel_tree($root)) {
  97. die "$P: $root: --root does not point at a valid tree\n";
  98. }
  99. } else {
  100. if (top_of_kernel_tree('.')) {
  101. $root = '.';
  102. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  103. top_of_kernel_tree($1)) {
  104. $root = $1;
  105. }
  106. }
  107. if (!defined $root) {
  108. print "Must be run from the top-level dir. of a kernel tree\n";
  109. exit(2);
  110. }
  111. }
  112. my $emitted_corrupt = 0;
  113. our $Ident = qr{
  114. [A-Za-z_][A-Za-z\d_]*
  115. (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
  116. }x;
  117. our $Storage = qr{extern|static|asmlinkage};
  118. our $Sparse = qr{
  119. __user|
  120. __kernel|
  121. __force|
  122. __iomem|
  123. __must_check|
  124. __init_refok|
  125. __kprobes|
  126. __ref
  127. }x;
  128. # Notes to $Attribute:
  129. # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
  130. our $Attribute = qr{
  131. const|
  132. __read_mostly|
  133. __kprobes|
  134. __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
  135. ____cacheline_aligned|
  136. ____cacheline_aligned_in_smp|
  137. ____cacheline_internodealigned_in_smp|
  138. __weak
  139. }x;
  140. our $Modifier;
  141. our $Inline = qr{inline|__always_inline|noinline};
  142. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  143. our $Lval = qr{$Ident(?:$Member)*};
  144. our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
  145. our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
  146. our $Compare = qr{<=|>=|==|!=|<|>};
  147. our $Operators = qr{
  148. <=|>=|==|!=|
  149. =>|->|<<|>>|<|>|!|~|
  150. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  151. }x;
  152. our $NonptrType;
  153. our $Type;
  154. our $Declare;
  155. our $UTF8 = qr {
  156. [\x09\x0A\x0D\x20-\x7E] # ASCII
  157. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  158. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  159. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  160. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  161. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  162. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  163. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  164. }x;
  165. our $typeTypedefs = qr{(?x:
  166. (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
  167. atomic_t
  168. )};
  169. our $logFunctions = qr{(?x:
  170. printk|
  171. pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
  172. (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
  173. WARN|
  174. panic
  175. )};
  176. our @typeList = (
  177. qr{void},
  178. qr{(?:unsigned\s+)?char},
  179. qr{(?:unsigned\s+)?short},
  180. qr{(?:unsigned\s+)?int},
  181. qr{(?:unsigned\s+)?long},
  182. qr{(?:unsigned\s+)?long\s+int},
  183. qr{(?:unsigned\s+)?long\s+long},
  184. qr{(?:unsigned\s+)?long\s+long\s+int},
  185. qr{unsigned},
  186. qr{float},
  187. qr{double},
  188. qr{bool},
  189. qr{struct\s+$Ident},
  190. qr{union\s+$Ident},
  191. qr{enum\s+$Ident},
  192. qr{${Ident}_t},
  193. qr{${Ident}_handler},
  194. qr{${Ident}_handler_fn},
  195. );
  196. our @modifierList = (
  197. qr{fastcall},
  198. );
  199. our $allowed_asm_includes = qr{(?x:
  200. irq|
  201. memory
  202. )};
  203. # memory.h: ARM has a custom one
  204. sub build_types {
  205. my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
  206. my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
  207. $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
  208. $NonptrType = qr{
  209. (?:$Modifier\s+|const\s+)*
  210. (?:
  211. (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
  212. (?:$typeTypedefs\b)|
  213. (?:${all}\b)
  214. )
  215. (?:\s+$Modifier|\s+const)*
  216. }x;
  217. $Type = qr{
  218. $NonptrType
  219. (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
  220. (?:\s+$Inline|\s+$Modifier)*
  221. }x;
  222. $Declare = qr{(?:$Storage\s+)?$Type};
  223. }
  224. build_types();
  225. $chk_signoff = 0 if ($file);
  226. my @dep_includes = ();
  227. my @dep_functions = ();
  228. my $removal = "Documentation/feature-removal-schedule.txt";
  229. if ($tree && -f "$root/$removal") {
  230. open(my $REMOVE, '<', "$root/$removal") ||
  231. die "$P: $removal: open failed - $!\n";
  232. while (<$REMOVE>) {
  233. if (/^Check:\s+(.*\S)/) {
  234. for my $entry (split(/[, ]+/, $1)) {
  235. if ($entry =~ m@include/(.*)@) {
  236. push(@dep_includes, $1);
  237. } elsif ($entry !~ m@/@) {
  238. push(@dep_functions, $entry);
  239. }
  240. }
  241. }
  242. }
  243. close($REMOVE);
  244. }
  245. my @rawlines = ();
  246. my @lines = ();
  247. my $vname;
  248. for my $filename (@ARGV) {
  249. my $FILE;
  250. if ($file) {
  251. open($FILE, '-|', "diff -u /dev/null $filename") ||
  252. die "$P: $filename: diff failed - $!\n";
  253. } elsif ($filename eq '-') {
  254. open($FILE, '<&STDIN');
  255. } else {
  256. open($FILE, '<', "$filename") ||
  257. die "$P: $filename: open failed - $!\n";
  258. }
  259. if ($filename eq '-') {
  260. $vname = 'Your patch';
  261. } else {
  262. $vname = $filename;
  263. }
  264. while (<$FILE>) {
  265. chomp;
  266. push(@rawlines, $_);
  267. }
  268. close($FILE);
  269. if (!process($filename)) {
  270. $exit = 1;
  271. }
  272. @rawlines = ();
  273. @lines = ();
  274. }
  275. exit($exit);
  276. sub top_of_kernel_tree {
  277. my ($root) = @_;
  278. my @tree_check = (
  279. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  280. "README", "Documentation", "arch", "include", "drivers",
  281. "fs", "init", "ipc", "kernel", "lib", "scripts",
  282. );
  283. foreach my $check (@tree_check) {
  284. if (! -e $root . '/' . $check) {
  285. return 0;
  286. }
  287. }
  288. return 1;
  289. }
  290. sub expand_tabs {
  291. my ($str) = @_;
  292. my $res = '';
  293. my $n = 0;
  294. for my $c (split(//, $str)) {
  295. if ($c eq "\t") {
  296. $res .= ' ';
  297. $n++;
  298. for (; ($n % 8) != 0; $n++) {
  299. $res .= ' ';
  300. }
  301. next;
  302. }
  303. $res .= $c;
  304. $n++;
  305. }
  306. return $res;
  307. }
  308. sub copy_spacing {
  309. (my $res = shift) =~ tr/\t/ /c;
  310. return $res;
  311. }
  312. sub line_stats {
  313. my ($line) = @_;
  314. # Drop the diff line leader and expand tabs
  315. $line =~ s/^.//;
  316. $line = expand_tabs($line);
  317. # Pick the indent from the front of the line.
  318. my ($white) = ($line =~ /^(\s*)/);
  319. return (length($line), length($white));
  320. }
  321. my $sanitise_quote = '';
  322. sub sanitise_line_reset {
  323. my ($in_comment) = @_;
  324. if ($in_comment) {
  325. $sanitise_quote = '*/';
  326. } else {
  327. $sanitise_quote = '';
  328. }
  329. }
  330. sub sanitise_line {
  331. my ($line) = @_;
  332. my $res = '';
  333. my $l = '';
  334. my $qlen = 0;
  335. my $off = 0;
  336. my $c;
  337. # Always copy over the diff marker.
  338. $res = substr($line, 0, 1);
  339. for ($off = 1; $off < length($line); $off++) {
  340. $c = substr($line, $off, 1);
  341. # Comments we are wacking completly including the begin
  342. # and end, all to $;.
  343. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
  344. $sanitise_quote = '*/';
  345. substr($res, $off, 2, "$;$;");
  346. $off++;
  347. next;
  348. }
  349. if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
  350. $sanitise_quote = '';
  351. substr($res, $off, 2, "$;$;");
  352. $off++;
  353. next;
  354. }
  355. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
  356. $sanitise_quote = '//';
  357. substr($res, $off, 2, $sanitise_quote);
  358. $off++;
  359. next;
  360. }
  361. # A \ in a string means ignore the next character.
  362. if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
  363. $c eq "\\") {
  364. substr($res, $off, 2, 'XX');
  365. $off++;
  366. next;
  367. }
  368. # Regular quotes.
  369. if ($c eq "'" || $c eq '"') {
  370. if ($sanitise_quote eq '') {
  371. $sanitise_quote = $c;
  372. substr($res, $off, 1, $c);
  373. next;
  374. } elsif ($sanitise_quote eq $c) {
  375. $sanitise_quote = '';
  376. }
  377. }
  378. #print "c<$c> SQ<$sanitise_quote>\n";
  379. if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
  380. substr($res, $off, 1, $;);
  381. } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
  382. substr($res, $off, 1, $;);
  383. } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
  384. substr($res, $off, 1, 'X');
  385. } else {
  386. substr($res, $off, 1, $c);
  387. }
  388. }
  389. if ($sanitise_quote eq '//') {
  390. $sanitise_quote = '';
  391. }
  392. # The pathname on a #include may be surrounded by '<' and '>'.
  393. if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
  394. my $clean = 'X' x length($1);
  395. $res =~ s@\<.*\>@<$clean>@;
  396. # The whole of a #error is a string.
  397. } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
  398. my $clean = 'X' x length($1);
  399. $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
  400. }
  401. return $res;
  402. }
  403. sub ctx_statement_block {
  404. my ($linenr, $remain, $off) = @_;
  405. my $line = $linenr - 1;
  406. my $blk = '';
  407. my $soff = $off;
  408. my $coff = $off - 1;
  409. my $coff_set = 0;
  410. my $loff = 0;
  411. my $type = '';
  412. my $level = 0;
  413. my @stack = ();
  414. my $p;
  415. my $c;
  416. my $len = 0;
  417. my $remainder;
  418. while (1) {
  419. @stack = (['', 0]) if ($#stack == -1);
  420. #warn "CSB: blk<$blk> remain<$remain>\n";
  421. # If we are about to drop off the end, pull in more
  422. # context.
  423. if ($off >= $len) {
  424. for (; $remain > 0; $line++) {
  425. last if (!defined $lines[$line]);
  426. next if ($lines[$line] =~ /^-/);
  427. $remain--;
  428. $loff = $len;
  429. $blk .= $lines[$line] . "\n";
  430. $len = length($blk);
  431. $line++;
  432. last;
  433. }
  434. # Bail if there is no further context.
  435. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  436. if ($off >= $len) {
  437. last;
  438. }
  439. }
  440. $p = $c;
  441. $c = substr($blk, $off, 1);
  442. $remainder = substr($blk, $off);
  443. #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
  444. # Handle nested #if/#else.
  445. if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
  446. push(@stack, [ $type, $level ]);
  447. } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
  448. ($type, $level) = @{$stack[$#stack - 1]};
  449. } elsif ($remainder =~ /^#\s*endif\b/) {
  450. ($type, $level) = @{pop(@stack)};
  451. }
  452. # Statement ends at the ';' or a close '}' at the
  453. # outermost level.
  454. if ($level == 0 && $c eq ';') {
  455. last;
  456. }
  457. # An else is really a conditional as long as its not else if
  458. if ($level == 0 && $coff_set == 0 &&
  459. (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
  460. $remainder =~ /^(else)(?:\s|{)/ &&
  461. $remainder !~ /^else\s+if\b/) {
  462. $coff = $off + length($1) - 1;
  463. $coff_set = 1;
  464. #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
  465. #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
  466. }
  467. if (($type eq '' || $type eq '(') && $c eq '(') {
  468. $level++;
  469. $type = '(';
  470. }
  471. if ($type eq '(' && $c eq ')') {
  472. $level--;
  473. $type = ($level != 0)? '(' : '';
  474. if ($level == 0 && $coff < $soff) {
  475. $coff = $off;
  476. $coff_set = 1;
  477. #warn "CSB: mark coff<$coff>\n";
  478. }
  479. }
  480. if (($type eq '' || $type eq '{') && $c eq '{') {
  481. $level++;
  482. $type = '{';
  483. }
  484. if ($type eq '{' && $c eq '}') {
  485. $level--;
  486. $type = ($level != 0)? '{' : '';
  487. if ($level == 0) {
  488. if (substr($blk, $off + 1, 1) eq ';') {
  489. $off++;
  490. }
  491. last;
  492. }
  493. }
  494. $off++;
  495. }
  496. # We are truly at the end, so shuffle to the next line.
  497. if ($off == $len) {
  498. $loff = $len + 1;
  499. $line++;
  500. $remain--;
  501. }
  502. my $statement = substr($blk, $soff, $off - $soff + 1);
  503. my $condition = substr($blk, $soff, $coff - $soff + 1);
  504. #warn "STATEMENT<$statement>\n";
  505. #warn "CONDITION<$condition>\n";
  506. #print "coff<$coff> soff<$off> loff<$loff>\n";
  507. return ($statement, $condition,
  508. $line, $remain + 1, $off - $loff + 1, $level);
  509. }
  510. sub statement_lines {
  511. my ($stmt) = @_;
  512. # Strip the diff line prefixes and rip blank lines at start and end.
  513. $stmt =~ s/(^|\n)./$1/g;
  514. $stmt =~ s/^\s*//;
  515. $stmt =~ s/\s*$//;
  516. my @stmt_lines = ($stmt =~ /\n/g);
  517. return $#stmt_lines + 2;
  518. }
  519. sub statement_rawlines {
  520. my ($stmt) = @_;
  521. my @stmt_lines = ($stmt =~ /\n/g);
  522. return $#stmt_lines + 2;
  523. }
  524. sub statement_block_size {
  525. my ($stmt) = @_;
  526. $stmt =~ s/(^|\n)./$1/g;
  527. $stmt =~ s/^\s*{//;
  528. $stmt =~ s/}\s*$//;
  529. $stmt =~ s/^\s*//;
  530. $stmt =~ s/\s*$//;
  531. my @stmt_lines = ($stmt =~ /\n/g);
  532. my @stmt_statements = ($stmt =~ /;/g);
  533. my $stmt_lines = $#stmt_lines + 2;
  534. my $stmt_statements = $#stmt_statements + 1;
  535. if ($stmt_lines > $stmt_statements) {
  536. return $stmt_lines;
  537. } else {
  538. return $stmt_statements;
  539. }
  540. }
  541. sub ctx_statement_full {
  542. my ($linenr, $remain, $off) = @_;
  543. my ($statement, $condition, $level);
  544. my (@chunks);
  545. # Grab the first conditional/block pair.
  546. ($statement, $condition, $linenr, $remain, $off, $level) =
  547. ctx_statement_block($linenr, $remain, $off);
  548. #print "F: c<$condition> s<$statement> remain<$remain>\n";
  549. push(@chunks, [ $condition, $statement ]);
  550. if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
  551. return ($level, $linenr, @chunks);
  552. }
  553. # Pull in the following conditional/block pairs and see if they
  554. # could continue the statement.
  555. for (;;) {
  556. ($statement, $condition, $linenr, $remain, $off, $level) =
  557. ctx_statement_block($linenr, $remain, $off);
  558. #print "C: c<$condition> s<$statement> remain<$remain>\n";
  559. last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
  560. #print "C: push\n";
  561. push(@chunks, [ $condition, $statement ]);
  562. }
  563. return ($level, $linenr, @chunks);
  564. }
  565. sub ctx_block_get {
  566. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  567. my $line;
  568. my $start = $linenr - 1;
  569. my $blk = '';
  570. my @o;
  571. my @c;
  572. my @res = ();
  573. my $level = 0;
  574. my @stack = ($level);
  575. for ($line = $start; $remain > 0; $line++) {
  576. next if ($rawlines[$line] =~ /^-/);
  577. $remain--;
  578. $blk .= $rawlines[$line];
  579. # Handle nested #if/#else.
  580. if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
  581. push(@stack, $level);
  582. } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
  583. $level = $stack[$#stack - 1];
  584. } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
  585. $level = pop(@stack);
  586. }
  587. foreach my $c (split(//, $rawlines[$line])) {
  588. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  589. if ($off > 0) {
  590. $off--;
  591. next;
  592. }
  593. if ($c eq $close && $level > 0) {
  594. $level--;
  595. last if ($level == 0);
  596. } elsif ($c eq $open) {
  597. $level++;
  598. }
  599. }
  600. if (!$outer || $level <= 1) {
  601. push(@res, $rawlines[$line]);
  602. }
  603. last if ($level == 0);
  604. }
  605. return ($level, @res);
  606. }
  607. sub ctx_block_outer {
  608. my ($linenr, $remain) = @_;
  609. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  610. return @r;
  611. }
  612. sub ctx_block {
  613. my ($linenr, $remain) = @_;
  614. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  615. return @r;
  616. }
  617. sub ctx_statement {
  618. my ($linenr, $remain, $off) = @_;
  619. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  620. return @r;
  621. }
  622. sub ctx_block_level {
  623. my ($linenr, $remain) = @_;
  624. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  625. }
  626. sub ctx_statement_level {
  627. my ($linenr, $remain, $off) = @_;
  628. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  629. }
  630. sub ctx_locate_comment {
  631. my ($first_line, $end_line) = @_;
  632. # Catch a comment on the end of the line itself.
  633. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
  634. return $current_comment if (defined $current_comment);
  635. # Look through the context and try and figure out if there is a
  636. # comment.
  637. my $in_comment = 0;
  638. $current_comment = '';
  639. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  640. my $line = $rawlines[$linenr - 1];
  641. #warn " $line\n";
  642. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  643. $in_comment = 1;
  644. }
  645. if ($line =~ m@/\*@) {
  646. $in_comment = 1;
  647. }
  648. if (!$in_comment && $current_comment ne '') {
  649. $current_comment = '';
  650. }
  651. $current_comment .= $line . "\n" if ($in_comment);
  652. if ($line =~ m@\*/@) {
  653. $in_comment = 0;
  654. }
  655. }
  656. chomp($current_comment);
  657. return($current_comment);
  658. }
  659. sub ctx_has_comment {
  660. my ($first_line, $end_line) = @_;
  661. my $cmt = ctx_locate_comment($first_line, $end_line);
  662. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  663. ##print "CMMT: $cmt\n";
  664. return ($cmt ne '');
  665. }
  666. sub raw_line {
  667. my ($linenr, $cnt) = @_;
  668. my $offset = $linenr - 1;
  669. $cnt++;
  670. my $line;
  671. while ($cnt) {
  672. $line = $rawlines[$offset++];
  673. next if (defined($line) && $line =~ /^-/);
  674. $cnt--;
  675. }
  676. return $line;
  677. }
  678. sub cat_vet {
  679. my ($vet) = @_;
  680. my ($res, $coded);
  681. $res = '';
  682. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  683. $res .= $1;
  684. if ($2 ne '') {
  685. $coded = sprintf("^%c", unpack('C', $2) + 64);
  686. $res .= $coded;
  687. }
  688. }
  689. $res =~ s/$/\$/;
  690. return $res;
  691. }
  692. my $av_preprocessor = 0;
  693. my $av_pending;
  694. my @av_paren_type;
  695. my $av_pend_colon;
  696. sub annotate_reset {
  697. $av_preprocessor = 0;
  698. $av_pending = '_';
  699. @av_paren_type = ('E');
  700. $av_pend_colon = 'O';
  701. }
  702. sub annotate_values {
  703. my ($stream, $type) = @_;
  704. my $res;
  705. my $var = '_' x length($stream);
  706. my $cur = $stream;
  707. print "$stream\n" if ($dbg_values > 1);
  708. while (length($cur)) {
  709. @av_paren_type = ('E') if ($#av_paren_type < 0);
  710. print " <" . join('', @av_paren_type) .
  711. "> <$type> <$av_pending>" if ($dbg_values > 1);
  712. if ($cur =~ /^(\s+)/o) {
  713. print "WS($1)\n" if ($dbg_values > 1);
  714. if ($1 =~ /\n/ && $av_preprocessor) {
  715. $type = pop(@av_paren_type);
  716. $av_preprocessor = 0;
  717. }
  718. } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) {
  719. print "CAST($1)\n" if ($dbg_values > 1);
  720. push(@av_paren_type, $type);
  721. $type = 'C';
  722. } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
  723. print "DECLARE($1)\n" if ($dbg_values > 1);
  724. $type = 'T';
  725. } elsif ($cur =~ /^($Modifier)\s*/) {
  726. print "MODIFIER($1)\n" if ($dbg_values > 1);
  727. $type = 'T';
  728. } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
  729. print "DEFINE($1,$2)\n" if ($dbg_values > 1);
  730. $av_preprocessor = 1;
  731. push(@av_paren_type, $type);
  732. if ($2 ne '') {
  733. $av_pending = 'N';
  734. }
  735. $type = 'E';
  736. } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
  737. print "UNDEF($1)\n" if ($dbg_values > 1);
  738. $av_preprocessor = 1;
  739. push(@av_paren_type, $type);
  740. } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
  741. print "PRE_START($1)\n" if ($dbg_values > 1);
  742. $av_preprocessor = 1;
  743. push(@av_paren_type, $type);
  744. push(@av_paren_type, $type);
  745. $type = 'E';
  746. } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
  747. print "PRE_RESTART($1)\n" if ($dbg_values > 1);
  748. $av_preprocessor = 1;
  749. push(@av_paren_type, $av_paren_type[$#av_paren_type]);
  750. $type = 'E';
  751. } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
  752. print "PRE_END($1)\n" if ($dbg_values > 1);
  753. $av_preprocessor = 1;
  754. # Assume all arms of the conditional end as this
  755. # one does, and continue as if the #endif was not here.
  756. pop(@av_paren_type);
  757. push(@av_paren_type, $type);
  758. $type = 'E';
  759. } elsif ($cur =~ /^(\\\n)/o) {
  760. print "PRECONT($1)\n" if ($dbg_values > 1);
  761. } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
  762. print "ATTR($1)\n" if ($dbg_values > 1);
  763. $av_pending = $type;
  764. $type = 'N';
  765. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  766. print "SIZEOF($1)\n" if ($dbg_values > 1);
  767. if (defined $2) {
  768. $av_pending = 'V';
  769. }
  770. $type = 'N';
  771. } elsif ($cur =~ /^(if|while|for)\b/o) {
  772. print "COND($1)\n" if ($dbg_values > 1);
  773. $av_pending = 'E';
  774. $type = 'N';
  775. } elsif ($cur =~/^(case)/o) {
  776. print "CASE($1)\n" if ($dbg_values > 1);
  777. $av_pend_colon = 'C';
  778. $type = 'N';
  779. } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
  780. print "KEYWORD($1)\n" if ($dbg_values > 1);
  781. $type = 'N';
  782. } elsif ($cur =~ /^(\()/o) {
  783. print "PAREN('$1')\n" if ($dbg_values > 1);
  784. push(@av_paren_type, $av_pending);
  785. $av_pending = '_';
  786. $type = 'N';
  787. } elsif ($cur =~ /^(\))/o) {
  788. my $new_type = pop(@av_paren_type);
  789. if ($new_type ne '_') {
  790. $type = $new_type;
  791. print "PAREN('$1') -> $type\n"
  792. if ($dbg_values > 1);
  793. } else {
  794. print "PAREN('$1')\n" if ($dbg_values > 1);
  795. }
  796. } elsif ($cur =~ /^($Ident)\s*\(/o) {
  797. print "FUNC($1)\n" if ($dbg_values > 1);
  798. $type = 'V';
  799. $av_pending = 'V';
  800. } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
  801. if (defined $2 && $type eq 'C' || $type eq 'T') {
  802. $av_pend_colon = 'B';
  803. } elsif ($type eq 'E') {
  804. $av_pend_colon = 'L';
  805. }
  806. print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
  807. $type = 'V';
  808. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  809. print "IDENT($1)\n" if ($dbg_values > 1);
  810. $type = 'V';
  811. } elsif ($cur =~ /^($Assignment)/o) {
  812. print "ASSIGN($1)\n" if ($dbg_values > 1);
  813. $type = 'N';
  814. } elsif ($cur =~/^(;|{|})/) {
  815. print "END($1)\n" if ($dbg_values > 1);
  816. $type = 'E';
  817. $av_pend_colon = 'O';
  818. } elsif ($cur =~/^(,)/) {
  819. print "COMMA($1)\n" if ($dbg_values > 1);
  820. $type = 'C';
  821. } elsif ($cur =~ /^(\?)/o) {
  822. print "QUESTION($1)\n" if ($dbg_values > 1);
  823. $type = 'N';
  824. } elsif ($cur =~ /^(:)/o) {
  825. print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
  826. substr($var, length($res), 1, $av_pend_colon);
  827. if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
  828. $type = 'E';
  829. } else {
  830. $type = 'N';
  831. }
  832. $av_pend_colon = 'O';
  833. } elsif ($cur =~ /^(\[)/o) {
  834. print "CLOSE($1)\n" if ($dbg_values > 1);
  835. $type = 'N';
  836. } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
  837. my $variant;
  838. print "OPV($1)\n" if ($dbg_values > 1);
  839. if ($type eq 'V') {
  840. $variant = 'B';
  841. } else {
  842. $variant = 'U';
  843. }
  844. substr($var, length($res), 1, $variant);
  845. $type = 'N';
  846. } elsif ($cur =~ /^($Operators)/o) {
  847. print "OP($1)\n" if ($dbg_values > 1);
  848. if ($1 ne '++' && $1 ne '--') {
  849. $type = 'N';
  850. }
  851. } elsif ($cur =~ /(^.)/o) {
  852. print "C($1)\n" if ($dbg_values > 1);
  853. }
  854. if (defined $1) {
  855. $cur = substr($cur, length($1));
  856. $res .= $type x length($1);
  857. }
  858. }
  859. return ($res, $var);
  860. }
  861. sub possible {
  862. my ($possible, $line) = @_;
  863. my $notPermitted = qr{(?:
  864. ^(?:
  865. $Modifier|
  866. $Storage|
  867. $Type|
  868. DEFINE_\S+
  869. )$|
  870. ^(?:
  871. goto|
  872. return|
  873. case|
  874. else|
  875. asm|__asm__|
  876. do
  877. )(?:\s|$)|
  878. ^(?:typedef|struct|enum)\b
  879. )}x;
  880. warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
  881. if ($possible !~ $notPermitted) {
  882. # Check for modifiers.
  883. $possible =~ s/\s*$Storage\s*//g;
  884. $possible =~ s/\s*$Sparse\s*//g;
  885. if ($possible =~ /^\s*$/) {
  886. } elsif ($possible =~ /\s/) {
  887. $possible =~ s/\s*$Type\s*//g;
  888. for my $modifier (split(' ', $possible)) {
  889. if ($modifier !~ $notPermitted) {
  890. warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
  891. push(@modifierList, $modifier);
  892. }
  893. }
  894. } else {
  895. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  896. push(@typeList, $possible);
  897. }
  898. build_types();
  899. } else {
  900. warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
  901. }
  902. }
  903. my $prefix = '';
  904. sub report {
  905. if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
  906. return 0;
  907. }
  908. my $line = $prefix . $_[0];
  909. $line = (split('\n', $line))[0] . "\n" if ($terse);
  910. push(our @report, $line);
  911. return 1;
  912. }
  913. sub report_dump {
  914. our @report;
  915. }
  916. sub ERROR {
  917. if (report("ERROR: $_[0]\n")) {
  918. our $clean = 0;
  919. our $cnt_error++;
  920. }
  921. }
  922. sub WARN {
  923. if (report("WARNING: $_[0]\n")) {
  924. our $clean = 0;
  925. our $cnt_warn++;
  926. }
  927. }
  928. sub CHK {
  929. if ($check && report("CHECK: $_[0]\n")) {
  930. our $clean = 0;
  931. our $cnt_chk++;
  932. }
  933. }
  934. sub check_absolute_file {
  935. my ($absolute, $herecurr) = @_;
  936. my $file = $absolute;
  937. ##print "absolute<$absolute>\n";
  938. # See if any suffix of this path is a path within the tree.
  939. while ($file =~ s@^[^/]*/@@) {
  940. if (-f "$root/$file") {
  941. ##print "file<$file>\n";
  942. last;
  943. }
  944. }
  945. if (! -f _) {
  946. return 0;
  947. }
  948. # It is, so see if the prefix is acceptable.
  949. my $prefix = $absolute;
  950. substr($prefix, -length($file)) = '';
  951. ##print "prefix<$prefix>\n";
  952. if ($prefix ne ".../") {
  953. WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
  954. }
  955. }
  956. sub process {
  957. my $filename = shift;
  958. my $linenr=0;
  959. my $prevline="";
  960. my $prevrawline="";
  961. my $stashline="";
  962. my $stashrawline="";
  963. my $length;
  964. my $indent;
  965. my $previndent=0;
  966. my $stashindent=0;
  967. our $clean = 1;
  968. my $signoff = 0;
  969. my $is_patch = 0;
  970. our @report = ();
  971. our $cnt_lines = 0;
  972. our $cnt_error = 0;
  973. our $cnt_warn = 0;
  974. our $cnt_chk = 0;
  975. # Trace the real file/line as we go.
  976. my $realfile = '';
  977. my $realline = 0;
  978. my $realcnt = 0;
  979. my $here = '';
  980. my $in_comment = 0;
  981. my $comment_edge = 0;
  982. my $first_line = 0;
  983. my $p1_prefix = '';
  984. my $prev_values = 'E';
  985. # suppression flags
  986. my %suppress_ifbraces;
  987. my %suppress_whiletrailers;
  988. my %suppress_export;
  989. # Pre-scan the patch sanitizing the lines.
  990. # Pre-scan the patch looking for any __setup documentation.
  991. #
  992. my @setup_docs = ();
  993. my $setup_docs = 0;
  994. sanitise_line_reset();
  995. my $line;
  996. foreach my $rawline (@rawlines) {
  997. $linenr++;
  998. $line = $rawline;
  999. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  1000. $setup_docs = 0;
  1001. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  1002. $setup_docs = 1;
  1003. }
  1004. #next;
  1005. }
  1006. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1007. $realline=$1-1;
  1008. if (defined $2) {
  1009. $realcnt=$3+1;
  1010. } else {
  1011. $realcnt=1+1;
  1012. }
  1013. $in_comment = 0;
  1014. # Guestimate if this is a continuing comment. Run
  1015. # the context looking for a comment "edge". If this
  1016. # edge is a close comment then we must be in a comment
  1017. # at context start.
  1018. my $edge;
  1019. my $cnt = $realcnt;
  1020. for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
  1021. next if (defined $rawlines[$ln - 1] &&
  1022. $rawlines[$ln - 1] =~ /^-/);
  1023. $cnt--;
  1024. #print "RAW<$rawlines[$ln - 1]>\n";
  1025. last if (!defined $rawlines[$ln - 1]);
  1026. if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
  1027. $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
  1028. ($edge) = $1;
  1029. last;
  1030. }
  1031. }
  1032. if (defined $edge && $edge eq '*/') {
  1033. $in_comment = 1;
  1034. }
  1035. # Guestimate if this is a continuing comment. If this
  1036. # is the start of a diff block and this line starts
  1037. # ' *' then it is very likely a comment.
  1038. if (!defined $edge &&
  1039. $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
  1040. {
  1041. $in_comment = 1;
  1042. }
  1043. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  1044. sanitise_line_reset($in_comment);
  1045. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  1046. # Standardise the strings and chars within the input to
  1047. # simplify matching -- only bother with positive lines.
  1048. $line = sanitise_line($rawline);
  1049. }
  1050. push(@lines, $line);
  1051. if ($realcnt > 1) {
  1052. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  1053. } else {
  1054. $realcnt = 0;
  1055. }
  1056. #print "==>$rawline\n";
  1057. #print "-->$line\n";
  1058. if ($setup_docs && $line =~ /^\+/) {
  1059. push(@setup_docs, $line);
  1060. }
  1061. }
  1062. $prefix = '';
  1063. $realcnt = 0;
  1064. $linenr = 0;
  1065. foreach my $line (@lines) {
  1066. $linenr++;
  1067. my $rawline = $rawlines[$linenr - 1];
  1068. #extract the line range in the file after the patch is applied
  1069. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1070. $is_patch = 1;
  1071. $first_line = $linenr + 1;
  1072. $realline=$1-1;
  1073. if (defined $2) {
  1074. $realcnt=$3+1;
  1075. } else {
  1076. $realcnt=1+1;
  1077. }
  1078. annotate_reset();
  1079. $prev_values = 'E';
  1080. %suppress_ifbraces = ();
  1081. %suppress_whiletrailers = ();
  1082. %suppress_export = ();
  1083. next;
  1084. # track the line number as we move through the hunk, note that
  1085. # new versions of GNU diff omit the leading space on completely
  1086. # blank context lines so we need to count that too.
  1087. } elsif ($line =~ /^( |\+|$)/) {
  1088. $realline++;
  1089. $realcnt-- if ($realcnt != 0);
  1090. # Measure the line length and indent.
  1091. ($length, $indent) = line_stats($rawline);
  1092. # Track the previous line.
  1093. ($prevline, $stashline) = ($stashline, $line);
  1094. ($previndent, $stashindent) = ($stashindent, $indent);
  1095. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  1096. #warn "line<$line>\n";
  1097. } elsif ($realcnt == 1) {
  1098. $realcnt--;
  1099. }
  1100. my $hunk_line = ($realcnt != 0);
  1101. #make up the handle for any error we report on this line
  1102. $prefix = "$filename:$realline: " if ($emacs && $file);
  1103. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  1104. $here = "#$linenr: " if (!$file);
  1105. $here = "#$realline: " if ($file);
  1106. # extract the filename as it passes
  1107. if ($line=~/^\+\+\+\s+(\S+)/) {
  1108. $realfile = $1;
  1109. $realfile =~ s@^([^/]*)/@@;
  1110. $p1_prefix = $1;
  1111. if (!$file && $tree && $p1_prefix ne '' &&
  1112. -e "$root/$p1_prefix") {
  1113. WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
  1114. }
  1115. if ($realfile =~ m@^include/asm/@) {
  1116. ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  1117. }
  1118. next;
  1119. }
  1120. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  1121. my $hereline = "$here\n$rawline\n";
  1122. my $herecurr = "$here\n$rawline\n";
  1123. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  1124. $cnt_lines++ if ($realcnt != 0);
  1125. #check the patch for a signoff:
  1126. if ($line =~ /^\s*signed-off-by:/i) {
  1127. # This is a signoff, if ugly, so do not double report.
  1128. $signoff++;
  1129. if (!($line =~ /^\s*Signed-off-by:/)) {
  1130. WARN("Signed-off-by: is the preferred form\n" .
  1131. $herecurr);
  1132. }
  1133. if ($line =~ /^\s*signed-off-by:\S/i) {
  1134. WARN("space required after Signed-off-by:\n" .
  1135. $herecurr);
  1136. }
  1137. }
  1138. # Check for wrappage within a valid hunk of the file
  1139. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  1140. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  1141. $herecurr) if (!$emitted_corrupt++);
  1142. }
  1143. # Check for absolute kernel paths.
  1144. if ($tree) {
  1145. while ($line =~ m{(?:^|\s)(/\S*)}g) {
  1146. my $file = $1;
  1147. if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
  1148. check_absolute_file($1, $herecurr)) {
  1149. #
  1150. } else {
  1151. check_absolute_file($file, $herecurr);
  1152. }
  1153. }
  1154. }
  1155. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  1156. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  1157. $rawline !~ m/^$UTF8*$/) {
  1158. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  1159. my $blank = copy_spacing($rawline);
  1160. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  1161. my $hereptr = "$hereline$ptr\n";
  1162. ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  1163. }
  1164. # ignore non-hunk lines and lines being removed
  1165. next if (!$hunk_line || $line =~ /^-/);
  1166. #trailing whitespace
  1167. if ($line =~ /^\+.*\015/) {
  1168. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1169. ERROR("DOS line endings\n" . $herevet);
  1170. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  1171. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1172. ERROR("trailing whitespace\n" . $herevet);
  1173. $rpt_cleaners = 1;
  1174. }
  1175. # check for Kconfig help text having a real description
  1176. if ($realfile =~ /Kconfig/ &&
  1177. $line =~ /\+?\s*(---)?help(---)?$/) {
  1178. my $length = 0;
  1179. for (my $l = $linenr; defined($lines[$l]); $l++) {
  1180. my $f = $lines[$l];
  1181. $f =~ s/#.*//;
  1182. $f =~ s/^\s+//;
  1183. next if ($f =~ /^$/);
  1184. last if ($f =~ /^\s*config\s/);
  1185. $length++;
  1186. }
  1187. WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
  1188. }
  1189. # check we are in a valid source file if not then ignore this hunk
  1190. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  1191. #80 column limit
  1192. if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
  1193. $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
  1194. !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
  1195. $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
  1196. $length > 80)
  1197. {
  1198. WARN("line over 80 characters\n" . $herecurr);
  1199. }
  1200. # check for spaces before a quoted newline
  1201. if ($rawline =~ /^.*\".*\s\\n/) {
  1202. WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
  1203. }
  1204. # check for adding lines without a newline.
  1205. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  1206. WARN("adding a line without newline at end of file\n" . $herecurr);
  1207. }
  1208. # Blackfin: use hi/lo macros
  1209. if ($realfile =~ m@arch/blackfin/.*\.S$@) {
  1210. if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
  1211. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1212. ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
  1213. }
  1214. if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
  1215. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1216. ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
  1217. }
  1218. }
  1219. # check we are in a valid source file C or perl if not then ignore this hunk
  1220. next if ($realfile !~ /\.(h|c|pl)$/);
  1221. # at the beginning of a line any tabs must come first and anything
  1222. # more than 8 must use tabs.
  1223. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  1224. $rawline =~ /^\+\s* \s*/) {
  1225. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1226. ERROR("code indent should use tabs where possible\n" . $herevet);
  1227. $rpt_cleaners = 1;
  1228. }
  1229. # check for space before tabs.
  1230. if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
  1231. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1232. WARN("please, no space before tabs\n" . $herevet);
  1233. }
  1234. # check for spaces at the beginning of a line.
  1235. # Exceptions:
  1236. # 1) within comments
  1237. # 2) indented preprocessor commands
  1238. # 3) hanging labels
  1239. if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
  1240. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1241. WARN("please, no spaces at the start of a line\n" . $herevet);
  1242. }
  1243. # check we are in a valid C source file if not then ignore this hunk
  1244. next if ($realfile !~ /\.(h|c)$/);
  1245. # check for RCS/CVS revision markers
  1246. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  1247. WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  1248. }
  1249. # Blackfin: don't use __builtin_bfin_[cs]sync
  1250. if ($line =~ /__builtin_bfin_csync/) {
  1251. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1252. ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
  1253. }
  1254. if ($line =~ /__builtin_bfin_ssync/) {
  1255. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1256. ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
  1257. }
  1258. # Check for potential 'bare' types
  1259. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  1260. $realline_next);
  1261. if ($realcnt && $line =~ /.\s*\S/) {
  1262. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  1263. ctx_statement_block($linenr, $realcnt, 0);
  1264. $stat =~ s/\n./\n /g;
  1265. $cond =~ s/\n./\n /g;
  1266. # Find the real next line.
  1267. $realline_next = $line_nr_next;
  1268. if (defined $realline_next &&
  1269. (!defined $lines[$realline_next - 1] ||
  1270. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  1271. $realline_next++;
  1272. }
  1273. my $s = $stat;
  1274. $s =~ s/{.*$//s;
  1275. # Ignore goto labels.
  1276. if ($s =~ /$Ident:\*$/s) {
  1277. # Ignore functions being called
  1278. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  1279. } elsif ($s =~ /^.\s*else\b/s) {
  1280. # declarations always start with types
  1281. } 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) {
  1282. my $type = $1;
  1283. $type =~ s/\s+/ /g;
  1284. possible($type, "A:" . $s);
  1285. # definitions in global scope can only start with types
  1286. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  1287. possible($1, "B:" . $s);
  1288. }
  1289. # any (foo ... *) is a pointer cast, and foo is a type
  1290. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  1291. possible($1, "C:" . $s);
  1292. }
  1293. # Check for any sort of function declaration.
  1294. # int foo(something bar, other baz);
  1295. # void (*store_gdt)(x86_descr_ptr *);
  1296. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1297. my ($name_len) = length($1);
  1298. my $ctx = $s;
  1299. substr($ctx, 0, $name_len + 1, '');
  1300. $ctx =~ s/\)[^\)]*$//;
  1301. for my $arg (split(/\s*,\s*/, $ctx)) {
  1302. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1303. possible($1, "D:" . $s);
  1304. }
  1305. }
  1306. }
  1307. }
  1308. #
  1309. # Checks which may be anchored in the context.
  1310. #
  1311. # Check for switch () and associated case and default
  1312. # statements should be at the same indent.
  1313. if ($line=~/\bswitch\s*\(.*\)/) {
  1314. my $err = '';
  1315. my $sep = '';
  1316. my @ctx = ctx_block_outer($linenr, $realcnt);
  1317. shift(@ctx);
  1318. for my $ctx (@ctx) {
  1319. my ($clen, $cindent) = line_stats($ctx);
  1320. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1321. $indent != $cindent) {
  1322. $err .= "$sep$ctx\n";
  1323. $sep = '';
  1324. } else {
  1325. $sep = "[...]\n";
  1326. }
  1327. }
  1328. if ($err ne '') {
  1329. ERROR("switch and case should be at the same indent\n$hereline$err");
  1330. }
  1331. }
  1332. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1333. # or if that brace on the next line is for something else
  1334. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1335. my $pre_ctx = "$1$2";
  1336. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1337. my $ctx_cnt = $realcnt - $#ctx - 1;
  1338. my $ctx = join("\n", @ctx);
  1339. my $ctx_ln = $linenr;
  1340. my $ctx_skip = $realcnt;
  1341. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  1342. defined $lines[$ctx_ln - 1] &&
  1343. $lines[$ctx_ln - 1] =~ /^-/)) {
  1344. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  1345. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  1346. $ctx_ln++;
  1347. }
  1348. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1349. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1350. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  1351. ERROR("that open brace { should be on the previous line\n" .
  1352. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1353. }
  1354. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1355. $ctx =~ /\)\s*\;\s*$/ &&
  1356. defined $lines[$ctx_ln - 1])
  1357. {
  1358. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1359. if ($nindent > $indent) {
  1360. WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
  1361. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1362. }
  1363. }
  1364. }
  1365. # Check relative indent for conditionals and blocks.
  1366. if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  1367. my ($s, $c) = ($stat, $cond);
  1368. substr($s, 0, length($c), '');
  1369. # Make sure we remove the line prefixes as we have
  1370. # none on the first line, and are going to readd them
  1371. # where necessary.
  1372. $s =~ s/\n./\n/gs;
  1373. # Find out how long the conditional actually is.
  1374. my @newlines = ($c =~ /\n/gs);
  1375. my $cond_lines = 1 + $#newlines;
  1376. # We want to check the first line inside the block
  1377. # starting at the end of the conditional, so remove:
  1378. # 1) any blank line termination
  1379. # 2) any opening brace { on end of the line
  1380. # 3) any do (...) {
  1381. my $continuation = 0;
  1382. my $check = 0;
  1383. $s =~ s/^.*\bdo\b//;
  1384. $s =~ s/^\s*{//;
  1385. if ($s =~ s/^\s*\\//) {
  1386. $continuation = 1;
  1387. }
  1388. if ($s =~ s/^\s*?\n//) {
  1389. $check = 1;
  1390. $cond_lines++;
  1391. }
  1392. # Also ignore a loop construct at the end of a
  1393. # preprocessor statement.
  1394. if (($prevline =~ /^.\s*#\s*define\s/ ||
  1395. $prevline =~ /\\\s*$/) && $continuation == 0) {
  1396. $check = 0;
  1397. }
  1398. my $cond_ptr = -1;
  1399. $continuation = 0;
  1400. while ($cond_ptr != $cond_lines) {
  1401. $cond_ptr = $cond_lines;
  1402. # If we see an #else/#elif then the code
  1403. # is not linear.
  1404. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  1405. $check = 0;
  1406. }
  1407. # Ignore:
  1408. # 1) blank lines, they should be at 0,
  1409. # 2) preprocessor lines, and
  1410. # 3) labels.
  1411. if ($continuation ||
  1412. $s =~ /^\s*?\n/ ||
  1413. $s =~ /^\s*#\s*?/ ||
  1414. $s =~ /^\s*$Ident\s*:/) {
  1415. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  1416. if ($s =~ s/^.*?\n//) {
  1417. $cond_lines++;
  1418. }
  1419. }
  1420. }
  1421. my (undef, $sindent) = line_stats("+" . $s);
  1422. my $stat_real = raw_line($linenr, $cond_lines);
  1423. # Check if either of these lines are modified, else
  1424. # this is not this patch's fault.
  1425. if (!defined($stat_real) ||
  1426. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  1427. $check = 0;
  1428. }
  1429. if (defined($stat_real) && $cond_lines > 1) {
  1430. $stat_real = "[...]\n$stat_real";
  1431. }
  1432. #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";
  1433. if ($check && (($sindent % 8) != 0 ||
  1434. ($sindent <= $indent && $s ne ''))) {
  1435. WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  1436. }
  1437. }
  1438. # Track the 'values' across context and added lines.
  1439. my $opline = $line; $opline =~ s/^./ /;
  1440. my ($curr_values, $curr_vars) =
  1441. annotate_values($opline . "\n", $prev_values);
  1442. $curr_values = $prev_values . $curr_values;
  1443. if ($dbg_values) {
  1444. my $outline = $opline; $outline =~ s/\t/ /g;
  1445. print "$linenr > .$outline\n";
  1446. print "$linenr > $curr_values\n";
  1447. print "$linenr > $curr_vars\n";
  1448. }
  1449. $prev_values = substr($curr_values, -1);
  1450. #ignore lines not being added
  1451. if ($line=~/^[^\+]/) {next;}
  1452. # TEST: allow direct testing of the type matcher.
  1453. if ($dbg_type) {
  1454. if ($line =~ /^.\s*$Declare\s*$/) {
  1455. ERROR("TEST: is type\n" . $herecurr);
  1456. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  1457. ERROR("TEST: is not type ($1 is)\n". $herecurr);
  1458. }
  1459. next;
  1460. }
  1461. # TEST: allow direct testing of the attribute matcher.
  1462. if ($dbg_attr) {
  1463. if ($line =~ /^.\s*$Modifier\s*$/) {
  1464. ERROR("TEST: is attr\n" . $herecurr);
  1465. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  1466. ERROR("TEST: is not attr ($1 is)\n". $herecurr);
  1467. }
  1468. next;
  1469. }
  1470. # check for initialisation to aggregates open brace on the next line
  1471. if ($line =~ /^.\s*{/ &&
  1472. $prevline =~ /(?:^|[^=])=\s*$/) {
  1473. ERROR("that open brace { should be on the previous line\n" . $hereprev);
  1474. }
  1475. #
  1476. # Checks which are anchored on the added line.
  1477. #
  1478. # check for malformed paths in #include statements (uses RAW line)
  1479. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  1480. my $path = $1;
  1481. if ($path =~ m{//}) {
  1482. ERROR("malformed #include filename\n" .
  1483. $herecurr);
  1484. }
  1485. }
  1486. # no C99 // comments
  1487. if ($line =~ m{//}) {
  1488. ERROR("do not use C99 // comments\n" . $herecurr);
  1489. }
  1490. # Remove C99 comments.
  1491. $line =~ s@//.*@@;
  1492. $opline =~ s@//.*@@;
  1493. # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
  1494. # the whole statement.
  1495. #print "APW <$lines[$realline_next - 1]>\n";
  1496. if (defined $realline_next &&
  1497. exists $lines[$realline_next - 1] &&
  1498. !defined $suppress_export{$realline_next} &&
  1499. ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  1500. $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1501. my $name = $1;
  1502. if ($stat !~ /(?:
  1503. \n.}\s*$|
  1504. ^.DEFINE_$Ident\(\Q$name\E\)|
  1505. ^.DECLARE_$Ident\(\Q$name\E\)|
  1506. ^.LIST_HEAD\(\Q$name\E\)|
  1507. ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
  1508. \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
  1509. )/x) {
  1510. #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
  1511. $suppress_export{$realline_next} = 2;
  1512. } else {
  1513. $suppress_export{$realline_next} = 1;
  1514. }
  1515. }
  1516. if (!defined $suppress_export{$linenr} &&
  1517. $prevline =~ /^.\s*$/ &&
  1518. ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  1519. $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1520. #print "FOO B <$lines[$linenr - 1]>\n";
  1521. $suppress_export{$linenr} = 2;
  1522. }
  1523. if (defined $suppress_export{$linenr} &&
  1524. $suppress_export{$linenr} == 2) {
  1525. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  1526. }
  1527. # check for global initialisers.
  1528. if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
  1529. ERROR("do not initialise globals to 0 or NULL\n" .
  1530. $herecurr);
  1531. }
  1532. # check for static initialisers.
  1533. if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
  1534. ERROR("do not initialise statics to 0 or NULL\n" .
  1535. $herecurr);
  1536. }
  1537. # check for new typedefs, only function parameters and sparse annotations
  1538. # make sense.
  1539. if ($line =~ /\btypedef\s/ &&
  1540. $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
  1541. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  1542. $line !~ /\b$typeTypedefs\b/ &&
  1543. $line !~ /\b__bitwise(?:__|)\b/) {
  1544. WARN("do not add new typedefs\n" . $herecurr);
  1545. }
  1546. # * goes on variable not on type
  1547. # (char*[ const])
  1548. if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
  1549. my ($from, $to) = ($1, $1);
  1550. # Should start with a space.
  1551. $to =~ s/^(\S)/ $1/;
  1552. # Should not end with a space.
  1553. $to =~ s/\s+$//;
  1554. # '*'s should not have spaces between.
  1555. while ($to =~ s/\*\s+\*/\*\*/) {
  1556. }
  1557. #print "from<$from> to<$to>\n";
  1558. if ($from ne $to) {
  1559. ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
  1560. }
  1561. } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
  1562. my ($from, $to, $ident) = ($1, $1, $2);
  1563. # Should start with a space.
  1564. $to =~ s/^(\S)/ $1/;
  1565. # Should not end with a space.
  1566. $to =~ s/\s+$//;
  1567. # '*'s should not have spaces between.
  1568. while ($to =~ s/\*\s+\*/\*\*/) {
  1569. }
  1570. # Modifiers should have spaces.
  1571. $to =~ s/(\b$Modifier$)/$1 /;
  1572. #print "from<$from> to<$to> ident<$ident>\n";
  1573. if ($from ne $to && $ident !~ /^$Modifier$/) {
  1574. ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
  1575. }
  1576. }
  1577. # # no BUG() or BUG_ON()
  1578. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1579. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1580. # print "$herecurr";
  1581. # $clean = 0;
  1582. # }
  1583. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1584. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1585. }
  1586. # printk should use KERN_* levels. Note that follow on printk's on the
  1587. # same line do not need a level, so we use the current block context
  1588. # to try and find and validate the current printk. In summary the current
  1589. # printk includes all preceeding printk's which have no newline on the end.
  1590. # we assume the first bad printk is the one to report.
  1591. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1592. my $ok = 0;
  1593. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1594. #print "CHECK<$lines[$ln - 1]\n";
  1595. # we have a preceeding printk if it ends
  1596. # with "\n" ignore it, else it is to blame
  1597. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1598. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1599. $ok = 1;
  1600. }
  1601. last;
  1602. }
  1603. }
  1604. if ($ok == 0) {
  1605. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  1606. }
  1607. }
  1608. # function brace can't be on same line, except for #defines of do while,
  1609. # or if closed on same line
  1610. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  1611. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  1612. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1613. }
  1614. # open braces for enum, union and struct go on the same line.
  1615. if ($line =~ /^.\s*{/ &&
  1616. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1617. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1618. }
  1619. # missing space after union, struct or enum definition
  1620. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
  1621. WARN("missing space after $1 definition\n" . $herecurr);
  1622. }
  1623. # check for spacing round square brackets; allowed:
  1624. # 1. with a type on the left -- int [] a;
  1625. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  1626. # 3. inside a curly brace -- = { [0...10] = 5 }
  1627. while ($line =~ /(.*?\s)\[/g) {
  1628. my ($where, $prefix) = ($-[1], $1);
  1629. if ($prefix !~ /$Type\s+$/ &&
  1630. ($where != 0 || $prefix !~ /^.\s+$/) &&
  1631. $prefix !~ /{\s+$/) {
  1632. ERROR("space prohibited before open square bracket '['\n" . $herecurr);
  1633. }
  1634. }
  1635. # check for spaces between functions and their parentheses.
  1636. while ($line =~ /($Ident)\s+\(/g) {
  1637. my $name = $1;
  1638. my $ctx_before = substr($line, 0, $-[1]);
  1639. my $ctx = "$ctx_before$name";
  1640. # Ignore those directives where spaces _are_ permitted.
  1641. if ($name =~ /^(?:
  1642. if|for|while|switch|return|case|
  1643. volatile|__volatile__|
  1644. __attribute__|format|__extension__|
  1645. asm|__asm__)$/x)
  1646. {
  1647. # cpp #define statements have non-optional spaces, ie
  1648. # if there is a space between the name and the open
  1649. # parenthesis it is simply not a parameter group.
  1650. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1651. # cpp #elif statement condition may start with a (
  1652. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1653. # If this whole things ends with a type its most
  1654. # likely a typedef for a function.
  1655. } elsif ($ctx =~ /$Type$/) {
  1656. } else {
  1657. WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1658. }
  1659. }
  1660. # Check operator spacing.
  1661. if (!($line=~/\#\s*include/)) {
  1662. my $ops = qr{
  1663. <<=|>>=|<=|>=|==|!=|
  1664. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1665. =>|->|<<|>>|<|>|=|!|~|
  1666. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  1667. \?|:
  1668. }x;
  1669. my @elements = split(/($ops|;)/, $opline);
  1670. my $off = 0;
  1671. my $blank = copy_spacing($opline);
  1672. for (my $n = 0; $n < $#elements; $n += 2) {
  1673. $off += length($elements[$n]);
  1674. # Pick up the preceeding and succeeding characters.
  1675. my $ca = substr($opline, 0, $off);
  1676. my $cc = '';
  1677. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1678. $cc = substr($opline, $off + length($elements[$n + 1]));
  1679. }
  1680. my $cb = "$ca$;$cc";
  1681. my $a = '';
  1682. $a = 'V' if ($elements[$n] ne '');
  1683. $a = 'W' if ($elements[$n] =~ /\s$/);
  1684. $a = 'C' if ($elements[$n] =~ /$;$/);
  1685. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1686. $a = 'O' if ($elements[$n] eq '');
  1687. $a = 'E' if ($ca =~ /^\s*$/);
  1688. my $op = $elements[$n + 1];
  1689. my $c = '';
  1690. if (defined $elements[$n + 2]) {
  1691. $c = 'V' if ($elements[$n + 2] ne '');
  1692. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1693. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1694. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1695. $c = 'O' if ($elements[$n + 2] eq '');
  1696. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  1697. } else {
  1698. $c = 'E';
  1699. }
  1700. my $ctx = "${a}x${c}";
  1701. my $at = "(ctx:$ctx)";
  1702. my $ptr = substr($blank, 0, $off) . "^";
  1703. my $hereptr = "$hereline$ptr\n";
  1704. # Pull out the value of this operator.
  1705. my $op_type = substr($curr_values, $off + 1, 1);
  1706. # Get the full operator variant.
  1707. my $opv = $op . substr($curr_vars, $off, 1);
  1708. # Ignore operators passed as parameters.
  1709. if ($op_type ne 'V' &&
  1710. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1711. # # Ignore comments
  1712. # } elsif ($op =~ /^$;+$/) {
  1713. # ; should have either the end of line or a space or \ after it
  1714. } elsif ($op eq ';') {
  1715. if ($ctx !~ /.x[WEBC]/ &&
  1716. $cc !~ /^\\/ && $cc !~ /^;/) {
  1717. ERROR("space required after that '$op' $at\n" . $hereptr);
  1718. }
  1719. # // is a comment
  1720. } elsif ($op eq '//') {
  1721. # No spaces for:
  1722. # ->
  1723. # : when part of a bitfield
  1724. } elsif ($op eq '->' || $opv eq ':B') {
  1725. if ($ctx =~ /Wx.|.xW/) {
  1726. ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
  1727. }
  1728. # , must have a space on the right.
  1729. } elsif ($op eq ',') {
  1730. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  1731. ERROR("space required after that '$op' $at\n" . $hereptr);
  1732. }
  1733. # '*' as part of a type definition -- reported already.
  1734. } elsif ($opv eq '*_') {
  1735. #warn "'*' is part of type\n";
  1736. # unary operators should have a space before and
  1737. # none after. May be left adjacent to another
  1738. # unary operator, or a cast
  1739. } elsif ($op eq '!' || $op eq '~' ||
  1740. $opv eq '*U' || $opv eq '-U' ||
  1741. $opv eq '&U' || $opv eq '&&U') {
  1742. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1743. ERROR("space required before that '$op' $at\n" . $hereptr);
  1744. }
  1745. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  1746. # A unary '*' may be const
  1747. } elsif ($ctx =~ /.xW/) {
  1748. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1749. }
  1750. # unary ++ and unary -- are allowed no space on one side.
  1751. } elsif ($op eq '++' or $op eq '--') {
  1752. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  1753. ERROR("space required one side of that '$op' $at\n" . $hereptr);
  1754. }
  1755. if ($ctx =~ /Wx[BE]/ ||
  1756. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  1757. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1758. }
  1759. if ($ctx =~ /ExW/) {
  1760. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1761. }
  1762. # << and >> may either have or not have spaces both sides
  1763. } elsif ($op eq '<<' or $op eq '>>' or
  1764. $op eq '&' or $op eq '^' or $op eq '|' or
  1765. $op eq '+' or $op eq '-' or
  1766. $op eq '*' or $op eq '/' or
  1767. $op eq '%')
  1768. {
  1769. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  1770. ERROR("need consistent spacing around '$op' $at\n" .
  1771. $hereptr);
  1772. }
  1773. # A colon needs no spaces before when it is
  1774. # terminating a case value or a label.
  1775. } elsif ($opv eq ':C' || $opv eq ':L') {
  1776. if ($ctx =~ /Wx./) {
  1777. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1778. }
  1779. # All the others need spaces both sides.
  1780. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  1781. my $ok = 0;
  1782. # Ignore email addresses <foo@bar>
  1783. if (($op eq '<' &&
  1784. $cc =~ /^\S+\@\S+>/) ||
  1785. ($op eq '>' &&
  1786. $ca =~ /<\S+\@\S+$/))
  1787. {
  1788. $ok = 1;
  1789. }
  1790. # Ignore ?:
  1791. if (($opv eq ':O' && $ca =~ /\?$/) ||
  1792. ($op eq '?' && $cc =~ /^:/)) {
  1793. $ok = 1;
  1794. }
  1795. if ($ok == 0) {
  1796. ERROR("spaces required around that '$op' $at\n" . $hereptr);
  1797. }
  1798. }
  1799. $off += length($elements[$n + 1]);
  1800. }
  1801. }
  1802. # check for multiple assignments
  1803. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1804. CHK("multiple assignments should be avoided\n" . $herecurr);
  1805. }
  1806. ## # check for multiple declarations, allowing for a function declaration
  1807. ## # continuation.
  1808. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1809. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1810. ##
  1811. ## # Remove any bracketed sections to ensure we do not
  1812. ## # falsly report the parameters of functions.
  1813. ## my $ln = $line;
  1814. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1815. ## }
  1816. ## if ($ln =~ /,/) {
  1817. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1818. ## }
  1819. ## }
  1820. #need space before brace following if, while, etc
  1821. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1822. $line =~ /do{/) {
  1823. ERROR("space required before the open brace '{'\n" . $herecurr);
  1824. }
  1825. # closing brace should have a space following it when it has anything
  1826. # on the line
  1827. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1828. ERROR("space required after that close brace '}'\n" . $herecurr);
  1829. }
  1830. # check spacing on square brackets
  1831. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1832. ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
  1833. }
  1834. if ($line =~ /\s\]/) {
  1835. ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
  1836. }
  1837. # check spacing on parentheses
  1838. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1839. $line !~ /for\s*\(\s+;/) {
  1840. ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
  1841. }
  1842. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  1843. $line !~ /for\s*\(.*;\s+\)/ &&
  1844. $line !~ /:\s+\)/) {
  1845. ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
  1846. }
  1847. #goto labels aren't indented, allow a single space however
  1848. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1849. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1850. WARN("labels should not be indented\n" . $herecurr);
  1851. }
  1852. # Return is not a function.
  1853. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  1854. my $spacing = $1;
  1855. my $value = $2;
  1856. # Flatten any parentheses
  1857. $value =~ s/\(/ \(/g;
  1858. $value =~ s/\)/\) /g;
  1859. while ($value =~ s/\[[^\{\}]*\]/1/ ||
  1860. $value !~ /(?:$Ident|-?$Constant)\s*
  1861. $Compare\s*
  1862. (?:$Ident|-?$Constant)/x &&
  1863. $value =~ s/\([^\(\)]*\)/1/) {
  1864. }
  1865. #print "value<$value>\n";
  1866. if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
  1867. ERROR("return is not a function, parentheses are not required\n" . $herecurr);
  1868. } elsif ($spacing !~ /\s+/) {
  1869. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1870. }
  1871. }
  1872. # Return of what appears to be an errno should normally be -'ve
  1873. if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
  1874. my $name = $1;
  1875. if ($name ne 'EOF' && $name ne 'ERROR') {
  1876. WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
  1877. }
  1878. }
  1879. # Need a space before open parenthesis after if, while etc
  1880. if ($line=~/\b(if|while|for|switch)\(/) {
  1881. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1882. }
  1883. # Check for illegal assignment in if conditional -- and check for trailing
  1884. # statements after the conditional.
  1885. if ($line =~ /do\s*(?!{)/) {
  1886. my ($stat_next) = ctx_statement_block($line_nr_next,
  1887. $remain_next, $off_next);
  1888. $stat_next =~ s/\n./\n /g;
  1889. ##print "stat<$stat> stat_next<$stat_next>\n";
  1890. if ($stat_next =~ /^\s*while\b/) {
  1891. # If the statement carries leading newlines,
  1892. # then count those as offsets.
  1893. my ($whitespace) =
  1894. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  1895. my $offset =
  1896. statement_rawlines($whitespace) - 1;
  1897. $suppress_whiletrailers{$line_nr_next +
  1898. $offset} = 1;
  1899. }
  1900. }
  1901. if (!defined $suppress_whiletrailers{$linenr} &&
  1902. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  1903. my ($s, $c) = ($stat, $cond);
  1904. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  1905. ERROR("do not use assignment in if condition\n" . $herecurr);
  1906. }
  1907. # Find out what is on the end of the line after the
  1908. # conditional.
  1909. substr($s, 0, length($c), '');
  1910. $s =~ s/\n.*//g;
  1911. $s =~ s/$;//g; # Remove any comments
  1912. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  1913. $c !~ /}\s*while\s*/)
  1914. {
  1915. # Find out how long the conditional actually is.
  1916. my @newlines = ($c =~ /\n/gs);
  1917. my $cond_lines = 1 + $#newlines;
  1918. my $stat_real = '';
  1919. $stat_real = raw_line($linenr, $cond_lines)
  1920. . "\n" if ($cond_lines);
  1921. if (defined($stat_real) && $cond_lines > 1) {
  1922. $stat_real = "[...]\n$stat_real";
  1923. }
  1924. ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
  1925. }
  1926. }
  1927. # Check for bitwise tests written as boolean
  1928. if ($line =~ /
  1929. (?:
  1930. (?:\[|\(|\&\&|\|\|)
  1931. \s*0[xX][0-9]+\s*
  1932. (?:\&\&|\|\|)
  1933. |
  1934. (?:\&\&|\|\|)
  1935. \s*0[xX][0-9]+\s*
  1936. (?:\&\&|\|\||\)|\])
  1937. )/x)
  1938. {
  1939. WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  1940. }
  1941. # if and else should not have general statements after it
  1942. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  1943. my $s = $1;
  1944. $s =~ s/$;//g; # Remove any comments
  1945. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  1946. ERROR("trailing statements should be on next line\n" . $herecurr);
  1947. }
  1948. }
  1949. # if should not continue a brace
  1950. if ($line =~ /}\s*if\b/) {
  1951. ERROR("trailing statements should be on next line\n" .
  1952. $herecurr);
  1953. }
  1954. # case and default should not have general statements after them
  1955. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  1956. $line !~ /\G(?:
  1957. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  1958. \s*return\s+
  1959. )/xg)
  1960. {
  1961. ERROR("trailing statements should be on next line\n" . $herecurr);
  1962. }
  1963. # Check for }<nl>else {, these must be at the same
  1964. # indent level to be relevant to each other.
  1965. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1966. $previndent == $indent) {
  1967. ERROR("else should follow close brace '}'\n" . $hereprev);
  1968. }
  1969. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  1970. $previndent == $indent) {
  1971. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1972. # Find out what is on the end of the line after the
  1973. # conditional.
  1974. substr($s, 0, length($c), '');
  1975. $s =~ s/\n.*//g;
  1976. if ($s =~ /^\s*;/) {
  1977. ERROR("while should follow close brace '}'\n" . $hereprev);
  1978. }
  1979. }
  1980. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1981. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1982. # print "No studly caps, use _\n";
  1983. # print "$herecurr";
  1984. # $clean = 0;
  1985. # }
  1986. #no spaces allowed after \ in define
  1987. if ($line=~/\#\s*define.*\\\s$/) {
  1988. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1989. }
  1990. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1991. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1992. my $file = "$1.h";
  1993. my $checkfile = "include/linux/$file";
  1994. if (-f "$root/$checkfile" &&
  1995. $realfile ne $checkfile &&
  1996. $1 !~ /$allowed_asm_includes/)
  1997. {
  1998. if ($realfile =~ m{^arch/}) {
  1999. CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2000. } else {
  2001. WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2002. }
  2003. }
  2004. }
  2005. # multi-statement macros should be enclosed in a do while loop, grab the
  2006. # first statement and ensure its the whole macro if its not enclosed
  2007. # in a known good container
  2008. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  2009. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  2010. my $ln = $linenr;
  2011. my $cnt = $realcnt;
  2012. my ($off, $dstat, $dcond, $rest);
  2013. my $ctx = '';
  2014. my $args = defined($1);
  2015. # Find the end of the macro and limit our statement
  2016. # search to that.
  2017. while ($cnt > 0 && defined $lines[$ln - 1] &&
  2018. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  2019. {
  2020. $ctx .= $rawlines[$ln - 1] . "\n";
  2021. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  2022. $ln++;
  2023. }
  2024. $ctx .= $rawlines[$ln - 1];
  2025. ($dstat, $dcond, $ln, $cnt, $off) =
  2026. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  2027. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  2028. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  2029. # Extract the remainder of the define (if any) and
  2030. # rip off surrounding spaces, and trailing \'s.
  2031. $rest = '';
  2032. while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
  2033. #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
  2034. if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
  2035. $rest .= substr($lines[$ln - 1], $off) . "\n";
  2036. $cnt--;
  2037. }
  2038. $ln++;
  2039. $off = 0;
  2040. }
  2041. $rest =~ s/\\\n.//g;
  2042. $rest =~ s/^\s*//s;
  2043. $rest =~ s/\s*$//s;
  2044. # Clean up the original statement.
  2045. if ($args) {
  2046. substr($dstat, 0, length($dcond), '');
  2047. } else {
  2048. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  2049. }
  2050. $dstat =~ s/$;//g;
  2051. $dstat =~ s/\\\n.//g;
  2052. $dstat =~ s/^\s*//s;
  2053. $dstat =~ s/\s*$//s;
  2054. # Flatten any parentheses and braces
  2055. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  2056. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  2057. $dstat =~ s/\[[^\{\}]*\]/1/)
  2058. {
  2059. }
  2060. my $exceptions = qr{
  2061. $Declare|
  2062. module_param_named|
  2063. MODULE_PARAM_DESC|
  2064. DECLARE_PER_CPU|
  2065. DEFINE_PER_CPU|
  2066. __typeof__\(|
  2067. union|
  2068. struct|
  2069. \.$Ident\s*=\s*|
  2070. ^\"|\"$
  2071. }x;
  2072. #print "REST<$rest> dstat<$dstat>\n";
  2073. if ($rest ne '') {
  2074. if ($rest !~ /while\s*\(/ &&
  2075. $dstat !~ /$exceptions/)
  2076. {
  2077. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  2078. }
  2079. } elsif ($ctx !~ /;/) {
  2080. if ($dstat ne '' &&
  2081. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  2082. $dstat !~ /$exceptions/ &&
  2083. $dstat !~ /^\.$Ident\s*=/ &&
  2084. $dstat =~ /$Operators/)
  2085. {
  2086. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  2087. }
  2088. }
  2089. }
  2090. # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
  2091. # all assignments may have only one of the following with an assignment:
  2092. # .
  2093. # ALIGN(...)
  2094. # VMLINUX_SYMBOL(...)
  2095. if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
  2096. WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
  2097. }
  2098. # check for redundant bracing round if etc
  2099. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  2100. my ($level, $endln, @chunks) =
  2101. ctx_statement_full($linenr, $realcnt, 1);
  2102. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  2103. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  2104. if ($#chunks > 0 && $level == 0) {
  2105. my $allowed = 0;
  2106. my $seen = 0;
  2107. my $herectx = $here . "\n";
  2108. my $ln = $linenr - 1;
  2109. for my $chunk (@chunks) {
  2110. my ($cond, $block) = @{$chunk};
  2111. # If the condition carries leading newlines, then count those as offsets.
  2112. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  2113. my $offset = statement_rawlines($whitespace) - 1;
  2114. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  2115. # We have looked at and allowed this specific line.
  2116. $suppress_ifbraces{$ln + $offset} = 1;
  2117. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  2118. $ln += statement_rawlines($block) - 1;
  2119. substr($block, 0, length($cond), '');
  2120. $seen++ if ($block =~ /^\s*{/);
  2121. #print "cond<$cond> block<$block> allowed<$allowed>\n";
  2122. if (statement_lines($cond) > 1) {
  2123. #print "APW: ALLOWED: cond<$cond>\n";
  2124. $allowed = 1;
  2125. }
  2126. if ($block =~/\b(?:if|for|while)\b/) {
  2127. #print "APW: ALLOWED: block<$block>\n";
  2128. $allowed = 1;
  2129. }
  2130. if (statement_block_size($block) > 1) {
  2131. #print "APW: ALLOWED: lines block<$block>\n";
  2132. $allowed = 1;
  2133. }
  2134. }
  2135. if ($seen && !$allowed) {
  2136. WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
  2137. }
  2138. }
  2139. }
  2140. if (!defined $suppress_ifbraces{$linenr - 1} &&
  2141. $line =~ /\b(if|while|for|else)\b/) {
  2142. my $allowed = 0;
  2143. # Check the pre-context.
  2144. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  2145. #print "APW: ALLOWED: pre<$1>\n";
  2146. $allowed = 1;
  2147. }
  2148. my ($level, $endln, @chunks) =
  2149. ctx_statement_full($linenr, $realcnt, $-[0]);
  2150. # Check the condition.
  2151. my ($cond, $block) = @{$chunks[0]};
  2152. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  2153. if (defined $cond) {
  2154. substr($block, 0, length($cond), '');
  2155. }
  2156. if (statement_lines($cond) > 1) {
  2157. #print "APW: ALLOWED: cond<$cond>\n";
  2158. $allowed = 1;
  2159. }
  2160. if ($block =~/\b(?:if|for|while)\b/) {
  2161. #print "APW: ALLOWED: block<$block>\n";
  2162. $allowed = 1;
  2163. }
  2164. if (statement_block_size($block) > 1) {
  2165. #print "APW: ALLOWED: lines block<$block>\n";
  2166. $allowed = 1;
  2167. }
  2168. # Check the post-context.
  2169. if (defined $chunks[1]) {
  2170. my ($cond, $block) = @{$chunks[1]};
  2171. if (defined $cond) {
  2172. substr($block, 0, length($cond), '');
  2173. }
  2174. if ($block =~ /^\s*\{/) {
  2175. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  2176. $allowed = 1;
  2177. }
  2178. }
  2179. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  2180. my $herectx = $here . "\n";;
  2181. my $cnt = statement_rawlines($block);
  2182. for (my $n = 0; $n < $cnt; $n++) {
  2183. $herectx .= raw_line($linenr, $n) . "\n";;
  2184. }
  2185. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  2186. }
  2187. }
  2188. # don't include deprecated include files (uses RAW line)
  2189. for my $inc (@dep_includes) {
  2190. if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
  2191. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  2192. }
  2193. }
  2194. # don't use deprecated functions
  2195. for my $func (@dep_functions) {
  2196. if ($line =~ /\b$func\b/) {
  2197. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  2198. }
  2199. }
  2200. # no volatiles please
  2201. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  2202. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  2203. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  2204. }
  2205. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  2206. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  2207. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  2208. }
  2209. # warn about #if 0
  2210. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  2211. CHK("if this code is redundant consider removing it\n" .
  2212. $herecurr);
  2213. }
  2214. # check for needless kfree() checks
  2215. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  2216. my $expr = $1;
  2217. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  2218. WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
  2219. }
  2220. }
  2221. # check for needless usb_free_urb() checks
  2222. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  2223. my $expr = $1;
  2224. if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
  2225. WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
  2226. }
  2227. }
  2228. # prefer usleep_range over udelay
  2229. if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
  2230. # ignore udelay's < 10, however
  2231. if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
  2232. CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
  2233. }
  2234. }
  2235. # warn about unexpectedly long msleep's
  2236. if ($line =~ /\bmsleep\s*\((\d+)\);/) {
  2237. if ($1 < 20) {
  2238. WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
  2239. }
  2240. }
  2241. # warn about #ifdefs in C files
  2242. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  2243. # print "#ifdef in C files should be avoided\n";
  2244. # print "$herecurr";
  2245. # $clean = 0;
  2246. # }
  2247. # warn about spacing in #ifdefs
  2248. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  2249. ERROR("exactly one space required after that #$1\n" . $herecurr);
  2250. }
  2251. # check for spinlock_t definitions without a comment.
  2252. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  2253. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  2254. my $which = $1;
  2255. if (!ctx_has_comment($first_line, $linenr)) {
  2256. CHK("$1 definition without comment\n" . $herecurr);
  2257. }
  2258. }
  2259. # check for memory barriers without a comment.
  2260. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  2261. if (!ctx_has_comment($first_line, $linenr)) {
  2262. CHK("memory barrier without comment\n" . $herecurr);
  2263. }
  2264. }
  2265. # check of hardware specific defines
  2266. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  2267. CHK("architecture specific defines should be avoided\n" . $herecurr);
  2268. }
  2269. # Check that the storage class is at the beginning of a declaration
  2270. if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
  2271. WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
  2272. }
  2273. # check the location of the inline attribute, that it is between
  2274. # storage class and type.
  2275. if ($line =~ /\b$Type\s+$Inline\b/ ||
  2276. $line =~ /\b$Inline\s+$Storage\b/) {
  2277. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  2278. }
  2279. # Check for __inline__ and __inline, prefer inline
  2280. if ($line =~ /\b(__inline__|__inline)\b/) {
  2281. WARN("plain inline is preferred over $1\n" . $herecurr);
  2282. }
  2283. # check for sizeof(&)
  2284. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  2285. WARN("sizeof(& should be avoided\n" . $herecurr);
  2286. }
  2287. # check for new externs in .c files.
  2288. if ($realfile =~ /\.c$/ && defined $stat &&
  2289. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  2290. {
  2291. my $function_name = $1;
  2292. my $paren_space = $2;
  2293. my $s = $stat;
  2294. if (defined $cond) {
  2295. substr($s, 0, length($cond), '');
  2296. }
  2297. if ($s =~ /^\s*;/ &&
  2298. $function_name ne 'uninitialized_var')
  2299. {
  2300. WARN("externs should be avoided in .c files\n" . $herecurr);
  2301. }
  2302. if ($paren_space =~ /\n/) {
  2303. WARN("arguments for function declarations should follow identifier\n" . $herecurr);
  2304. }
  2305. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  2306. $stat =~ /^.\s*extern\s+/)
  2307. {
  2308. WARN("externs should be avoided in .c files\n" . $herecurr);
  2309. }
  2310. # checks for new __setup's
  2311. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  2312. my $name = $1;
  2313. if (!grep(/$name/, @setup_docs)) {
  2314. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  2315. }
  2316. }
  2317. # check for pointless casting of kmalloc return
  2318. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  2319. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  2320. }
  2321. # check for gcc specific __FUNCTION__
  2322. if ($line =~ /__FUNCTION__/) {
  2323. WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  2324. }
  2325. # check for semaphores used as mutexes
  2326. if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
  2327. WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
  2328. }
  2329. # check for semaphores used as mutexes
  2330. if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
  2331. WARN("consider using a completion\n" . $herecurr);
  2332. }
  2333. # recommend strict_strto* over simple_strto*
  2334. if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
  2335. WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
  2336. }
  2337. # check for __initcall(), use device_initcall() explicitly please
  2338. if ($line =~ /^.\s*__initcall\s*\(/) {
  2339. WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
  2340. }
  2341. # check for various ops structs, ensure they are const.
  2342. my $struct_ops = qr{acpi_dock_ops|
  2343. address_space_operations|
  2344. backlight_ops|
  2345. block_device_operations|
  2346. dentry_operations|
  2347. dev_pm_ops|
  2348. dma_map_ops|
  2349. extent_io_ops|
  2350. file_lock_operations|
  2351. file_operations|
  2352. hv_ops|
  2353. ide_dma_ops|
  2354. intel_dvo_dev_ops|
  2355. item_operations|
  2356. iwl_ops|
  2357. kgdb_arch|
  2358. kgdb_io|
  2359. kset_uevent_ops|
  2360. lock_manager_operations|
  2361. microcode_ops|
  2362. mtrr_ops|
  2363. neigh_ops|
  2364. nlmsvc_binding|
  2365. pci_raw_ops|
  2366. pipe_buf_operations|
  2367. platform_hibernation_ops|
  2368. platform_suspend_ops|
  2369. proto_ops|
  2370. rpc_pipe_ops|
  2371. seq_operations|
  2372. snd_ac97_build_ops|
  2373. soc_pcmcia_socket_ops|
  2374. stacktrace_ops|
  2375. sysfs_ops|
  2376. tty_operations|
  2377. usb_mon_operations|
  2378. wd_ops}x;
  2379. if ($line !~ /\bconst\b/ &&
  2380. $line =~ /\bstruct\s+($struct_ops)\b/) {
  2381. WARN("struct $1 should normally be const\n" .
  2382. $herecurr);
  2383. }
  2384. # use of NR_CPUS is usually wrong
  2385. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  2386. if ($line =~ /\bNR_CPUS\b/ &&
  2387. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  2388. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  2389. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  2390. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  2391. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  2392. {
  2393. WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  2394. }
  2395. # check for %L{u,d,i} in strings
  2396. my $string;
  2397. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  2398. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  2399. $string =~ s/%%/__/g;
  2400. if ($string =~ /(?<!%)%L[udi]/) {
  2401. WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  2402. last;
  2403. }
  2404. }
  2405. # whine mightly about in_atomic
  2406. if ($line =~ /\bin_atomic\s*\(/) {
  2407. if ($realfile =~ m@^drivers/@) {
  2408. ERROR("do not use in_atomic in drivers\n" . $herecurr);
  2409. } elsif ($realfile !~ m@^kernel/@) {
  2410. WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  2411. }
  2412. }
  2413. # check for lockdep_set_novalidate_class
  2414. if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
  2415. $line =~ /__lockdep_no_validate__\s*\)/ ) {
  2416. if ($realfile !~ m@^kernel/lockdep@ &&
  2417. $realfile !~ m@^include/linux/lockdep@ &&
  2418. $realfile !~ m@^drivers/base/core@) {
  2419. ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
  2420. }
  2421. }
  2422. }
  2423. # If we have no input at all, then there is nothing to report on
  2424. # so just keep quiet.
  2425. if ($#rawlines == -1) {
  2426. exit(0);
  2427. }
  2428. # In mailback mode only produce a report in the negative, for
  2429. # things that appear to be patches.
  2430. if ($mailback && ($clean == 1 || !$is_patch)) {
  2431. exit(0);
  2432. }
  2433. # This is not a patch, and we are are in 'no-patch' mode so
  2434. # just keep quiet.
  2435. if (!$chk_patch && !$is_patch) {
  2436. exit(0);
  2437. }
  2438. if (!$is_patch) {
  2439. ERROR("Does not appear to be a unified-diff format patch\n");
  2440. }
  2441. if ($is_patch && $chk_signoff && $signoff == 0) {
  2442. ERROR("Missing Signed-off-by: line(s)\n");
  2443. }
  2444. print report_dump();
  2445. if ($summary && !($clean == 1 && $quiet == 1)) {
  2446. print "$filename " if ($summary_file);
  2447. print "total: $cnt_error errors, $cnt_warn warnings, " .
  2448. (($check)? "$cnt_chk checks, " : "") .
  2449. "$cnt_lines lines checked\n";
  2450. print "\n" if ($quiet == 0);
  2451. }
  2452. if ($quiet == 0) {
  2453. # If there were whitespace errors which cleanpatch can fix
  2454. # then suggest that.
  2455. if ($rpt_cleaners) {
  2456. print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
  2457. print " scripts/cleanfile\n\n";
  2458. }
  2459. }
  2460. if ($clean == 1 && $quiet == 0) {
  2461. print "$vname has no obvious style problems and is ready for submission.\n"
  2462. }
  2463. if ($clean == 0 && $quiet == 0) {
  2464. print "$vname has style problems, please review. If any of these errors\n";
  2465. print "are false positives report them to the maintainer, see\n";
  2466. print "CHECKPATCH in MAINTAINERS.\n";
  2467. }
  2468. return $clean;
  2469. }