checksrc.pl 32 KB

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