checksrc.pl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2011 - 2018, 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.haxx.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. ###########################################################################
  23. my $max_column = 79;
  24. my $indent = 2;
  25. my $warnings;
  26. my $errors;
  27. my $suppressed; # whitelisted problems
  28. my $file;
  29. my $dir=".";
  30. my $wlist;
  31. my $windows_os = $^O eq 'MSWin32' || $^O eq 'msys' || $^O eq 'cygwin';
  32. my $verbose;
  33. my %whitelist;
  34. my %warnings = (
  35. 'LONGLINE' => "Line longer than $max_column",
  36. 'TABS' => 'TAB characters not allowed',
  37. 'TRAILINGSPACE' => 'Trailing white space on the line',
  38. 'CPPCOMMENTS' => '// comment detected',
  39. 'SPACEBEFOREPAREN' => 'space before an open parenthesis',
  40. 'SPACEAFTERPAREN' => 'space after open parenthesis',
  41. 'SPACEBEFORECLOSE' => 'space before a close parenthesis',
  42. 'SPACEBEFORECOMMA' => 'space before a comma',
  43. 'RETURNNOSPACE' => 'return without space',
  44. 'COMMANOSPACE' => 'comma without following space',
  45. 'BRACEELSE' => '} else on the same line',
  46. 'PARENBRACE' => '){ without sufficient space',
  47. 'SPACESEMICOLON' => 'space before semicolon',
  48. 'BANNEDFUNC' => 'a banned function was used',
  49. 'FOPENMODE' => 'fopen needs a macro for the mode string',
  50. 'BRACEPOS' => 'wrong position for an open brace',
  51. 'INDENTATION' => 'wrong start column for code',
  52. 'COPYRIGHT' => 'file missing a copyright statement',
  53. 'BADCOMMAND' => 'bad !checksrc! instruction',
  54. 'UNUSEDIGNORE' => 'a warning ignore was not used',
  55. 'OPENCOMMENT' => 'file ended with a /* comment still "open"',
  56. 'ASTERISKSPACE' => 'pointer declared with space after asterisk',
  57. 'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
  58. 'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
  59. 'EQUALSNOSPACE' => 'equals sign without following space',
  60. 'NOSPACEEQUALS' => 'equals sign without preceding space',
  61. 'SEMINOSPACE' => 'semicolon without following space',
  62. 'MULTISPACE' => 'multiple spaces used when not suitable',
  63. 'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
  64. );
  65. sub readwhitelist {
  66. open(W, "<$dir/checksrc.whitelist");
  67. my @all=<W>;
  68. for(@all) {
  69. $windows_os ? $_ =~ s/\r?\n$// : chomp;
  70. $whitelist{$_}=1;
  71. }
  72. close(W);
  73. }
  74. sub checkwarn {
  75. my ($name, $num, $col, $file, $line, $msg, $error) = @_;
  76. my $w=$error?"error":"warning";
  77. my $nowarn=0;
  78. #if(!$warnings{$name}) {
  79. # print STDERR "Dev! there's no description for $name!\n";
  80. #}
  81. # checksrc.whitelist
  82. if($whitelist{$line}) {
  83. $nowarn = 1;
  84. }
  85. # !checksrc! controlled
  86. elsif($ignore{$name}) {
  87. $ignore{$name}--;
  88. $ignore_used{$name}++;
  89. $nowarn = 1;
  90. if(!$ignore{$name}) {
  91. # reached zero, enable again
  92. enable_warn($name, $line, $file, $l);
  93. }
  94. }
  95. if($nowarn) {
  96. $suppressed++;
  97. if($w) {
  98. $swarnings++;
  99. }
  100. else {
  101. $serrors++;
  102. }
  103. return;
  104. }
  105. if($w) {
  106. $warnings++;
  107. }
  108. else {
  109. $errors++;
  110. }
  111. $col++;
  112. print "$file:$num:$col: $w: $msg ($name)\n";
  113. print " $line\n";
  114. if($col < 80) {
  115. my $pref = (' ' x $col);
  116. print "${pref}^\n";
  117. }
  118. }
  119. $file = shift @ARGV;
  120. while(1) {
  121. if($file =~ /-D(.*)/) {
  122. $dir = $1;
  123. $file = shift @ARGV;
  124. next;
  125. }
  126. elsif($file =~ /-W(.*)/) {
  127. $wlist .= " $1 ";
  128. $file = shift @ARGV;
  129. next;
  130. }
  131. elsif($file =~ /-i([1-9])/) {
  132. $indent = $1 + 0;
  133. $file = shift @ARGV;
  134. next;
  135. }
  136. elsif($file =~ /-m([0-9]+)/) {
  137. $max_column = $1 + 0;
  138. $file = shift @ARGV;
  139. next;
  140. }
  141. elsif($file =~ /^(-h|--help)/) {
  142. undef $file;
  143. last;
  144. }
  145. last;
  146. }
  147. if(!$file) {
  148. print "checksrc.pl [option] <file1> [file2] ...\n";
  149. print " Options:\n";
  150. print " -D[DIR] Directory to prepend file names\n";
  151. print " -h Show help output\n";
  152. print " -W[file] Whitelist the given file - ignore all its flaws\n";
  153. print " -i<n> Indent spaces. Default: 2\n";
  154. print " -m<n> Maximum line length. Default: 79\n";
  155. print "\nDetects and warns for these problems:\n";
  156. for(sort keys %warnings) {
  157. printf (" %-18s: %s\n", $_, $warnings{$_});
  158. }
  159. exit;
  160. }
  161. readwhitelist();
  162. do {
  163. if("$wlist" !~ / $file /) {
  164. my $fullname = $file;
  165. $fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
  166. scanfile($fullname);
  167. }
  168. $file = shift @ARGV;
  169. } while($file);
  170. sub checksrc_clear {
  171. undef %ignore;
  172. undef %ignore_set;
  173. undef @ignore_line;
  174. }
  175. sub checksrc_endoffile {
  176. my ($file) = @_;
  177. for(keys %ignore_set) {
  178. if($ignore_set{$_} && !$ignore_used{$_}) {
  179. checkwarn("UNUSEDIGNORE", $ignore_set{$_},
  180. length($_)+11, $file,
  181. $ignore_line[$ignore_set{$_}],
  182. "Unused ignore: $_");
  183. }
  184. }
  185. }
  186. sub enable_warn {
  187. my ($what, $line, $file, $l) = @_;
  188. # switch it back on, but warn if not triggered!
  189. if(!$ignore_used{$what}) {
  190. checkwarn("UNUSEDIGNORE",
  191. $line, length($what) + 11, $file, $l,
  192. "No warning was inhibited!");
  193. }
  194. $ignore_set{$what}=0;
  195. $ignore_used{$what}=0;
  196. $ignore{$what}=0;
  197. }
  198. sub checksrc {
  199. my ($cmd, $line, $file, $l) = @_;
  200. if($cmd =~ / *([^ ]*) *(.*)/) {
  201. my ($enable, $what) = ($1, $2);
  202. $what =~ s: *\*/$::; # cut off end of C comment
  203. # print "ENABLE $enable WHAT $what\n";
  204. if($enable eq "disable") {
  205. my ($warn, $scope)=($1, $2);
  206. if($what =~ /([^ ]*) +(.*)/) {
  207. ($warn, $scope)=($1, $2);
  208. }
  209. else {
  210. $warn = $what;
  211. $scope = 1;
  212. }
  213. # print "IGNORE $warn for SCOPE $scope\n";
  214. if($scope eq "all") {
  215. $scope=999999;
  216. }
  217. if($ignore_set{$warn}) {
  218. checkwarn("BADCOMMAND",
  219. $line, 0, $file, $l,
  220. "$warn already disabled from line $ignore_set{$warn}");
  221. }
  222. else {
  223. $ignore{$warn}=$scope;
  224. $ignore_set{$warn}=$line;
  225. $ignore_line[$line]=$l;
  226. }
  227. }
  228. elsif($enable eq "enable") {
  229. enable_warn($what, $line, $file, $l);
  230. }
  231. else {
  232. checkwarn("BADCOMMAND",
  233. $line, 0, $file, $l,
  234. "Illegal !checksrc! command");
  235. }
  236. }
  237. }
  238. sub nostrings {
  239. my ($str) = @_;
  240. $str =~ s/\".*\"//g;
  241. return $str;
  242. }
  243. sub scanfile {
  244. my ($file) = @_;
  245. my $line = 1;
  246. my $prevl;
  247. my $l;
  248. open(R, "<$file") || die "failed to open $file";
  249. my $incomment=0;
  250. my $copyright=0;
  251. checksrc_clear(); # for file based ignores
  252. while(<R>) {
  253. $windows_os ? $_ =~ s/\r?\n$// : chomp;
  254. my $l = $_;
  255. my $ol = $l; # keep the unmodified line for error reporting
  256. my $column = 0;
  257. # check for !checksrc! commands
  258. if($l =~ /\!checksrc\! (.*)/) {
  259. my $cmd = $1;
  260. checksrc($cmd, $line, $file, $l)
  261. }
  262. # check for a copyright statement
  263. if(!$copyright && ($l =~ /copyright .* \d\d\d\d/i)) {
  264. $copyright=1;
  265. }
  266. # detect long lines
  267. if(length($l) > $max_column) {
  268. checkwarn("LONGLINE", $line, length($l), $file, $l,
  269. "Longer than $max_column columns");
  270. }
  271. # detect TAB characters
  272. if($l =~ /^(.*)\t/) {
  273. checkwarn("TABS",
  274. $line, length($1), $file, $l, "Contains TAB character", 1);
  275. }
  276. # detect trailing white space
  277. if($l =~ /^(.*)[ \t]+\z/) {
  278. checkwarn("TRAILINGSPACE",
  279. $line, length($1), $file, $l, "Trailing whitespace");
  280. }
  281. # ------------------------------------------------------------
  282. # Above this marker, the checks were done on lines *including*
  283. # comments
  284. # ------------------------------------------------------------
  285. # strip off C89 comments
  286. comment:
  287. if(!$incomment) {
  288. if($l =~ s/\/\*.*\*\// /g) {
  289. # full /* comments */ were removed!
  290. }
  291. if($l =~ s/\/\*.*//) {
  292. # start of /* comment was removed
  293. $incomment = 1;
  294. }
  295. }
  296. else {
  297. if($l =~ s/.*\*\///) {
  298. # end of comment */ was removed
  299. $incomment = 0;
  300. goto comment;
  301. }
  302. else {
  303. # still within a comment
  304. $l="";
  305. }
  306. }
  307. # ------------------------------------------------------------
  308. # Below this marker, the checks were done on lines *without*
  309. # comments
  310. # ------------------------------------------------------------
  311. # crude attempt to detect // comments without too many false
  312. # positives
  313. if($l =~ /^([^"\*]*)[^:"]\/\//) {
  314. checkwarn("CPPCOMMENTS",
  315. $line, length($1), $file, $l, "\/\/ comment");
  316. }
  317. my $nostr = nostrings($l);
  318. # check spaces after for/if/while/function call
  319. if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) {
  320. if($1 =~ / *\#/) {
  321. # this is a #if, treat it differently
  322. }
  323. elsif($3 eq "return") {
  324. # return must have a space
  325. }
  326. elsif($3 eq "case") {
  327. # case must have a space
  328. }
  329. elsif($4 eq "*") {
  330. # (* beginning makes the space OK!
  331. }
  332. elsif($1 =~ / *typedef/) {
  333. # typedefs can use space-paren
  334. }
  335. else {
  336. checkwarn("SPACEBEFOREPAREN", $line, length($1)+length($2), $file, $l,
  337. "$2 with space");
  338. }
  339. }
  340. if($nostr =~ /^((.*)(if) *\()(.*)\)/) {
  341. my $pos = length($1);
  342. if($4 =~ / = /) {
  343. checkwarn("ASSIGNWITHINCONDITION",
  344. $line, $pos+1, $file, $l,
  345. "assignment within conditional expression");
  346. }
  347. }
  348. # check spaces after open parentheses
  349. if($l =~ /^(.*[a-z])\( /i) {
  350. checkwarn("SPACEAFTERPAREN",
  351. $line, length($1)+1, $file, $l,
  352. "space after open parenthesis");
  353. }
  354. # check spaces before close parentheses, unless it was a space or a
  355. # close parenthesis!
  356. if($l =~ /(.*[^\) ]) \)/) {
  357. checkwarn("SPACEBEFORECLOSE",
  358. $line, length($1)+1, $file, $l,
  359. "space before close parenthesis");
  360. }
  361. # check spaces before comma!
  362. if($l =~ /(.*[^ ]) ,/) {
  363. checkwarn("SPACEBEFORECOMMA",
  364. $line, length($1)+1, $file, $l,
  365. "space before comma");
  366. }
  367. # check for "return(" without space
  368. if($l =~ /^(.*)return\(/) {
  369. if($1 =~ / *\#/) {
  370. # this is a #if, treat it differently
  371. }
  372. else {
  373. checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
  374. "return without space before paren");
  375. }
  376. }
  377. # check for "sizeof" without parenthesis
  378. if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) {
  379. if($1 =~ / *\#/) {
  380. # this is a #if, treat it differently
  381. }
  382. else {
  383. checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l,
  384. "sizeof without parenthesis");
  385. }
  386. }
  387. # check for comma without space
  388. if($l =~ /^(.*),[^ \n]/) {
  389. my $pref=$1;
  390. my $ign=0;
  391. if($pref =~ / *\#/) {
  392. # this is a #if, treat it differently
  393. $ign=1;
  394. }
  395. elsif($pref =~ /\/\*/) {
  396. # this is a comment
  397. $ign=1;
  398. }
  399. elsif($pref =~ /[\"\']/) {
  400. $ign = 1;
  401. # There is a quote here, figure out whether the comma is
  402. # within a string or '' or not.
  403. if($pref =~ /\"/) {
  404. # within a string
  405. }
  406. elsif($pref =~ /\'$/) {
  407. # a single letter
  408. }
  409. else {
  410. $ign = 0;
  411. }
  412. }
  413. if(!$ign) {
  414. checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
  415. "comma without following space");
  416. }
  417. }
  418. # check for "} else"
  419. if($l =~ /^(.*)\} *else/) {
  420. checkwarn("BRACEELSE",
  421. $line, length($1), $file, $l, "else after closing brace on same line");
  422. }
  423. # check for "){"
  424. if($l =~ /^(.*)\)\{/) {
  425. checkwarn("PARENBRACE",
  426. $line, length($1)+1, $file, $l, "missing space after close paren");
  427. }
  428. # check for space before the semicolon last in a line
  429. if($l =~ /^(.*[^ ].*) ;$/) {
  430. checkwarn("SPACESEMICOLON",
  431. $line, length($1), $file, $ol, "space before last semicolon");
  432. }
  433. # scan for use of banned functions
  434. if($l =~ /^(.*\W)
  435. (gets|
  436. strtok|
  437. v?sprintf|
  438. (str|_mbs|_tcs|_wcs)n?cat|
  439. LoadLibrary(Ex)?(A|W)?)
  440. \s*\(
  441. /x) {
  442. checkwarn("BANNEDFUNC",
  443. $line, length($1), $file, $ol,
  444. "use of $2 is banned");
  445. }
  446. # scan for use of non-binary fopen without the macro
  447. if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
  448. my $mode = $2;
  449. if($mode !~ /b/) {
  450. checkwarn("FOPENMODE",
  451. $line, length($1), $file, $ol,
  452. "use of non-binary fopen without FOPEN_* macro: $mode");
  453. }
  454. }
  455. # check for open brace first on line but not first column
  456. # only alert if previous line ended with a close paren and wasn't a cpp
  457. # line
  458. if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
  459. checkwarn("BRACEPOS",
  460. $line, length($1), $file, $ol, "badly placed open brace");
  461. }
  462. # if the previous line starts with if/while/for AND ends with an open
  463. # brace, or an else statement, check that this line is indented $indent
  464. # more steps, if not a cpp line
  465. if($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/) {
  466. my $first = length($1);
  467. # this line has some character besides spaces
  468. if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
  469. my $second = length($1);
  470. my $expect = $first+$indent;
  471. if($expect != $second) {
  472. my $diff = $second - $first;
  473. checkwarn("INDENTATION", $line, length($1), $file, $ol,
  474. "not indented $indent steps (uses $diff)");
  475. }
  476. }
  477. }
  478. # check for 'char * name'
  479. if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost) *(\*+)) (\w+)/) && ($4 ne "const")) {
  480. checkwarn("ASTERISKNOSPACE",
  481. $line, length($1), $file, $ol,
  482. "no space after declarative asterisk");
  483. }
  484. # check for 'char*'
  485. if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
  486. checkwarn("ASTERISKNOSPACE",
  487. $line, length($1)-1, $file, $ol,
  488. "no space before asterisk");
  489. }
  490. # check for 'void func() {', but avoid false positives by requiring
  491. # both an open and closed parentheses before the open brace
  492. if($l =~ /^((\w).*)\{\z/) {
  493. my $k = $1;
  494. $k =~ s/const *//;
  495. $k =~ s/static *//;
  496. if($k =~ /\(.*\)/) {
  497. checkwarn("BRACEPOS",
  498. $line, length($l)-1, $file, $ol,
  499. "wrongly placed open brace");
  500. }
  501. }
  502. # check for equals sign without spaces next to it
  503. if($nostr =~ /(.*)\=[a-z0-9]/i) {
  504. checkwarn("EQUALSNOSPACE",
  505. $line, length($1)+1, $file, $ol,
  506. "no space after equals sign");
  507. }
  508. # check for equals sign without spaces before it
  509. elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
  510. checkwarn("NOSPACEEQUALS",
  511. $line, length($1)+1, $file, $ol,
  512. "no space before equals sign");
  513. }
  514. # check for plus signs without spaces next to it
  515. if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
  516. checkwarn("PLUSNOSPACE",
  517. $line, length($1)+1, $file, $ol,
  518. "no space after plus sign");
  519. }
  520. # check for plus sign without spaces before it
  521. elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
  522. checkwarn("NOSPACEPLUS",
  523. $line, length($1)+1, $file, $ol,
  524. "no space before plus sign");
  525. }
  526. # check for semicolons without space next to it
  527. if($nostr =~ /(.*)\;[a-z0-9]/i) {
  528. checkwarn("SEMINOSPACE",
  529. $line, length($1)+1, $file, $ol,
  530. "no space after semicolon");
  531. }
  532. # check for more than one consecutive space before open brace or
  533. # question mark. Skip lines containing strings since they make it hard
  534. # due to artificially getting multiple spaces
  535. if(($l eq $nostr) &&
  536. $nostr =~ /^(.*(\S)) + [{?]/i) {
  537. checkwarn("MULTISPACE",
  538. $line, length($1)+1, $file, $ol,
  539. "multiple space");
  540. print STDERR "L: $l\n";
  541. print STDERR "nostr: $nostr\n";
  542. }
  543. $line++;
  544. $prevl = $ol;
  545. }
  546. if(!$copyright) {
  547. checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
  548. }
  549. if($incomment) {
  550. checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
  551. }
  552. checksrc_endoffile($file);
  553. close(R);
  554. }
  555. if($errors || $warnings || $verbose) {
  556. printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
  557. if($suppressed) {
  558. printf "checksrc: %d errors and %d warnings suppressed\n",
  559. $serrors,
  560. $swarnings;
  561. }
  562. exit 5; # return failure
  563. }