find-doc-nits 15 KB

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