find-doc-nits 15 KB

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