checksrc.pl 25 KB

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