find-doc-nits 16 KB

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