find-doc-nits 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. #! /usr/bin/env perl
  2. # Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. require 5.10.0;
  9. use warnings;
  10. use strict;
  11. use Pod::Checker;
  12. use File::Find;
  13. use File::Basename;
  14. use File::Spec::Functions;
  15. use Getopt::Std;
  16. use lib catdir(dirname($0), "perl");
  17. use OpenSSL::Util::Pod;
  18. # Options.
  19. our($opt_d);
  20. our($opt_e);
  21. our($opt_s);
  22. our($opt_o);
  23. our($opt_h);
  24. our($opt_l);
  25. our($opt_n);
  26. our($opt_p);
  27. our($opt_u);
  28. our($opt_v);
  29. our($opt_c);
  30. sub help()
  31. {
  32. print <<EOF;
  33. Find small errors (nits) in documentation. Options:
  34. -d Detailed list of undocumented (implies -u)
  35. -e Detailed list of new undocumented (implies -v)
  36. -s Same as -e except no output is generated if nothing is undocumented
  37. -o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v)
  38. -l Print bogus links
  39. -n Print nits in POD pages
  40. -p Warn if non-public name documented (implies -n)
  41. -u Count undocumented functions
  42. -v Count new undocumented functions
  43. -h Print this help message
  44. -c List undocumented commands and options
  45. EOF
  46. exit;
  47. }
  48. my $temp = '/tmp/docnits.txt';
  49. my $OUT;
  50. my %public;
  51. my %mandatory_sections =
  52. ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
  53. 1 => [ 'SYNOPSIS', 'OPTIONS' ],
  54. 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
  55. 5 => [ ],
  56. 7 => [ ] );
  57. # Cross-check functions in the NAME and SYNOPSIS section.
  58. sub name_synopsis()
  59. {
  60. my $id = shift;
  61. my $filename = shift;
  62. my $contents = shift;
  63. # Get NAME section and all words in it.
  64. return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
  65. my $tmp = $1;
  66. $tmp =~ tr/\n/ /;
  67. print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
  68. $tmp =~ s/ -.*//g;
  69. print "$id POD markup among the names in NAME\n" if $tmp =~ /[<>]/;
  70. $tmp =~ s/ */ /g;
  71. print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
  72. my $dirname = dirname($filename);
  73. my $simplename = basename($filename);
  74. $simplename =~ s/.pod$//;
  75. my $foundfilename = 0;
  76. my %foundfilenames = ();
  77. my %names;
  78. foreach my $n ( split ',', $tmp ) {
  79. $n =~ s/^\s+//;
  80. $n =~ s/\s+$//;
  81. print "$id the name '$n' contains white-space\n"
  82. if $n =~ /\s/;
  83. $names{$n} = 1;
  84. $foundfilename++ if $n eq $simplename;
  85. $foundfilenames{$n} = 1
  86. if -f "$dirname/$n.pod" && $n ne $simplename;
  87. }
  88. print "$id the following exist as other .pod files:\n",
  89. join(" ", sort keys %foundfilenames), "\n"
  90. if %foundfilenames;
  91. print "$id $simplename (filename) missing from NAME section\n"
  92. unless $foundfilename;
  93. foreach my $n ( keys %names ) {
  94. print "$id $n is not public\n"
  95. if $opt_p and !defined $public{$n};
  96. }
  97. # Find all functions in SYNOPSIS
  98. return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
  99. my $syn = $1;
  100. foreach my $line ( split /\n+/, $syn ) {
  101. next unless $line =~ /^\s/;
  102. my $sym;
  103. $line =~ s/STACK_OF\([^)]+\)/int/g;
  104. $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
  105. $line =~ s/__declspec\([^)]+\)//;
  106. if ( $line =~ /env (\S*)=/ ) {
  107. # environment variable env NAME=...
  108. $sym = $1;
  109. } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
  110. # a callback function pointer: typedef ... (*NAME)(...
  111. $sym = $1;
  112. } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
  113. # a callback function signature: typedef ... NAME(...
  114. $sym = $1;
  115. } elsif ( $line =~ /typedef.* (\S+);/ ) {
  116. # a simple typedef: typedef ... NAME;
  117. $sym = $1;
  118. } elsif ( $line =~ /enum (\S*) \{/ ) {
  119. # an enumeration: enum ... {
  120. $sym = $1;
  121. } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
  122. $sym = $1;
  123. } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
  124. $sym = $1;
  125. }
  126. else {
  127. next;
  128. }
  129. print "$id $sym missing from NAME section\n"
  130. unless defined $names{$sym};
  131. $names{$sym} = 2;
  132. # Do some sanity checks on the prototype.
  133. print "$id prototype missing spaces around commas: $line\n"
  134. if ( $line =~ /[a-z0-9],[^ ]/ );
  135. }
  136. foreach my $n ( keys %names ) {
  137. next if $names{$n} == 2;
  138. print "$id $n missing from SYNOPSIS\n";
  139. }
  140. }
  141. # Check if SECTION is located before BEFORE
  142. sub check_section_location()
  143. {
  144. my $filename = shift;
  145. my $contents = shift;
  146. my $section = shift;
  147. my $before = shift;
  148. return unless $contents =~ /=head1 $section/
  149. and $contents =~ /=head1 $before/;
  150. print "$filename: $section should be placed before $before section\n"
  151. if $contents =~ /=head1 $before.*=head1 $section/ms;
  152. }
  153. sub check()
  154. {
  155. my $filename = shift;
  156. my $dirname = basename(dirname($filename));
  157. my $contents = '';
  158. {
  159. local $/ = undef;
  160. open POD, $filename or die "Couldn't open $filename, $!";
  161. $contents = <POD>;
  162. close POD;
  163. }
  164. # Check if EXAMPLES is located after RETURN VALUES section.
  165. &check_section_location($filename, $contents, "RETURN VALUES", "EXAMPLES") if $filename =~ m|man3/|;
  166. # Check if HISTORY is located after SEE ALSO
  167. &check_section_location($filename, $contents, "SEE ALSO", "HISTORY") if $filename =~ m|man3/|;
  168. # Check if SEE ALSO is located after EXAMPLES
  169. &check_section_location($filename, $contents, "EXAMPLES", "SEE ALSO") if $filename =~ m|man3/|;
  170. my $id = "${filename}:1:";
  171. &name_synopsis($id, $filename, $contents)
  172. unless $contents =~ /=for comment generic/
  173. or $filename =~ m@man[157]/@;
  174. print "$id doesn't start with =pod\n"
  175. if $contents !~ /^=pod/;
  176. print "$id doesn't end with =cut\n"
  177. if $contents !~ /=cut\n$/;
  178. print "$id more than one cut line.\n"
  179. if $contents =~ /=cut.*=cut/ms;
  180. print "$id missing copyright\n"
  181. if $contents !~ /Copyright .* The OpenSSL Project Authors/;
  182. print "$id copyright not last\n"
  183. if $contents =~ /head1 COPYRIGHT.*=head/ms;
  184. print "$id head2 in All uppercase\n"
  185. if $contents =~ /head2\s+[A-Z ]+\n/;
  186. print "$id extra space after head\n"
  187. if $contents =~ /=head\d\s\s+/;
  188. print "$id period in NAME section\n"
  189. if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
  190. print "$id Duplicate $1 in L<>\n"
  191. if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
  192. print "$id Bad =over $1\n"
  193. if $contents =~ /=over([^ ][^24])/;
  194. print "$id Possible version style issue\n"
  195. if $contents =~ /OpenSSL version [019]/;
  196. if ( $contents !~ /=for comment multiple includes/ ) {
  197. # Look for multiple consecutive openssl #include lines
  198. # (non-consecutive lines are okay; see man3/MD5.pod).
  199. if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
  200. my $count = 0;
  201. foreach my $line ( split /\n+/, $1 ) {
  202. if ( $line =~ m@include <openssl/@ ) {
  203. print "$id has multiple includes\n" if ++$count == 2;
  204. } else {
  205. $count = 0;
  206. }
  207. }
  208. }
  209. }
  210. open my $OUT, '>', $temp
  211. or die "Can't open $temp, $!";
  212. podchecker($filename, $OUT);
  213. close $OUT;
  214. open $OUT, '<', $temp
  215. or die "Can't read $temp, $!";
  216. while ( <$OUT> ) {
  217. next if /\(section\) in.*deprecated/;
  218. print;
  219. }
  220. close $OUT;
  221. unlink $temp || warn "Can't remove $temp, $!";
  222. # Find what section this page is in; assume 3.
  223. my $section = 3;
  224. $section = $1 if $dirname =~ /man([1-9])/;
  225. foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
  226. # Skip "return values" if not -s
  227. print "$id: missing $_ head1 section\n"
  228. if $contents !~ /^=head1\s+${_}\s*$/m;
  229. }
  230. }
  231. my %dups;
  232. sub parsenum()
  233. {
  234. my $file = shift;
  235. my @apis;
  236. open my $IN, '<', $file
  237. or die "Can't open $file, $!, stopped";
  238. while ( <$IN> ) {
  239. next if /^#/;
  240. next if /\bNOEXIST\b/;
  241. next if /\bEXPORT_VAR_AS_FUNC\b/;
  242. my @fields = split();
  243. die "Malformed line $_"
  244. if scalar @fields != 2 && scalar @fields != 4;
  245. push @apis, $fields[0];
  246. }
  247. close $IN;
  248. print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
  249. return sort @apis;
  250. }
  251. sub getdocced
  252. {
  253. my $dir = shift;
  254. my %return;
  255. foreach my $pod ( glob("$dir/*.pod") ) {
  256. my %podinfo = extract_pod_info($pod);
  257. foreach my $n ( @{$podinfo{names}} ) {
  258. $return{$n} = $pod;
  259. print "# Duplicate $n in $pod and $dups{$n}\n"
  260. if defined $dups{$n} && $dups{$n} ne $pod;
  261. $dups{$n} = $pod;
  262. }
  263. }
  264. return %return;
  265. }
  266. my %docced;
  267. sub loadmissing($)
  268. {
  269. my $missingfile = shift;
  270. my @missing;
  271. open FH, $missingfile
  272. || die "Can't open $missingfile";
  273. while ( <FH> ) {
  274. chomp;
  275. next if /^#/;
  276. push @missing, $_;
  277. }
  278. close FH;
  279. return @missing;
  280. }
  281. sub checkmacros()
  282. {
  283. my $count = 0;
  284. my %seen;
  285. my @missing;
  286. if ($opt_o) {
  287. @missing = loadmissing('util/missingmacro111.txt');
  288. } elsif ($opt_v) {
  289. @missing = loadmissing('util/missingmacro.txt');
  290. }
  291. print "# Checking macros (approximate)\n" if !$opt_s;
  292. foreach my $f ( glob('include/openssl/*.h') ) {
  293. # Skip some internals we don't want to document yet.
  294. next if $f eq 'include/openssl/asn1.h';
  295. next if $f eq 'include/openssl/asn1t.h';
  296. next if $f eq 'include/openssl/err.h';
  297. open(IN, $f) || die "Can't open $f, $!";
  298. while ( <IN> ) {
  299. next unless /^#\s*define\s*(\S+)\(/;
  300. my $macro = $1;
  301. next if $docced{$macro} || defined $seen{$macro};
  302. next if $macro =~ /i2d_/
  303. || $macro =~ /d2i_/
  304. || $macro =~ /DEPRECATEDIN/
  305. || $macro =~ /IMPLEMENT_/
  306. || $macro =~ /DECLARE_/;
  307. # Skip macros known to be missing
  308. next if $opt_v && grep( /^$macro$/, @missing);
  309. print "$f:$macro\n" if $opt_d || $opt_e;
  310. $count++;
  311. $seen{$macro} = 1;
  312. }
  313. close(IN);
  314. }
  315. print "# Found $count macros missing\n" if !$opt_s || $count > 0;
  316. }
  317. sub printem()
  318. {
  319. my $libname = shift;
  320. my $numfile = shift;
  321. my $missingfile = shift;
  322. my $count = 0;
  323. my %seen;
  324. my @missing = loadmissing($missingfile) if ($opt_v);
  325. foreach my $func ( &parsenum($numfile) ) {
  326. next if $docced{$func} || defined $seen{$func};
  327. # Skip ASN1 utilities
  328. next if $func =~ /^ASN1_/;
  329. # Skip functions known to be missing
  330. next if $opt_v && grep( /^$func$/, @missing);
  331. print "$libname:$func\n" if $opt_d || $opt_e;
  332. $count++;
  333. $seen{$func} = 1;
  334. }
  335. print "# Found $count missing from $numfile\n\n" if !$opt_s || $count > 0;
  336. }
  337. # Collection of links in each POD file.
  338. # filename => [ "foo(1)", "bar(3)", ... ]
  339. my %link_collection = ();
  340. # Collection of names in each POD file.
  341. # "name(s)" => filename
  342. my %name_collection = ();
  343. sub collectnames {
  344. my $filename = shift;
  345. $filename =~ m|man(\d)/|;
  346. my $section = $1;
  347. my $simplename = basename($filename, ".pod");
  348. my $id = "${filename}:1:";
  349. my $contents = '';
  350. {
  351. local $/ = undef;
  352. open POD, $filename or die "Couldn't open $filename, $!";
  353. $contents = <POD>;
  354. close POD;
  355. }
  356. $contents =~ /=head1 NAME([^=]*)=head1 /ms;
  357. my $tmp = $1;
  358. unless (defined $tmp) {
  359. print "$id weird name section\n";
  360. return;
  361. }
  362. $tmp =~ tr/\n/ /;
  363. $tmp =~ s/-.*//g;
  364. my @names = map { s/^\s+//g; s/\s+$//g; $_ } split(/,/, $tmp);
  365. unless (grep { $simplename eq $_ } @names) {
  366. print "$id missing $simplename\n";
  367. push @names, $simplename;
  368. }
  369. foreach my $name (@names) {
  370. next if $name eq "";
  371. if ($name =~ /\s/) {
  372. print "$id '$name' contains white space\n";
  373. }
  374. my $name_sec = "$name($section)";
  375. if (! exists $name_collection{$name_sec}) {
  376. $name_collection{$name_sec} = $filename;
  377. } else { #elsif ($filename ne $name_collection{$name_sec}) {
  378. print "$id $name_sec also in $name_collection{$name_sec}\n";
  379. }
  380. }
  381. my @foreign_names =
  382. map { map { s/\s+//g; $_ } split(/,/, $_) }
  383. $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
  384. foreach (@foreign_names) {
  385. $name_collection{$_} = undef; # It still exists!
  386. }
  387. my @links = $contents =~ /L<
  388. # if the link is of the form L<something|name(s)>,
  389. # then remove 'something'. Note that 'something'
  390. # may contain POD codes as well...
  391. (?:(?:[^\|]|<[^>]*>)*\|)?
  392. # we're only interested in references that have
  393. # a one digit section number
  394. ([^\/>\(]+\(\d\))
  395. /gx;
  396. $link_collection{$filename} = [ @links ];
  397. }
  398. sub checklinks {
  399. foreach my $filename (sort keys %link_collection) {
  400. foreach my $link (@{$link_collection{$filename}}) {
  401. print "${filename}:1: reference to non-existing $link\n"
  402. unless exists $name_collection{$link};
  403. }
  404. }
  405. }
  406. sub publicize() {
  407. foreach my $name ( &parsenum('util/libcrypto.num') ) {
  408. $public{$name} = 1;
  409. }
  410. foreach my $name ( &parsenum('util/libssl.num') ) {
  411. $public{$name} = 1;
  412. }
  413. foreach my $name ( &parsenum('util/private.num') ) {
  414. $public{$name} = 1;
  415. }
  416. }
  417. my %skips = (
  418. 'aes128' => 1,
  419. 'aes192' => 1,
  420. 'aes256' => 1,
  421. 'aria128' => 1,
  422. 'aria192' => 1,
  423. 'aria256' => 1,
  424. 'camellia128' => 1,
  425. 'camellia192' => 1,
  426. 'camellia256' => 1,
  427. 'des' => 1,
  428. 'des3' => 1,
  429. 'idea' => 1,
  430. '[cipher]' => 1,
  431. '[digest]' => 1,
  432. );
  433. sub checkflags() {
  434. my $cmd = shift;
  435. my %cmdopts;
  436. my %docopts;
  437. my $ok = 1;
  438. # Get the list of options in the command.
  439. open CFH, "./apps/openssl list --options $cmd|"
  440. || die "Can list options for $cmd, $!";
  441. while ( <CFH> ) {
  442. chop;
  443. s/ .$//;
  444. $cmdopts{$_} = 1;
  445. }
  446. close CFH;
  447. # Get the list of flags from the synopsis
  448. open CFH, "<doc/man1/$cmd.pod"
  449. || die "Can't open $cmd.pod, $!";
  450. while ( <CFH> ) {
  451. chop;
  452. last if /DESCRIPTION/;
  453. next unless /\[B<-([^ >]+)/;
  454. $docopts{$1} = 1;
  455. }
  456. close CFH;
  457. # See what's in the command not the manpage.
  458. my @undocced = ();
  459. foreach my $k ( keys %cmdopts ) {
  460. push @undocced, $k unless $docopts{$k};
  461. }
  462. if ( scalar @undocced > 0 ) {
  463. $ok = 0;
  464. foreach ( @undocced ) {
  465. print "doc/man1/$cmd.pod: Missing -$_\n";
  466. }
  467. }
  468. # See what's in the command not the manpage.
  469. my @unimpl = ();
  470. foreach my $k ( keys %docopts ) {
  471. push @unimpl, $k unless $cmdopts{$k};
  472. }
  473. if ( scalar @unimpl > 0 ) {
  474. $ok = 0;
  475. foreach ( @unimpl ) {
  476. next if defined $skips{$_};
  477. print "doc/man1/$cmd.pod: Not implemented -$_\n";
  478. }
  479. }
  480. return $ok;
  481. }
  482. getopts('cdesolnphuv');
  483. &help() if $opt_h;
  484. $opt_n = 1 if $opt_p;
  485. $opt_u = 1 if $opt_d;
  486. $opt_e = 1 if $opt_s;
  487. $opt_v = 1 if $opt_o || $opt_e;
  488. die "Cannot use both -u and -v" if $opt_u && $opt_v;
  489. die "Cannot use both -d and -e" if $opt_d && $opt_e;
  490. # We only need to check c, l, n, u and v.
  491. # Options d, e, s, o and p imply one of the above.
  492. die "Need one of -[cdesolnpuv] flags.\n"
  493. unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v;
  494. if ( $opt_c ) {
  495. my $ok = 1;
  496. my @commands = ();
  497. # Get list of commands.
  498. open FH, "./apps/openssl list -1 -commands|"
  499. || die "Can't list commands, $!";
  500. while ( <FH> ) {
  501. chop;
  502. push @commands, $_;
  503. }
  504. close FH;
  505. # See if each has a manpage.
  506. foreach ( @commands ) {
  507. next if $_ eq 'help' || $_ eq 'exit';
  508. if ( ! -f "doc/man1/$_.pod" ) {
  509. print "doc/man1/$_.pod does not exist\n";
  510. $ok = 0;
  511. } else {
  512. $ok = 0 if not &checkflags($_);
  513. }
  514. }
  515. # See what help is missing.
  516. open FH, "./apps/openssl list --missing-help |"
  517. || die "Can't list missing help, $!";
  518. while ( <FH> ) {
  519. chop;
  520. my ($cmd, $flag) = split;
  521. print "$cmd has no help for -$flag\n";
  522. $ok = 0;
  523. }
  524. close FH;
  525. exit 1 if not $ok;
  526. }
  527. if ( $opt_l ) {
  528. foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'),
  529. glob('doc/internal/*/*.pod'))) {
  530. collectnames($_);
  531. }
  532. checklinks();
  533. }
  534. if ( $opt_n ) {
  535. &publicize() if $opt_p;
  536. foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
  537. &check($_);
  538. }
  539. {
  540. local $opt_p = undef;
  541. foreach (@ARGV ? @ARGV : glob('doc/internal/*/*.pod')) {
  542. &check($_);
  543. }
  544. }
  545. }
  546. if ( $opt_u || $opt_v) {
  547. my %temp = getdocced('doc/man3');
  548. foreach ( keys %temp ) {
  549. $docced{$_} = $temp{$_};
  550. }
  551. if ($opt_o) {
  552. &printem('crypto', 'util/libcrypto.num', 'util/missingcrypto111.txt');
  553. &printem('ssl', 'util/libssl.num', 'util/missingssl111.txt');
  554. } else {
  555. &printem('crypto', 'util/libcrypto.num', 'util/missingcrypto.txt');
  556. &printem('ssl', 'util/libssl.num', 'util/missingssl.txt');
  557. }
  558. &checkmacros();
  559. }
  560. exit;