1 #! /usr/bin/runlisp -Lsbcl,cmucl
4 (cl:defpackage #:sod-exports
9 ;; Load the target system so that we can poke about in it.
10 (cl:in-package #:sod-exports)
11 (eval-when (:compile-toplevel :load-toplevel :execute)
12 (asdf:clear-configuration)
13 (mapc #'asdf:load-system '(:sod :sod/frontend)))
15 ;;;--------------------------------------------------------------------------
16 ;;; Miscelleneous utilities.
18 (defun symbolicate (&rest things)
19 "Concatenate the THINGS and turn the result into a symbol."
20 (intern (apply #'concatenate 'string (mapcar #'string things))))
22 ;;;--------------------------------------------------------------------------
23 ;;; Determining the symbols exported by particular files.
25 (defun incomprehensible-form (head tail)
26 "Report an incomprehensible form (HEAD . TAIL)."
27 (format *error-output* ";; incomprehensible: ~S~%" (cons head tail)))
29 (defgeneric form-list-exports (head tail)
31 "Return a list of symbols exported by the form (HEAD . TAIL).
33 This is called from `form-exports' below.")
35 "By default, a form exports nothing."
36 (declare (ignore head tail))
39 (defmethod form-list-exports ((head (eql 'cl:export)) tail)
40 "Return the symbols exported by a toplevel `export' form.
42 We can cope with (export 'SYMBOLS), where SYMBOLS is a symbol or a list."
44 (let ((symbols (car tail)))
45 (if (and (consp symbols)
46 (eq (car symbols) 'quote))
47 (let ((thing (cadr symbols)))
48 (if (atom thing) (list thing) thing))
49 (incomprehensible-form head tail))))
51 (defmethod form-list-exports ((head (eql 'sod:definst)) tail)
52 "Return the symbols exported by a `form-list-exports' form.
56 (definst CODE (STREAMVAR [[:export FLAG]]) ARGS
59 If FLAG is non-nil, then we export `CODE-inst', `make-CODE-inst', and
60 `inst-ARG' for each argument ARG in the lambda-list ARGS. There are some
61 quirks in this lambda-list:
63 * If we find a list (PUBLIC PRIVATE) where we expected an argument-name
64 symbol (but not a list), then the argument is PUBLIC. (PRIVATE is
65 used to name a slot in the class created by the macro, presumably
66 because PUBLIC on its own is a public symbol in some package.)
68 * If we find a symbol %NAME, this means the same as the list (NAME
69 %NAME), only we recognize it even where the lambda-list syntax expects
72 (destructuring-bind (code (streamvar &key export) args &body body) tail
73 (declare (ignore streamvar body))
76 (list* (symbolicate code '-inst)
77 (symbolicate 'make- code '-inst)
79 (labels ((dig (tree path)
80 ;; Dig down into a TREE, following the PATH. Stop
81 ;; when we find an atom, or reach the end of the
83 (if (or (atom tree) (null path)) tree
84 (dig (nth (car path) tree) (cdr path))))
86 ;; Convert an ARG name which might start with `%'.
87 (if (consp arg) (car arg)
88 (let ((name (symbol-name arg)))
89 (if (char= (char name 0) #\%)
90 (intern (subseq name 1))
93 ;; Convert ARG name into the `inst-ARG' accessor.
94 (symbolicate 'inst- (cook arg))))
96 ;; Work through the lambda-list, keeping track of where we
97 ;; expect the argument symbols to be.
98 (loop with state = :mandatory
100 if (and (symbolp arg)
101 (char= (char (symbol-name arg) 0) #\&))
103 else if (member state '(:mandatory &rest))
104 collect (instify arg)
105 else if (member state '(&optional &aux))
106 collect (instify (dig arg '(0)))
107 else if (eq state '&key)
108 collect (instify (dig arg '(0 1)))
110 do (error "Confused by ~S." arg)))))))
112 (defmethod form-list-exports ((head (eql 'sod::define-tagged-type)) tail)
113 "Return the symbols exported by a `define-tagged-type' form.
115 This is a scummy internal macro in `c-types-impl.lisp'. The syntax is
117 (define-tagged-type KIND DESCRIPTION)
119 It exports `KIND' and `make-KIND'."
121 (destructuring-bind (kind what) tail
122 (declare (ignore what))
124 (symbolicate 'c- kind '-type)
125 (symbolicate 'make- kind '-type))))
127 (defmethod form-list-exports ((head (eql 'sod:defctype)) tail)
128 "Return the symbols exported by a `defctype' form.
132 (defctype {NAME | (NAME SYNONYM*)} VALUE [[:export FLAG]])
134 If FLAG is non-nil, this form exports `c-type-NAME', `NAME', and all of
137 (destructuring-bind (names value &key export) tail
138 (declare (ignore value))
139 (let ((names (if (listp names) names (list names))))
141 (list* (symbolicate 'c-type- (car names)) names)))))
143 (defmethod form-list-exports ((head (eql 'sod:define-simple-c-type)) tail)
144 "Return the symbols exported by a `define-simple-c-type' form.
148 (define-simple-c-type {NAME | (NAME SYNONYM*)} TYPE [[:export FLAG]])
150 If FLAG is non-nil, this form exports `c-type-NAME', `NAME', and all of
153 (destructuring-bind (names type &key export) tail
154 (declare (ignore type))
155 (let ((names (if (listp names) names (list names))))
157 (list* (symbolicate 'c-type- (car names)) names)))))
159 (defmethod form-list-exports
160 ((head (eql 'sod::define-cross-product-types)) tail)
161 "Return the symbols exported by a `define-cross-product-types' form.
163 This is a scummy internal macro in `c-types-impl.lisp'. The syntax is
165 (define-cross-product-types PIECES)
167 Each piece can be a list of strings, or an atomic string (which is
168 equivalent to a list containing just that string). For each string formed
169 by concatenating one element from each list in order, define a C type with
170 that name; the Lisp name is constructed by translating the letters to
171 uppercase and replacing underscores by hyphens. For each such name,
172 export `NAME' and `c-type-NAME'."
174 ;; Huh. I feel a hack coming on.
175 (mapcar (lambda (row)
176 (intern (with-output-to-string (out)
178 (dotimes (i (length s))
179 (let ((ch (char s i)))
182 (write-char (char-upcase ch) out))))))))
183 (reduce (lambda (piece tails)
184 (mapcan (lambda (tail)
185 (mapcar (lambda (head)
187 (if (listp piece) piece
190 (cons '("" "c-type_") tail)
192 :initial-value '(nil))))
195 (defmethod form-list-exports ((head (eql 'cl:macrolet)) tail)
196 "Return the symbols expored by a toplevel `macrolet' form.
198 Which are simply the symbols exported by its body."
199 (mapcan #'form-exports (cdr tail)))
201 (defmethod form-list-exports ((head (eql 'cl:eval-when)) tail)
202 "Return the symbols expored by a toplevel `eval-when' form.
204 Which are simply the symbols exported by its body."
206 ;; We don't bother checking when it'd actually be evaluated.
207 (mapcan #'form-exports (cdr tail)))
209 (defmethod form-list-exports ((head (eql 'cl:progn)) tail)
210 "Return the symbols expored by a toplevel `progn' form.
212 Which are simply the symbols exported by its body."
213 (mapcan #'form-exports tail))
215 (defgeneric form-exports (form)
217 "Return a list of symbols exported by a toplevel FORM.")
219 (:method ((form cons)) (form-list-exports (car form) (cdr form))))
221 (defgeneric list-exports (thing)
223 "Return a list of symbols exported by THING."))
225 (defmethod list-exports ((stream stream))
226 "Return a list of symbols exported by a STREAM.
228 By reading it and analysing the forms."
230 (loop with eof = '#:eof
231 for form = (read stream nil eof)
233 when (consp form) nconc (form-exports form)))
235 (defmethod list-exports ((path pathname))
236 "Return a list of symbols exported by a directory PATHNAME.
238 Return an alist of pairs (PATH . SYMBOL) listing each SYMBOL exported by a
239 PATH of the form PATHNAME/*.lisp."
241 (mapcar (lambda (each)
242 (cons each (with-open-file (stream each) (list-exports stream))))
243 (directory (merge-pathnames path #p"*.lisp"))))
245 (defmethod list-exports ((path string))
246 "Return a list of symbols exported by a PATH string.
248 By converting it into a pathname."
250 (list-exports (pathname path)))
252 (defun list-exported-symbols (package)
253 "Return a sorted list of symbols exported by PACKAGE."
254 (sort (loop for s being the external-symbols of package collect s)
255 #'string< :key #'symbol-name))
257 (defun list-all-symbols (package)
258 "Return a sorted list of all symbols exported by or private to PACKAGE."
259 (let ((externs (make-hash-table)))
260 (dolist (sym (list-exported-symbols package))
261 (setf (gethash sym externs) t))
262 (sort (loop for s being the symbols of package
263 when (or (not (exported-symbol-p s))
266 #'string< :key #'symbol-name)))
268 (defun find-symbol-homes (paths package)
269 "Determine the `home' file for the symbols exported by PACKAGE.
271 Return an alist of pairs (PATH . SYMBOL) listing each SYMBOL exported by a
272 PATH of the form PATHNAME/*.lisp where PATHNAME is a member of PATHS. Do
273 this by finding all the files and parsing them (somewhat superficially),
274 and cross-checking the result against the actual symbols exported by the
277 ;; Building the alist is exactly what `list-exports' is for. The rest of
278 ;; this function is the cross-checking.
279 (let* ((symbols (list-exported-symbols package))
280 (exports-alist (let ((*package* package))
281 (mapcan #'list-exports paths)))
282 (homes (make-hash-table :test #'equal)))
284 ;; Work through the alist recording where we found each symbol. Check
285 ;; that they're actually exported by poking at the package.
286 (dolist (assoc exports-alist)
287 (let ((home (car assoc)))
288 (dolist (symbol (cdr assoc))
289 (let ((name (symbol-name symbol)))
290 (unless (nth-value 1 (find-symbol name package))
291 (format *error-output* ";; unexported: ~S~%" symbol))
292 (setf (gethash name homes) home)))))
294 ;; Check that all of the symbols exported by the package are accounted
296 (dolist (symbol symbols)
297 (unless (gethash (symbol-name symbol) homes)
298 (format *error-output* ";; mysterious: ~S~%" symbol)))
303 ;;;--------------------------------------------------------------------------
304 ;;; Determining the kinds of definitions attached to symbols.
306 (defun boring-setf-expansion-p (symbol)
307 "Return non-nil if SYMBOL has a trivial `setf' expansion.
309 i.e., (setf (SYMBOL ...) ...) works by (funcall #'(setf SYMBOL) ...)."
311 (multiple-value-bind (temps args stores store fetch)
312 (ignore-errors (get-setf-expansion (list symbol)))
313 (declare (ignore temps args stores fetch))
315 (eq (car store) 'funcall)
316 (consp (cdr store)) (consp (cadr store))
317 (eq (caadr store) 'function)
318 (let ((func (cadadr store)))
319 (and (consp func) (consp (cdr func))
320 (eq (car func) 'setf))))))
322 (defun specialized-on-p (func arg what)
323 "Check whether FUNC has a method specialized for the symbol WHAT.
325 We assume FUNC is a (well-known) generic function. ARG is a small integer
326 identifying one of FUNC's mandatory arguments. Return non-nil if FUNC has
327 a method for which this ARG is `eql'-specialized on WHAT."
329 (some (lambda (method)
330 (let ((spec (nth arg (method-specializers method))))
331 (and (typep spec 'eql-specializer)
332 (eql (eql-specializer-object spec) what))))
333 (generic-function-methods func)))
335 (defun categorize (symbol)
336 "Determine what things SYMBOL is defined to do.
338 Return a list of keywords:
340 * :constant -- SYMBOL's value cell is `boundp' and `constantp'
341 * :variable -- SYMBOL's value cell is `boundp' but not `constantp'
342 * :macro -- SYMBOL's function cell is `macro-function'
343 * :generic -- SYMBOL's function cell is a `generic-function'
344 * :function -- SYMBOL's function cell is a non-generic `function'
345 * :setf-generic -- (setf SYMBOL) is a `generic-function'
346 * :setf-function -- (setf SYMBOL) is a non-generic `function'
347 * :class -- SYMBOL is `find-class'
348 * :c-type -- `expand-c-type-spec' or `expand-c-type-form' has a method
349 specialized on SYMBOL
350 * :parser -- `expand-parser-spec' or `expand-parser-form' has a method
351 specialized on SYMBOL
352 * :opthandler -- SYMBOL has an `opthandler' property
353 * :optmacro -- SYMBOL has an `optmacro' property
355 categorizing the kinds of definitions that SYMBOL has."
358 (when (or (boundp symbol) (documentation symbol 'variable))
359 (push (if (constantp symbol) :constant :variable) things))
360 (when (or (fboundp symbol) (documentation symbol 'function))
361 (push (cond ((macro-function symbol) :macro)
362 ((typep (fdefinition symbol) 'generic-function)
366 (etypecase (ignore-errors (fdefinition (list 'setf symbol)))
367 (generic-function (push :setf-generic things))
368 (function (push :setf-function things))
370 (when (or (find-class symbol nil) (documentation symbol 'type))
371 (push (if (find-class symbol nil) :class :type) things))
372 (when (specialized-on-p #'sod:expand-c-type-spec 0 symbol)
373 (push :c-type-spec things))
374 (when (specialized-on-p #'sod:expand-c-type-form 0 symbol)
375 (push :c-type-form things))
376 (when (specialized-on-p #'sod:expand-c-storage-specifier 0 symbol)
377 (push :c-storage-spec things))
378 (when (specialized-on-p #'sod:expand-c-storage-specifier-form 0 symbol)
379 (push :c-storage-form things))
380 (when (specialized-on-p #'sod-parser:expand-parser-spec 1 symbol)
381 (push :parser-spec things))
382 (when (specialized-on-p #'sod-parser:expand-parser-form 1 symbol)
383 (push :parser-form things))
384 (when (get symbol 'optparse::opthandler-function)
385 (push :opthandler things))
386 (when (get symbol 'optparse::optmacro-function)
387 (push :optmacro things))
390 (defun categorize-symbols (paths package)
391 "Return a categorized list of the symbols exported by PACKAGE.
393 Return an alist of PAIRS (PATH . SYMBOLS), for each PATH in PATHS, where
394 SYMBOLS is itself an alist (SYMBOL . KEYWORDS) listing the kinds of
395 definitions that SYMBOL has (see `categorize')."
396 (mapcar (lambda (assoc)
397 (let ((home (car assoc))
398 (symbols (delete-duplicates
399 (sort (mapcan (lambda (sym)
405 (and foundp (list symbol))))
407 #'string< :key #'symbol-name))))
408 (cons home (mapcar (lambda (symbol)
409 (cons symbol (categorize symbol)))
411 (find-symbol-homes paths package)))
413 ;;;--------------------------------------------------------------------------
416 (defun best-package-name (package)
417 "Return a convenient name for PACKAGE."
419 ;; We pick the shortest one. Strangely, there's no `find minimal thing
420 ;; according to this valuation' function in Common Lisp.
421 (loop with best = (package-name package)
422 with best-length = (length best)
423 for name in (package-nicknames package)
424 for name-length = (length name)
425 when (< name-length best-length)
427 best-length name-length)
428 finally (return best)))
430 (defvar charbuf-size 0)
432 (defun exported-symbol-p (symbol &optional (package (symbol-package symbol)))
433 "Return whether SYMBOL is exported by PACKAGE.
435 PACKAGE default's to the SYMBOL's home package, but may be different."
437 (multiple-value-bind (sym how)
438 (find-symbol (symbol-name symbol) package)
440 (eq how :external)))))
442 (defun downcase-or-escape (name)
443 "Return a presentable form for a symbol or package name.
445 If NAME consists only of uppercase letters and ordinary punctuation, then
446 return NAME in lowercase; otherwise wrap it in `|...|' and escape as
449 (if (every (lambda (char)
450 (or (upper-case-p char)
452 (member char '(#\% #\+ #\- #\* #\/ #\= #\[ #\] #\?))))
454 (string-downcase name)
455 (with-output-to-string (out)
457 (map nil (lambda (char)
458 (when (or (char= char #\|)
460 (write-char #\\ out))
461 (write-char char out))
463 (write-char #\| out))))
465 (defun pretty-symbol-name (symbol package)
466 "Return a presentable form for SYMBOL, relative to PACKAGE.
468 If SYMBOL is exported by PACKAGE then just write the SYMBOL's name
469 otherwise prefix the name with the SYMBOL's home package name, separated
470 joined with one or two colons. Uninterned symbols and keywords are also
473 (let ((pkg (symbol-package symbol))
474 (exportp (exported-symbol-p symbol)))
475 (format nil "~:[~A:~:[:~;~]~;~2*~]~A"
476 (and exportp (eq pkg package))
477 (cond ((keywordp symbol) "")
479 (t (downcase-or-escape (best-package-name pkg))))
480 (or exportp (null pkg))
481 (downcase-or-escape (symbol-name symbol)))))
483 (deftype interesting-class ()
484 "The type of `interesting' classes, which might be user-defined."
487 #.(class-name (class-of (find-class 'condition)))))
489 (defun analyse-classes (package)
490 "Print a report on the classes defined by PACKAGE."
492 ;; Canonify PACKAGE into a package object.
493 (setf package (find-package package))
495 (let ((classes (mapcan (lambda (symbol)
496 (let ((class (find-class symbol nil)))
498 (typep class 'interesting-class)
500 (list-exported-symbols package)))
501 (subs (make-hash-table)))
502 ;; CLASSES is a list of the `interesting' classes defined by (i.e., whose
503 ;; names are exported by) PACKAGE. SUBS maps a class to those of its
504 ;; direct subclasses which are relevant to our report.
506 ;; Populate the SUBS table.
507 (let ((done (make-hash-table)))
508 (labels ((walk-up (class)
509 (unless (gethash class done)
510 (dolist (super (class-direct-superclasses class))
511 (push class (gethash super subs))
513 (setf (gethash class done) t))))
514 (dolist (class classes)
517 (labels ((walk-down (this super depth)
518 ;; Recursively traverse the class graph from THIS, recalling
519 ;; that our parent is SUPER, and that we are DEPTH levels
522 (format t "~v,0T~A~@[ [~{~A~^ ~}]~]~%"
524 (pretty-symbol-name (class-name this) package)
525 (mapcar (lambda (class)
526 (pretty-symbol-name (class-name class)
529 (class-direct-superclasses this))))
530 (dolist (sub (sort (copy-list (gethash this subs))
531 #'string< :key #'class-name))
532 (walk-down sub this (1+ depth)))))
534 ;; Print the relevant fragment of the class graph.
535 (walk-down (find-class t) nil 0))))
537 (defmacro deep-compare ((left right) &body body)
538 "Helper macro for traversing two similar objects in parallel.
540 Specifically it's good at defining complex structural ordering relations,
541 answering the question: is the LEFT value strictly less than the RIGHT
544 Evaluate the BODY forms, maintaining a pair of `cursors', initially at the
545 LEFT and RIGHT values.
547 The following local macros are defined to do useful things.
549 * (focus EXPR . BODY) -- EXPR is an expression in terms of `it': advance
550 each of the cursors to the result of evaluating this expression, with
551 `it' bound to the current cursor value, and evaluate the BODY in the
552 resulting environment.
554 * (update EXPR) -- as `focus', but mutate the cursors rather than
557 * (compare EXPR) -- EXPR is an expression in terms of the literal
558 symbols `left' and `right', which returns non-nil if it thinks `left'
559 is (strictly) less than `right' in some sense: evaluate this both ways
560 round, and return if LEFT is determined to be less than or greater
563 * (typesw (TYPE . BODY)*) -- process each clause in turn: if the left
564 cursor value has TYPE, but the right does not, then LEFT is less than
565 RIGHT; if the right cursor value has TYPE but the left does not, then
566 LEFT is greater than RIGHT; otherwise, evaluate BODY."
568 (let ((block (gensym "BLOCK-")) (func (gensym "FUNC-"))
569 (l (gensym "LEFT-")) (r (gensym "RIGHT-")))
570 `(macrolet ((focus (expr &body body)
571 `(flet ((,',func (it) ,expr))
572 (let ((,',l (,',func ,',l))
573 (,',r (,',func ,',r)))
576 `(flet ((,',func (it) ,expr))
577 (psetf ,',l (,',func ,',l)
578 ,',r (,',func ,',r))))
580 `(cond ((let ((left ,',l) (right ,',r)) ,expr)
581 (return-from ,',block t))
582 ((let ((right ,',l) (left ,',r)) ,expr)
583 (return-from ,',block nil))))
584 (typesw (&rest clauses)
585 (labels ((iter (clauses)
588 (destructuring-bind (type &rest body)
592 `(if (typep ,',l ',type)
593 (if (typep ,',r ',type)
595 (return-from ,',block t))
596 (if (typep ,',r ',type)
597 (return-from ,',block nil)
598 ,(iter (cdr clauses)))))))))
600 (let ((,l ,left) (,r ,right))
604 (defun order-specializers (la lb)
605 "Return whether specializers LA should be sorted before LB."
607 (deep-compare (la lb)
608 ;; Iterate over the two lists. The cursors advance down the spine, and
609 ;; we focus on each car in turn.
612 (typesw (null (return nil)))
613 ;; If one list reaches the end, then it's lesser; if both, they're
617 ;; Examine the two specializers at this position.
619 (typesw (eql-specializer
620 (focus (eql-specializer-object it)
621 ;; We found an `eql' specializer. Compare the objects.
624 ;; Keywords compare by name.
626 (compare (string< left right)))
629 ;; Symbols compare by package and name.
631 (focus (package-name (symbol-package it))
632 (compare (string< left right)))
633 (compare (string< left right)))
636 ;; Compare two other objects by comparing their
637 ;; string representations.
639 (focus (with-output-to-string (out)
642 (compare (string< left right)))))))
645 ;; We found a class, Compare the class names.
646 (focus (class-name it)
647 (focus (package-name (symbol-package it))
648 (compare (string< left right)))
649 (compare (string< left right))))
652 ;; We found some other kind of specializer that we don't
655 (error "unexpected things"))))
657 ;; No joy with that pair of specializers: try the next.
660 (defun analyse-generic-functions (package)
661 "Print a report of the generic functions and methods defined by PACKAGE."
663 ;; Canonify package into a package object.
664 (setf package (find-package package))
666 (flet ((function-name-core (name)
667 ;; Return the underlying name for a function NAME. Specifically,
668 ;; if NAME is (setf THING) then the core is THING; if NAME is a
669 ;; symbol then the core is simply NAME; otherwise we're confused.
670 ;; Return a second value to say whether we got the job done.
673 (symbol (values name t))
674 ((cons (eql setf) t) (values (cadr name) t))
675 (t (values nil nil)))))
677 (let ((methods (make-hash-table))
678 (functions (make-hash-table))
679 (externs (make-hash-table)))
680 ;; EXTERNS is a set of the symbols exported by PACKAGE. FUNCTIONS and
681 ;; METHODS are sets of generic function names (not cores), and method
682 ;; objects, which we've decided are worth reporting.
684 ;; Collect the EXTERNS symbols.
685 (dolist (symbol (list-exported-symbols package))
686 (setf (gethash symbol externs) t))
688 ;; Collect the FUNCTIONS and METHODS.
689 (dolist (symbol (list-exported-symbols package))
691 ;; Mark the generic functions and `setf'-functions named by exported
692 ;; symbols as interesting, along with all of their methods.
693 (flet ((dofunc (func)
694 (when (typep func 'generic-function)
695 (setf (gethash func functions) t)
696 (dolist (method (generic-function-methods func))
697 (setf (gethash method methods) t)))))
698 (dofunc (and (fboundp symbol) (fdefinition symbol)))
699 (dofunc (ignore-errors (fdefinition (list 'setf symbol)))))
701 ;; For symbols whose home package is PACKAGE, and which name a class,
702 ;; also collect functions with methods specialized on that class, and
703 ;; (only) the specialized methods.
704 (when (eq (symbol-package symbol) package)
705 (let ((class (find-class symbol nil)))
707 (dolist (func (specializer-direct-generic-functions class))
708 (multiple-value-bind (name knownp)
709 (function-name-core (generic-function-name func))
711 (or (not (eq (symbol-package name) package))
712 (gethash name externs)))
713 (setf (gethash func functions) t)
714 (dolist (method (specializer-direct-methods class))
715 (setf (gethash method methods) t)))))))))
718 (let ((funclist nil))
720 ;; Gather the functions we've decided are interesting, and sort them.
721 (maphash (lambda (func value)
722 (declare (ignore value))
723 (push func funclist))
725 (setf funclist (sort funclist
727 ;; Sort by the core symbols, and order the
728 ;; `setf' variant after the base version.
729 (let ((core-a (function-name-core a))
730 (core-b (function-name-core b)))
731 (if (eq core-a core-b)
732 (and (atom a) (consp b))
733 (string< core-a core-b))))
734 :key #'generic-function-name))
736 (dolist (function funclist)
737 ;; Print out each function in turn.
739 ;; Print the header line.
740 (let ((name (generic-function-name function)))
743 (format t "~A~%" (pretty-symbol-name name package)))
745 (format t "(setf ~A)~%"
746 (pretty-symbol-name (cadr name) package)))))
748 ;; Report on the function's (interesting) methods.
749 (dolist (method (sort (copy-list
750 (generic-function-methods function))
752 :key #'method-specializers))
754 (when (gethash method methods)
755 (format t "~2T~{~A~^ ~}~@[ [~{~(~S~)~^ ~}]~]~%"
760 (let ((name (class-name spec)))
762 (pretty-symbol-name name package))))
764 (let ((obj (eql-specializer-object spec)))
765 (format nil "(eql ~A)"
767 (pretty-symbol-name obj package)
769 (method-specializers method))
770 (method-qualifiers method)))))))))
772 (defun check-slot-names (package)
773 "Check that PACKAGE defines no slots whose names are exported symbols.
775 This acts to discourage the use of `slot-value' by external callers.
778 * an alist of entries (CLASS . SLOT-NAMES), listing for each offending
779 class, whose of its slot names which are either (a) external or (b)
780 from a foreign package; and
782 * the distilled list of bad SLOT-NAMES."
784 ;; Canonify PACKAGE into a package objects.
785 (setf package (find-package package))
787 (let* ((symbols (list-all-symbols package))
789 ;; Determine all of the named classes.
790 (classes (mapcan (lambda (symbol)
791 (when (eq (symbol-package symbol) package)
792 (let ((class (find-class symbol nil)))
793 (and class (list class)))))
796 ;; Build the main alist of offending classes and slots.
800 (mapcar #'slot-definition-name
801 (class-direct-slots class)))
804 (or (not (symbol-package sym))
805 (and (not (exported-symbol-p
807 (eq (symbol-package sym)
811 (list (cons (class-name class)
815 ;; Distill the bad slot names into a separate list.
816 (bad-words (remove-duplicates (mapcan (lambda (list)
817 (copy-list (cdr list)))
821 (values offenders bad-words)))
823 (defun report-symbols (paths package)
824 "Report on all of the symbols defined in PACKAGE by the files in PATHS."
826 ;; Canonify PACKAGE to a package object.
827 (setf package (find-package package))
829 ;; Print the breakdown of symbols by source file, with their purposes.
830 (format t "~A~%Package `~(~A~)'~2%"
831 (make-string 77 :initial-element #\-)
832 (package-name package))
833 (dolist (assoc (sort (categorize-symbols paths package) #'string<
835 (file-namestring (car assoc)))))
837 (format t "~A~%" (file-namestring (car assoc)))
838 (dolist (def (cdr assoc))
839 (let ((sym (car def)))
840 (format t " ~A~@[~48T~{~(~A~)~^ ~}~]~%"
841 (pretty-symbol-name sym package)
845 ;; Report on leaked slot names, if any are exported or foreign.
846 (multiple-value-bind (alist names) (check-slot-names package)
848 (format t "Leaked slot names: ~{~A~^, ~}~%"
849 (mapcar (lambda (name) (pretty-symbol-name name package))
851 (dolist (assoc alist)
852 (format t "~2T~A: ~{~A~^, ~}~%"
853 (pretty-symbol-name (car assoc) package)
854 (mapcar (lambda (name) (pretty-symbol-name name package))
858 ;; Report on classes and generic functions.
859 (format t "Classes:~%")
860 (analyse-classes package)
862 (format t "Methods:~%")
863 (analyse-generic-functions package)
866 (export 'report-project-symbols)
867 (defun report-project-symbols ()
868 "Write to `*standard-output*' a report on all of the symbols in Sod."
870 (labels ((components (comp)
871 ;; Return the subcomponents of an ASDF component.
873 (asdf:component-children comp))
876 ;; Return a list of files needed by an ASDF component.
878 (sort (remove-if-not (lambda (comp)
879 (typep comp 'asdf:cl-source-file))
881 #'string< :key #'asdf:component-name))
884 ;; Find the subcomponent called NAME of an ASDF component.
886 (gethash name (asdf:component-children-by-name comp)))
889 ;; Return the pathname of an ASDF file component.
891 (slot-value file 'asdf/component:absolute-pathname)))
893 (let* ((sod (asdf:find-system "sod"))
894 (parser-files (files (by-name sod "parser")))
895 (utilities (by-name sod "utilities"))
896 (sod-frontend (asdf:find-system "sod/frontend"))
897 (optparse (by-name sod "optparse"))
898 (frontend (by-name sod-frontend "frontend"))
899 (sod-files (set-difference (files sod) (list optparse utilities))))
901 ;; Report on the various major pieces of the project.
902 (report-symbols (mapcar #'file-name sod-files) "SOD")
903 (report-symbols (mapcar #'file-name (list frontend)) "SOD-FRONTEND")
904 (report-symbols (mapcar #'file-name parser-files) "SOD-PARSER")
905 (report-symbols (mapcar #'file-name (list optparse)) "OPTPARSE")
906 (report-symbols (mapcar #'file-name (list utilities)) "SOD-UTILITIES"))))
908 ;;;--------------------------------------------------------------------------
909 ;;; Command-line use.
912 "Write a report to *standard-output*."
913 (report-project-symbols))
915 #+runlisp-script (main)
917 ;;;----- That's all, folks --------------------------------------------------