2
0

checksrc.pl 37 KB

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