checksrc.pl 37 KB

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