check-doc-strings 8.7 KB

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