checksrc.pl 17 KB

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