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.
 
 
 
 
 
 

3351 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. ERROR("FSF_MAILING_ADDRESS",
  1379. "Do not include the paragraph about writing to the Free Software Foundation's mailing address " .
  1380. "from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. " .
  1381. "OpenOCD already includes a copy of the GPL.\n" . $herevet)
  1382. }
  1383. # check for Kconfig help text having a real description
  1384. # Only applies when adding the entry originally, after that we do not have
  1385. # sufficient context to determine whether it is indeed long enough.
  1386. if ($realfile =~ /Kconfig/ &&
  1387. $line =~ /\+\s*(?:---)?help(?:---)?$/) {
  1388. my $length = 0;
  1389. my $cnt = $realcnt;
  1390. my $ln = $linenr + 1;
  1391. my $f;
  1392. my $is_end = 0;
  1393. while ($cnt > 0 && defined $lines[$ln - 1]) {
  1394. $f = $lines[$ln - 1];
  1395. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  1396. $is_end = $lines[$ln - 1] =~ /^\+/;
  1397. $ln++;
  1398. next if ($f =~ /^-/);
  1399. $f =~ s/^.//;
  1400. $f =~ s/#.*//;
  1401. $f =~ s/^\s+//;
  1402. next if ($f =~ /^$/);
  1403. if ($f =~ /^\s*config\s/) {
  1404. $is_end = 1;
  1405. last;
  1406. }
  1407. $length++;
  1408. }
  1409. WARN("CONFIG_DESCRIPTION",
  1410. "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
  1411. #print "is_end<$is_end> length<$length>\n";
  1412. }
  1413. # check we are in a valid source file if not then ignore this hunk
  1414. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  1415. #120 column limit
  1416. if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
  1417. $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
  1418. !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
  1419. $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
  1420. $length > 120)
  1421. {
  1422. WARN("LONG_LINE",
  1423. "line over 120 characters\n" . $herecurr);
  1424. }
  1425. # check for spaces before a quoted newline
  1426. if ($rawline =~ /^.*\".*\s\\n/) {
  1427. WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
  1428. "unnecessary whitespace before a quoted newline\n" . $herecurr);
  1429. }
  1430. # check for adding lines without a newline.
  1431. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  1432. WARN("MISSING_EOF_NEWLINE",
  1433. "adding a line without newline at end of file\n" . $herecurr);
  1434. }
  1435. # Blackfin: use hi/lo macros
  1436. if ($realfile =~ m@arch/blackfin/.*\.S$@) {
  1437. if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
  1438. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1439. ERROR("LO_MACRO",
  1440. "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
  1441. }
  1442. if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
  1443. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1444. ERROR("HI_MACRO",
  1445. "use the HI() macro, not (... >> 16)\n" . $herevet);
  1446. }
  1447. }
  1448. # check we are in a valid source file C or perl if not then ignore this hunk
  1449. next if ($realfile !~ /\.(h|c|pl)$/);
  1450. # at the beginning of a line any tabs must come first and anything
  1451. # more than 8 must use tabs.
  1452. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  1453. $rawline =~ /^\+\s* \s*/) {
  1454. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1455. ERROR("CODE_INDENT",
  1456. "code indent should use tabs where possible\n" . $herevet);
  1457. $rpt_cleaners = 1;
  1458. }
  1459. # check for space before tabs.
  1460. if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
  1461. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1462. WARN("SPACE_BEFORE_TAB",
  1463. "please, no space before tabs\n" . $herevet);
  1464. }
  1465. # check we are in a valid C source file if not then ignore this hunk
  1466. next if ($realfile !~ /\.(h|c)$/);
  1467. # check for spaces at the beginning of a line.
  1468. # Exceptions:
  1469. # 1) within comments
  1470. # 2) indented preprocessor commands
  1471. # 3) hanging labels
  1472. if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
  1473. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1474. WARN("LEADING_SPACE",
  1475. "please, no spaces at the start of a line\n" . $herevet);
  1476. }
  1477. # check for RCS/CVS revision markers
  1478. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  1479. WARN("CVS_KEYWORD",
  1480. "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  1481. }
  1482. # Blackfin: don't use __builtin_bfin_[cs]sync
  1483. if ($line =~ /__builtin_bfin_csync/) {
  1484. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1485. ERROR("CSYNC",
  1486. "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
  1487. }
  1488. if ($line =~ /__builtin_bfin_ssync/) {
  1489. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1490. ERROR("SSYNC",
  1491. "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
  1492. }
  1493. # Check for potential 'bare' types
  1494. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  1495. $realline_next);
  1496. if ($realcnt && $line =~ /.\s*\S/) {
  1497. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  1498. ctx_statement_block($linenr, $realcnt, 0);
  1499. $stat =~ s/\n./\n /g;
  1500. $cond =~ s/\n./\n /g;
  1501. # Find the real next line.
  1502. $realline_next = $line_nr_next;
  1503. if (defined $realline_next &&
  1504. (!defined $lines[$realline_next - 1] ||
  1505. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  1506. $realline_next++;
  1507. }
  1508. my $s = $stat;
  1509. $s =~ s/{.*$//s;
  1510. # Ignore goto labels.
  1511. if ($s =~ /$Ident:\*$/s) {
  1512. # Ignore functions being called
  1513. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  1514. } elsif ($s =~ /^.\s*else\b/s) {
  1515. # declarations always start with types
  1516. } 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) {
  1517. my $type = $1;
  1518. $type =~ s/\s+/ /g;
  1519. possible($type, "A:" . $s);
  1520. # definitions in global scope can only start with types
  1521. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  1522. possible($1, "B:" . $s);
  1523. }
  1524. # any (foo ... *) is a pointer cast, and foo is a type
  1525. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  1526. possible($1, "C:" . $s);
  1527. }
  1528. # Check for any sort of function declaration.
  1529. # int foo(something bar, other baz);
  1530. # void (*store_gdt)(x86_descr_ptr *);
  1531. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1532. my ($name_len) = length($1);
  1533. my $ctx = $s;
  1534. substr($ctx, 0, $name_len + 1, '');
  1535. $ctx =~ s/\)[^\)]*$//;
  1536. for my $arg (split(/\s*,\s*/, $ctx)) {
  1537. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1538. possible($1, "D:" . $s);
  1539. }
  1540. }
  1541. }
  1542. }
  1543. #
  1544. # Checks which may be anchored in the context.
  1545. #
  1546. # Check for switch () and associated case and default
  1547. # statements should be at the same indent.
  1548. # if ($line=~/\bswitch\s*\(.*\)/) {
  1549. # my $err = '';
  1550. # my $sep = '';
  1551. # my @ctx = ctx_block_outer($linenr, $realcnt);
  1552. # shift(@ctx);
  1553. # for my $ctx (@ctx) {
  1554. # my ($clen, $cindent) = line_stats($ctx);
  1555. # if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1556. # $indent != $cindent) {
  1557. # $err .= "$sep$ctx\n";
  1558. # $sep = '';
  1559. # } else {
  1560. # $sep = "[...]\n";
  1561. # }
  1562. # }
  1563. # if ($err ne '') {
  1564. # ERROR("SWITCH_CASE_INDENT_LEVEL",
  1565. # "switch and case should be at the same indent\n$hereline$err");
  1566. # }
  1567. # }
  1568. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1569. # or if that brace on the next line is for something else
  1570. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1571. my $pre_ctx = "$1$2";
  1572. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1573. my $ctx_cnt = $realcnt - $#ctx - 1;
  1574. my $ctx = join("\n", @ctx);
  1575. my $ctx_ln = $linenr;
  1576. my $ctx_skip = $realcnt;
  1577. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  1578. defined $lines[$ctx_ln - 1] &&
  1579. $lines[$ctx_ln - 1] =~ /^-/)) {
  1580. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  1581. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  1582. $ctx_ln++;
  1583. }
  1584. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1585. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1586. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  1587. ERROR("OPEN_BRACE",
  1588. "that open brace { should be on the previous line\n" .
  1589. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  1590. }
  1591. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1592. $ctx =~ /\)\s*\;\s*$/ &&
  1593. defined $lines[$ctx_ln - 1])
  1594. {
  1595. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1596. if ($nindent > $indent) {
  1597. WARN("TRAILING_SEMICOLON",
  1598. "trailing semicolon indicates no statements, indent implies otherwise\n" .
  1599. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  1600. }
  1601. }
  1602. }
  1603. # Check relative indent for conditionals and blocks.
  1604. if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  1605. my ($s, $c) = ($stat, $cond);
  1606. substr($s, 0, length($c), '');
  1607. # Make sure we remove the line prefixes as we have
  1608. # none on the first line, and are going to readd them
  1609. # where necessary.
  1610. $s =~ s/\n./\n/gs;
  1611. # Find out how long the conditional actually is.
  1612. my @newlines = ($c =~ /\n/gs);
  1613. my $cond_lines = 1 + $#newlines;
  1614. # We want to check the first line inside the block
  1615. # starting at the end of the conditional, so remove:
  1616. # 1) any blank line termination
  1617. # 2) any opening brace { on end of the line
  1618. # 3) any do (...) {
  1619. my $continuation = 0;
  1620. my $check = 0;
  1621. $s =~ s/^.*\bdo\b//;
  1622. $s =~ s/^\s*{//;
  1623. if ($s =~ s/^\s*\\//) {
  1624. $continuation = 1;
  1625. }
  1626. if ($s =~ s/^\s*?\n//) {
  1627. $check = 1;
  1628. $cond_lines++;
  1629. }
  1630. # Also ignore a loop construct at the end of a
  1631. # preprocessor statement.
  1632. if (($prevline =~ /^.\s*#\s*define\s/ ||
  1633. $prevline =~ /\\\s*$/) && $continuation == 0) {
  1634. $check = 0;
  1635. }
  1636. my $cond_ptr = -1;
  1637. $continuation = 0;
  1638. while ($cond_ptr != $cond_lines) {
  1639. $cond_ptr = $cond_lines;
  1640. # If we see an #else/#elif then the code
  1641. # is not linear.
  1642. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  1643. $check = 0;
  1644. }
  1645. # Ignore:
  1646. # 1) blank lines, they should be at 0,
  1647. # 2) preprocessor lines, and
  1648. # 3) labels.
  1649. if ($continuation ||
  1650. $s =~ /^\s*?\n/ ||
  1651. $s =~ /^\s*#\s*?/ ||
  1652. $s =~ /^\s*$Ident\s*:/) {
  1653. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  1654. if ($s =~ s/^.*?\n//) {
  1655. $cond_lines++;
  1656. }
  1657. }
  1658. }
  1659. my (undef, $sindent) = line_stats("+" . $s);
  1660. my $stat_real = raw_line($linenr, $cond_lines);
  1661. # Check if either of these lines are modified, else
  1662. # this is not this patch's fault.
  1663. if (!defined($stat_real) ||
  1664. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  1665. $check = 0;
  1666. }
  1667. if (defined($stat_real) && $cond_lines > 1) {
  1668. $stat_real = "[...]\n$stat_real";
  1669. }
  1670. #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";
  1671. if ($check && (($sindent % 4) != 0 ||
  1672. ($sindent <= $indent && $s ne ''))) {
  1673. WARN("SUSPECT_CODE_INDENT",
  1674. "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  1675. }
  1676. }
  1677. # Track the 'values' across context and added lines.
  1678. my $opline = $line; $opline =~ s/^./ /;
  1679. my ($curr_values, $curr_vars) =
  1680. annotate_values($opline . "\n", $prev_values);
  1681. $curr_values = $prev_values . $curr_values;
  1682. if ($dbg_values) {
  1683. my $outline = $opline; $outline =~ s/\t/ /g;
  1684. print "$linenr > .$outline\n";
  1685. print "$linenr > $curr_values\n";
  1686. print "$linenr > $curr_vars\n";
  1687. }
  1688. $prev_values = substr($curr_values, -1);
  1689. #ignore lines not being added
  1690. if ($line=~/^[^\+]/) {next;}
  1691. # TEST: allow direct testing of the type matcher.
  1692. if ($dbg_type) {
  1693. if ($line =~ /^.\s*$Declare\s*$/) {
  1694. ERROR("TEST_TYPE",
  1695. "TEST: is type\n" . $herecurr);
  1696. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  1697. ERROR("TEST_NOT_TYPE",
  1698. "TEST: is not type ($1 is)\n". $herecurr);
  1699. }
  1700. next;
  1701. }
  1702. # TEST: allow direct testing of the attribute matcher.
  1703. if ($dbg_attr) {
  1704. if ($line =~ /^.\s*$Modifier\s*$/) {
  1705. ERROR("TEST_ATTR",
  1706. "TEST: is attr\n" . $herecurr);
  1707. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  1708. ERROR("TEST_NOT_ATTR",
  1709. "TEST: is not attr ($1 is)\n". $herecurr);
  1710. }
  1711. next;
  1712. }
  1713. # check for initialisation to aggregates open brace on the next line
  1714. if ($line =~ /^.\s*{/ &&
  1715. $prevline =~ /(?:^|[^=])=\s*$/) {
  1716. ERROR("OPEN_BRACE",
  1717. "that open brace { should be on the previous line\n" . $hereprev);
  1718. }
  1719. #
  1720. # Checks which are anchored on the added line.
  1721. #
  1722. # check for malformed paths in #include statements (uses RAW line)
  1723. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  1724. my $path = $1;
  1725. if ($path =~ m{//}) {
  1726. ERROR("MALFORMED_INCLUDE",
  1727. "malformed #include filename\n" .
  1728. $herecurr);
  1729. }
  1730. }
  1731. # no C99 // comments
  1732. if ($line =~ m{//}) {
  1733. ERROR("C99_COMMENTS",
  1734. "do not use C99 // comments\n" . $herecurr);
  1735. }
  1736. # Remove C99 comments.
  1737. $line =~ s@//.*@@;
  1738. $opline =~ s@//.*@@;
  1739. # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
  1740. # the whole statement.
  1741. #print "APW <$lines[$realline_next - 1]>\n";
  1742. if (defined $realline_next &&
  1743. exists $lines[$realline_next - 1] &&
  1744. !defined $suppress_export{$realline_next} &&
  1745. ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  1746. $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1747. # Handle definitions which produce identifiers with
  1748. # a prefix:
  1749. # XXX(foo);
  1750. # EXPORT_SYMBOL(something_foo);
  1751. my $name = $1;
  1752. if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
  1753. $name =~ /^${Ident}_$2/) {
  1754. #print "FOO C name<$name>\n";
  1755. $suppress_export{$realline_next} = 1;
  1756. } elsif ($stat !~ /(?:
  1757. \n.}\s*$|
  1758. ^.DEFINE_$Ident\(\Q$name\E\)|
  1759. ^.DECLARE_$Ident\(\Q$name\E\)|
  1760. ^.LIST_HEAD\(\Q$name\E\)|
  1761. ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
  1762. \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
  1763. )/x) {
  1764. #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
  1765. $suppress_export{$realline_next} = 2;
  1766. } else {
  1767. $suppress_export{$realline_next} = 1;
  1768. }
  1769. }
  1770. if (!defined $suppress_export{$linenr} &&
  1771. $prevline =~ /^.\s*$/ &&
  1772. ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  1773. $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1774. #print "FOO B <$lines[$linenr - 1]>\n";
  1775. $suppress_export{$linenr} = 2;
  1776. }
  1777. if (defined $suppress_export{$linenr} &&
  1778. $suppress_export{$linenr} == 2) {
  1779. WARN("EXPORT_SYMBOL",
  1780. "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  1781. }
  1782. # check for global initialisers.
  1783. if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
  1784. ERROR("GLOBAL_INITIALISERS",
  1785. "do not initialise globals to 0 or NULL\n" .
  1786. $herecurr);
  1787. }
  1788. # check for static initialisers.
  1789. if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
  1790. ERROR("INITIALISED_STATIC",
  1791. "do not initialise statics to 0 or NULL\n" .
  1792. $herecurr);
  1793. }
  1794. # check for static const char * arrays.
  1795. if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
  1796. WARN("STATIC_CONST_CHAR_ARRAY",
  1797. "static const char * array should probably be static const char * const\n" .
  1798. $herecurr);
  1799. }
  1800. # check for static char foo[] = "bar" declarations.
  1801. if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
  1802. WARN("STATIC_CONST_CHAR_ARRAY",
  1803. "static char array declaration should probably be static const char\n" .
  1804. $herecurr);
  1805. }
  1806. # check for declarations of struct pci_device_id
  1807. if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
  1808. WARN("DEFINE_PCI_DEVICE_TABLE",
  1809. "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
  1810. }
  1811. # check for new typedefs, only function parameters and sparse annotations
  1812. # make sense.
  1813. # if ($line =~ /\btypedef\s/ &&
  1814. # $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
  1815. # $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  1816. # $line !~ /\b$typeTypedefs\b/ &&
  1817. # $line !~ /\b__bitwise(?:__|)\b/) {
  1818. # WARN("NEW_TYPEDEFS",
  1819. # "do not add new typedefs\n" . $herecurr);
  1820. # }
  1821. # * goes on variable not on type
  1822. # (char*[ const])
  1823. if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
  1824. my ($from, $to) = ($1, $1);
  1825. # Should start with a space.
  1826. $to =~ s/^(\S)/ $1/;
  1827. # Should not end with a space.
  1828. $to =~ s/\s+$//;
  1829. # '*'s should not have spaces between.
  1830. while ($to =~ s/\*\s+\*/\*\*/) {
  1831. }
  1832. #print "from<$from> to<$to>\n";
  1833. if ($from ne $to) {
  1834. ERROR("POINTER_LOCATION",
  1835. "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
  1836. }
  1837. } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
  1838. my ($from, $to, $ident) = ($1, $1, $2);
  1839. # Should start with a space.
  1840. $to =~ s/^(\S)/ $1/;
  1841. # Should not end with a space.
  1842. $to =~ s/\s+$//;
  1843. # '*'s should not have spaces between.
  1844. while ($to =~ s/\*\s+\*/\*\*/) {
  1845. }
  1846. # Modifiers should have spaces.
  1847. $to =~ s/(\b$Modifier$)/$1 /;
  1848. #print "from<$from> to<$to> ident<$ident>\n";
  1849. if ($from ne $to && $ident !~ /^$Modifier$/) {
  1850. ERROR("POINTER_LOCATION",
  1851. "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
  1852. }
  1853. }
  1854. # # no BUG() or BUG_ON()
  1855. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1856. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1857. # print "$herecurr";
  1858. # $clean = 0;
  1859. # }
  1860. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1861. WARN("LINUX_VERSION_CODE",
  1862. "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1863. }
  1864. # check for uses of printk_ratelimit
  1865. if ($line =~ /\bprintk_ratelimit\s*\(/) {
  1866. WARN("PRINTK_RATELIMITED",
  1867. "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
  1868. }
  1869. # printk should use KERN_* levels. Note that follow on printk's on the
  1870. # same line do not need a level, so we use the current block context
  1871. # to try and find and validate the current printk. In summary the current
  1872. # printk includes all preceding printk's which have no newline on the end.
  1873. # we assume the first bad printk is the one to report.
  1874. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1875. my $ok = 0;
  1876. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1877. #print "CHECK<$lines[$ln - 1]\n";
  1878. # we have a preceding printk if it ends
  1879. # with "\n" ignore it, else it is to blame
  1880. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1881. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1882. $ok = 1;
  1883. }
  1884. last;
  1885. }
  1886. }
  1887. if ($ok == 0) {
  1888. WARN("PRINTK_WITHOUT_KERN_LEVEL",
  1889. "printk() should include KERN_ facility level\n" . $herecurr);
  1890. }
  1891. }
  1892. # function brace can't be on same line, except for #defines of do while,
  1893. # or if closed on same line
  1894. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  1895. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  1896. ERROR("OPEN_BRACE",
  1897. "open brace '{' following function declarations go on the next line\n" . $herecurr);
  1898. }
  1899. # open braces for enum, union and struct go on the same line.
  1900. if ($line =~ /^.\s*{/ &&
  1901. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1902. ERROR("OPEN_BRACE",
  1903. "open brace '{' following $1 go on the same line\n" . $hereprev);
  1904. }
  1905. # missing space after union, struct or enum definition
  1906. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
  1907. WARN("SPACING",
  1908. "missing space after $1 definition\n" . $herecurr);
  1909. }
  1910. # check for spacing round square brackets; allowed:
  1911. # 1. with a type on the left -- int [] a;
  1912. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  1913. # 3. inside a curly brace -- = { [0...10] = 5 }
  1914. while ($line =~ /(.*?\s)\[/g) {
  1915. my ($where, $prefix) = ($-[1], $1);
  1916. if ($prefix !~ /$Type\s+$/ &&
  1917. ($where != 0 || $prefix !~ /^.\s+$/) &&
  1918. $prefix !~ /{\s+$/) {
  1919. ERROR("BRACKET_SPACE",
  1920. "space prohibited before open square bracket '['\n" . $herecurr);
  1921. }
  1922. }
  1923. # check for spaces between functions and their parentheses.
  1924. while ($line =~ /($Ident)\s+\(/g) {
  1925. my $name = $1;
  1926. my $ctx_before = substr($line, 0, $-[1]);
  1927. my $ctx = "$ctx_before$name";
  1928. # Ignore those directives where spaces _are_ permitted.
  1929. if ($name =~ /^(?:
  1930. if|for|while|switch|return|case|
  1931. volatile|__volatile__|
  1932. __attribute__|format|__extension__|
  1933. asm|__asm__)$/x)
  1934. {
  1935. # cpp #define statements have non-optional spaces, ie
  1936. # if there is a space between the name and the open
  1937. # parenthesis it is simply not a parameter group.
  1938. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1939. # cpp #elif statement condition may start with a (
  1940. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1941. # If this whole things ends with a type its most
  1942. # likely a typedef for a function.
  1943. } elsif ($ctx =~ /$Type$/) {
  1944. } else {
  1945. WARN("SPACING",
  1946. "space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1947. }
  1948. }
  1949. # Check operator spacing.
  1950. if (!($line=~/\#\s*include/)) {
  1951. my $ops = qr{
  1952. <<=|>>=|<=|>=|==|!=|
  1953. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1954. =>|->|<<|>>|<|>|=|!|~|
  1955. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  1956. \?|:
  1957. }x;
  1958. my @elements = split(/($ops|;)/, $opline);
  1959. my $off = 0;
  1960. my $blank = copy_spacing($opline);
  1961. for (my $n = 0; $n < $#elements; $n += 2) {
  1962. $off += length($elements[$n]);
  1963. # Pick up the preceding and succeeding characters.
  1964. my $ca = substr($opline, 0, $off);
  1965. my $cc = '';
  1966. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1967. $cc = substr($opline, $off + length($elements[$n + 1]));
  1968. }
  1969. my $cb = "$ca$;$cc";
  1970. my $a = '';
  1971. $a = 'V' if ($elements[$n] ne '');
  1972. $a = 'W' if ($elements[$n] =~ /\s$/);
  1973. $a = 'C' if ($elements[$n] =~ /$;$/);
  1974. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1975. $a = 'O' if ($elements[$n] eq '');
  1976. $a = 'E' if ($ca =~ /^\s*$/);
  1977. my $op = $elements[$n + 1];
  1978. my $c = '';
  1979. if (defined $elements[$n + 2]) {
  1980. $c = 'V' if ($elements[$n + 2] ne '');
  1981. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1982. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1983. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1984. $c = 'O' if ($elements[$n + 2] eq '');
  1985. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  1986. } else {
  1987. $c = 'E';
  1988. }
  1989. my $ctx = "${a}x${c}";
  1990. my $at = "(ctx:$ctx)";
  1991. my $ptr = substr($blank, 0, $off) . "^";
  1992. my $hereptr = "$hereline$ptr\n";
  1993. # Pull out the value of this operator.
  1994. my $op_type = substr($curr_values, $off + 1, 1);
  1995. # Get the full operator variant.
  1996. my $opv = $op . substr($curr_vars, $off, 1);
  1997. # Ignore operators passed as parameters.
  1998. if ($op_type ne 'V' &&
  1999. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  2000. # # Ignore comments
  2001. # } elsif ($op =~ /^$;+$/) {
  2002. # ; should have either the end of line or a space or \ after it
  2003. } elsif ($op eq ';') {
  2004. if ($ctx !~ /.x[WEBC]/ &&
  2005. $cc !~ /^\\/ && $cc !~ /^;/) {
  2006. ERROR("SPACING",
  2007. "space required after that '$op' $at\n" . $hereptr);
  2008. }
  2009. # // is a comment
  2010. } elsif ($op eq '//') {
  2011. # No spaces for:
  2012. # ->
  2013. # : when part of a bitfield
  2014. } elsif ($op eq '->' || $opv eq ':B') {
  2015. if ($ctx =~ /Wx.|.xW/) {
  2016. ERROR("SPACING",
  2017. "spaces prohibited around that '$op' $at\n" . $hereptr);
  2018. }
  2019. # , must have a space on the right.
  2020. } elsif ($op eq ',') {
  2021. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  2022. ERROR("SPACING",
  2023. "space required after that '$op' $at\n" . $hereptr);
  2024. }
  2025. # '*' as part of a type definition -- reported already.
  2026. } elsif ($opv eq '*_') {
  2027. #warn "'*' is part of type\n";
  2028. # unary operators should have a space before and
  2029. # none after. May be left adjacent to another
  2030. # unary operator, or a cast
  2031. } elsif ($op eq '!' || $op eq '~' ||
  2032. $opv eq '*U' || $opv eq '-U' ||
  2033. $opv eq '&U' || $opv eq '&&U') {
  2034. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  2035. ERROR("SPACING",
  2036. "space required before that '$op' $at\n" . $hereptr);
  2037. }
  2038. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  2039. # A unary '*' may be const
  2040. } elsif ($ctx =~ /.xW/) {
  2041. ERROR("SPACING",
  2042. "space prohibited after that '$op' $at\n" . $hereptr);
  2043. }
  2044. # unary ++ and unary -- are allowed no space on one side.
  2045. } elsif ($op eq '++' or $op eq '--') {
  2046. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  2047. ERROR("SPACING",
  2048. "space required one side of that '$op' $at\n" . $hereptr);
  2049. }
  2050. if ($ctx =~ /Wx[BE]/ ||
  2051. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  2052. ERROR("SPACING",
  2053. "space prohibited before that '$op' $at\n" . $hereptr);
  2054. }
  2055. if ($ctx =~ /ExW/) {
  2056. ERROR("SPACING",
  2057. "space prohibited after that '$op' $at\n" . $hereptr);
  2058. }
  2059. # << and >> may either have or not have spaces both sides
  2060. } elsif ($op eq '<<' or $op eq '>>' or
  2061. $op eq '&' or $op eq '^' or $op eq '|' or
  2062. $op eq '+' or $op eq '-' or
  2063. $op eq '*' or $op eq '/' or
  2064. $op eq '%')
  2065. {
  2066. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  2067. ERROR("SPACING",
  2068. "need consistent spacing around '$op' $at\n" .
  2069. $hereptr);
  2070. }
  2071. # A colon needs no spaces before when it is
  2072. # terminating a case value or a label.
  2073. } elsif ($opv eq ':C' || $opv eq ':L') {
  2074. if ($ctx =~ /Wx./) {
  2075. ERROR("SPACING",
  2076. "space prohibited before that '$op' $at\n" . $hereptr);
  2077. }
  2078. # All the others need spaces both sides.
  2079. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  2080. my $ok = 0;
  2081. # Ignore email addresses <foo@bar>
  2082. if (($op eq '<' &&
  2083. $cc =~ /^\S+\@\S+>/) ||
  2084. ($op eq '>' &&
  2085. $ca =~ /<\S+\@\S+$/))
  2086. {
  2087. $ok = 1;
  2088. }
  2089. # Ignore ?:
  2090. if (($opv eq ':O' && $ca =~ /\?$/) ||
  2091. ($op eq '?' && $cc =~ /^:/)) {
  2092. $ok = 1;
  2093. }
  2094. if ($ok == 0) {
  2095. ERROR("SPACING",
  2096. "spaces required around that '$op' $at\n" . $hereptr);
  2097. }
  2098. }
  2099. $off += length($elements[$n + 1]);
  2100. }
  2101. }
  2102. # check for multiple assignments
  2103. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  2104. CHK("MULTIPLE_ASSIGNMENTS",
  2105. "multiple assignments should be avoided\n" . $herecurr);
  2106. }
  2107. ## # check for multiple declarations, allowing for a function declaration
  2108. ## # continuation.
  2109. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  2110. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  2111. ##
  2112. ## # Remove any bracketed sections to ensure we do not
  2113. ## # falsly report the parameters of functions.
  2114. ## my $ln = $line;
  2115. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  2116. ## }
  2117. ## if ($ln =~ /,/) {
  2118. ## WARN("MULTIPLE_DECLARATION",
  2119. ## "declaring multiple variables together should be avoided\n" . $herecurr);
  2120. ## }
  2121. ## }
  2122. #need space before brace following if, while, etc
  2123. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  2124. $line =~ /do{/) {
  2125. ERROR("SPACING",
  2126. "space required before the open brace '{'\n" . $herecurr);
  2127. }
  2128. # closing brace should have a space following it when it has anything
  2129. # on the line
  2130. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  2131. ERROR("SPACING",
  2132. "space required after that close brace '}'\n" . $herecurr);
  2133. }
  2134. # check spacing on square brackets
  2135. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  2136. ERROR("SPACING",
  2137. "space prohibited after that open square bracket '['\n" . $herecurr);
  2138. }
  2139. if ($line =~ /\s\]/) {
  2140. ERROR("SPACING",
  2141. "space prohibited before that close square bracket ']'\n" . $herecurr);
  2142. }
  2143. # check spacing on parentheses
  2144. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  2145. $line !~ /for\s*\(\s+;/) {
  2146. ERROR("SPACING",
  2147. "space prohibited after that open parenthesis '('\n" . $herecurr);
  2148. }
  2149. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  2150. $line !~ /for\s*\(.*;\s+\)/ &&
  2151. $line !~ /:\s+\)/) {
  2152. ERROR("SPACING",
  2153. "space prohibited before that close parenthesis ')'\n" . $herecurr);
  2154. }
  2155. #goto labels aren't indented, allow a single space however
  2156. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  2157. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  2158. WARN("INDENTED_LABEL",
  2159. "labels should not be indented\n" . $herecurr);
  2160. }
  2161. # Return is not a function.
  2162. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  2163. my $spacing = $1;
  2164. my $value = $2;
  2165. # Flatten any parentheses
  2166. $value =~ s/\(/ \(/g;
  2167. $value =~ s/\)/\) /g;
  2168. while ($value =~ s/\[[^\{\}]*\]/1/ ||
  2169. $value !~ /(?:$Ident|-?$Constant)\s*
  2170. $Compare\s*
  2171. (?:$Ident|-?$Constant)/x &&
  2172. $value =~ s/\([^\(\)]*\)/1/) {
  2173. }
  2174. #print "value<$value>\n";
  2175. if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
  2176. ERROR("RETURN_PARENTHESES",
  2177. "return is not a function, parentheses are not required\n" . $herecurr);
  2178. } elsif ($spacing !~ /\s+/) {
  2179. ERROR("SPACING",
  2180. "space required before the open parenthesis '('\n" . $herecurr);
  2181. }
  2182. }
  2183. # Return of what appears to be an errno should normally be -'ve
  2184. if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
  2185. my $name = $1;
  2186. if ($name ne 'EOF' && $name ne 'ERROR') {
  2187. WARN("USE_NEGATIVE_ERRNO",
  2188. "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
  2189. }
  2190. }
  2191. # typecasts on min/max could be min_t/max_t
  2192. if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
  2193. if (defined $2 || defined $8) {
  2194. my $call = $1;
  2195. my $cast1 = deparenthesize($2);
  2196. my $arg1 = $3;
  2197. my $cast2 = deparenthesize($8);
  2198. my $arg2 = $9;
  2199. my $cast;
  2200. if ($cast1 ne "" && $cast2 ne "") {
  2201. $cast = "$cast1 or $cast2";
  2202. } elsif ($cast1 ne "") {
  2203. $cast = $cast1;
  2204. } else {
  2205. $cast = $cast2;
  2206. }
  2207. WARN("MINMAX",
  2208. "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
  2209. }
  2210. }
  2211. # Need a space before open parenthesis after if, while etc
  2212. if ($line=~/\b(if|while|for|switch)\(/) {
  2213. ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
  2214. }
  2215. # Check for illegal assignment in if conditional -- and check for trailing
  2216. # statements after the conditional.
  2217. if ($line =~ /do\s*(?!{)/) {
  2218. my ($stat_next) = ctx_statement_block($line_nr_next,
  2219. $remain_next, $off_next);
  2220. $stat_next =~ s/\n./\n /g;
  2221. ##print "stat<$stat> stat_next<$stat_next>\n";
  2222. if ($stat_next =~ /^\s*while\b/) {
  2223. # If the statement carries leading newlines,
  2224. # then count those as offsets.
  2225. my ($whitespace) =
  2226. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  2227. my $offset =
  2228. statement_rawlines($whitespace) - 1;
  2229. $suppress_whiletrailers{$line_nr_next +
  2230. $offset} = 1;
  2231. }
  2232. }
  2233. if (!defined $suppress_whiletrailers{$linenr} &&
  2234. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  2235. my ($s, $c) = ($stat, $cond);
  2236. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  2237. ERROR("ASSIGN_IN_IF",
  2238. "do not use assignment in if condition\n" . $herecurr);
  2239. }
  2240. # Find out what is on the end of the line after the
  2241. # conditional.
  2242. substr($s, 0, length($c), '');
  2243. $s =~ s/\n.*//g;
  2244. $s =~ s/$;//g; # Remove any comments
  2245. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  2246. $c !~ /}\s*while\s*/)
  2247. {
  2248. # Find out how long the conditional actually is.
  2249. my @newlines = ($c =~ /\n/gs);
  2250. my $cond_lines = 1 + $#newlines;
  2251. my $stat_real = '';
  2252. $stat_real = raw_line($linenr, $cond_lines)
  2253. . "\n" if ($cond_lines);
  2254. if (defined($stat_real) && $cond_lines > 1) {
  2255. $stat_real = "[...]\n$stat_real";
  2256. }
  2257. ERROR("TRAILING_STATEMENTS",
  2258. "trailing statements should be on next line\n" . $herecurr . $stat_real);
  2259. }
  2260. }
  2261. # Check for bitwise tests written as boolean
  2262. if ($line =~ /
  2263. (?:
  2264. (?:\[|\(|\&\&|\|\|)
  2265. \s*0[xX][0-9]+\s*
  2266. (?:\&\&|\|\|)
  2267. |
  2268. (?:\&\&|\|\|)
  2269. \s*0[xX][0-9]+\s*
  2270. (?:\&\&|\|\||\)|\])
  2271. )/x)
  2272. {
  2273. WARN("HEXADECIMAL_BOOLEAN_TEST",
  2274. "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  2275. }
  2276. # if and else should not have general statements after it
  2277. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  2278. my $s = $1;
  2279. $s =~ s/$;//g; # Remove any comments
  2280. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  2281. ERROR("TRAILING_STATEMENTS",
  2282. "trailing statements should be on next line\n" . $herecurr);
  2283. }
  2284. }
  2285. # if should not continue a brace
  2286. if ($line =~ /}\s*if\b/) {
  2287. ERROR("TRAILING_STATEMENTS",
  2288. "trailing statements should be on next line\n" .
  2289. $herecurr);
  2290. }
  2291. # case and default should not have general statements after them
  2292. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  2293. $line !~ /\G(?:
  2294. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  2295. \s*return\s+
  2296. )/xg)
  2297. {
  2298. ERROR("TRAILING_STATEMENTS",
  2299. "trailing statements should be on next line\n" . $herecurr);
  2300. }
  2301. # Check for }<nl>else {, these must be at the same
  2302. # indent level to be relevant to each other.
  2303. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  2304. $previndent == $indent) {
  2305. ERROR("ELSE_AFTER_BRACE",
  2306. "else should follow close brace '}'\n" . $hereprev);
  2307. }
  2308. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  2309. $previndent == $indent) {
  2310. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  2311. # Find out what is on the end of the line after the
  2312. # conditional.
  2313. substr($s, 0, length($c), '');
  2314. $s =~ s/\n.*//g;
  2315. if ($s =~ /^\s*;/) {
  2316. ERROR("WHILE_AFTER_BRACE",
  2317. "while should follow close brace '}'\n" . $hereprev);
  2318. }
  2319. }
  2320. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  2321. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  2322. # print "No studly caps, use _\n";
  2323. # print "$herecurr";
  2324. # $clean = 0;
  2325. # }
  2326. #no spaces allowed after \ in define
  2327. if ($line=~/\#\s*define.*\\\s$/) {
  2328. WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
  2329. "Whitepspace after \\ makes next lines useless\n" . $herecurr);
  2330. }
  2331. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  2332. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  2333. my $file = "$1.h";
  2334. my $checkfile = "include/linux/$file";
  2335. if (-f "$root/$checkfile" &&
  2336. $realfile ne $checkfile &&
  2337. $1 !~ /$allowed_asm_includes/)
  2338. {
  2339. if ($realfile =~ m{^arch/}) {
  2340. CHK("ARCH_INCLUDE_LINUX",
  2341. "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2342. } else {
  2343. WARN("INCLUDE_LINUX",
  2344. "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2345. }
  2346. }
  2347. }
  2348. # multi-statement macros should be enclosed in a do while loop, grab the
  2349. # first statement and ensure its the whole macro if its not enclosed
  2350. # in a known good container
  2351. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  2352. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  2353. my $ln = $linenr;
  2354. my $cnt = $realcnt;
  2355. my ($off, $dstat, $dcond, $rest);
  2356. my $ctx = '';
  2357. my $args = defined($1);
  2358. # Find the end of the macro and limit our statement
  2359. # search to that.
  2360. while ($cnt > 0 && defined $lines[$ln - 1] &&
  2361. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  2362. {
  2363. $ctx .= $rawlines[$ln - 1] . "\n";
  2364. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  2365. $ln++;
  2366. }
  2367. $ctx .= $rawlines[$ln - 1];
  2368. ($dstat, $dcond, $ln, $cnt, $off) =
  2369. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  2370. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  2371. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  2372. # Extract the remainder of the define (if any) and
  2373. # rip off surrounding spaces, and trailing \'s.
  2374. $rest = '';
  2375. while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
  2376. #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
  2377. if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
  2378. $rest .= substr($lines[$ln - 1], $off) . "\n";
  2379. $cnt--;
  2380. }
  2381. $ln++;
  2382. $off = 0;
  2383. }
  2384. $rest =~ s/\\\n.//g;
  2385. $rest =~ s/^\s*//s;
  2386. $rest =~ s/\s*$//s;
  2387. # Clean up the original statement.
  2388. if ($args) {
  2389. substr($dstat, 0, length($dcond), '');
  2390. } else {
  2391. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  2392. }
  2393. $dstat =~ s/$;//g;
  2394. $dstat =~ s/\\\n.//g;
  2395. $dstat =~ s/^\s*//s;
  2396. $dstat =~ s/\s*$//s;
  2397. # Flatten any parentheses and braces
  2398. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  2399. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  2400. $dstat =~ s/\[[^\{\}]*\]/1/)
  2401. {
  2402. }
  2403. my $exceptions = qr{
  2404. $Declare|
  2405. module_param_named|
  2406. MODULE_PARAM_DESC|
  2407. DECLARE_PER_CPU|
  2408. DEFINE_PER_CPU|
  2409. __typeof__\(|
  2410. union|
  2411. struct|
  2412. \.$Ident\s*=\s*|
  2413. ^\"|\"$
  2414. }x;
  2415. #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
  2416. if ($rest ne '' && $rest ne ',') {
  2417. if ($rest !~ /while\s*\(/ &&
  2418. $dstat !~ /$exceptions/)
  2419. {
  2420. ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
  2421. "Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  2422. }
  2423. } elsif ($ctx !~ /;/) {
  2424. if ($dstat ne '' &&
  2425. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  2426. $dstat !~ /$exceptions/ &&
  2427. $dstat !~ /^\.$Ident\s*=/ &&
  2428. $dstat =~ /$Operators/)
  2429. {
  2430. ERROR("COMPLEX_MACRO",
  2431. "Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  2432. }
  2433. }
  2434. }
  2435. # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
  2436. # all assignments may have only one of the following with an assignment:
  2437. # .
  2438. # ALIGN(...)
  2439. # VMLINUX_SYMBOL(...)
  2440. if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
  2441. WARN("MISSING_VMLINUX_SYMBOL",
  2442. "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
  2443. }
  2444. # check for redundant bracing round if etc
  2445. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  2446. my ($level, $endln, @chunks) =
  2447. ctx_statement_full($linenr, $realcnt, 1);
  2448. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  2449. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  2450. if ($#chunks > 0 && $level == 0) {
  2451. my $allowed = 0;
  2452. my $seen = 0;
  2453. my $herectx = $here . "\n";
  2454. my $ln = $linenr - 1;
  2455. for my $chunk (@chunks) {
  2456. my ($cond, $block) = @{$chunk};
  2457. # If the condition carries leading newlines, then count those as offsets.
  2458. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  2459. my $offset = statement_rawlines($whitespace) - 1;
  2460. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  2461. # We have looked at and allowed this specific line.
  2462. $suppress_ifbraces{$ln + $offset} = 1;
  2463. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  2464. $ln += statement_rawlines($block) - 1;
  2465. substr($block, 0, length($cond), '');
  2466. $seen++ if ($block =~ /^\s*{/);
  2467. #print "cond<$cond> block<$block> allowed<$allowed>\n";
  2468. if (statement_lines($cond) > 1) {
  2469. #print "APW: ALLOWED: cond<$cond>\n";
  2470. $allowed = 1;
  2471. }
  2472. if ($block =~/\b(?:if|for|while)\b/) {
  2473. #print "APW: ALLOWED: block<$block>\n";
  2474. $allowed = 1;
  2475. }
  2476. if (statement_block_size($block) > 1) {
  2477. #print "APW: ALLOWED: lines block<$block>\n";
  2478. $allowed = 1;
  2479. }
  2480. }
  2481. if ($seen && !$allowed) {
  2482. WARN("BRACES",
  2483. "braces {} are not necessary for any arm of this statement\n" . $herectx);
  2484. }
  2485. }
  2486. }
  2487. if (!defined $suppress_ifbraces{$linenr - 1} &&
  2488. $line =~ /\b(if|while|for|else)\b/) {
  2489. my $allowed = 0;
  2490. # Check the pre-context.
  2491. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  2492. #print "APW: ALLOWED: pre<$1>\n";
  2493. $allowed = 1;
  2494. }
  2495. my ($level, $endln, @chunks) =
  2496. ctx_statement_full($linenr, $realcnt, $-[0]);
  2497. # Check the condition.
  2498. my ($cond, $block) = @{$chunks[0]};
  2499. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  2500. if (defined $cond) {
  2501. substr($block, 0, length($cond), '');
  2502. }
  2503. if (statement_lines($cond) > 1) {
  2504. #print "APW: ALLOWED: cond<$cond>\n";
  2505. $allowed = 1;
  2506. }
  2507. if ($block =~/\b(?:if|for|while)\b/) {
  2508. #print "APW: ALLOWED: block<$block>\n";
  2509. $allowed = 1;
  2510. }
  2511. if (statement_block_size($block) > 1) {
  2512. #print "APW: ALLOWED: lines block<$block>\n";
  2513. $allowed = 1;
  2514. }
  2515. # Check the post-context.
  2516. if (defined $chunks[1]) {
  2517. my ($cond, $block) = @{$chunks[1]};
  2518. if (defined $cond) {
  2519. substr($block, 0, length($cond), '');
  2520. }
  2521. if ($block =~ /^\s*\{/) {
  2522. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  2523. $allowed = 1;
  2524. }
  2525. }
  2526. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  2527. my $herectx = $here . "\n";
  2528. my $cnt = statement_rawlines($block);
  2529. for (my $n = 0; $n < $cnt; $n++) {
  2530. $herectx .= raw_line($linenr, $n) . "\n";
  2531. }
  2532. WARN("BRACES",
  2533. "braces {} are not necessary for single statement blocks\n" . $herectx);
  2534. }
  2535. }
  2536. # don't include deprecated include files (uses RAW line)
  2537. for my $inc (@dep_includes) {
  2538. if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
  2539. ERROR("DEPRECATED_INCLUDE",
  2540. "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  2541. }
  2542. }
  2543. # don't use deprecated functions
  2544. for my $func (@dep_functions) {
  2545. if ($line =~ /\b$func\b/) {
  2546. ERROR("DEPRECATED_FUNCTION",
  2547. "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  2548. }
  2549. }
  2550. # no volatiles please
  2551. # my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  2552. # if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  2553. # WARN("VOLATILE",
  2554. # "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  2555. # }
  2556. # warn about #if 0
  2557. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  2558. CHK("REDUNDANT_CODE",
  2559. "if this code is redundant consider removing it\n" .
  2560. $herecurr);
  2561. }
  2562. # check for needless kfree() checks
  2563. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  2564. my $expr = $1;
  2565. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  2566. WARN("NEEDLESS_KFREE",
  2567. "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
  2568. }
  2569. }
  2570. # check for needless usb_free_urb() checks
  2571. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  2572. my $expr = $1;
  2573. if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
  2574. WARN("NEEDLESS_USB_FREE_URB",
  2575. "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
  2576. }
  2577. }
  2578. # prefer usleep_range over udelay
  2579. if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
  2580. # ignore udelay's < 10, however
  2581. if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
  2582. CHK("USLEEP_RANGE",
  2583. "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
  2584. }
  2585. }
  2586. # warn about unexpectedly long msleep's
  2587. if ($line =~ /\bmsleep\s*\((\d+)\);/) {
  2588. if ($1 < 20) {
  2589. WARN("MSLEEP",
  2590. "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
  2591. }
  2592. }
  2593. # warn about #ifdefs in C files
  2594. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  2595. # print "#ifdef in C files should be avoided\n";
  2596. # print "$herecurr";
  2597. # $clean = 0;
  2598. # }
  2599. # warn about spacing in #ifdefs
  2600. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  2601. ERROR("SPACING",
  2602. "exactly one space required after that #$1\n" . $herecurr);
  2603. }
  2604. # check for spinlock_t definitions without a comment.
  2605. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  2606. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  2607. my $which = $1;
  2608. if (!ctx_has_comment($first_line, $linenr)) {
  2609. CHK("UNCOMMENTED_DEFINITION",
  2610. "$1 definition without comment\n" . $herecurr);
  2611. }
  2612. }
  2613. # check for memory barriers without a comment.
  2614. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  2615. if (!ctx_has_comment($first_line, $linenr)) {
  2616. CHK("MEMORY_BARRIER",
  2617. "memory barrier without comment\n" . $herecurr);
  2618. }
  2619. }
  2620. # check of hardware specific defines
  2621. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  2622. CHK("ARCH_DEFINES",
  2623. "architecture specific defines should be avoided\n" . $herecurr);
  2624. }
  2625. # Check that the storage class is at the beginning of a declaration
  2626. if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
  2627. WARN("STORAGE_CLASS",
  2628. "storage class should be at the beginning of the declaration\n" . $herecurr)
  2629. }
  2630. # check the location of the inline attribute, that it is between
  2631. # storage class and type.
  2632. if ($line =~ /\b$Type\s+$Inline\b/ ||
  2633. $line =~ /\b$Inline\s+$Storage\b/) {
  2634. ERROR("INLINE_LOCATION",
  2635. "inline keyword should sit between storage class and type\n" . $herecurr);
  2636. }
  2637. # Check for __inline__ and __inline, prefer inline
  2638. if ($line =~ /\b(__inline__|__inline)\b/) {
  2639. WARN("INLINE",
  2640. "plain inline is preferred over $1\n" . $herecurr);
  2641. }
  2642. # Check for __attribute__ packed, prefer __packed
  2643. # if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
  2644. # WARN("PREFER_PACKED",
  2645. # "__packed is preferred over __attribute__((packed))\n" . $herecurr);
  2646. # }
  2647. # Check for __attribute__ aligned, prefer __aligned
  2648. # if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
  2649. # WARN("PREFER_ALIGNED",
  2650. # "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
  2651. # }
  2652. # check for sizeof(&)
  2653. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  2654. WARN("SIZEOF_ADDRESS",
  2655. "sizeof(& should be avoided\n" . $herecurr);
  2656. }
  2657. # check for line continuations in quoted strings with odd counts of "
  2658. if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
  2659. WARN("LINE_CONTINUATIONS",
  2660. "Avoid line continuations in quoted strings\n" . $herecurr);
  2661. }
  2662. # check for new externs in .c files.
  2663. # if ($realfile =~ /\.c$/ && defined $stat &&
  2664. # $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  2665. # {
  2666. # my $function_name = $1;
  2667. # my $paren_space = $2;
  2668. #
  2669. # my $s = $stat;
  2670. # if (defined $cond) {
  2671. # substr($s, 0, length($cond), '');
  2672. # }
  2673. # if ($s =~ /^\s*;/ &&
  2674. # $function_name ne 'uninitialized_var')
  2675. # {
  2676. # WARN("AVOID_EXTERNS",
  2677. # "externs should be avoided in .c files\n" . $herecurr);
  2678. # }
  2679. #
  2680. # if ($paren_space =~ /\n/) {
  2681. # WARN("FUNCTION_ARGUMENTS",
  2682. # "arguments for function declarations should follow identifier\n" . $herecurr);
  2683. # }
  2684. #
  2685. # } elsif ($realfile =~ /\.c$/ && defined $stat &&
  2686. # $stat =~ /^.\s*extern\s+/)
  2687. # {
  2688. # WARN("AVOID_EXTERNS",
  2689. # "externs should be avoided in .c files\n" . $herecurr);
  2690. # }
  2691. # checks for new __setup's
  2692. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  2693. my $name = $1;
  2694. if (!grep(/$name/, @setup_docs)) {
  2695. CHK("UNDOCUMENTED_SETUP",
  2696. "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  2697. }
  2698. }
  2699. # check for pointless casting of kmalloc return
  2700. if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
  2701. WARN("UNNECESSARY_CASTS",
  2702. "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  2703. }
  2704. # check for multiple semicolons
  2705. if ($line =~ /;\s*;\s*$/) {
  2706. WARN("ONE_SEMICOLON",
  2707. "Statements terminations use 1 semicolon\n" . $herecurr);
  2708. }
  2709. # check for gcc specific __FUNCTION__
  2710. if ($line =~ /__FUNCTION__/) {
  2711. WARN("USE_FUNC",
  2712. "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  2713. }
  2714. # check for semaphores initialized locked
  2715. if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
  2716. WARN("CONSIDER_COMPLETION",
  2717. "consider using a completion\n" . $herecurr);
  2718. }
  2719. # recommend kstrto* over simple_strto*
  2720. if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
  2721. WARN("CONSIDER_KSTRTO",
  2722. "consider using kstrto* in preference to simple_$1\n" . $herecurr);
  2723. }
  2724. # check for __initcall(), use device_initcall() explicitly please
  2725. if ($line =~ /^.\s*__initcall\s*\(/) {
  2726. WARN("USE_DEVICE_INITCALL",
  2727. "please use device_initcall() instead of __initcall()\n" . $herecurr);
  2728. }
  2729. # check for various ops structs, ensure they are const.
  2730. my $struct_ops = qr{acpi_dock_ops|
  2731. address_space_operations|
  2732. backlight_ops|
  2733. block_device_operations|
  2734. dentry_operations|
  2735. dev_pm_ops|
  2736. dma_map_ops|
  2737. extent_io_ops|
  2738. file_lock_operations|
  2739. file_operations|
  2740. hv_ops|
  2741. ide_dma_ops|
  2742. intel_dvo_dev_ops|
  2743. item_operations|
  2744. iwl_ops|
  2745. kgdb_arch|
  2746. kgdb_io|
  2747. kset_uevent_ops|
  2748. lock_manager_operations|
  2749. microcode_ops|
  2750. mtrr_ops|
  2751. neigh_ops|
  2752. nlmsvc_binding|
  2753. pci_raw_ops|
  2754. pipe_buf_operations|
  2755. platform_hibernation_ops|
  2756. platform_suspend_ops|
  2757. proto_ops|
  2758. rpc_pipe_ops|
  2759. seq_operations|
  2760. snd_ac97_build_ops|
  2761. soc_pcmcia_socket_ops|
  2762. stacktrace_ops|
  2763. sysfs_ops|
  2764. tty_operations|
  2765. usb_mon_operations|
  2766. wd_ops}x;
  2767. if ($line !~ /\bconst\b/ &&
  2768. $line =~ /\bstruct\s+($struct_ops)\b/) {
  2769. WARN("CONST_STRUCT",
  2770. "struct $1 should normally be const\n" .
  2771. $herecurr);
  2772. }
  2773. # use of NR_CPUS is usually wrong
  2774. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  2775. if ($line =~ /\bNR_CPUS\b/ &&
  2776. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  2777. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  2778. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  2779. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  2780. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  2781. {
  2782. WARN("NR_CPUS",
  2783. "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  2784. }
  2785. # check for %L{u,d,i} in strings
  2786. my $string;
  2787. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  2788. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  2789. $string =~ s/%%/__/g;
  2790. if ($string =~ /(?<!%)%L[udi]/) {
  2791. WARN("PRINTF_L",
  2792. "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  2793. last;
  2794. }
  2795. }
  2796. # whine mightly about in_atomic
  2797. if ($line =~ /\bin_atomic\s*\(/) {
  2798. if ($realfile =~ m@^drivers/@) {
  2799. ERROR("IN_ATOMIC",
  2800. "do not use in_atomic in drivers\n" . $herecurr);
  2801. } elsif ($realfile !~ m@^kernel/@) {
  2802. WARN("IN_ATOMIC",
  2803. "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  2804. }
  2805. }
  2806. # check for lockdep_set_novalidate_class
  2807. if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
  2808. $line =~ /__lockdep_no_validate__\s*\)/ ) {
  2809. if ($realfile !~ m@^kernel/lockdep@ &&
  2810. $realfile !~ m@^include/linux/lockdep@ &&
  2811. $realfile !~ m@^drivers/base/core@) {
  2812. ERROR("LOCKDEP",
  2813. "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
  2814. }
  2815. }
  2816. if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
  2817. $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
  2818. WARN("EXPORTED_WORLD_WRITABLE",
  2819. "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
  2820. }
  2821. # Check for memset with swapped arguments
  2822. if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
  2823. ERROR("MEMSET",
  2824. "memset size is 3rd argument, not the second.\n" . $herecurr);
  2825. }
  2826. }
  2827. # If we have no input at all, then there is nothing to report on
  2828. # so just keep quiet.
  2829. if ($#rawlines == -1) {
  2830. exit(0);
  2831. }
  2832. # In mailback mode only produce a report in the negative, for
  2833. # things that appear to be patches.
  2834. if ($mailback && ($clean == 1 || !$is_patch)) {
  2835. exit(0);
  2836. }
  2837. # This is not a patch, and we are are in 'no-patch' mode so
  2838. # just keep quiet.
  2839. if (!$chk_patch && !$is_patch) {
  2840. exit(0);
  2841. }
  2842. if (!$is_patch) {
  2843. ERROR("NOT_UNIFIED_DIFF",
  2844. "Does not appear to be a unified-diff format patch\n");
  2845. }
  2846. if ($is_patch && $chk_signoff && $signoff == 0) {
  2847. ERROR("MISSING_SIGN_OFF",
  2848. "Missing Signed-off-by: line(s)\n");
  2849. }
  2850. print report_dump();
  2851. if ($summary && !($clean == 1 && $quiet == 1)) {
  2852. print "$filename " if ($summary_file);
  2853. print "total: $cnt_error errors, $cnt_warn warnings, " .
  2854. (($check)? "$cnt_chk checks, " : "") .
  2855. "$cnt_lines lines checked\n";
  2856. print "\n" if ($quiet == 0);
  2857. }
  2858. if ($quiet == 0) {
  2859. # If there were whitespace errors which cleanpatch can fix
  2860. # then suggest that.
  2861. if ($rpt_cleaners) {
  2862. print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
  2863. print " scripts/cleanfile\n\n";
  2864. $rpt_cleaners = 0;
  2865. }
  2866. }
  2867. if (keys %ignore_type) {
  2868. print "NOTE: Ignored message types:";
  2869. foreach my $ignore (sort keys %ignore_type) {
  2870. print " $ignore";
  2871. }
  2872. print "\n";
  2873. print "\n" if ($quiet == 0);
  2874. }
  2875. if ($clean == 1 && $quiet == 0) {
  2876. print "$vname has no obvious style problems and is ready for submission.\n"
  2877. }
  2878. if ($clean == 0 && $quiet == 0) {
  2879. print << "EOM";
  2880. $vname has style problems, please review.
  2881. If any of these errors are false positives, please report
  2882. them to the openocd-devel mailing list or prepare a patch
  2883. and send it to Gerrit for review.
  2884. EOM
  2885. }
  2886. return $clean;
  2887. }