2
0

checksrc.pl 33 KB

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