2
0

checksrc.pl 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. _?w?access)
  660. \s*\(
  661. /x) {
  662. checkwarn("BANNEDFUNC",
  663. $line, length($1), $file, $ol,
  664. "use of $2 is banned");
  665. }
  666. if($warnings{"STRERROR"}) {
  667. # scan for use of banned strerror. This is not a BANNEDFUNC to
  668. # allow for individual enable/disable of this warning.
  669. if($l =~ /^(.*\W)(strerror)\s*\(/x) {
  670. if($1 !~ /^ *\#/) {
  671. # skip preprocessor lines
  672. checkwarn("STRERROR",
  673. $line, length($1), $file, $ol,
  674. "use of $2 is banned");
  675. }
  676. }
  677. }
  678. if($warnings{"STDERR"}) {
  679. # scan for use of banned stderr. This is not a BANNEDFUNC to
  680. # allow for individual enable/disable of this warning.
  681. if($l =~ /^([^\"-]*\W)(stderr)[^\"_]/x) {
  682. if($1 !~ /^ *\#/) {
  683. # skip preprocessor lines
  684. checkwarn("STDERR",
  685. $line, length($1), $file, $ol,
  686. "use of $2 is banned (use tool_stderr instead)");
  687. }
  688. }
  689. }
  690. # scan for use of snprintf for curl-internals reasons
  691. if($l =~ /^(.*\W)(v?snprintf)\s*\(/x) {
  692. checkwarn("SNPRINTF",
  693. $line, length($1), $file, $ol,
  694. "use of $2 is banned");
  695. }
  696. # scan for use of non-binary fopen without the macro
  697. if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
  698. my $mode = $2;
  699. if($mode !~ /b/) {
  700. checkwarn("FOPENMODE",
  701. $line, length($1), $file, $ol,
  702. "use of non-binary fopen without FOPEN_* macro: $mode");
  703. }
  704. }
  705. # check for open brace first on line but not first column only alert
  706. # if previous line ended with a close paren and it wasn't a cpp line
  707. if(($prevl =~ /\)\z/) && ($l =~ /^( +)\{/) && !$prevp) {
  708. checkwarn("BRACEPOS",
  709. $line, length($1), $file, $ol, "badly placed open brace");
  710. }
  711. # if the previous line starts with if/while/for AND ends with an open
  712. # brace, or an else statement, check that this line is indented $indent
  713. # more steps, if not a cpp line
  714. if(!$prevp && ($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/)) {
  715. my $first = length($1);
  716. # this line has some character besides spaces
  717. if($l =~ /^( *)[^ ]/) {
  718. my $second = length($1);
  719. my $expect = $first+$indent;
  720. if($expect != $second) {
  721. my $diff = $second - $first;
  722. checkwarn("INDENTATION", $line, length($1), $file, $ol,
  723. "not indented $indent steps (uses $diff)");
  724. }
  725. }
  726. }
  727. # if the previous line starts with if/while/for AND ends with a closed
  728. # parenthesis and there's an equal number of open and closed
  729. # parentheses, check that this line is indented $indent more steps, if
  730. # not a cpp line
  731. elsif(!$prevp && ($prevl =~ /^( *)(if|while|for)(\(.*\))\z/)) {
  732. my $first = length($1);
  733. my $op = $3;
  734. my $cl = $3;
  735. $op =~ s/[^(]//g;
  736. $cl =~ s/[^)]//g;
  737. if(length($op) == length($cl)) {
  738. # this line has some character besides spaces
  739. if($l =~ /^( *)[^ ]/) {
  740. my $second = length($1);
  741. my $expect = $first+$indent;
  742. if($expect != $second) {
  743. my $diff = $second - $first;
  744. checkwarn("INDENTATION", $line, length($1), $file, $ol,
  745. "not indented $indent steps (uses $diff)");
  746. }
  747. }
  748. }
  749. }
  750. # check for 'char * name'
  751. if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) {
  752. checkwarn("ASTERISKSPACE",
  753. $line, length($1), $file, $ol,
  754. "space after declarative asterisk");
  755. }
  756. # check for 'char*'
  757. if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
  758. checkwarn("ASTERISKNOSPACE",
  759. $line, length($1)-1, $file, $ol,
  760. "no space before asterisk");
  761. }
  762. # check for 'void func() {', but avoid false positives by requiring
  763. # both an open and closed parentheses before the open brace
  764. if($l =~ /^((\w).*)\{\z/) {
  765. my $k = $1;
  766. $k =~ s/const *//;
  767. $k =~ s/static *//;
  768. if($k =~ /\(.*\)/) {
  769. checkwarn("BRACEPOS",
  770. $line, length($l)-1, $file, $ol,
  771. "wrongly placed open brace");
  772. }
  773. }
  774. # check for equals sign without spaces next to it
  775. if($nostr =~ /(.*)\=[a-z0-9]/i) {
  776. checkwarn("EQUALSNOSPACE",
  777. $line, length($1)+1, $file, $ol,
  778. "no space after equals sign");
  779. }
  780. # check for equals sign without spaces before it
  781. elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
  782. checkwarn("NOSPACEEQUALS",
  783. $line, length($1)+1, $file, $ol,
  784. "no space before equals sign");
  785. }
  786. # check for plus signs without spaces next to it
  787. if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
  788. checkwarn("PLUSNOSPACE",
  789. $line, length($1)+1, $file, $ol,
  790. "no space after plus sign");
  791. }
  792. # check for plus sign without spaces before it
  793. elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
  794. checkwarn("NOSPACEPLUS",
  795. $line, length($1)+1, $file, $ol,
  796. "no space before plus sign");
  797. }
  798. # check for semicolons without space next to it
  799. if($nostr =~ /(.*)\;[a-z0-9]/i) {
  800. checkwarn("SEMINOSPACE",
  801. $line, length($1)+1, $file, $ol,
  802. "no space after semicolon");
  803. }
  804. # typedef struct ... {
  805. if($nostr =~ /^(.*)typedef struct.*{/) {
  806. checkwarn("TYPEDEFSTRUCT",
  807. $line, length($1)+1, $file, $ol,
  808. "typedef'ed struct");
  809. }
  810. if($nostr =~ /(.*)! +(\w|\()/) {
  811. checkwarn("EXCLAMATIONSPACE",
  812. $line, length($1)+1, $file, $ol,
  813. "space after exclamation mark");
  814. }
  815. # check for more than one consecutive space before open brace or
  816. # question mark. Skip lines containing strings since they make it hard
  817. # due to artificially getting multiple spaces
  818. if(($l eq $nostr) &&
  819. $nostr =~ /^(.*(\S)) + [{?]/i) {
  820. checkwarn("MULTISPACE",
  821. $line, length($1)+1, $file, $ol,
  822. "multiple spaces");
  823. }
  824. preproc:
  825. if($prep) {
  826. # scan for use of banned symbols on a preprocessor line
  827. if($l =~ /^(^|.*\W)
  828. (WIN32)
  829. (\W|$)
  830. /x) {
  831. checkwarn("BANNEDPREPROC",
  832. $line, length($1), $file, $ol,
  833. "use of $2 is banned from preprocessor lines" .
  834. (($2 eq "WIN32") ? ", use _WIN32 instead" : ""));
  835. }
  836. }
  837. $line++;
  838. $prevp = $prep;
  839. $prevl = $ol if(!$prep);
  840. $prevpl = $ol if($prep);
  841. }
  842. if(!scalar(@copyright)) {
  843. checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
  844. }
  845. # COPYRIGHTYEAR is an extended warning so we must first see if it has been
  846. # enabled in .checksrc
  847. if(defined($warnings{"COPYRIGHTYEAR"})) {
  848. # The check for updated copyrightyear is overly complicated in order to
  849. # not punish current hacking for past sins. The copyright years are
  850. # right now a bit behind, so enforcing copyright year checking on all
  851. # files would cause hundreds of errors. Instead we only look at files
  852. # which are tracked in the Git repo and edited in the workdir, or
  853. # committed locally on the branch without being in upstream master.
  854. #
  855. # The simple and naive test is to simply check for the current year,
  856. # but updating the year even without an edit is against project policy
  857. # (and it would fail every file on January 1st).
  858. #
  859. # A rather more interesting, and correct, check would be to not test
  860. # only locally committed files but inspect all files wrt the year of
  861. # their last commit. Removing the `git rev-list origin/master..HEAD`
  862. # condition below will enforce copyright year checks against the year
  863. # the file was last committed (and thus edited to some degree).
  864. my $commityear = undef;
  865. @copyright = sort {$$b{year} cmp $$a{year}} @copyright;
  866. # if the file is modified, assume commit year this year
  867. if(`git status -s -- "$file"` =~ /^ [MARCU]/) {
  868. $commityear = (localtime(time))[5] + 1900;
  869. }
  870. else {
  871. # min-parents=1 to ignore wrong initial commit in truncated repos
  872. my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- "$file"`;
  873. if($grl) {
  874. chomp $grl;
  875. $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
  876. }
  877. }
  878. if(defined($commityear) && scalar(@copyright) &&
  879. $copyright[0]{year} != $commityear) {
  880. checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col},
  881. $file, $copyright[0]{code},
  882. "Copyright year out of date, should be $commityear, " .
  883. "is $copyright[0]{year}", 1);
  884. }
  885. }
  886. if($incomment) {
  887. checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
  888. }
  889. checksrc_endoffile($file);
  890. close($R);
  891. }
  892. if($errors || $warnings || $verbose) {
  893. printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
  894. if($suppressed) {
  895. printf "checksrc: %d errors and %d warnings suppressed\n",
  896. $serrors,
  897. $swarnings;
  898. }
  899. exit 5; # return failure
  900. }