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.
 
 
 
 
 
 

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