checksrc.pl 34 KB

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