checksrc.pl 27 KB

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