2
0

checksrc.pl 38 KB

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