checkpatch.pl 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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.11';
  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 $file = 0;
  18. my $check = 0;
  19. my $root;
  20. GetOptions(
  21. 'q|quiet+' => \$quiet,
  22. 'tree!' => \$tree,
  23. 'signoff!' => \$chk_signoff,
  24. 'patch!' => \$chk_patch,
  25. 'test-type!' => \$tst_type,
  26. 'emacs!' => \$emacs,
  27. 'file!' => \$file,
  28. 'subjective!' => \$check,
  29. 'strict!' => \$check,
  30. 'root=s' => \$root,
  31. ) or exit;
  32. my $exit = 0;
  33. if ($#ARGV < 0) {
  34. print "usage: $P [options] patchfile\n";
  35. print "version: $V\n";
  36. print "options: -q => quiet\n";
  37. print " --no-tree => run without a kernel tree\n";
  38. print " --emacs => emacs compile window format\n";
  39. print " --file => check a source file\n";
  40. print " --strict => enable more subjective tests\n";
  41. print " --root => path to the kernel tree root\n";
  42. exit(1);
  43. }
  44. if ($tree) {
  45. if (defined $root) {
  46. if (!top_of_kernel_tree($root)) {
  47. die "$P: $root: --root does not point at a valid tree\n";
  48. }
  49. } else {
  50. if (top_of_kernel_tree('.')) {
  51. $root = '.';
  52. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  53. top_of_kernel_tree($1)) {
  54. $root = $1;
  55. }
  56. }
  57. if (!defined $root) {
  58. print "Must be run from the top-level dir. of a kernel tree\n";
  59. exit(2);
  60. }
  61. }
  62. my $emitted_corrupt = 0;
  63. our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
  64. our $Storage = qr{extern|static|asmlinkage};
  65. our $Sparse = qr{
  66. __user|
  67. __kernel|
  68. __force|
  69. __iomem|
  70. __must_check|
  71. __init_refok|
  72. __kprobes|
  73. fastcall
  74. }x;
  75. our $Attribute = qr{
  76. const|
  77. __read_mostly|
  78. __kprobes|
  79. __(?:mem|cpu|dev|)(?:initdata|init)
  80. }x;
  81. our $Inline = qr{inline|__always_inline|noinline};
  82. our $NonptrType = qr{
  83. \b
  84. (?:const\s+)?
  85. (?:unsigned\s+)?
  86. (?:
  87. void|
  88. char|
  89. short|
  90. int|
  91. long|
  92. unsigned|
  93. float|
  94. double|
  95. bool|
  96. long\s+int|
  97. long\s+long|
  98. long\s+long\s+int|
  99. (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
  100. struct\s+$Ident|
  101. union\s+$Ident|
  102. enum\s+$Ident|
  103. ${Ident}_t|
  104. ${Ident}_handler|
  105. ${Ident}_handler_fn
  106. )
  107. (?:\s+$Sparse)*
  108. \b
  109. }x;
  110. our $Type = qr{
  111. \b$NonptrType\b
  112. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  113. (?:\s+$Sparse|\s+$Attribute)*
  114. }x;
  115. our $Declare = qr{(?:$Storage\s+)?$Type};
  116. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  117. our $Lval = qr{$Ident(?:$Member)*};
  118. our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
  119. our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
  120. our $Operators = qr{
  121. <=|>=|==|!=|
  122. =>|->|<<|>>|<|>|!|~|
  123. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/
  124. }x;
  125. our $Bare = '';
  126. $chk_signoff = 0 if ($file);
  127. my @dep_includes = ();
  128. my @dep_functions = ();
  129. my $removal = "Documentation/feature-removal-schedule.txt";
  130. if ($tree && -f "$root/$removal") {
  131. open(REMOVE, "<$root/$removal") ||
  132. die "$P: $removal: open failed - $!\n";
  133. while (<REMOVE>) {
  134. if (/^Check:\s+(.*\S)/) {
  135. for my $entry (split(/[, ]+/, $1)) {
  136. if ($entry =~ m@include/(.*)@) {
  137. push(@dep_includes, $1);
  138. } elsif ($entry !~ m@/@) {
  139. push(@dep_functions, $entry);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. my @rawlines = ();
  146. for my $filename (@ARGV) {
  147. if ($file) {
  148. open(FILE, "diff -u /dev/null $filename|") ||
  149. die "$P: $filename: diff failed - $!\n";
  150. } else {
  151. open(FILE, "<$filename") ||
  152. die "$P: $filename: open failed - $!\n";
  153. }
  154. while (<FILE>) {
  155. chomp;
  156. push(@rawlines, $_);
  157. }
  158. close(FILE);
  159. if (!process($filename, @rawlines)) {
  160. $exit = 1;
  161. }
  162. @rawlines = ();
  163. }
  164. exit($exit);
  165. sub top_of_kernel_tree {
  166. my ($root) = @_;
  167. my @tree_check = (
  168. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  169. "README", "Documentation", "arch", "include", "drivers",
  170. "fs", "init", "ipc", "kernel", "lib", "scripts",
  171. );
  172. foreach my $check (@tree_check) {
  173. if (! -e $root . '/' . $check) {
  174. return 0;
  175. }
  176. }
  177. return 1;
  178. }
  179. sub expand_tabs {
  180. my ($str) = @_;
  181. my $res = '';
  182. my $n = 0;
  183. for my $c (split(//, $str)) {
  184. if ($c eq "\t") {
  185. $res .= ' ';
  186. $n++;
  187. for (; ($n % 8) != 0; $n++) {
  188. $res .= ' ';
  189. }
  190. next;
  191. }
  192. $res .= $c;
  193. $n++;
  194. }
  195. return $res;
  196. }
  197. sub copy_spacing {
  198. my ($str) = @_;
  199. my $res = '';
  200. for my $c (split(//, $str)) {
  201. if ($c eq "\t") {
  202. $res .= $c;
  203. } else {
  204. $res .= ' ';
  205. }
  206. }
  207. return $res;
  208. }
  209. sub line_stats {
  210. my ($line) = @_;
  211. # Drop the diff line leader and expand tabs
  212. $line =~ s/^.//;
  213. $line = expand_tabs($line);
  214. # Pick the indent from the front of the line.
  215. my ($white) = ($line =~ /^(\s*)/);
  216. return (length($line), length($white));
  217. }
  218. sub sanitise_line {
  219. my ($line) = @_;
  220. my $res = '';
  221. my $l = '';
  222. my $quote = '';
  223. foreach my $c (split(//, $line)) {
  224. if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
  225. if ($quote eq '') {
  226. $quote = $c;
  227. $res .= $c;
  228. $l = $c;
  229. next;
  230. } elsif ($quote eq $c) {
  231. $quote = '';
  232. }
  233. }
  234. if ($quote && $c ne "\t") {
  235. $res .= "X";
  236. } else {
  237. $res .= $c;
  238. }
  239. $l = $c;
  240. }
  241. return $res;
  242. }
  243. sub ctx_block_get {
  244. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  245. my $line;
  246. my $start = $linenr - 1;
  247. my $blk = '';
  248. my @o;
  249. my @c;
  250. my @res = ();
  251. my $level = 0;
  252. for ($line = $start; $remain > 0; $line++) {
  253. next if ($rawlines[$line] =~ /^-/);
  254. $remain--;
  255. $blk .= $rawlines[$line];
  256. foreach my $c (split(//, $rawlines[$line])) {
  257. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  258. if ($off > 0) {
  259. $off--;
  260. next;
  261. }
  262. if ($c eq $close && $level > 0) {
  263. $level--;
  264. last if ($level == 0);
  265. } elsif ($c eq $open) {
  266. $level++;
  267. }
  268. }
  269. if (!$outer || $level <= 1) {
  270. push(@res, $rawlines[$line]);
  271. }
  272. last if ($level == 0);
  273. }
  274. return ($level, @res);
  275. }
  276. sub ctx_block_outer {
  277. my ($linenr, $remain) = @_;
  278. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  279. return @r;
  280. }
  281. sub ctx_block {
  282. my ($linenr, $remain) = @_;
  283. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  284. return @r;
  285. }
  286. sub ctx_statement {
  287. my ($linenr, $remain, $off) = @_;
  288. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  289. return @r;
  290. }
  291. sub ctx_block_level {
  292. my ($linenr, $remain) = @_;
  293. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  294. }
  295. sub ctx_statement_level {
  296. my ($linenr, $remain, $off) = @_;
  297. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  298. }
  299. sub ctx_locate_comment {
  300. my ($first_line, $end_line) = @_;
  301. # Catch a comment on the end of the line itself.
  302. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
  303. return $current_comment if (defined $current_comment);
  304. # Look through the context and try and figure out if there is a
  305. # comment.
  306. my $in_comment = 0;
  307. $current_comment = '';
  308. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  309. my $line = $rawlines[$linenr - 1];
  310. #warn " $line\n";
  311. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  312. $in_comment = 1;
  313. }
  314. if ($line =~ m@/\*@) {
  315. $in_comment = 1;
  316. }
  317. if (!$in_comment && $current_comment ne '') {
  318. $current_comment = '';
  319. }
  320. $current_comment .= $line . "\n" if ($in_comment);
  321. if ($line =~ m@\*/@) {
  322. $in_comment = 0;
  323. }
  324. }
  325. chomp($current_comment);
  326. return($current_comment);
  327. }
  328. sub ctx_has_comment {
  329. my ($first_line, $end_line) = @_;
  330. my $cmt = ctx_locate_comment($first_line, $end_line);
  331. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  332. ##print "CMMT: $cmt\n";
  333. return ($cmt ne '');
  334. }
  335. sub cat_vet {
  336. my ($vet) = @_;
  337. my ($res, $coded);
  338. $res = '';
  339. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  340. $res .= $1;
  341. if ($2 ne '') {
  342. $coded = sprintf("^%c", unpack('C', $2) + 64);
  343. $res .= $coded;
  344. }
  345. }
  346. $res =~ s/$/\$/;
  347. return $res;
  348. }
  349. sub annotate_values {
  350. my ($stream, $type) = @_;
  351. my $res;
  352. my $cur = $stream;
  353. my $debug = 0;
  354. print "$stream\n" if ($debug);
  355. ##my $type = 'N';
  356. my $pos = 0;
  357. my $preprocessor = 0;
  358. my $paren = 0;
  359. my @paren_type;
  360. # Include any user defined types we may have found as we went.
  361. my $type_match = "(?:$Type$Bare)";
  362. while (length($cur)) {
  363. print " <$type> " if ($debug);
  364. if ($cur =~ /^(\s+)/o) {
  365. print "WS($1)\n" if ($debug);
  366. if ($1 =~ /\n/ && $preprocessor) {
  367. $preprocessor = 0;
  368. $type = 'N';
  369. }
  370. } elsif ($cur =~ /^($type_match)/) {
  371. print "DECLARE($1)\n" if ($debug);
  372. $type = 'T';
  373. } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
  374. print "DEFINE($1)\n" if ($debug);
  375. $preprocessor = 1;
  376. $paren_type[$paren] = 'N';
  377. } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|endif))/o) {
  378. print "PRE($1)\n" if ($debug);
  379. $preprocessor = 1;
  380. $type = 'N';
  381. } elsif ($cur =~ /^(\\\n)/o) {
  382. print "PRECONT($1)\n" if ($debug);
  383. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  384. print "SIZEOF($1)\n" if ($debug);
  385. if (defined $2) {
  386. $paren_type[$paren] = 'V';
  387. }
  388. $type = 'N';
  389. } elsif ($cur =~ /^(if|while|typeof)\b/o) {
  390. print "COND($1)\n" if ($debug);
  391. $paren_type[$paren] = 'N';
  392. $type = 'N';
  393. } elsif ($cur =~/^(return|case|else)/o) {
  394. print "KEYWORD($1)\n" if ($debug);
  395. $type = 'N';
  396. } elsif ($cur =~ /^(\()/o) {
  397. print "PAREN('$1')\n" if ($debug);
  398. $paren++;
  399. $type = 'N';
  400. } elsif ($cur =~ /^(\))/o) {
  401. $paren-- if ($paren > 0);
  402. if (defined $paren_type[$paren]) {
  403. $type = $paren_type[$paren];
  404. undef $paren_type[$paren];
  405. print "PAREN('$1') -> $type\n" if ($debug);
  406. } else {
  407. print "PAREN('$1')\n" if ($debug);
  408. }
  409. } elsif ($cur =~ /^($Ident)\(/o) {
  410. print "FUNC($1)\n" if ($debug);
  411. $paren_type[$paren] = 'V';
  412. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  413. print "IDENT($1)\n" if ($debug);
  414. $type = 'V';
  415. } elsif ($cur =~ /^($Assignment)/o) {
  416. print "ASSIGN($1)\n" if ($debug);
  417. $type = 'N';
  418. } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) {
  419. print "END($1)\n" if ($debug);
  420. $type = 'N';
  421. } elsif ($cur =~ /^($Operators)/o) {
  422. print "OP($1)\n" if ($debug);
  423. if ($1 ne '++' && $1 ne '--') {
  424. $type = 'N';
  425. }
  426. } elsif ($cur =~ /(^.)/o) {
  427. print "C($1)\n" if ($debug);
  428. }
  429. if (defined $1) {
  430. $cur = substr($cur, length($1));
  431. $res .= $type x length($1);
  432. }
  433. }
  434. return $res;
  435. }
  436. my $prefix = '';
  437. my @report = ();
  438. sub report {
  439. push(@report, $prefix . $_[0]);
  440. }
  441. sub report_dump {
  442. @report;
  443. }
  444. sub ERROR {
  445. report("ERROR: $_[0]\n");
  446. our $clean = 0;
  447. our $cnt_error++;
  448. }
  449. sub WARN {
  450. report("WARNING: $_[0]\n");
  451. our $clean = 0;
  452. our $cnt_warn++;
  453. }
  454. sub CHK {
  455. if ($check) {
  456. report("CHECK: $_[0]\n");
  457. our $clean = 0;
  458. our $cnt_chk++;
  459. }
  460. }
  461. sub process {
  462. my $filename = shift;
  463. my @lines = @_;
  464. my $linenr=0;
  465. my $prevline="";
  466. my $stashline="";
  467. my $length;
  468. my $indent;
  469. my $previndent=0;
  470. my $stashindent=0;
  471. our $clean = 1;
  472. my $signoff = 0;
  473. my $is_patch = 0;
  474. our $cnt_lines = 0;
  475. our $cnt_error = 0;
  476. our $cnt_warn = 0;
  477. our $cnt_chk = 0;
  478. # Trace the real file/line as we go.
  479. my $realfile = '';
  480. my $realline = 0;
  481. my $realcnt = 0;
  482. my $here = '';
  483. my $in_comment = 0;
  484. my $first_line = 0;
  485. my $prev_values = 'N';
  486. # Possible bare types.
  487. my @bare = ();
  488. # Pre-scan the patch looking for any __setup documentation.
  489. my @setup_docs = ();
  490. my $setup_docs = 0;
  491. foreach my $line (@lines) {
  492. if ($line=~/^\+\+\+\s+(\S+)/) {
  493. $setup_docs = 0;
  494. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  495. $setup_docs = 1;
  496. }
  497. next;
  498. }
  499. if ($setup_docs && $line =~ /^\+/) {
  500. push(@setup_docs, $line);
  501. }
  502. }
  503. $prefix = '';
  504. foreach my $line (@lines) {
  505. $linenr++;
  506. my $rawline = $line;
  507. #extract the filename as it passes
  508. if ($line=~/^\+\+\+\s+(\S+)/) {
  509. $realfile=$1;
  510. $realfile =~ s@^[^/]*/@@;
  511. $in_comment = 0;
  512. next;
  513. }
  514. #extract the line range in the file after the patch is applied
  515. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  516. $is_patch = 1;
  517. $first_line = $linenr + 1;
  518. $in_comment = 0;
  519. $realline=$1-1;
  520. if (defined $2) {
  521. $realcnt=$3+1;
  522. } else {
  523. $realcnt=1+1;
  524. }
  525. $prev_values = 'N';
  526. next;
  527. }
  528. # track the line number as we move through the hunk, note that
  529. # new versions of GNU diff omit the leading space on completely
  530. # blank context lines so we need to count that too.
  531. if ($line =~ /^( |\+|$)/) {
  532. $realline++;
  533. $realcnt-- if ($realcnt != 0);
  534. # track any sort of multi-line comment. Obviously if
  535. # the added text or context do not include the whole
  536. # comment we will not see it. Such is life.
  537. #
  538. # Guestimate if this is a continuing comment. If this
  539. # is the start of a diff block and this line starts
  540. # ' *' then it is very likely a comment.
  541. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  542. $in_comment = 1;
  543. }
  544. if ($line =~ m@/\*@) {
  545. $in_comment = 1;
  546. }
  547. if ($line =~ m@\*/@) {
  548. $in_comment = 0;
  549. }
  550. # Measure the line length and indent.
  551. ($length, $indent) = line_stats($line);
  552. # Track the previous line.
  553. ($prevline, $stashline) = ($stashline, $line);
  554. ($previndent, $stashindent) = ($stashindent, $indent);
  555. } elsif ($realcnt == 1) {
  556. $realcnt--;
  557. }
  558. #make up the handle for any error we report on this line
  559. $here = "#$linenr: " if (!$file);
  560. $here = "#$realline: " if ($file);
  561. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  562. my $hereline = "$here\n$line\n";
  563. my $herecurr = "$here\n$line\n";
  564. my $hereprev = "$here\n$prevline\n$line\n";
  565. $prefix = "$filename:$realline: " if ($emacs && $file);
  566. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  567. $cnt_lines++ if ($realcnt != 0);
  568. #check the patch for a signoff:
  569. if ($line =~ /^\s*signed-off-by:/i) {
  570. # This is a signoff, if ugly, so do not double report.
  571. $signoff++;
  572. if (!($line =~ /^\s*Signed-off-by:/)) {
  573. WARN("Signed-off-by: is the preferred form\n" .
  574. $herecurr);
  575. }
  576. if ($line =~ /^\s*signed-off-by:\S/i) {
  577. WARN("need space after Signed-off-by:\n" .
  578. $herecurr);
  579. }
  580. }
  581. # Check for wrappage within a valid hunk of the file
  582. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
  583. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  584. $herecurr) if (!$emitted_corrupt++);
  585. }
  586. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  587. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  588. !($line =~ m/^(
  589. [\x09\x0A\x0D\x20-\x7E] # ASCII
  590. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  591. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  592. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  593. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  594. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  595. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  596. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  597. )*$/x )) {
  598. ERROR("Invalid UTF-8\n" . $herecurr);
  599. }
  600. #ignore lines being removed
  601. if ($line=~/^-/) {next;}
  602. # check we are in a valid source file if not then ignore this hunk
  603. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  604. #trailing whitespace
  605. if ($line =~ /^\+.*\015/) {
  606. my $herevet = "$here\n" . cat_vet($line) . "\n";
  607. ERROR("DOS line endings\n" . $herevet);
  608. } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
  609. my $herevet = "$here\n" . cat_vet($line) . "\n";
  610. ERROR("trailing whitespace\n" . $herevet);
  611. }
  612. #80 column limit
  613. if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
  614. WARN("line over 80 characters\n" . $herecurr);
  615. }
  616. # check we are in a valid source file *.[hc] if not then ignore this hunk
  617. next if ($realfile !~ /\.[hc]$/);
  618. # at the beginning of a line any tabs must come first and anything
  619. # more than 8 must use tabs.
  620. if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
  621. my $herevet = "$here\n" . cat_vet($line) . "\n";
  622. ERROR("use tabs not spaces\n" . $herevet);
  623. }
  624. # Remove comments from the line before processing.
  625. my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
  626. ($line =~ s@/\*.*@@) +
  627. ($line =~ s@^(.).*\*/@$1@);
  628. # The rest of our checks refer specifically to C style
  629. # only apply those _outside_ comments. Only skip
  630. # lines in the middle of comments.
  631. next if (!$comment_edge && $in_comment);
  632. # Standardise the strings and chars within the input to simplify matching.
  633. $line = sanitise_line($line);
  634. # Check for potential 'bare' types
  635. if ($realcnt &&
  636. $line !~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?$Type\b/ &&
  637. $line !~ /$Ident:\s*$/ &&
  638. $line !~ /^.\s*$Ident\s*\(/ &&
  639. # definitions in global scope can only start with types
  640. ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?($Ident)\b/ ||
  641. # declarations always start with types
  642. $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/) ||
  643. # any (foo ... *) is a pointer cast, and foo is a type
  644. $line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/) {
  645. my $possible = $1;
  646. if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
  647. $possible ne 'goto' && $possible ne 'return' &&
  648. $possible ne 'struct' && $possible ne 'enum' &&
  649. $possible ne 'case' && $possible ne 'else' &&
  650. $possible ne 'typedef') {
  651. #print "POSSIBLE<$possible>\n";
  652. push(@bare, $possible);
  653. my $bare = join("|", @bare);
  654. $Bare = '|' . qr{
  655. \b(?:$bare)\b
  656. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  657. (?:\s+$Sparse)*
  658. }x;
  659. }
  660. }
  661. #
  662. # Checks which may be anchored in the context.
  663. #
  664. # Check for switch () and associated case and default
  665. # statements should be at the same indent.
  666. if ($line=~/\bswitch\s*\(.*\)/) {
  667. my $err = '';
  668. my $sep = '';
  669. my @ctx = ctx_block_outer($linenr, $realcnt);
  670. shift(@ctx);
  671. for my $ctx (@ctx) {
  672. my ($clen, $cindent) = line_stats($ctx);
  673. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  674. $indent != $cindent) {
  675. $err .= "$sep$ctx\n";
  676. $sep = '';
  677. } else {
  678. $sep = "[...]\n";
  679. }
  680. }
  681. if ($err ne '') {
  682. ERROR("switch and case should be at the same indent\n$hereline$err");
  683. }
  684. }
  685. # if/while/etc brace do not go on next line, unless defining a do while loop,
  686. # or if that brace on the next line is for something else
  687. if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
  688. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  689. my $ctx_ln = $linenr + $#ctx + 1;
  690. my $ctx_cnt = $realcnt - $#ctx - 1;
  691. my $ctx = join("\n", @ctx);
  692. # Skip over any removed lines in the context following statement.
  693. while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
  694. $ctx_ln++;
  695. $ctx_cnt--;
  696. }
  697. ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
  698. if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  699. ERROR("That open brace { should be on the previous line\n" .
  700. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  701. }
  702. if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
  703. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  704. if ($nindent > $indent) {
  705. WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
  706. "$here\n$ctx\n$lines[$ctx_ln - 1]");
  707. }
  708. }
  709. }
  710. # Track the 'values' across context and added lines.
  711. my $opline = $line; $opline =~ s/^./ /;
  712. my $curr_values = annotate_values($opline . "\n", $prev_values);
  713. $curr_values = $prev_values . $curr_values;
  714. #warn "--> $opline\n";
  715. #warn "--> $curr_values ($prev_values)\n";
  716. $prev_values = substr($curr_values, -1);
  717. #ignore lines not being added
  718. if ($line=~/^[^\+]/) {next;}
  719. # TEST: allow direct testing of the type matcher.
  720. if ($tst_type && $line =~ /^.$Declare$/) {
  721. ERROR("TEST: is type $Declare\n" . $herecurr);
  722. next;
  723. }
  724. # check for initialisation to aggregates open brace on the next line
  725. if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
  726. $line =~ /^.\s*{/) {
  727. ERROR("That open brace { should be on the previous line\n" . $hereprev);
  728. }
  729. #
  730. # Checks which are anchored on the added line.
  731. #
  732. # check for malformed paths in #include statements (uses RAW line)
  733. if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
  734. my $path = $1;
  735. if ($path =~ m{//}) {
  736. ERROR("malformed #include filename\n" .
  737. $herecurr);
  738. }
  739. # Sanitise this special form of string.
  740. $path = 'X' x length($path);
  741. $line =~ s{\<.*\>}{<$path>};
  742. }
  743. # no C99 // comments
  744. if ($line =~ m{//}) {
  745. ERROR("do not use C99 // comments\n" . $herecurr);
  746. }
  747. # Remove C99 comments.
  748. $line =~ s@//.*@@;
  749. $opline =~ s@//.*@@;
  750. #EXPORT_SYMBOL should immediately follow its function closing }.
  751. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  752. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  753. my $name = $1;
  754. if (($prevline !~ /^}/) &&
  755. ($prevline !~ /^\+}/) &&
  756. ($prevline !~ /^ }/) &&
  757. ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
  758. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  759. }
  760. }
  761. # check for external initialisers.
  762. if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
  763. ERROR("do not initialise externals to 0 or NULL\n" .
  764. $herecurr);
  765. }
  766. # check for static initialisers.
  767. if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
  768. ERROR("do not initialise statics to 0 or NULL\n" .
  769. $herecurr);
  770. }
  771. # check for new typedefs, only function parameters and sparse annotations
  772. # make sense.
  773. if ($line =~ /\btypedef\s/ &&
  774. $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
  775. $line !~ /\b__bitwise(?:__|)\b/) {
  776. WARN("do not add new typedefs\n" . $herecurr);
  777. }
  778. # * goes on variable not on type
  779. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  780. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  781. $herecurr);
  782. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  783. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  784. $herecurr);
  785. } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  786. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  787. $herecurr);
  788. } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  789. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  790. $herecurr);
  791. }
  792. # # no BUG() or BUG_ON()
  793. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  794. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  795. # print "$herecurr";
  796. # $clean = 0;
  797. # }
  798. # printk should use KERN_* levels. Note that follow on printk's on the
  799. # same line do not need a level, so we use the current block context
  800. # to try and find and validate the current printk. In summary the current
  801. # printk includes all preceeding printk's which have no newline on the end.
  802. # we assume the first bad printk is the one to report.
  803. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  804. my $ok = 0;
  805. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  806. #print "CHECK<$lines[$ln - 1]\n";
  807. # we have a preceeding printk if it ends
  808. # with "\n" ignore it, else it is to blame
  809. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  810. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  811. $ok = 1;
  812. }
  813. last;
  814. }
  815. }
  816. if ($ok == 0) {
  817. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  818. }
  819. }
  820. # function brace can't be on same line, except for #defines of do while,
  821. # or if closed on same line
  822. if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
  823. !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
  824. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  825. }
  826. # check for spaces between functions and their parentheses.
  827. while ($line =~ /($Ident)\s+\(/g) {
  828. if ($1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/ &&
  829. $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
  830. WARN("no space between function name and open parenthesis '('\n" . $herecurr);
  831. }
  832. }
  833. # Check operator spacing.
  834. if (!($line=~/\#\s*include/)) {
  835. my $ops = qr{
  836. <<=|>>=|<=|>=|==|!=|
  837. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  838. =>|->|<<|>>|<|>|=|!|~|
  839. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/
  840. }x;
  841. my @elements = split(/($ops|;)/, $opline);
  842. my $off = 0;
  843. my $blank = copy_spacing($opline);
  844. for (my $n = 0; $n < $#elements; $n += 2) {
  845. $off += length($elements[$n]);
  846. my $a = '';
  847. $a = 'V' if ($elements[$n] ne '');
  848. $a = 'W' if ($elements[$n] =~ /\s$/);
  849. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  850. $a = 'O' if ($elements[$n] eq '');
  851. $a = 'E' if ($elements[$n] eq '' && $n == 0);
  852. my $op = $elements[$n + 1];
  853. my $c = '';
  854. if (defined $elements[$n + 2]) {
  855. $c = 'V' if ($elements[$n + 2] ne '');
  856. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  857. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  858. $c = 'O' if ($elements[$n + 2] eq '');
  859. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  860. } else {
  861. $c = 'E';
  862. }
  863. # Pick up the preceeding and succeeding characters.
  864. my $ca = substr($opline, 0, $off);
  865. my $cc = '';
  866. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  867. $cc = substr($opline, $off + length($elements[$n + 1]));
  868. }
  869. my $cb = "$ca$;$cc";
  870. my $ctx = "${a}x${c}";
  871. my $at = "(ctx:$ctx)";
  872. my $ptr = substr($blank, 0, $off) . "^";
  873. my $hereptr = "$hereline$ptr\n";
  874. # Classify operators into binary, unary, or
  875. # definitions (* only) where they have more
  876. # than one mode.
  877. my $op_type = substr($curr_values, $off + 1, 1);
  878. my $op_left = substr($curr_values, $off, 1);
  879. my $is_unary;
  880. if ($op_type eq 'T') {
  881. $is_unary = 2;
  882. } elsif ($op_left eq 'V') {
  883. $is_unary = 0;
  884. } else {
  885. $is_unary = 1;
  886. }
  887. #if ($op eq '-' || $op eq '&' || $op eq '*') {
  888. # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
  889. #}
  890. # ; should have either the end of line or a space or \ after it
  891. if ($op eq ';') {
  892. if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
  893. $cc !~ /^;/) {
  894. ERROR("need space after that '$op' $at\n" . $hereptr);
  895. }
  896. # // is a comment
  897. } elsif ($op eq '//') {
  898. # -> should have no spaces
  899. } elsif ($op eq '->') {
  900. if ($ctx =~ /Wx.|.xW/) {
  901. ERROR("no spaces around that '$op' $at\n" . $hereptr);
  902. }
  903. # , must have a space on the right.
  904. } elsif ($op eq ',') {
  905. if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
  906. ERROR("need space after that '$op' $at\n" . $hereptr);
  907. }
  908. # '*' as part of a type definition -- reported already.
  909. } elsif ($op eq '*' && $is_unary == 2) {
  910. #warn "'*' is part of type\n";
  911. # unary operators should have a space before and
  912. # none after. May be left adjacent to another
  913. # unary operator, or a cast
  914. } elsif ($op eq '!' || $op eq '~' ||
  915. ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
  916. if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  917. ERROR("need space before that '$op' $at\n" . $hereptr);
  918. }
  919. if ($ctx =~ /.xW/) {
  920. ERROR("no space after that '$op' $at\n" . $hereptr);
  921. }
  922. # unary ++ and unary -- are allowed no space on one side.
  923. } elsif ($op eq '++' or $op eq '--') {
  924. if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
  925. ERROR("need space one side of that '$op' $at\n" . $hereptr);
  926. }
  927. if ($ctx =~ /Wx./ && $cc =~ /^;/) {
  928. ERROR("no space before that '$op' $at\n" . $hereptr);
  929. }
  930. # << and >> may either have or not have spaces both sides
  931. } elsif ($op eq '<<' or $op eq '>>' or
  932. $op eq '&' or $op eq '^' or $op eq '|' or
  933. $op eq '+' or $op eq '-' or
  934. $op eq '*' or $op eq '/')
  935. {
  936. if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
  937. ERROR("need consistent spacing around '$op' $at\n" .
  938. $hereptr);
  939. }
  940. # All the others need spaces both sides.
  941. } elsif ($ctx !~ /[EW]x[WE]/) {
  942. # Ignore email addresses <foo@bar>
  943. if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
  944. !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
  945. ERROR("need spaces around that '$op' $at\n" . $hereptr);
  946. }
  947. }
  948. $off += length($elements[$n + 1]);
  949. }
  950. }
  951. # check for multiple assignments
  952. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  953. CHK("multiple assignments should be avoided\n" . $herecurr);
  954. }
  955. ## # check for multiple declarations, allowing for a function declaration
  956. ## # continuation.
  957. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  958. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  959. ##
  960. ## # Remove any bracketed sections to ensure we do not
  961. ## # falsly report the parameters of functions.
  962. ## my $ln = $line;
  963. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  964. ## }
  965. ## if ($ln =~ /,/) {
  966. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  967. ## }
  968. ## }
  969. #need space before brace following if, while, etc
  970. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  971. $line =~ /do{/) {
  972. ERROR("need a space before the open brace '{'\n" . $herecurr);
  973. }
  974. # closing brace should have a space following it when it has anything
  975. # on the line
  976. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  977. ERROR("need a space after that close brace '}'\n" . $herecurr);
  978. }
  979. # check spacing on square brackets
  980. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  981. ERROR("no space after that open square bracket '['\n" . $herecurr);
  982. }
  983. if ($line =~ /\s\]/) {
  984. ERROR("no space before that close square bracket ']'\n" . $herecurr);
  985. }
  986. # check spacing on paretheses
  987. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  988. $line !~ /for\s*\(\s+;/) {
  989. ERROR("no space after that open parenthesis '('\n" . $herecurr);
  990. }
  991. if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ &&
  992. $line !~ /for\s*\(.*;\s+\)/) {
  993. ERROR("no space before that close parenthesis ')'\n" . $herecurr);
  994. }
  995. #goto labels aren't indented, allow a single space however
  996. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  997. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  998. WARN("labels should not be indented\n" . $herecurr);
  999. }
  1000. # Need a space before open parenthesis after if, while etc
  1001. if ($line=~/\b(if|while|for|switch)\(/) {
  1002. ERROR("need a space before the open parenthesis '('\n" . $herecurr);
  1003. }
  1004. # Check for illegal assignment in if conditional.
  1005. if ($line=~/\bif\s*\(.*[^<>!=]=[^=]/) {
  1006. #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
  1007. ERROR("do not use assignment in if condition\n" . $herecurr);
  1008. }
  1009. # Check for }<nl>else {, these must be at the same
  1010. # indent level to be relevant to each other.
  1011. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1012. $previndent == $indent) {
  1013. ERROR("else should follow close brace '}'\n" . $hereprev);
  1014. }
  1015. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1016. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1017. # print "No studly caps, use _\n";
  1018. # print "$herecurr";
  1019. # $clean = 0;
  1020. # }
  1021. #no spaces allowed after \ in define
  1022. if ($line=~/\#define.*\\\s$/) {
  1023. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1024. }
  1025. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1026. if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1027. my $checkfile = "$root/include/linux/$1.h";
  1028. if (-f $checkfile && $1 ne 'irq.h') {
  1029. CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
  1030. $herecurr);
  1031. }
  1032. }
  1033. # if and else should not have general statements after it
  1034. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
  1035. $1 !~ /^\s*(?:\sif|{|\\|$)/) {
  1036. ERROR("trailing statements should be on next line\n" . $herecurr);
  1037. }
  1038. # multi-statement macros should be enclosed in a do while loop, grab the
  1039. # first statement and ensure its the whole macro if its not enclosed
  1040. # in a known goot container
  1041. if ($prevline =~ /\#define.*\\/ &&
  1042. $prevline !~/(?:do\s+{|\(\{|\{)/ &&
  1043. $line !~ /(?:do\s+{|\(\{|\{)/ &&
  1044. $line !~ /^.\s*$Declare\s/) {
  1045. # Grab the first statement, if that is the entire macro
  1046. # its ok. This may start either on the #define line
  1047. # or the one below.
  1048. my $ln = $linenr;
  1049. my $cnt = $realcnt;
  1050. my $off = 0;
  1051. # If the macro starts on the define line start
  1052. # grabbing the statement after the identifier
  1053. $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
  1054. ##print "1<$1> 2<$2>\n";
  1055. if (defined $2 && $2 ne '') {
  1056. $off = length($1);
  1057. $ln--;
  1058. $cnt++;
  1059. }
  1060. my @ctx = ctx_statement($ln, $cnt, $off);
  1061. my $ctx_ln = $ln + $#ctx + 1;
  1062. my $ctx = join("\n", @ctx);
  1063. # Pull in any empty extension lines.
  1064. while ($ctx =~ /\\$/ &&
  1065. $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
  1066. $ctx .= $lines[$ctx_ln - 1];
  1067. $ctx_ln++;
  1068. }
  1069. if ($ctx =~ /\\$/) {
  1070. if ($ctx =~ /;/) {
  1071. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  1072. } else {
  1073. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  1074. }
  1075. }
  1076. }
  1077. # check for redundant bracing round if etc
  1078. if ($line =~ /\b(if|while|for|else)\b/) {
  1079. # Locate the end of the opening statement.
  1080. my @control = ctx_statement($linenr, $realcnt, 0);
  1081. my $nr = $linenr + (scalar(@control) - 1);
  1082. my $cnt = $realcnt - (scalar(@control) - 1);
  1083. my $off = $realcnt - $cnt;
  1084. #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
  1085. # If this is is a braced statement group check it
  1086. if ($lines[$nr - 1] =~ /{\s*$/) {
  1087. my ($lvl, @block) = ctx_block_level($nr, $cnt);
  1088. my $stmt = join(' ', @block);
  1089. $stmt =~ s/(^[^{]*){//;
  1090. my $before = $1;
  1091. $stmt =~ s/}([^}]*$)//;
  1092. my $after = $1;
  1093. #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
  1094. #print "stmt<$stmt>\n\n";
  1095. # Count the ;'s if there is fewer than two
  1096. # then there can only be one statement,
  1097. # if there is a brace inside we cannot
  1098. # trivially detect if its one statement.
  1099. # Also nested if's often require braces to
  1100. # disambiguate the else binding so shhh there.
  1101. my @semi = ($stmt =~ /;/g);
  1102. push(@semi, "/**/") if ($stmt =~ m@/\*@);
  1103. ##print "semi<" . scalar(@semi) . ">\n";
  1104. if ($lvl == 0 && scalar(@semi) < 2 &&
  1105. $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
  1106. $before !~ /}/ && $after !~ /{/) {
  1107. my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
  1108. shift(@block);
  1109. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  1110. }
  1111. }
  1112. }
  1113. # don't include deprecated include files (uses RAW line)
  1114. for my $inc (@dep_includes) {
  1115. if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
  1116. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1117. }
  1118. }
  1119. # don't use deprecated functions
  1120. for my $func (@dep_functions) {
  1121. if ($line =~ /\b$func\b/) {
  1122. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1123. }
  1124. }
  1125. # no volatiles please
  1126. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  1127. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  1128. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1129. }
  1130. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1131. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1132. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1133. }
  1134. # warn about #if 0
  1135. if ($line =~ /^.#\s*if\s+0\b/) {
  1136. CHK("if this code is redundant consider removing it\n" .
  1137. $herecurr);
  1138. }
  1139. # check for needless kfree() checks
  1140. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1141. my $expr = $1;
  1142. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1143. WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
  1144. }
  1145. }
  1146. # warn about #ifdefs in C files
  1147. # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1148. # print "#ifdef in C files should be avoided\n";
  1149. # print "$herecurr";
  1150. # $clean = 0;
  1151. # }
  1152. # warn about spacing in #ifdefs
  1153. if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
  1154. ERROR("exactly one space required after that #$1\n" . $herecurr);
  1155. }
  1156. # check for spinlock_t definitions without a comment.
  1157. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
  1158. my $which = $1;
  1159. if (!ctx_has_comment($first_line, $linenr)) {
  1160. CHK("$1 definition without comment\n" . $herecurr);
  1161. }
  1162. }
  1163. # check for memory barriers without a comment.
  1164. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  1165. if (!ctx_has_comment($first_line, $linenr)) {
  1166. CHK("memory barrier without comment\n" . $herecurr);
  1167. }
  1168. }
  1169. # check of hardware specific defines
  1170. if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  1171. CHK("architecture specific defines should be avoided\n" . $herecurr);
  1172. }
  1173. # check the location of the inline attribute, that it is between
  1174. # storage class and type.
  1175. if ($line =~ /\b$Type\s+$Inline\b/ ||
  1176. $line =~ /\b$Inline\s+$Storage\b/) {
  1177. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  1178. }
  1179. # check for new externs in .c files.
  1180. if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
  1181. WARN("externs should be avoided in .c files\n" . $herecurr);
  1182. }
  1183. # checks for new __setup's
  1184. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  1185. my $name = $1;
  1186. if (!grep(/$name/, @setup_docs)) {
  1187. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  1188. }
  1189. }
  1190. # check for pointless casting of kmalloc return
  1191. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  1192. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  1193. }
  1194. }
  1195. if ($chk_patch && !$is_patch) {
  1196. ERROR("Does not appear to be a unified-diff format patch\n");
  1197. }
  1198. if ($is_patch && $chk_signoff && $signoff == 0) {
  1199. ERROR("Missing Signed-off-by: line(s)\n");
  1200. }
  1201. if ($clean == 0 && ($chk_patch || $is_patch)) {
  1202. print report_dump();
  1203. if ($quiet < 2) {
  1204. print "total: $cnt_error errors, $cnt_warn warnings, " .
  1205. (($check)? "$cnt_chk checks, " : "") .
  1206. "$cnt_lines lines checked\n";
  1207. }
  1208. }
  1209. if ($clean == 1 && $quiet == 0) {
  1210. print "Your patch has no obvious style problems and is ready for submission.\n"
  1211. }
  1212. if ($clean == 0 && $quiet == 0) {
  1213. print "Your patch has style problems, please review. If any of these errors\n";
  1214. print "are false positives report them to the maintainer, see\n";
  1215. print "CHECKPATCH in MAINTAINERS.\n";
  1216. }
  1217. return $clean;
  1218. }