checksrc.pl 19 KB

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