checkpatch.pl 63 KB

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