checksrc.pl 33 KB

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