find-doc-nits 15 KB

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