Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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