checksrc.pl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2011 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.haxx.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. ###########################################################################
  23. use strict;
  24. use warnings;
  25. my $max_column = 79;
  26. my $indent = 2;
  27. my $warnings = 0;
  28. my $swarnings = 0;
  29. my $errors = 0;
  30. my $serrors = 0;
  31. my $suppressed; # skipped problems
  32. my $file;
  33. my $dir=".";
  34. my $wlist="";
  35. my @alist;
  36. my $windows_os = $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys';
  37. my $verbose;
  38. my %skiplist;
  39. my %ignore;
  40. my %ignore_set;
  41. my %ignore_used;
  42. my @ignore_line;
  43. my %warnings_extended = (
  44. 'COPYRIGHTYEAR' => 'copyright year incorrect',
  45. );
  46. my %warnings = (
  47. 'LONGLINE' => "Line longer than $max_column",
  48. 'TABS' => 'TAB characters not allowed',
  49. 'TRAILINGSPACE' => 'Trailing white space on the line',
  50. 'CPPCOMMENTS' => '// comment detected',
  51. 'SPACEBEFOREPAREN' => 'space before an open parenthesis',
  52. 'SPACEAFTERPAREN' => 'space after open parenthesis',
  53. 'SPACEBEFORECLOSE' => 'space before a close parenthesis',
  54. 'SPACEBEFORECOMMA' => 'space before a comma',
  55. 'RETURNNOSPACE' => 'return without space',
  56. 'COMMANOSPACE' => 'comma without following space',
  57. 'BRACEELSE' => '} else on the same line',
  58. 'PARENBRACE' => '){ without sufficient space',
  59. 'SPACESEMICOLON' => 'space before semicolon',
  60. 'BANNEDFUNC' => 'a banned function was used',
  61. 'FOPENMODE' => 'fopen needs a macro for the mode string',
  62. 'BRACEPOS' => 'wrong position for an open brace',
  63. 'INDENTATION' => 'wrong start column for code',
  64. 'COPYRIGHT' => 'file missing a copyright statement',
  65. 'BADCOMMAND' => 'bad !checksrc! instruction',
  66. 'UNUSEDIGNORE' => 'a warning ignore was not used',
  67. 'OPENCOMMENT' => 'file ended with a /* comment still "open"',
  68. 'ASTERISKSPACE' => 'pointer declared with space after asterisk',
  69. 'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
  70. 'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
  71. 'EQUALSNOSPACE' => 'equals sign without following space',
  72. 'NOSPACEEQUALS' => 'equals sign without preceding space',
  73. 'SEMINOSPACE' => 'semicolon without following space',
  74. 'MULTISPACE' => 'multiple spaces used when not suitable',
  75. 'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
  76. 'SNPRINTF' => 'use of snprintf',
  77. 'ONELINECONDITION' => 'conditional block on the same line as the if()',
  78. 'TYPEDEFSTRUCT' => 'typedefed struct',
  79. );
  80. sub readskiplist {
  81. open(W, "<$dir/checksrc.skip") or return;
  82. my @all=<W>;
  83. for(@all) {
  84. $windows_os ? $_ =~ s/\r?\n$// : chomp;
  85. $skiplist{$_}=1;
  86. }
  87. close(W);
  88. }
  89. # Reads the .checksrc in $dir for any extended warnings to enable locally.
  90. # Currently there is no support for disabling warnings from the standard set,
  91. # and since that's already handled via !checksrc! commands there is probably
  92. # little use to add it.
  93. sub readlocalfile {
  94. my $i = 0;
  95. open(my $rcfile, "<", "$dir/.checksrc") or return;
  96. while(<$rcfile>) {
  97. $i++;
  98. # Lines starting with '#' are considered comments
  99. if (/^\s*(#.*)/) {
  100. next;
  101. }
  102. elsif (/^\s*enable ([A-Z]+)$/) {
  103. if(!defined($warnings_extended{$1})) {
  104. print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
  105. next;
  106. }
  107. $warnings{$1} = $warnings_extended{$1};
  108. }
  109. elsif (/^\s*disable ([A-Z]+)$/) {
  110. if(!defined($warnings{$1})) {
  111. print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
  112. next;
  113. }
  114. # Accept-list
  115. push @alist, $1;
  116. }
  117. else {
  118. die "Invalid format in $dir/.checksrc on line $i\n";
  119. }
  120. }
  121. close($rcfile);
  122. }
  123. sub checkwarn {
  124. my ($name, $num, $col, $file, $line, $msg, $error) = @_;
  125. my $w=$error?"error":"warning";
  126. my $nowarn=0;
  127. #if(!$warnings{$name}) {
  128. # print STDERR "Dev! there's no description for $name!\n";
  129. #}
  130. # checksrc.skip
  131. if($skiplist{$line}) {
  132. $nowarn = 1;
  133. }
  134. # !checksrc! controlled
  135. elsif($ignore{$name}) {
  136. $ignore{$name}--;
  137. $ignore_used{$name}++;
  138. $nowarn = 1;
  139. if(!$ignore{$name}) {
  140. # reached zero, enable again
  141. enable_warn($name, $num, $file, $line);
  142. }
  143. }
  144. if($nowarn) {
  145. $suppressed++;
  146. if($w) {
  147. $swarnings++;
  148. }
  149. else {
  150. $serrors++;
  151. }
  152. return;
  153. }
  154. if($w) {
  155. $warnings++;
  156. }
  157. else {
  158. $errors++;
  159. }
  160. $col++;
  161. print "$file:$num:$col: $w: $msg ($name)\n";
  162. print " $line\n";
  163. if($col < 80) {
  164. my $pref = (' ' x $col);
  165. print "${pref}^\n";
  166. }
  167. }
  168. $file = shift @ARGV;
  169. while(defined $file) {
  170. if($file =~ /-D(.*)/) {
  171. $dir = $1;
  172. $file = shift @ARGV;
  173. next;
  174. }
  175. elsif($file =~ /-W(.*)/) {
  176. $wlist .= " $1 ";
  177. $file = shift @ARGV;
  178. next;
  179. }
  180. elsif($file =~ /-A(.+)/) {
  181. push @alist, $1;
  182. $file = shift @ARGV;
  183. next;
  184. }
  185. elsif($file =~ /-i([1-9])/) {
  186. $indent = $1 + 0;
  187. $file = shift @ARGV;
  188. next;
  189. }
  190. elsif($file =~ /-m([0-9]+)/) {
  191. $max_column = $1 + 0;
  192. $file = shift @ARGV;
  193. next;
  194. }
  195. elsif($file =~ /^(-h|--help)/) {
  196. undef $file;
  197. last;
  198. }
  199. last;
  200. }
  201. if(!$file) {
  202. print "checksrc.pl [option] <file1> [file2] ...\n";
  203. print " Options:\n";
  204. print " -A[rule] Accept this violation, can be used multiple times\n";
  205. print " -D[DIR] Directory to prepend file names\n";
  206. print " -h Show help output\n";
  207. print " -W[file] Skip the given file - ignore all its flaws\n";
  208. print " -i<n> Indent spaces. Default: 2\n";
  209. print " -m<n> Maximum line length. Default: 79\n";
  210. print "\nDetects and warns for these problems:\n";
  211. for(sort keys %warnings) {
  212. printf (" %-18s: %s\n", $_, $warnings{$_});
  213. }
  214. exit;
  215. }
  216. readskiplist();
  217. readlocalfile();
  218. do {
  219. if("$wlist" !~ / $file /) {
  220. my $fullname = $file;
  221. $fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
  222. scanfile($fullname);
  223. }
  224. $file = shift @ARGV;
  225. } while($file);
  226. sub accept_violations {
  227. for my $r (@alist) {
  228. if(!$warnings{$r}) {
  229. print "'$r' is not a warning to accept!\n";
  230. exit;
  231. }
  232. $ignore{$r}=999999;
  233. $ignore_used{$r}=0;
  234. }
  235. }
  236. sub checksrc_clear {
  237. undef %ignore;
  238. undef %ignore_set;
  239. undef @ignore_line;
  240. }
  241. sub checksrc_endoffile {
  242. my ($file) = @_;
  243. for(keys %ignore_set) {
  244. if($ignore_set{$_} && !$ignore_used{$_}) {
  245. checkwarn("UNUSEDIGNORE", $ignore_set{$_},
  246. length($_)+11, $file,
  247. $ignore_line[$ignore_set{$_}],
  248. "Unused ignore: $_");
  249. }
  250. }
  251. }
  252. sub enable_warn {
  253. my ($what, $line, $file, $l) = @_;
  254. # switch it back on, but warn if not triggered!
  255. if(!$ignore_used{$what}) {
  256. checkwarn("UNUSEDIGNORE",
  257. $line, length($what) + 11, $file, $l,
  258. "No warning was inhibited!");
  259. }
  260. $ignore_set{$what}=0;
  261. $ignore_used{$what}=0;
  262. $ignore{$what}=0;
  263. }
  264. sub checksrc {
  265. my ($cmd, $line, $file, $l) = @_;
  266. if($cmd =~ / *([^ ]*) *(.*)/) {
  267. my ($enable, $what) = ($1, $2);
  268. $what =~ s: *\*/$::; # cut off end of C comment
  269. # print "ENABLE $enable WHAT $what\n";
  270. if($enable eq "disable") {
  271. my ($warn, $scope)=($1, $2);
  272. if($what =~ /([^ ]*) +(.*)/) {
  273. ($warn, $scope)=($1, $2);
  274. }
  275. else {
  276. $warn = $what;
  277. $scope = 1;
  278. }
  279. # print "IGNORE $warn for SCOPE $scope\n";
  280. if($scope eq "all") {
  281. $scope=999999;
  282. }
  283. # Comparing for a literal zero rather than the scalar value zero
  284. # covers the case where $scope contains the ending '*' from the
  285. # comment. If we use a scalar comparison (==) we induce warnings
  286. # on non-scalar contents.
  287. if($scope eq "0") {
  288. checkwarn("BADCOMMAND",
  289. $line, 0, $file, $l,
  290. "Disable zero not supported, did you mean to enable?");
  291. }
  292. elsif($ignore_set{$warn}) {
  293. checkwarn("BADCOMMAND",
  294. $line, 0, $file, $l,
  295. "$warn already disabled from line $ignore_set{$warn}");
  296. }
  297. else {
  298. $ignore{$warn}=$scope;
  299. $ignore_set{$warn}=$line;
  300. $ignore_line[$line]=$l;
  301. }
  302. }
  303. elsif($enable eq "enable") {
  304. enable_warn($what, $line, $file, $l);
  305. }
  306. else {
  307. checkwarn("BADCOMMAND",
  308. $line, 0, $file, $l,
  309. "Illegal !checksrc! command");
  310. }
  311. }
  312. }
  313. sub nostrings {
  314. my ($str) = @_;
  315. $str =~ s/\".*\"//g;
  316. return $str;
  317. }
  318. sub scanfile {
  319. my ($file) = @_;
  320. my $line = 1;
  321. my $prevl="";
  322. my $l;
  323. open(R, "<$file") || die "failed to open $file";
  324. my $incomment=0;
  325. my @copyright=();
  326. checksrc_clear(); # for file based ignores
  327. accept_violations();
  328. while(<R>) {
  329. $windows_os ? $_ =~ s/\r?\n$// : chomp;
  330. my $l = $_;
  331. my $ol = $l; # keep the unmodified line for error reporting
  332. my $column = 0;
  333. # check for !checksrc! commands
  334. if($l =~ /\!checksrc\! (.*)/) {
  335. my $cmd = $1;
  336. checksrc($cmd, $line, $file, $l)
  337. }
  338. # check for a copyright statement and save the years
  339. if($l =~ /\* +copyright .* \d\d\d\d/i) {
  340. while($l =~ /([\d]{4})/g) {
  341. push @copyright, {
  342. year => $1,
  343. line => $line,
  344. col => index($l, $1),
  345. code => $l
  346. };
  347. }
  348. }
  349. # detect long lines
  350. if(length($l) > $max_column) {
  351. checkwarn("LONGLINE", $line, length($l), $file, $l,
  352. "Longer than $max_column columns");
  353. }
  354. # detect TAB characters
  355. if($l =~ /^(.*)\t/) {
  356. checkwarn("TABS",
  357. $line, length($1), $file, $l, "Contains TAB character", 1);
  358. }
  359. # detect trailing white space
  360. if($l =~ /^(.*)[ \t]+\z/) {
  361. checkwarn("TRAILINGSPACE",
  362. $line, length($1), $file, $l, "Trailing whitespace");
  363. }
  364. # ------------------------------------------------------------
  365. # Above this marker, the checks were done on lines *including*
  366. # comments
  367. # ------------------------------------------------------------
  368. # strip off C89 comments
  369. comment:
  370. if(!$incomment) {
  371. if($l =~ s/\/\*.*\*\// /g) {
  372. # full /* comments */ were removed!
  373. }
  374. if($l =~ s/\/\*.*//) {
  375. # start of /* comment was removed
  376. $incomment = 1;
  377. }
  378. }
  379. else {
  380. if($l =~ s/.*\*\///) {
  381. # end of comment */ was removed
  382. $incomment = 0;
  383. goto comment;
  384. }
  385. else {
  386. # still within a comment
  387. $l="";
  388. }
  389. }
  390. # ------------------------------------------------------------
  391. # Below this marker, the checks were done on lines *without*
  392. # comments
  393. # ------------------------------------------------------------
  394. # crude attempt to detect // comments without too many false
  395. # positives
  396. if($l =~ /^([^"\*]*)[^:"]\/\//) {
  397. checkwarn("CPPCOMMENTS",
  398. $line, length($1), $file, $l, "\/\/ comment");
  399. }
  400. my $nostr = nostrings($l);
  401. # check spaces after for/if/while/function call
  402. if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) {
  403. if($1 =~ / *\#/) {
  404. # this is a #if, treat it differently
  405. }
  406. elsif(defined $3 && $3 eq "return") {
  407. # return must have a space
  408. }
  409. elsif(defined $3 && $3 eq "case") {
  410. # case must have a space
  411. }
  412. elsif($4 eq "*") {
  413. # (* beginning makes the space OK!
  414. }
  415. elsif($1 =~ / *typedef/) {
  416. # typedefs can use space-paren
  417. }
  418. else {
  419. checkwarn("SPACEBEFOREPAREN", $line, length($1)+length($2), $file, $l,
  420. "$2 with space");
  421. }
  422. }
  423. if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) {
  424. my $pos = length($1);
  425. my $postparen = $5;
  426. my $cond = $4;
  427. if($cond =~ / = /) {
  428. checkwarn("ASSIGNWITHINCONDITION",
  429. $line, $pos+1, $file, $l,
  430. "assignment within conditional expression");
  431. }
  432. my $temp = $cond;
  433. $temp =~ s/\(//g; # remove open parens
  434. my $openc = length($cond) - length($temp);
  435. $temp = $cond;
  436. $temp =~ s/\)//g; # remove close parens
  437. my $closec = length($cond) - length($temp);
  438. my $even = $openc == $closec;
  439. if($l =~ / *\#/) {
  440. # this is a #if, treat it differently
  441. }
  442. elsif($even && $postparen &&
  443. ($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) {
  444. print STDERR "5: '$postparen'\n";
  445. checkwarn("ONELINECONDITION",
  446. $line, length($l)-length($postparen), $file, $l,
  447. "conditional block on the same line");
  448. }
  449. }
  450. # check spaces after open parentheses
  451. if($l =~ /^(.*[a-z])\( /i) {
  452. checkwarn("SPACEAFTERPAREN",
  453. $line, length($1)+1, $file, $l,
  454. "space after open parenthesis");
  455. }
  456. # check spaces before close parentheses, unless it was a space or a
  457. # close parenthesis!
  458. if($l =~ /(.*[^\) ]) \)/) {
  459. checkwarn("SPACEBEFORECLOSE",
  460. $line, length($1)+1, $file, $l,
  461. "space before close parenthesis");
  462. }
  463. # check spaces before comma!
  464. if($l =~ /(.*[^ ]) ,/) {
  465. checkwarn("SPACEBEFORECOMMA",
  466. $line, length($1)+1, $file, $l,
  467. "space before comma");
  468. }
  469. # check for "return(" without space
  470. if($l =~ /^(.*)return\(/) {
  471. if($1 =~ / *\#/) {
  472. # this is a #if, treat it differently
  473. }
  474. else {
  475. checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
  476. "return without space before paren");
  477. }
  478. }
  479. # check for "sizeof" without parenthesis
  480. if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) {
  481. if($1 =~ / *\#/) {
  482. # this is a #if, treat it differently
  483. }
  484. else {
  485. checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l,
  486. "sizeof without parenthesis");
  487. }
  488. }
  489. # check for comma without space
  490. if($l =~ /^(.*),[^ \n]/) {
  491. my $pref=$1;
  492. my $ign=0;
  493. if($pref =~ / *\#/) {
  494. # this is a #if, treat it differently
  495. $ign=1;
  496. }
  497. elsif($pref =~ /\/\*/) {
  498. # this is a comment
  499. $ign=1;
  500. }
  501. elsif($pref =~ /[\"\']/) {
  502. $ign = 1;
  503. # There is a quote here, figure out whether the comma is
  504. # within a string or '' or not.
  505. if($pref =~ /\"/) {
  506. # within a string
  507. }
  508. elsif($pref =~ /\'$/) {
  509. # a single letter
  510. }
  511. else {
  512. $ign = 0;
  513. }
  514. }
  515. if(!$ign) {
  516. checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
  517. "comma without following space");
  518. }
  519. }
  520. # check for "} else"
  521. if($l =~ /^(.*)\} *else/) {
  522. checkwarn("BRACEELSE",
  523. $line, length($1), $file, $l, "else after closing brace on same line");
  524. }
  525. # check for "){"
  526. if($l =~ /^(.*)\)\{/) {
  527. checkwarn("PARENBRACE",
  528. $line, length($1)+1, $file, $l, "missing space after close paren");
  529. }
  530. # check for space before the semicolon last in a line
  531. if($l =~ /^(.*[^ ].*) ;$/) {
  532. checkwarn("SPACESEMICOLON",
  533. $line, length($1), $file, $ol, "space before last semicolon");
  534. }
  535. # scan for use of banned functions
  536. if($l =~ /^(.*\W)
  537. (gmtime|localtime|
  538. gets|
  539. strtok|
  540. v?sprintf|
  541. (str|_mbs|_tcs|_wcs)n?cat|
  542. LoadLibrary(Ex)?(A|W)?)
  543. \s*\(
  544. /x) {
  545. checkwarn("BANNEDFUNC",
  546. $line, length($1), $file, $ol,
  547. "use of $2 is banned");
  548. }
  549. # scan for use of snprintf for curl-internals reasons
  550. if($l =~ /^(.*\W)(v?snprintf)\s*\(/x) {
  551. checkwarn("SNPRINTF",
  552. $line, length($1), $file, $ol,
  553. "use of $2 is banned");
  554. }
  555. # scan for use of non-binary fopen without the macro
  556. if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
  557. my $mode = $2;
  558. if($mode !~ /b/) {
  559. checkwarn("FOPENMODE",
  560. $line, length($1), $file, $ol,
  561. "use of non-binary fopen without FOPEN_* macro: $mode");
  562. }
  563. }
  564. # check for open brace first on line but not first column
  565. # only alert if previous line ended with a close paren and wasn't a cpp
  566. # line
  567. if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
  568. checkwarn("BRACEPOS",
  569. $line, length($1), $file, $ol, "badly placed open brace");
  570. }
  571. # if the previous line starts with if/while/for AND ends with an open
  572. # brace, or an else statement, check that this line is indented $indent
  573. # more steps, if not a cpp line
  574. if($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/) {
  575. my $first = length($1);
  576. # this line has some character besides spaces
  577. if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
  578. my $second = length($1);
  579. my $expect = $first+$indent;
  580. if($expect != $second) {
  581. my $diff = $second - $first;
  582. checkwarn("INDENTATION", $line, length($1), $file, $ol,
  583. "not indented $indent steps (uses $diff)");
  584. }
  585. }
  586. }
  587. # check for 'char * name'
  588. if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) {
  589. checkwarn("ASTERISKSPACE",
  590. $line, length($1), $file, $ol,
  591. "space after declarative asterisk");
  592. }
  593. # check for 'char*'
  594. if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
  595. checkwarn("ASTERISKNOSPACE",
  596. $line, length($1)-1, $file, $ol,
  597. "no space before asterisk");
  598. }
  599. # check for 'void func() {', but avoid false positives by requiring
  600. # both an open and closed parentheses before the open brace
  601. if($l =~ /^((\w).*)\{\z/) {
  602. my $k = $1;
  603. $k =~ s/const *//;
  604. $k =~ s/static *//;
  605. if($k =~ /\(.*\)/) {
  606. checkwarn("BRACEPOS",
  607. $line, length($l)-1, $file, $ol,
  608. "wrongly placed open brace");
  609. }
  610. }
  611. # check for equals sign without spaces next to it
  612. if($nostr =~ /(.*)\=[a-z0-9]/i) {
  613. checkwarn("EQUALSNOSPACE",
  614. $line, length($1)+1, $file, $ol,
  615. "no space after equals sign");
  616. }
  617. # check for equals sign without spaces before it
  618. elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
  619. checkwarn("NOSPACEEQUALS",
  620. $line, length($1)+1, $file, $ol,
  621. "no space before equals sign");
  622. }
  623. # check for plus signs without spaces next to it
  624. if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
  625. checkwarn("PLUSNOSPACE",
  626. $line, length($1)+1, $file, $ol,
  627. "no space after plus sign");
  628. }
  629. # check for plus sign without spaces before it
  630. elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
  631. checkwarn("NOSPACEPLUS",
  632. $line, length($1)+1, $file, $ol,
  633. "no space before plus sign");
  634. }
  635. # check for semicolons without space next to it
  636. if($nostr =~ /(.*)\;[a-z0-9]/i) {
  637. checkwarn("SEMINOSPACE",
  638. $line, length($1)+1, $file, $ol,
  639. "no space after semicolon");
  640. }
  641. # typedef struct ... {
  642. if($nostr =~ /^(.*)typedef struct.*{/) {
  643. checkwarn("TYPEDEFSTRUCT",
  644. $line, length($1)+1, $file, $ol,
  645. "typedef'ed struct");
  646. }
  647. # check for more than one consecutive space before open brace or
  648. # question mark. Skip lines containing strings since they make it hard
  649. # due to artificially getting multiple spaces
  650. if(($l eq $nostr) &&
  651. $nostr =~ /^(.*(\S)) + [{?]/i) {
  652. checkwarn("MULTISPACE",
  653. $line, length($1)+1, $file, $ol,
  654. "multiple space");
  655. print STDERR "L: $l\n";
  656. print STDERR "nostr: $nostr\n";
  657. }
  658. $line++;
  659. $prevl = $ol;
  660. }
  661. if(!scalar(@copyright)) {
  662. checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
  663. }
  664. # COPYRIGHTYEAR is a extended warning so we must first see if it has been
  665. # enabled in .checksrc
  666. if(defined($warnings{"COPYRIGHTYEAR"})) {
  667. # The check for updated copyrightyear is overly complicated in order to
  668. # not punish current hacking for past sins. The copyright years are
  669. # right now a bit behind, so enforcing copyright year checking on all
  670. # files would cause hundreds of errors. Instead we only look at files
  671. # which are tracked in the Git repo and edited in the workdir, or
  672. # committed locally on the branch without being in upstream master.
  673. #
  674. # The simple and naive test is to simply check for the current year,
  675. # but updating the year even without an edit is against project policy
  676. # (and it would fail every file on January 1st).
  677. #
  678. # A rather more interesting, and correct, check would be to not test
  679. # only locally committed files but inspect all files wrt the year of
  680. # their last commit. Removing the `git rev-list origin/master..HEAD`
  681. # condition below will enfore copyright year checks against the year
  682. # the file was last committed (and thus edited to some degree).
  683. my $commityear = undef;
  684. @copyright = sort {$$b{year} cmp $$a{year}} @copyright;
  685. # if the file is modified, assume commit year this year
  686. if(`git status -s -- $file` =~ /^ [MARCU]/) {
  687. $commityear = (localtime(time))[5] + 1900;
  688. }
  689. else {
  690. # min-parents=1 to ignore wrong initial commit in truncated repos
  691. my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- $file`;
  692. if($grl) {
  693. chomp $grl;
  694. $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
  695. }
  696. }
  697. if(defined($commityear) && scalar(@copyright) &&
  698. $copyright[0]{year} != $commityear) {
  699. checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col},
  700. $file, $copyright[0]{code},
  701. "Copyright year out of date, should be $commityear, " .
  702. "is $copyright[0]{year}", 1);
  703. }
  704. }
  705. if($incomment) {
  706. checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
  707. }
  708. checksrc_endoffile($file);
  709. close(R);
  710. }
  711. if($errors || $warnings || $verbose) {
  712. printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
  713. if($suppressed) {
  714. printf "checksrc: %d errors and %d warnings suppressed\n",
  715. $serrors,
  716. $swarnings;
  717. }
  718. exit 5; # return failure
  719. }