check-doc-strings 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. : #-*- Perl -*-
  2. eval 'exec perl -S $0 ${1+"$@"}' # Portability kludge
  3. if 0;
  4. # Author: Martin Buchholz
  5. # This program is in the public domain.
  6. use strict;
  7. use warnings;
  8. use POSIX;
  9. (my $myName = $0) =~ s@.*/@@; my $usage="
  10. Usage: $myName
  11. Finds DOCSTRING arg mismatches between
  12. formal parameters, docstrings, and lispref texi.
  13. This program is in the public domain.\n";
  14. die $usage if @ARGV;
  15. die $usage unless -r "src/alloc.c" && -d "lisp";
  16. my %texi_funtype;
  17. my %texi_arglist;
  18. my %code_funtype;
  19. my %code_arglist;
  20. sub FileContents {
  21. local $/ = undef;
  22. open (FILE, "< $_[0]") or die "$_[0]: $!";
  23. return scalar <FILE>;
  24. }
  25. sub Show_details {
  26. my ($show_details, $function, $parms, $docstring) = @_;
  27. if ($show_details) {
  28. print "function = $function $parms\n$docstring\n", "-" x 70, "\n";
  29. }
  30. }
  31. sub Check_texi_function {
  32. my ($function, $funtype, $docstring, @parms) = @_;
  33. my %docstring_parm;
  34. my %docstring_word;
  35. my %arglist_parm;
  36. my $show_details = 0;
  37. if (exists $texi_funtype{$function}) {
  38. print "duplicate texidoc: $function @parms\n";
  39. return; # later definition likely bogus package def
  40. }
  41. $texi_funtype{$function} = $funtype;
  42. $texi_arglist{$function} = "@parms";
  43. foreach my $parm (@parms) {
  44. next if $parm eq '&optional' || $parm eq '&rest';
  45. $arglist_parm{$parm} = 1;
  46. }
  47. foreach my $parm ($docstring =~ /\@var{([^{}]+)}/g) {
  48. $docstring_parm{$parm} = 1;
  49. }
  50. foreach my $hit ($docstring =~ /[^\`]\`[A-Za-z-]+\'/g)
  51. {
  52. print "texi \@code missing: $function: $hit\n";
  53. $show_details = 1;
  54. }
  55. # (my $raw_docstring = $docstring) =~ s/\@var{[^{}]+}//g;
  56. # $raw_docstring =~ s/[^a-zA-Z_-]+/ /g;
  57. # foreach my $word (split (' ', $raw_docstring)) {
  58. # if ($word =~ /^[A-Z][A-Z-]+$/) {
  59. # print "Missing \@var: $function: $word\n";
  60. # }
  61. # }
  62. foreach my $parm (keys %docstring_parm) {
  63. if (! exists $arglist_parm{$parm}) {
  64. print "bogus texi parm: $function: $parm\n";
  65. $show_details = 1;
  66. }
  67. }
  68. foreach my $parm (keys %arglist_parm) {
  69. if (! exists $docstring_parm{$parm}) {
  70. print "undocumented texi parm: $function: $parm\n";
  71. $show_details = 1;
  72. }
  73. }
  74. Show_details $show_details, $function, "@parms", $docstring;
  75. }
  76. sub Check_function {
  77. my ($function, $funtype, $docstring, @parms) = @_;
  78. my %docstring_parm;
  79. my %arglist_parm;
  80. my $show_details = 0;
  81. if (exists $code_funtype{$function}) {
  82. print "duplicate codedef: $function @parms\n";
  83. return; # later definition likely bogus package def
  84. }
  85. $code_funtype{$function} = $funtype;
  86. $code_arglist{$function} = "@parms";
  87. #foreach my $parm ($parms =~ /\b[a-z0-9-]{3,}\b/g) {
  88. # $arglist_parm{$parm} = 1;
  89. #}
  90. foreach my $parm (@parms) {
  91. next if $parm eq '&optional' || $parm eq '&rest';
  92. $arglist_parm{$parm} = 1;
  93. }
  94. my $doc_tmp = $docstring;
  95. $doc_tmp =~ s/[^A-Za-z0-9_-]/ /g;
  96. foreach my $parm (split (' ', $doc_tmp)) {
  97. if ($parm =~ /^[A-Z][A-Z0-9-]*$/) {
  98. next if $parm =~ /I18N/;
  99. next if $parm =~ /M17N/;
  100. $parm =~ tr[A-Z][a-z];
  101. $docstring_parm{$parm} = 1;
  102. }
  103. }
  104. # foreach my $parm ($docstring =~ /\b[A-Z0-9-]{1,}\b/g) {
  105. # next if $parm =~ /-$/;
  106. # $parm =~ tr[A-Z][a-z];
  107. # $docstring_parm{$parm} = 1;
  108. # }
  109. foreach my $parm (keys %docstring_parm) {
  110. next if $parm eq 'tty';
  111. next if $parm eq 'fsf';
  112. next if $parm eq 'note';
  113. next if $parm eq 'warning';
  114. next if $parm eq 'bug';
  115. next if $parm eq 'ascii';
  116. next if $parm eq 'iso';
  117. next if $parm eq 'and';
  118. next if $parm eq 'absolutely';
  119. next if $parm eq 'doc';
  120. next if $parm eq 'user';
  121. next if $parm eq 'not';
  122. next if $parm eq 'must';
  123. next if $parm eq 'nil';
  124. next if $parm eq 'esc';
  125. next if $parm eq 'lfd';
  126. next if $parm eq 'gpm';
  127. next if $parm eq 'primary';
  128. next if $parm eq 'secondary';
  129. next if $parm eq 'clipboard';
  130. next if length $parm < 3;
  131. if (! exists $arglist_parm{$parm}) {
  132. print "bogus parm: $function: $parm\n";
  133. $show_details = 1;
  134. }
  135. }
  136. foreach my $parm (keys %arglist_parm) {
  137. if (! exists $docstring_parm{$parm}) {
  138. print "Undocumented parm: $function: $parm\n";
  139. $show_details = 1;
  140. }
  141. }
  142. if ($docstring !~ /[\]}!\)\.]\s*\Z/m &&
  143. $docstring =~ /\S/ &&
  144. $docstring !~ /Keywords supported/)
  145. {
  146. print "Missing trailing period: $function\n";
  147. $show_details = 1;
  148. }
  149. if (exists $texi_arglist{$function}
  150. and "@parms" ne $texi_arglist{$function}
  151. and not ("@parms" eq 'int nargs Lisp-Object *args'
  152. && $texi_arglist{$function} =~ /&rest/)) {
  153. my @texi_parms = split (' ', $texi_arglist{$function});
  154. my @a = ("@parms" =~ /&optional/g);
  155. my @b = ("@parms" =~ /&rest/g);
  156. my @c = ("@texi_parms" =~ /&optional/g);
  157. my @d = ("@texi_parms" =~ /&rest/g);
  158. if (@parms != @texi_parms
  159. || (@a != @c) || (@b != @d)) {
  160. print "serious mismatch: $function: @parms --- @texi_parms\n";
  161. } else {
  162. print "texi mismatch: $function: @parms --- $texi_arglist{$function}\n";
  163. }
  164. $show_details = 1;
  165. }
  166. if (exists $texi_funtype{$function}
  167. && $texi_funtype{$function} ne $funtype) {
  168. print "interactiveness mismatch: $function: $funtype --- $texi_funtype{$function}\n";
  169. $show_details = 1;
  170. }
  171. Show_details $show_details, $function, "@parms", $docstring;
  172. }
  173. my $lisprefdir = "doc/lispref";
  174. die "Can't find lispref texi directory.\n" unless -d $lisprefdir;
  175. open (FIND, "find $lisprefdir -name '*.texi' -print |") or die;
  176. while (my $file = <FIND>) {
  177. my @matches = ((FileContents $file) =~
  178. /\@(def(?:fn|un))([^\n]+)\n(.*?)\n\@end def(?:un|fn)/sgo);
  179. # /^\@(def(?:un|fn))\s+(.*)\n([.|\n]*?)^\@end def(?:un|fn)\n/mgo);
  180. while (@matches) {
  181. my ($defform, $defn, $docstring) = splice (@matches, 0, 3);
  182. #print "defform = $defform\n";
  183. #print "defn = $defn\n";
  184. #print "docstring = $docstring\n";
  185. my ($function, @parms, $funtype);
  186. if ($defform eq 'defun') {
  187. ($funtype, $function, @parms) = ('Function', split (' ', $defn));
  188. } else {
  189. die unless $defform eq 'deffn';
  190. ($funtype, $function, @parms) = split (' ', $defn);
  191. }
  192. next if $funtype eq '{Syntax' or $funtype eq '{Prefix';
  193. Check_texi_function $function, $funtype, $docstring, @parms;
  194. }
  195. }
  196. open (FIND, "find src -name '*.c' -print |") or die;
  197. while (my $file = <FIND>) {
  198. my @matches =
  199. ((FileContents $file) =~
  200. /\bDEFUN\s*\(\s*\"((?:[^\\\"]|\\.)+)\"\s*,\s*\S+\s*,\s*(\S+)\s*,\s*(\S+)\s*,\s*((?:0|\"(?:(?:[^\\\"]|\\.)*)\"))\s*,\s*\/\*(.*?)\*\/\s*\(([^()]*)\)\)/sgo);
  201. while (@matches) {
  202. my ($function, $minargs, $maxargs, $interactive, $docstring, $parms) = splice (@matches, 0, 6);
  203. $docstring =~ s/^\n+//s;
  204. $docstring =~ s/\n+$//s;
  205. $parms =~ s/,/ /g;
  206. my @parms = split (' ',$parms);
  207. for (@parms) { tr/_/-/; s/-$//; }
  208. if ($parms !~ /Lisp_Object/) {
  209. if ($minargs < @parms) {
  210. if ($maxargs =~ /^\d+$/) {
  211. die unless $maxargs eq @parms;
  212. splice (@parms, $minargs, 0, '&optional');
  213. }
  214. }
  215. }
  216. my $funtype = ($interactive =~ /\"/ ? 'Command' : 'Function');
  217. Check_function $function, $funtype, $docstring, @parms;
  218. }
  219. }
  220. my @pkgs;
  221. if (-d "../xemacs-packages") {
  222. @pkgs = qw (libs/edebug libs/xemacs-base comm/eudc oa/edit-utils);
  223. } else {
  224. @pkgs = ();
  225. }
  226. for (@pkgs) { s@^@../xemacs-packages/@; }
  227. open (FIND, "find lisp @pkgs -name '*.el' -print |") or die;
  228. while (my $file = <FIND>) {
  229. my $contents = FileContents $file;
  230. $contents =~ s/(?:\s|;);.*//mog;
  231. my @matches =
  232. ($contents =~
  233. /\((def(?:un|subst|macro))\s+(\S+)\s+\(([^()]*)\)\s+\"((?:[^\\\"]|\\.)+)\"(.*?)\)/sgo);
  234. while (@matches) {
  235. my ($defform, $function, $parms, $docstring, $code_fragment) = splice (@matches, 0, 5);
  236. my $funtype =
  237. $defform eq 'defmacro' ? 'Macro' :
  238. $code_fragment =~ /^\s*\(interactive\b/so ? 'Command' :
  239. 'Function';
  240. $docstring =~ s/^\n+//s;
  241. $docstring =~ s/\n+$//s;
  242. my @parms = split (' ', $parms);
  243. Check_function $function, $funtype, $docstring, @parms;
  244. }
  245. }
  246. open (FIND, "find lisp @pkgs -name '*.el' -print |") or die;
  247. while (my $file = <FIND>) {
  248. my $contents = FileContents $file;
  249. $contents =~ s/(?:\s|;);.*//mog;
  250. my @matches = ($contents =~ /^\((?:defalias|fset|define-function)\s+\'([A-Za-z0-9_-]+)\s+\'([A-Za-z0-9_-]+)/mog);
  251. while (@matches) {
  252. my ($alias, $aliasee) = splice (@matches, 0, 2);
  253. print "alias $alias aliasee $aliasee\n";
  254. if (exists $code_funtype{$aliasee}) { $code_funtype{$alias} = $code_funtype{$aliasee}; }
  255. if (exists $code_arglist{$aliasee}) { $code_arglist{$alias} = $code_arglist{$aliasee}; }
  256. }
  257. }
  258. foreach my $fun (sort keys %texi_funtype) {
  259. if (not exists $code_funtype{$fun}) {
  260. print "nuke-this-doc: $fun $texi_funtype{$fun}\n";
  261. }
  262. }