checksrc.pl 33 KB

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