checksrc.pl 24 KB

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