checkpatch.pl 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  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.14';
  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_type = 0;
  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-type!' => \$tst_type,
  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. for my $key (keys %debug) {
  60. eval "\${dbg_$key} = '$debug{$key}';"
  61. }
  62. if ($terse) {
  63. $emacs = 1;
  64. $quiet++;
  65. }
  66. if ($tree) {
  67. if (defined $root) {
  68. if (!top_of_kernel_tree($root)) {
  69. die "$P: $root: --root does not point at a valid tree\n";
  70. }
  71. } else {
  72. if (top_of_kernel_tree('.')) {
  73. $root = '.';
  74. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  75. top_of_kernel_tree($1)) {
  76. $root = $1;
  77. }
  78. }
  79. if (!defined $root) {
  80. print "Must be run from the top-level dir. of a kernel tree\n";
  81. exit(2);
  82. }
  83. }
  84. my $emitted_corrupt = 0;
  85. our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
  86. our $Storage = qr{extern|static|asmlinkage};
  87. our $Sparse = qr{
  88. __user|
  89. __kernel|
  90. __force|
  91. __iomem|
  92. __must_check|
  93. __init_refok|
  94. __kprobes|
  95. fastcall
  96. }x;
  97. our $Attribute = qr{
  98. const|
  99. __read_mostly|
  100. __kprobes|
  101. __(?:mem|cpu|dev|)(?:initdata|init)
  102. }x;
  103. our $Inline = qr{inline|__always_inline|noinline};
  104. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  105. our $Lval = qr{$Ident(?:$Member)*};
  106. our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
  107. our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
  108. our $Operators = qr{
  109. <=|>=|==|!=|
  110. =>|->|<<|>>|<|>|!|~|
  111. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  112. }x;
  113. our $NonptrType;
  114. our $Type;
  115. our $Declare;
  116. our @typeList = (
  117. qr{void},
  118. qr{char},
  119. qr{short},
  120. qr{int},
  121. qr{long},
  122. qr{unsigned},
  123. qr{float},
  124. qr{double},
  125. qr{bool},
  126. qr{long\s+int},
  127. qr{long\s+long},
  128. qr{long\s+long\s+int},
  129. qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
  130. qr{struct\s+$Ident},
  131. qr{union\s+$Ident},
  132. qr{enum\s+$Ident},
  133. qr{${Ident}_t},
  134. qr{${Ident}_handler},
  135. qr{${Ident}_handler_fn},
  136. );
  137. sub build_types {
  138. my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
  139. $NonptrType = qr{
  140. \b
  141. (?:const\s+)?
  142. (?:unsigned\s+)?
  143. $all
  144. (?:\s+$Sparse|\s+const)*
  145. \b
  146. }x;
  147. $Type = qr{
  148. \b$NonptrType\b
  149. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  150. (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
  151. }x;
  152. $Declare = qr{(?:$Storage\s+)?$Type};
  153. }
  154. build_types();
  155. $chk_signoff = 0 if ($file);
  156. my @dep_includes = ();
  157. my @dep_functions = ();
  158. my $removal = "Documentation/feature-removal-schedule.txt";
  159. if ($tree && -f "$root/$removal") {
  160. open(REMOVE, "<$root/$removal") ||
  161. die "$P: $removal: open failed - $!\n";
  162. while (<REMOVE>) {
  163. if (/^Check:\s+(.*\S)/) {
  164. for my $entry (split(/[, ]+/, $1)) {
  165. if ($entry =~ m@include/(.*)@) {
  166. push(@dep_includes, $1);
  167. } elsif ($entry !~ m@/@) {
  168. push(@dep_functions, $entry);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. my @rawlines = ();
  175. my @lines = ();
  176. my $vname;
  177. for my $filename (@ARGV) {
  178. if ($file) {
  179. open(FILE, "diff -u /dev/null $filename|") ||
  180. die "$P: $filename: diff failed - $!\n";
  181. } else {
  182. open(FILE, "<$filename") ||
  183. die "$P: $filename: open failed - $!\n";
  184. }
  185. if ($filename eq '-') {
  186. $vname = 'Your patch';
  187. } else {
  188. $vname = $filename;
  189. }
  190. while (<FILE>) {
  191. chomp;
  192. push(@rawlines, $_);
  193. }
  194. close(FILE);
  195. if (!process($filename)) {
  196. $exit = 1;
  197. }
  198. @rawlines = ();
  199. @lines = ();
  200. }
  201. exit($exit);
  202. sub top_of_kernel_tree {
  203. my ($root) = @_;
  204. my @tree_check = (
  205. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  206. "README", "Documentation", "arch", "include", "drivers",
  207. "fs", "init", "ipc", "kernel", "lib", "scripts",
  208. );
  209. foreach my $check (@tree_check) {
  210. if (! -e $root . '/' . $check) {
  211. return 0;
  212. }
  213. }
  214. return 1;
  215. }
  216. sub expand_tabs {
  217. my ($str) = @_;
  218. my $res = '';
  219. my $n = 0;
  220. for my $c (split(//, $str)) {
  221. if ($c eq "\t") {
  222. $res .= ' ';
  223. $n++;
  224. for (; ($n % 8) != 0; $n++) {
  225. $res .= ' ';
  226. }
  227. next;
  228. }
  229. $res .= $c;
  230. $n++;
  231. }
  232. return $res;
  233. }
  234. sub copy_spacing {
  235. my ($str) = @_;
  236. my $res = '';
  237. for my $c (split(//, $str)) {
  238. if ($c eq "\t") {
  239. $res .= $c;
  240. } else {
  241. $res .= ' ';
  242. }
  243. }
  244. return $res;
  245. }
  246. sub line_stats {
  247. my ($line) = @_;
  248. # Drop the diff line leader and expand tabs
  249. $line =~ s/^.//;
  250. $line = expand_tabs($line);
  251. # Pick the indent from the front of the line.
  252. my ($white) = ($line =~ /^(\s*)/);
  253. return (length($line), length($white));
  254. }
  255. sub sanitise_line {
  256. my ($line) = @_;
  257. my $res = '';
  258. my $l = '';
  259. my $quote = '';
  260. my $qlen = 0;
  261. foreach my $c (split(//, $line)) {
  262. # The second backslash of a pair is not a "quote".
  263. if ($l eq "\\" && $c eq "\\") {
  264. $c = 'X';
  265. }
  266. if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
  267. if ($quote eq '') {
  268. $quote = $c;
  269. $res .= $c;
  270. $l = $c;
  271. $qlen = 0;
  272. next;
  273. } elsif ($quote eq $c) {
  274. $quote = '';
  275. }
  276. }
  277. if ($quote eq "'" && $qlen > 1) {
  278. $quote = '';
  279. }
  280. if ($quote && $c ne "\t") {
  281. $res .= "X";
  282. $qlen++;
  283. } else {
  284. $res .= $c;
  285. }
  286. $l = $c;
  287. }
  288. # Clear out the comments.
  289. while ($res =~ m@(/\*.*?\*/)@g) {
  290. substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
  291. }
  292. if ($res =~ m@(/\*.*)@) {
  293. substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
  294. }
  295. if ($res =~ m@^.(.*\*/)@) {
  296. substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
  297. }
  298. # The pathname on a #include may be surrounded by '<' and '>'.
  299. if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
  300. my $clean = 'X' x length($1);
  301. $res =~ s@\<.*\>@<$clean>@;
  302. # The whole of a #error is a string.
  303. } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
  304. my $clean = 'X' x length($1);
  305. $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
  306. }
  307. return $res;
  308. }
  309. sub ctx_statement_block {
  310. my ($linenr, $remain, $off) = @_;
  311. my $line = $linenr - 1;
  312. my $blk = '';
  313. my $soff = $off;
  314. my $coff = $off - 1;
  315. my $loff = 0;
  316. my $type = '';
  317. my $level = 0;
  318. my $c;
  319. my $len = 0;
  320. my $remainder;
  321. while (1) {
  322. #warn "CSB: blk<$blk>\n";
  323. # If we are about to drop off the end, pull in more
  324. # context.
  325. if ($off >= $len) {
  326. for (; $remain > 0; $line++) {
  327. next if ($lines[$line] =~ /^-/);
  328. $remain--;
  329. $loff = $len;
  330. $blk .= $lines[$line] . "\n";
  331. $len = length($blk);
  332. $line++;
  333. last;
  334. }
  335. # Bail if there is no further context.
  336. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  337. if ($off >= $len) {
  338. last;
  339. }
  340. }
  341. $c = substr($blk, $off, 1);
  342. $remainder = substr($blk, $off);
  343. #warn "CSB: c<$c> type<$type> level<$level>\n";
  344. # Statement ends at the ';' or a close '}' at the
  345. # outermost level.
  346. if ($level == 0 && $c eq ';') {
  347. last;
  348. }
  349. # An else is really a conditional as long as its not else if
  350. if ($level == 0 && $remainder =~ /(\s+else)(?:\s|{)/ &&
  351. $remainder !~ /\s+else\s+if\b/) {
  352. $coff = $off + length($1);
  353. }
  354. if (($type eq '' || $type eq '(') && $c eq '(') {
  355. $level++;
  356. $type = '(';
  357. }
  358. if ($type eq '(' && $c eq ')') {
  359. $level--;
  360. $type = ($level != 0)? '(' : '';
  361. if ($level == 0 && $coff < $soff) {
  362. $coff = $off;
  363. }
  364. }
  365. if (($type eq '' || $type eq '{') && $c eq '{') {
  366. $level++;
  367. $type = '{';
  368. }
  369. if ($type eq '{' && $c eq '}') {
  370. $level--;
  371. $type = ($level != 0)? '{' : '';
  372. if ($level == 0) {
  373. last;
  374. }
  375. }
  376. $off++;
  377. }
  378. if ($off == $len) {
  379. $line++;
  380. $remain--;
  381. }
  382. my $statement = substr($blk, $soff, $off - $soff + 1);
  383. my $condition = substr($blk, $soff, $coff - $soff + 1);
  384. #warn "STATEMENT<$statement>\n";
  385. #warn "CONDITION<$condition>\n";
  386. #print "off<$off> loff<$loff>\n";
  387. return ($statement, $condition,
  388. $line, $remain + 1, $off - $loff + 1, $level);
  389. }
  390. sub ctx_statement_full {
  391. my ($linenr, $remain, $off) = @_;
  392. my ($statement, $condition, $level);
  393. my (@chunks);
  394. ($statement, $condition, $linenr, $remain, $off, $level) =
  395. ctx_statement_block($linenr, $remain, $off);
  396. #print "F: c<$condition> s<$statement>\n";
  397. for (;;) {
  398. push(@chunks, [ $condition, $statement ]);
  399. last if (!($remain > 0 && $condition =~ /^.\s*(?:if|else|do)/));
  400. ($statement, $condition, $linenr, $remain, $off, $level) =
  401. ctx_statement_block($linenr, $remain, $off);
  402. #print "C: c<$condition> s<$statement>\n";
  403. }
  404. return ($level, $linenr, @chunks);
  405. }
  406. sub ctx_block_get {
  407. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  408. my $line;
  409. my $start = $linenr - 1;
  410. my $blk = '';
  411. my @o;
  412. my @c;
  413. my @res = ();
  414. my $level = 0;
  415. for ($line = $start; $remain > 0; $line++) {
  416. next if ($rawlines[$line] =~ /^-/);
  417. $remain--;
  418. $blk .= $rawlines[$line];
  419. foreach my $c (split(//, $rawlines[$line])) {
  420. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  421. if ($off > 0) {
  422. $off--;
  423. next;
  424. }
  425. if ($c eq $close && $level > 0) {
  426. $level--;
  427. last if ($level == 0);
  428. } elsif ($c eq $open) {
  429. $level++;
  430. }
  431. }
  432. if (!$outer || $level <= 1) {
  433. push(@res, $rawlines[$line]);
  434. }
  435. last if ($level == 0);
  436. }
  437. return ($level, @res);
  438. }
  439. sub ctx_block_outer {
  440. my ($linenr, $remain) = @_;
  441. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  442. return @r;
  443. }
  444. sub ctx_block {
  445. my ($linenr, $remain) = @_;
  446. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  447. return @r;
  448. }
  449. sub ctx_statement {
  450. my ($linenr, $remain, $off) = @_;
  451. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  452. return @r;
  453. }
  454. sub ctx_block_level {
  455. my ($linenr, $remain) = @_;
  456. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  457. }
  458. sub ctx_statement_level {
  459. my ($linenr, $remain, $off) = @_;
  460. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  461. }
  462. sub ctx_locate_comment {
  463. my ($first_line, $end_line) = @_;
  464. # Catch a comment on the end of the line itself.
  465. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
  466. return $current_comment if (defined $current_comment);
  467. # Look through the context and try and figure out if there is a
  468. # comment.
  469. my $in_comment = 0;
  470. $current_comment = '';
  471. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  472. my $line = $rawlines[$linenr - 1];
  473. #warn " $line\n";
  474. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  475. $in_comment = 1;
  476. }
  477. if ($line =~ m@/\*@) {
  478. $in_comment = 1;
  479. }
  480. if (!$in_comment && $current_comment ne '') {
  481. $current_comment = '';
  482. }
  483. $current_comment .= $line . "\n" if ($in_comment);
  484. if ($line =~ m@\*/@) {
  485. $in_comment = 0;
  486. }
  487. }
  488. chomp($current_comment);
  489. return($current_comment);
  490. }
  491. sub ctx_has_comment {
  492. my ($first_line, $end_line) = @_;
  493. my $cmt = ctx_locate_comment($first_line, $end_line);
  494. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  495. ##print "CMMT: $cmt\n";
  496. return ($cmt ne '');
  497. }
  498. sub cat_vet {
  499. my ($vet) = @_;
  500. my ($res, $coded);
  501. $res = '';
  502. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  503. $res .= $1;
  504. if ($2 ne '') {
  505. $coded = sprintf("^%c", unpack('C', $2) + 64);
  506. $res .= $coded;
  507. }
  508. }
  509. $res =~ s/$/\$/;
  510. return $res;
  511. }
  512. my $av_preprocessor = 0;
  513. my $av_paren = 0;
  514. my @av_paren_type;
  515. sub annotate_reset {
  516. $av_preprocessor = 0;
  517. $av_paren = 0;
  518. @av_paren_type = ();
  519. }
  520. sub annotate_values {
  521. my ($stream, $type) = @_;
  522. my $res;
  523. my $cur = $stream;
  524. print "$stream\n" if ($dbg_values > 1);
  525. while (length($cur)) {
  526. print " <$type> " if ($dbg_values > 1);
  527. if ($cur =~ /^(\s+)/o) {
  528. print "WS($1)\n" if ($dbg_values > 1);
  529. if ($1 =~ /\n/ && $av_preprocessor) {
  530. $av_preprocessor = 0;
  531. $type = 'N';
  532. }
  533. } elsif ($cur =~ /^($Type)/) {
  534. print "DECLARE($1)\n" if ($dbg_values > 1);
  535. $type = 'T';
  536. } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
  537. print "DEFINE($1)\n" if ($dbg_values > 1);
  538. $av_preprocessor = 1;
  539. $av_paren_type[$av_paren] = 'N';
  540. } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) {
  541. print "PRE($1)\n" if ($dbg_values > 1);
  542. $av_preprocessor = 1;
  543. $type = 'N';
  544. } elsif ($cur =~ /^(\\\n)/o) {
  545. print "PRECONT($1)\n" if ($dbg_values > 1);
  546. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  547. print "SIZEOF($1)\n" if ($dbg_values > 1);
  548. if (defined $2) {
  549. $av_paren_type[$av_paren] = 'V';
  550. }
  551. $type = 'N';
  552. } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
  553. print "COND($1)\n" if ($dbg_values > 1);
  554. $av_paren_type[$av_paren] = 'N';
  555. $type = 'N';
  556. } elsif ($cur =~/^(return|case|else)/o) {
  557. print "KEYWORD($1)\n" if ($dbg_values > 1);
  558. $type = 'N';
  559. } elsif ($cur =~ /^(\()/o) {
  560. print "PAREN('$1')\n" if ($dbg_values > 1);
  561. $av_paren++;
  562. $type = 'N';
  563. } elsif ($cur =~ /^(\))/o) {
  564. $av_paren-- if ($av_paren > 0);
  565. if (defined $av_paren_type[$av_paren]) {
  566. $type = $av_paren_type[$av_paren];
  567. undef $av_paren_type[$av_paren];
  568. print "PAREN('$1') -> $type\n"
  569. if ($dbg_values > 1);
  570. } else {
  571. print "PAREN('$1')\n" if ($dbg_values > 1);
  572. }
  573. } elsif ($cur =~ /^($Ident)\(/o) {
  574. print "FUNC($1)\n" if ($dbg_values > 1);
  575. $av_paren_type[$av_paren] = 'V';
  576. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  577. print "IDENT($1)\n" if ($dbg_values > 1);
  578. $type = 'V';
  579. } elsif ($cur =~ /^($Assignment)/o) {
  580. print "ASSIGN($1)\n" if ($dbg_values > 1);
  581. $type = 'N';
  582. } elsif ($cur =~/^(;)/) {
  583. print "END($1)\n" if ($dbg_values > 1);
  584. $type = 'E';
  585. } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) {
  586. print "CLOSE($1)\n" if ($dbg_values > 1);
  587. $type = 'N';
  588. } elsif ($cur =~ /^($Operators)/o) {
  589. print "OP($1)\n" if ($dbg_values > 1);
  590. if ($1 ne '++' && $1 ne '--') {
  591. $type = 'N';
  592. }
  593. } elsif ($cur =~ /(^.)/o) {
  594. print "C($1)\n" if ($dbg_values > 1);
  595. }
  596. if (defined $1) {
  597. $cur = substr($cur, length($1));
  598. $res .= $type x length($1);
  599. }
  600. }
  601. return $res;
  602. }
  603. sub possible {
  604. my ($possible, $line) = @_;
  605. #print "CHECK<$possible>\n";
  606. if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
  607. $possible ne 'goto' && $possible ne 'return' &&
  608. $possible ne 'struct' && $possible ne 'enum' &&
  609. $possible ne 'case' && $possible ne 'else' &&
  610. $possible ne 'typedef') {
  611. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  612. push(@typeList, $possible);
  613. build_types();
  614. }
  615. }
  616. my $prefix = '';
  617. sub report {
  618. my $line = $prefix . $_[0];
  619. $line = (split('\n', $line))[0] . "\n" if ($terse);
  620. push(our @report, $line);
  621. }
  622. sub report_dump {
  623. our @report;
  624. }
  625. sub ERROR {
  626. report("ERROR: $_[0]\n");
  627. our $clean = 0;
  628. our $cnt_error++;
  629. }
  630. sub WARN {
  631. report("WARNING: $_[0]\n");
  632. our $clean = 0;
  633. our $cnt_warn++;
  634. }
  635. sub CHK {
  636. if ($check) {
  637. report("CHECK: $_[0]\n");
  638. our $clean = 0;
  639. our $cnt_chk++;
  640. }
  641. }
  642. sub process {
  643. my $filename = shift;
  644. my $linenr=0;
  645. my $prevline="";
  646. my $prevrawline="";
  647. my $stashline="";
  648. my $stashrawline="";
  649. my $length;
  650. my $indent;
  651. my $previndent=0;
  652. my $stashindent=0;
  653. our $clean = 1;
  654. my $signoff = 0;
  655. my $is_patch = 0;
  656. our @report = ();
  657. our $cnt_lines = 0;
  658. our $cnt_error = 0;
  659. our $cnt_warn = 0;
  660. our $cnt_chk = 0;
  661. # Trace the real file/line as we go.
  662. my $realfile = '';
  663. my $realline = 0;
  664. my $realcnt = 0;
  665. my $here = '';
  666. my $in_comment = 0;
  667. my $comment_edge = 0;
  668. my $first_line = 0;
  669. my $prev_values = 'E';
  670. # suppression flags
  671. my $suppress_ifbraces = 0;
  672. # Pre-scan the patch sanitizing the lines.
  673. # Pre-scan the patch looking for any __setup documentation.
  674. #
  675. my @setup_docs = ();
  676. my $setup_docs = 0;
  677. my $line;
  678. foreach my $rawline (@rawlines) {
  679. # Standardise the strings and chars within the input to
  680. # simplify matching.
  681. $line = sanitise_line($rawline);
  682. push(@lines, $line);
  683. ##print "==>$rawline\n";
  684. ##print "-->$line\n";
  685. if ($line=~/^\+\+\+\s+(\S+)/) {
  686. $setup_docs = 0;
  687. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  688. $setup_docs = 1;
  689. }
  690. next;
  691. }
  692. if ($setup_docs && $line =~ /^\+/) {
  693. push(@setup_docs, $line);
  694. }
  695. }
  696. $prefix = '';
  697. foreach my $line (@lines) {
  698. $linenr++;
  699. my $rawline = $rawlines[$linenr - 1];
  700. #extract the filename as it passes
  701. if ($line=~/^\+\+\+\s+(\S+)/) {
  702. $realfile=$1;
  703. $realfile =~ s@^[^/]*/@@;
  704. $in_comment = 0;
  705. next;
  706. }
  707. #extract the line range in the file after the patch is applied
  708. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  709. $is_patch = 1;
  710. $first_line = $linenr + 1;
  711. $in_comment = 0;
  712. $realline=$1-1;
  713. if (defined $2) {
  714. $realcnt=$3+1;
  715. } else {
  716. $realcnt=1+1;
  717. }
  718. annotate_reset();
  719. $prev_values = 'E';
  720. $suppress_ifbraces = $linenr - 1;
  721. next;
  722. }
  723. # track the line number as we move through the hunk, note that
  724. # new versions of GNU diff omit the leading space on completely
  725. # blank context lines so we need to count that too.
  726. if ($line =~ /^( |\+|$)/) {
  727. $realline++;
  728. $realcnt-- if ($realcnt != 0);
  729. # Guestimate if this is a continuing comment. Run
  730. # the context looking for a comment "edge". If this
  731. # edge is a close comment then we must be in a comment
  732. # at context start.
  733. if ($linenr == $first_line) {
  734. my $edge;
  735. for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
  736. ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
  737. last if (defined $edge);
  738. }
  739. if (defined $edge && $edge eq '*/') {
  740. $in_comment = 1;
  741. }
  742. }
  743. # Guestimate if this is a continuing comment. If this
  744. # is the start of a diff block and this line starts
  745. # ' *' then it is very likely a comment.
  746. if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
  747. $in_comment = 1;
  748. }
  749. # Find the last comment edge on _this_ line.
  750. $comment_edge = 0;
  751. while (($rawline =~ m@(/\*|\*/)@g)) {
  752. if ($1 eq '/*') {
  753. $in_comment = 1;
  754. } else {
  755. $in_comment = 0;
  756. }
  757. $comment_edge = 1;
  758. }
  759. # Measure the line length and indent.
  760. ($length, $indent) = line_stats($rawline);
  761. # Track the previous line.
  762. ($prevline, $stashline) = ($stashline, $line);
  763. ($previndent, $stashindent) = ($stashindent, $indent);
  764. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  765. #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
  766. } elsif ($realcnt == 1) {
  767. $realcnt--;
  768. }
  769. #make up the handle for any error we report on this line
  770. $here = "#$linenr: " if (!$file);
  771. $here = "#$realline: " if ($file);
  772. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  773. my $hereline = "$here\n$rawline\n";
  774. my $herecurr = "$here\n$rawline\n";
  775. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  776. $prefix = "$filename:$realline: " if ($emacs && $file);
  777. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  778. $cnt_lines++ if ($realcnt != 0);
  779. #check the patch for a signoff:
  780. if ($line =~ /^\s*signed-off-by:/i) {
  781. # This is a signoff, if ugly, so do not double report.
  782. $signoff++;
  783. if (!($line =~ /^\s*Signed-off-by:/)) {
  784. WARN("Signed-off-by: is the preferred form\n" .
  785. $herecurr);
  786. }
  787. if ($line =~ /^\s*signed-off-by:\S/i) {
  788. WARN("need space after Signed-off-by:\n" .
  789. $herecurr);
  790. }
  791. }
  792. # Check for wrappage within a valid hunk of the file
  793. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  794. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  795. $herecurr) if (!$emitted_corrupt++);
  796. }
  797. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  798. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  799. !($rawline =~ m/^(
  800. [\x09\x0A\x0D\x20-\x7E] # ASCII
  801. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  802. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  803. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  804. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  805. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  806. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  807. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  808. )*$/x )) {
  809. ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
  810. }
  811. #ignore lines being removed
  812. if ($line=~/^-/) {next;}
  813. # check we are in a valid source file if not then ignore this hunk
  814. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  815. #trailing whitespace
  816. if ($line =~ /^\+.*\015/) {
  817. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  818. ERROR("DOS line endings\n" . $herevet);
  819. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  820. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  821. ERROR("trailing whitespace\n" . $herevet);
  822. }
  823. #80 column limit
  824. if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
  825. WARN("line over 80 characters\n" . $herecurr);
  826. }
  827. # check for adding lines without a newline.
  828. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  829. WARN("adding a line without newline at end of file\n" . $herecurr);
  830. }
  831. # check we are in a valid source file *.[hc] if not then ignore this hunk
  832. next if ($realfile !~ /\.[hc]$/);
  833. # at the beginning of a line any tabs must come first and anything
  834. # more than 8 must use tabs.
  835. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  836. $rawline =~ /^\+\s* \s*/) {
  837. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  838. ERROR("use tabs not spaces\n" . $herevet);
  839. }
  840. # check for RCS/CVS revision markers
  841. if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) {
  842. WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  843. }
  844. # The rest of our checks refer specifically to C style
  845. # only apply those _outside_ comments. Only skip
  846. # lines in the middle of comments.
  847. next if (!$comment_edge && $in_comment);
  848. # Check for potential 'bare' types
  849. if ($realcnt) {
  850. # Ignore goto labels.
  851. if ($line =~ /$Ident:\*$/) {
  852. # Ignore functions being called
  853. } elsif ($line =~ /^.\s*$Ident\s*\(/) {
  854. # definitions in global scope can only start with types
  855. } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
  856. possible($1, $line);
  857. # declarations always start with types
  858. } elsif ($prev_values eq 'E' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
  859. possible($1);
  860. }
  861. # any (foo ... *) is a pointer cast, and foo is a type
  862. while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
  863. possible($1, $line);
  864. }
  865. # Check for any sort of function declaration.
  866. # int foo(something bar, other baz);
  867. # void (*store_gdt)(x86_descr_ptr *);
  868. if ($prev_values eq 'E' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
  869. my ($name_len) = length($1);
  870. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len);
  871. my $ctx = join("\n", @ctx);
  872. $ctx =~ s/\n.//;
  873. substr($ctx, 0, $name_len + 1) = '';
  874. $ctx =~ s/\)[^\)]*$//;
  875. for my $arg (split(/\s*,\s*/, $ctx)) {
  876. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
  877. possible($1, $line);
  878. }
  879. }
  880. }
  881. }
  882. #
  883. # Checks which may be anchored in the context.
  884. #
  885. # Check for switch () and associated case and default
  886. # statements should be at the same indent.
  887. if ($line=~/\bswitch\s*\(.*\)/) {
  888. my $err = '';
  889. my $sep = '';
  890. my @ctx = ctx_block_outer($linenr, $realcnt);
  891. shift(@ctx);
  892. for my $ctx (@ctx) {
  893. my ($clen, $cindent) = line_stats($ctx);
  894. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  895. $indent != $cindent) {
  896. $err .= "$sep$ctx\n";
  897. $sep = '';
  898. } else {
  899. $sep = "[...]\n";
  900. }
  901. }
  902. if ($err ne '') {
  903. ERROR("switch and case should be at the same indent\n$hereline$err");
  904. }
  905. }
  906. # if/while/etc brace do not go on next line, unless defining a do while loop,
  907. # or if that brace on the next line is for something else
  908. if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
  909. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  910. my $ctx_ln = $linenr + $#ctx + 1;
  911. my $ctx_cnt = $realcnt - $#ctx - 1;
  912. my $ctx = join("\n", @ctx);
  913. # Skip over any removed lines in the context following statement.
  914. while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
  915. $ctx_ln++;
  916. $ctx_cnt--;
  917. }
  918. ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
  919. if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  920. ERROR("That open brace { should be on the previous line\n" .
  921. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  922. }
  923. if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
  924. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  925. if ($nindent > $indent) {
  926. WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
  927. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  928. }
  929. }
  930. }
  931. # Track the 'values' across context and added lines.
  932. my $opline = $line; $opline =~ s/^./ /;
  933. my $curr_values = annotate_values($opline . "\n", $prev_values);
  934. $curr_values = $prev_values . $curr_values;
  935. if ($dbg_values) {
  936. my $outline = $opline; $outline =~ s/\t/ /g;
  937. warn "--> .$outline\n";
  938. warn "--> $curr_values\n";
  939. }
  940. $prev_values = substr($curr_values, -1);
  941. #ignore lines not being added
  942. if ($line=~/^[^\+]/) {next;}
  943. # TEST: allow direct testing of the type matcher.
  944. if ($tst_type && $line =~ /^.$Declare$/) {
  945. ERROR("TEST: is type $Declare\n" . $herecurr);
  946. next;
  947. }
  948. # check for initialisation to aggregates open brace on the next line
  949. if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
  950. $line =~ /^.\s*{/) {
  951. ERROR("That open brace { should be on the previous line\n" . $hereprev);
  952. }
  953. #
  954. # Checks which are anchored on the added line.
  955. #
  956. # check for malformed paths in #include statements (uses RAW line)
  957. if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
  958. my $path = $1;
  959. if ($path =~ m{//}) {
  960. ERROR("malformed #include filename\n" .
  961. $herecurr);
  962. }
  963. }
  964. # no C99 // comments
  965. if ($line =~ m{//}) {
  966. ERROR("do not use C99 // comments\n" . $herecurr);
  967. }
  968. # Remove C99 comments.
  969. $line =~ s@//.*@@;
  970. $opline =~ s@//.*@@;
  971. #EXPORT_SYMBOL should immediately follow its function closing }.
  972. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  973. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  974. my $name = $1;
  975. if (($prevline !~ /^}/) &&
  976. ($prevline !~ /^\+}/) &&
  977. ($prevline !~ /^ }/) &&
  978. ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
  979. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  980. }
  981. }
  982. # check for external initialisers.
  983. if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
  984. ERROR("do not initialise externals to 0 or NULL\n" .
  985. $herecurr);
  986. }
  987. # check for static initialisers.
  988. if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
  989. ERROR("do not initialise statics to 0 or NULL\n" .
  990. $herecurr);
  991. }
  992. # check for new typedefs, only function parameters and sparse annotations
  993. # make sense.
  994. if ($line =~ /\btypedef\s/ &&
  995. $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
  996. $line !~ /\b__bitwise(?:__|)\b/) {
  997. WARN("do not add new typedefs\n" . $herecurr);
  998. }
  999. # * goes on variable not on type
  1000. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  1001. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  1002. $herecurr);
  1003. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  1004. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  1005. $herecurr);
  1006. } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  1007. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  1008. $herecurr);
  1009. } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  1010. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  1011. $herecurr);
  1012. }
  1013. # # no BUG() or BUG_ON()
  1014. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1015. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1016. # print "$herecurr";
  1017. # $clean = 0;
  1018. # }
  1019. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1020. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1021. }
  1022. # printk should use KERN_* levels. Note that follow on printk's on the
  1023. # same line do not need a level, so we use the current block context
  1024. # to try and find and validate the current printk. In summary the current
  1025. # printk includes all preceeding printk's which have no newline on the end.
  1026. # we assume the first bad printk is the one to report.
  1027. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1028. my $ok = 0;
  1029. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1030. #print "CHECK<$lines[$ln - 1]\n";
  1031. # we have a preceeding printk if it ends
  1032. # with "\n" ignore it, else it is to blame
  1033. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1034. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1035. $ok = 1;
  1036. }
  1037. last;
  1038. }
  1039. }
  1040. if ($ok == 0) {
  1041. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  1042. }
  1043. }
  1044. # function brace can't be on same line, except for #defines of do while,
  1045. # or if closed on same line
  1046. if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
  1047. !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
  1048. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1049. }
  1050. # open braces for enum, union and struct go on the same line.
  1051. if ($line =~ /^.\s*{/ &&
  1052. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1053. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1054. }
  1055. # check for spaces between functions and their parentheses.
  1056. while ($line =~ /($Ident)\s+\(/g) {
  1057. my $name = $1;
  1058. my $ctx = substr($line, 0, $-[1]);
  1059. # Ignore those directives where spaces _are_ permitted.
  1060. if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
  1061. # cpp #define statements have non-optional spaces, ie
  1062. # if there is a space between the name and the open
  1063. # parenthesis it is simply not a parameter group.
  1064. } elsif ($ctx =~ /^.\#\s*define\s*$/) {
  1065. # If this whole things ends with a type its most
  1066. # likely a typedef for a function.
  1067. } elsif ("$ctx$name" =~ /$Type$/) {
  1068. } else {
  1069. WARN("no space between function name and open parenthesis '('\n" . $herecurr);
  1070. }
  1071. }
  1072. # Check operator spacing.
  1073. if (!($line=~/\#\s*include/)) {
  1074. my $ops = qr{
  1075. <<=|>>=|<=|>=|==|!=|
  1076. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1077. =>|->|<<|>>|<|>|=|!|~|
  1078. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  1079. }x;
  1080. my @elements = split(/($;+|$ops|;)/, $opline);
  1081. my $off = 0;
  1082. my $blank = copy_spacing($opline);
  1083. for (my $n = 0; $n < $#elements; $n += 2) {
  1084. $off += length($elements[$n]);
  1085. my $a = '';
  1086. $a = 'V' if ($elements[$n] ne '');
  1087. $a = 'W' if ($elements[$n] =~ /\s$/);
  1088. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1089. $a = 'O' if ($elements[$n] eq '');
  1090. $a = 'E' if ($elements[$n] eq '' && $n == 0);
  1091. my $op = $elements[$n + 1];
  1092. my $c = '';
  1093. if (defined $elements[$n + 2]) {
  1094. $c = 'V' if ($elements[$n + 2] ne '');
  1095. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1096. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1097. $c = 'O' if ($elements[$n + 2] eq '');
  1098. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  1099. } else {
  1100. $c = 'E';
  1101. }
  1102. # Pick up the preceeding and succeeding characters.
  1103. my $ca = substr($opline, 0, $off);
  1104. my $cc = '';
  1105. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1106. $cc = substr($opline, $off + length($elements[$n + 1]));
  1107. }
  1108. my $cb = "$ca$;$cc";
  1109. my $ctx = "${a}x${c}";
  1110. my $at = "(ctx:$ctx)";
  1111. my $ptr = substr($blank, 0, $off) . "^";
  1112. my $hereptr = "$hereline$ptr\n";
  1113. # Classify operators into binary, unary, or
  1114. # definitions (* only) where they have more
  1115. # than one mode.
  1116. my $op_type = substr($curr_values, $off + 1, 1);
  1117. my $op_left = substr($curr_values, $off, 1);
  1118. my $is_unary;
  1119. if ($op_type eq 'T') {
  1120. $is_unary = 2;
  1121. } elsif ($op_left eq 'V') {
  1122. $is_unary = 0;
  1123. } else {
  1124. $is_unary = 1;
  1125. }
  1126. #if ($op eq '-' || $op eq '&' || $op eq '*') {
  1127. # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
  1128. #}
  1129. # Ignore operators passed as parameters.
  1130. if ($op_type ne 'V' &&
  1131. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1132. # Ignore comments
  1133. } elsif ($op =~ /^$;+$/) {
  1134. # ; should have either the end of line or a space or \ after it
  1135. } elsif ($op eq ';') {
  1136. if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
  1137. $cc !~ /^;/) {
  1138. ERROR("need space after that '$op' $at\n" . $hereptr);
  1139. }
  1140. # // is a comment
  1141. } elsif ($op eq '//') {
  1142. # -> should have no spaces
  1143. } elsif ($op eq '->') {
  1144. if ($ctx =~ /Wx.|.xW/) {
  1145. ERROR("no spaces around that '$op' $at\n" . $hereptr);
  1146. }
  1147. # , must have a space on the right.
  1148. } elsif ($op eq ',') {
  1149. if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
  1150. ERROR("need space after that '$op' $at\n" . $hereptr);
  1151. }
  1152. # '*' as part of a type definition -- reported already.
  1153. } elsif ($op eq '*' && $is_unary == 2) {
  1154. #warn "'*' is part of type\n";
  1155. # unary operators should have a space before and
  1156. # none after. May be left adjacent to another
  1157. # unary operator, or a cast
  1158. } elsif ($op eq '!' || $op eq '~' ||
  1159. ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
  1160. if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1161. ERROR("need space before that '$op' $at\n" . $hereptr);
  1162. }
  1163. if ($ctx =~ /.xW/) {
  1164. ERROR("no space after that '$op' $at\n" . $hereptr);
  1165. }
  1166. # unary ++ and unary -- are allowed no space on one side.
  1167. } elsif ($op eq '++' or $op eq '--') {
  1168. if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
  1169. ERROR("need space one side of that '$op' $at\n" . $hereptr);
  1170. }
  1171. if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  1172. ERROR("no space before that '$op' $at\n" . $hereptr);
  1173. }
  1174. # << and >> may either have or not have spaces both sides
  1175. } elsif ($op eq '<<' or $op eq '>>' or
  1176. $op eq '&' or $op eq '^' or $op eq '|' or
  1177. $op eq '+' or $op eq '-' or
  1178. $op eq '*' or $op eq '/' or
  1179. $op eq '%')
  1180. {
  1181. if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
  1182. ERROR("need consistent spacing around '$op' $at\n" .
  1183. $hereptr);
  1184. }
  1185. # All the others need spaces both sides.
  1186. } elsif ($ctx !~ /[EW]x[WE]/) {
  1187. # Ignore email addresses <foo@bar>
  1188. if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
  1189. !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
  1190. ERROR("need spaces around that '$op' $at\n" . $hereptr);
  1191. }
  1192. }
  1193. $off += length($elements[$n + 1]);
  1194. }
  1195. }
  1196. # check for multiple assignments
  1197. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1198. CHK("multiple assignments should be avoided\n" . $herecurr);
  1199. }
  1200. ## # check for multiple declarations, allowing for a function declaration
  1201. ## # continuation.
  1202. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1203. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1204. ##
  1205. ## # Remove any bracketed sections to ensure we do not
  1206. ## # falsly report the parameters of functions.
  1207. ## my $ln = $line;
  1208. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1209. ## }
  1210. ## if ($ln =~ /,/) {
  1211. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1212. ## }
  1213. ## }
  1214. #need space before brace following if, while, etc
  1215. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1216. $line =~ /do{/) {
  1217. ERROR("need a space before the open brace '{'\n" . $herecurr);
  1218. }
  1219. # closing brace should have a space following it when it has anything
  1220. # on the line
  1221. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1222. ERROR("need a space after that close brace '}'\n" . $herecurr);
  1223. }
  1224. # check spacing on square brackets
  1225. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1226. ERROR("no space after that open square bracket '['\n" . $herecurr);
  1227. }
  1228. if ($line =~ /\s\]/) {
  1229. ERROR("no space before that close square bracket ']'\n" . $herecurr);
  1230. }
  1231. # check spacing on paretheses
  1232. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1233. $line !~ /for\s*\(\s+;/) {
  1234. ERROR("no space after that open parenthesis '('\n" . $herecurr);
  1235. }
  1236. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  1237. $line !~ /for\s*\(.*;\s+\)/) {
  1238. ERROR("no space before that close parenthesis ')'\n" . $herecurr);
  1239. }
  1240. #goto labels aren't indented, allow a single space however
  1241. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1242. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1243. WARN("labels should not be indented\n" . $herecurr);
  1244. }
  1245. # Need a space before open parenthesis after if, while etc
  1246. if ($line=~/\b(if|while|for|switch)\(/) {
  1247. ERROR("need a space before the open parenthesis '('\n" . $herecurr);
  1248. }
  1249. # Check for illegal assignment in if conditional.
  1250. if ($line =~ /\bif\s*\(/) {
  1251. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1252. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
  1253. ERROR("do not use assignment in if condition\n" . $herecurr);
  1254. }
  1255. # Find out what is on the end of the line after the
  1256. # conditional.
  1257. substr($s, 0, length($c)) = '';
  1258. $s =~ s/\n.*//g;
  1259. $s =~ s/$;//g; # Remove any comments
  1260. if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
  1261. ERROR("trailing statements should be on next line\n" . $herecurr);
  1262. }
  1263. }
  1264. # Check for bitwise tests written as boolean
  1265. if ($line =~ /
  1266. (?:
  1267. (?:\[|\(|\&\&|\|\|)
  1268. \s*0[xX][0-9]+\s*
  1269. (?:\&\&|\|\|)
  1270. |
  1271. (?:\&\&|\|\|)
  1272. \s*0[xX][0-9]+\s*
  1273. (?:\&\&|\|\||\)|\])
  1274. )/x)
  1275. {
  1276. WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  1277. }
  1278. # if and else should not have general statements after it
  1279. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  1280. my $s = $1;
  1281. $s =~ s/$;//g; # Remove any comments
  1282. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  1283. ERROR("trailing statements should be on next line\n" . $herecurr);
  1284. }
  1285. }
  1286. # Check for }<nl>else {, these must be at the same
  1287. # indent level to be relevant to each other.
  1288. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1289. $previndent == $indent) {
  1290. ERROR("else should follow close brace '}'\n" . $hereprev);
  1291. }
  1292. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  1293. $previndent == $indent) {
  1294. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1295. # Find out what is on the end of the line after the
  1296. # conditional.
  1297. substr($s, 0, length($c)) = '';
  1298. $s =~ s/\n.*//g;
  1299. if ($s =~ /^\s*;/) {
  1300. ERROR("while should follow close brace '}'\n" . $hereprev);
  1301. }
  1302. }
  1303. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1304. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1305. # print "No studly caps, use _\n";
  1306. # print "$herecurr";
  1307. # $clean = 0;
  1308. # }
  1309. #no spaces allowed after \ in define
  1310. if ($line=~/\#define.*\\\s$/) {
  1311. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1312. }
  1313. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1314. if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1315. my $checkfile = "$root/include/linux/$1.h";
  1316. if (-f $checkfile && $1 ne 'irq.h') {
  1317. CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
  1318. $herecurr);
  1319. }
  1320. }
  1321. # multi-statement macros should be enclosed in a do while loop, grab the
  1322. # first statement and ensure its the whole macro if its not enclosed
  1323. # in a known goot container
  1324. if ($prevline =~ /\#define.*\\/ &&
  1325. $prevline !~/(?:do\s+{|\(\{|\{)/ &&
  1326. $line !~ /(?:do\s+{|\(\{|\{)/ &&
  1327. $line !~ /^.\s*$Declare\s/) {
  1328. # Grab the first statement, if that is the entire macro
  1329. # its ok. This may start either on the #define line
  1330. # or the one below.
  1331. my $ln = $linenr;
  1332. my $cnt = $realcnt;
  1333. my $off = 0;
  1334. # If the macro starts on the define line start
  1335. # grabbing the statement after the identifier
  1336. $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
  1337. ##print "1<$1> 2<$2>\n";
  1338. if (defined $2 && $2 ne '') {
  1339. $off = length($1);
  1340. $ln--;
  1341. $cnt++;
  1342. while ($lines[$ln - 1] =~ /^-/) {
  1343. $ln--;
  1344. $cnt++;
  1345. }
  1346. }
  1347. my @ctx = ctx_statement($ln, $cnt, $off);
  1348. my $ctx_ln = $ln + $#ctx + 1;
  1349. my $ctx = join("\n", @ctx);
  1350. # Pull in any empty extension lines.
  1351. while ($ctx =~ /\\$/ &&
  1352. $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
  1353. $ctx .= $lines[$ctx_ln - 1];
  1354. $ctx_ln++;
  1355. }
  1356. if ($ctx =~ /\\$/) {
  1357. if ($ctx =~ /;/) {
  1358. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  1359. } else {
  1360. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  1361. }
  1362. }
  1363. }
  1364. # check for redundant bracing round if etc
  1365. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  1366. my ($level, $endln, @chunks) =
  1367. ctx_statement_full($linenr, $realcnt, 0);
  1368. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  1369. if ($#chunks > 1 && $level == 0) {
  1370. my $allowed = 0;
  1371. my $seen = 0;
  1372. for my $chunk (@chunks) {
  1373. my ($cond, $block) = @{$chunk};
  1374. substr($block, 0, length($cond)) = '';
  1375. $seen++ if ($block =~ /^\s*{/);
  1376. $block =~ s/(^|\n)./$1/g;
  1377. $block =~ s/^\s*{//;
  1378. $block =~ s/}\s*$//;
  1379. $block =~ s/^\s*//;
  1380. $block =~ s/\s*$//;
  1381. my @lines = ($block =~ /\n/g);
  1382. my @statements = ($block =~ /;/g);
  1383. #print "cond<$cond> block<$block> lines<" . scalar(@lines) . "> statements<" . scalar(@statements) . "> seen<$seen> allowed<$allowed>\n";
  1384. if (scalar(@lines) != 0) {
  1385. $allowed = 1;
  1386. }
  1387. if ($block =~/\b(?:if|for|while)\b/) {
  1388. $allowed = 1;
  1389. }
  1390. if (scalar(@statements) > 1) {
  1391. $allowed = 1;
  1392. }
  1393. }
  1394. if ($seen && !$allowed) {
  1395. WARN("braces {} are not necessary for any arm of this statement\n" . $herecurr);
  1396. $suppress_ifbraces = $endln;
  1397. }
  1398. }
  1399. }
  1400. if ($linenr > $suppress_ifbraces &&
  1401. $line =~ /\b(if|while|for|else)\b/) {
  1402. # Locate the end of the opening statement.
  1403. my @control = ctx_statement($linenr, $realcnt, 0);
  1404. my $nr = $linenr + (scalar(@control) - 1);
  1405. my $cnt = $realcnt - (scalar(@control) - 1);
  1406. my $off = $realcnt - $cnt;
  1407. #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
  1408. # If this is is a braced statement group check it
  1409. if ($lines[$nr - 1] =~ /{\s*$/) {
  1410. my ($lvl, @block) = ctx_block_level($nr, $cnt);
  1411. my $stmt = join("\n", @block);
  1412. # Drop the diff line leader.
  1413. $stmt =~ s/\n./\n/g;
  1414. # Drop the code outside the block.
  1415. $stmt =~ s/(^[^{]*){\s*//;
  1416. my $before = $1;
  1417. $stmt =~ s/\s*}([^}]*$)//;
  1418. my $after = $1;
  1419. #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
  1420. #print "before<$before> stmt<$stmt> after<$after>\n\n";
  1421. # Count the newlines, if there is only one
  1422. # then the block should not have {}'s.
  1423. my @lines = ($stmt =~ /\n/g);
  1424. my @statements = ($stmt =~ /;/g);
  1425. #print "lines<" . scalar(@lines) . ">\n";
  1426. #print "statements<" . scalar(@statements) . ">\n";
  1427. if ($lvl == 0 && scalar(@lines) == 0 &&
  1428. scalar(@statements) < 2 &&
  1429. $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
  1430. $before !~ /}/ && $after !~ /{/) {
  1431. my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
  1432. shift(@block);
  1433. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  1434. }
  1435. }
  1436. }
  1437. # don't include deprecated include files (uses RAW line)
  1438. for my $inc (@dep_includes) {
  1439. if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
  1440. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1441. }
  1442. }
  1443. # don't use deprecated functions
  1444. for my $func (@dep_functions) {
  1445. if ($line =~ /\b$func\b/) {
  1446. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1447. }
  1448. }
  1449. # no volatiles please
  1450. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  1451. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  1452. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1453. }
  1454. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1455. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1456. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1457. }
  1458. # warn about #if 0
  1459. if ($line =~ /^.#\s*if\s+0\b/) {
  1460. CHK("if this code is redundant consider removing it\n" .
  1461. $herecurr);
  1462. }
  1463. # check for needless kfree() checks
  1464. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1465. my $expr = $1;
  1466. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1467. WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
  1468. }
  1469. }
  1470. # warn about #ifdefs in C files
  1471. # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1472. # print "#ifdef in C files should be avoided\n";
  1473. # print "$herecurr";
  1474. # $clean = 0;
  1475. # }
  1476. # warn about spacing in #ifdefs
  1477. if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
  1478. ERROR("exactly one space required after that #$1\n" . $herecurr);
  1479. }
  1480. # check for spinlock_t definitions without a comment.
  1481. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
  1482. my $which = $1;
  1483. if (!ctx_has_comment($first_line, $linenr)) {
  1484. CHK("$1 definition without comment\n" . $herecurr);
  1485. }
  1486. }
  1487. # check for memory barriers without a comment.
  1488. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  1489. if (!ctx_has_comment($first_line, $linenr)) {
  1490. CHK("memory barrier without comment\n" . $herecurr);
  1491. }
  1492. }
  1493. # check of hardware specific defines
  1494. if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  1495. CHK("architecture specific defines should be avoided\n" . $herecurr);
  1496. }
  1497. # check the location of the inline attribute, that it is between
  1498. # storage class and type.
  1499. if ($line =~ /\b$Type\s+$Inline\b/ ||
  1500. $line =~ /\b$Inline\s+$Storage\b/) {
  1501. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  1502. }
  1503. # Check for __inline__ and __inline, prefer inline
  1504. if ($line =~ /\b(__inline__|__inline)\b/) {
  1505. WARN("plain inline is preferred over $1\n" . $herecurr);
  1506. }
  1507. # check for new externs in .c files.
  1508. if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
  1509. WARN("externs should be avoided in .c files\n" . $herecurr);
  1510. }
  1511. # checks for new __setup's
  1512. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  1513. my $name = $1;
  1514. if (!grep(/$name/, @setup_docs)) {
  1515. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  1516. }
  1517. }
  1518. # check for pointless casting of kmalloc return
  1519. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  1520. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  1521. }
  1522. # check for gcc specific __FUNCTION__
  1523. if ($line =~ /__FUNCTION__/) {
  1524. WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  1525. }
  1526. }
  1527. # If we have no input at all, then there is nothing to report on
  1528. # so just keep quiet.
  1529. if ($#rawlines == -1) {
  1530. exit(0);
  1531. }
  1532. # In mailback mode only produce a report in the negative, for
  1533. # things that appear to be patches.
  1534. if ($mailback && ($clean == 1 || !$is_patch)) {
  1535. exit(0);
  1536. }
  1537. # This is not a patch, and we are are in 'no-patch' mode so
  1538. # just keep quiet.
  1539. if (!$chk_patch && !$is_patch) {
  1540. exit(0);
  1541. }
  1542. if (!$is_patch) {
  1543. ERROR("Does not appear to be a unified-diff format patch\n");
  1544. }
  1545. if ($is_patch && $chk_signoff && $signoff == 0) {
  1546. ERROR("Missing Signed-off-by: line(s)\n");
  1547. }
  1548. print report_dump();
  1549. if ($summary && !($clean == 1 && $quiet == 1)) {
  1550. print "$filename " if ($summary_file);
  1551. print "total: $cnt_error errors, $cnt_warn warnings, " .
  1552. (($check)? "$cnt_chk checks, " : "") .
  1553. "$cnt_lines lines checked\n";
  1554. print "\n" if ($quiet == 0);
  1555. }
  1556. if ($clean == 1 && $quiet == 0) {
  1557. print "$vname has no obvious style problems and is ready for submission.\n"
  1558. }
  1559. if ($clean == 0 && $quiet == 0) {
  1560. print "$vname has style problems, please review. If any of these errors\n";
  1561. print "are false positives report them to the maintainer, see\n";
  1562. print "CHECKPATCH in MAINTAINERS.\n";
  1563. }
  1564. print <<EOL if ($file == 1 && $quiet == 0);
  1565. WARNING: Using --file mode. Please do not send patches to linux-kernel
  1566. that change whole existing files if you did not significantly change most
  1567. of the the file for other reasons anyways or just wrote the file newly
  1568. from scratch. Pure code style patches have a significant cost in a
  1569. quickly changing code base like Linux because they cause rejects
  1570. with other changes.
  1571. EOL
  1572. return $clean;
  1573. }