You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3338 lines
88 KiB

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