1 ;;;; machinery for reporting errors/warnings/notes/whatnot from
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;;; compiler error context determination
17 (declaim (special *current-path*))
19 ;;; We bind print level and length when printing out messages so that
20 ;;; we don't dump huge amounts of garbage.
22 ;;; FIXME: It's not possible to get the defaults right for everyone.
23 ;;; So: Should these variables be in the SB-EXT package? Or should we
24 ;;; just get rid of them completely and just use the bare
25 ;;; CL:*PRINT-FOO* variables instead?
26 (declaim (type (or unsigned-byte null)
27 *compiler-error-print-level*
28 *compiler-error-print-length*
29 *compiler-error-print-lines*))
30 (defvar *compiler-error-print-level* 5
32 "the value for *PRINT-LEVEL* when printing compiler error messages")
33 (defvar *compiler-error-print-length* 10
35 "the value for *PRINT-LENGTH* when printing compiler error messages")
36 (defvar *compiler-error-print-lines* 12
38 "the value for *PRINT-LINES* when printing compiler error messages")
40 (defvar *enclosing-source-cutoff* 1
42 "The maximum number of enclosing non-original source forms (i.e. from
43 macroexpansion) that we print in full. For additional enclosing forms, we
45 (declaim (type unsigned-byte *enclosing-source-cutoff*))
47 ;;; We separate the determination of compiler error contexts from the
48 ;;; actual signalling of those errors by objectifying the error
49 ;;; context. This allows postponement of the determination of how (and
50 ;;; if) to signal the error.
52 ;;; We take care not to reference any of the IR1 so that pending
53 ;;; potential error messages won't prevent the IR1 from being GC'd. To
54 ;;; this end, we convert source forms to strings so that source forms
55 ;;; that contain IR1 references (e.g. %DEFUN) don't hold onto the IR.
56 (defstruct (compiler-error-context
57 #-no-ansi-print-object
58 (:print-object (lambda (x stream)
59 (print-unreadable-object (x stream :type t))))
61 ;; a list of the stringified CARs of the enclosing non-original source forms
62 ;; exceeding the *enclosing-source-cutoff*
63 (enclosing-source nil :type list)
64 ;; a list of stringified enclosing non-original source forms
65 (source nil :type list)
66 ;; the stringified form in the original source that expanded into SOURCE
67 (original-source (missing-arg) :type simple-string)
68 ;; a list of prefixes of "interesting" forms that enclose original-source
69 (context nil :type list)
70 ;; the FILE-INFO-NAME for the relevant FILE-INFO
71 (file-name (missing-arg) :type (or pathname (member :lisp :stream)))
72 ;; the file position at which the top level form starts, if applicable
73 (file-position nil :type (or index null))
74 ;; the original source part of the source path
75 (original-source-path nil :type list))
77 ;;; If true, this is the node which is used as context in compiler warning
79 (declaim (type (or null compiler-error-context node) *compiler-error-context*))
80 (defvar *compiler-error-context* nil)
82 ;;; a hashtable mapping macro names to source context parsers. Each parser
83 ;;; function returns the source-context list for that form.
84 (defvar *source-context-methods* (make-hash-table))
86 ;;; documentation originally from cmu-user.tex:
87 ;;; This macro defines how to extract an abbreviated source context from
88 ;;; the \var{name}d form when it appears in the compiler input.
89 ;;; \var{lambda-list} is a \code{defmacro} style lambda-list used to
90 ;;; parse the arguments. The \var{body} should return a list of
91 ;;; subforms that can be printed on about one line. There are
92 ;;; predefined methods for \code{defstruct}, \code{defmethod}, etc. If
93 ;;; no method is defined, then the first two subforms are returned.
94 ;;; Note that this facility implicitly determines the string name
95 ;;; associated with anonymous functions.
96 ;;; So even though SBCL itself only uses this macro within this file,
97 ;;; it's a reasonable thing to put in SB-EXT in case some dedicated
98 ;;; user wants to do some heavy tweaking to make SBCL give more
99 ;;; informative output about his code.
100 (defmacro define-source-context (name lambda-list &body body)
102 "DEFINE-SOURCE-CONTEXT Name Lambda-List Form*
103 This macro defines how to extract an abbreviated source context from the
104 Named form when it appears in the compiler input. Lambda-List is a DEFMACRO
105 style lambda-list used to parse the arguments. The Body should return a
106 list of subforms suitable for a \"~{~S ~}\" format string."
107 (let ((n-whole (gensym)))
108 `(setf (gethash ',name *source-context-methods*)
110 (destructuring-bind ,lambda-list ,n-whole ,@body)))))
112 (defmacro def-source-context (&rest rest)
113 (deprecation-warning 'def-source-context 'define-source-context)
114 `(define-source-context ,@rest))
116 (define-source-context defstruct (name-or-options &rest slots)
117 (declare (ignore slots))
118 `(defstruct ,(if (consp name-or-options)
119 (car name-or-options)
122 (define-source-context function (thing)
123 (if (and (consp thing) (eq (first thing) 'lambda) (consp (rest thing)))
124 `(lambda ,(second thing))
127 ;;; Return the first two elements of FORM if FORM is a list. Take the
128 ;;; CAR of the second form if appropriate.
129 (defun source-form-context (form)
130 (cond ((atom form) nil)
131 ((>= (length form) 2)
132 (let* ((context-fun-default (lambda (x)
134 (list (first form) (second form))))
135 (context-fun (gethash (first form)
136 *source-context-methods*
137 context-fun-default)))
138 (declare (type function context-fun))
139 (funcall context-fun (rest form))))
143 ;;; Given a source path, return the original source form and a
144 ;;; description of the interesting aspects of the context in which it
145 ;;; appeared. The context is a list of lists, one sublist per context
146 ;;; form. The sublist is a list of some of the initial subforms of the
149 ;;; For now, we use the first two subforms of each interesting form. A
150 ;;; form is interesting if the first element is a symbol beginning
151 ;;; with "DEF" and it is not the source form. If there is no
152 ;;; DEF-mumble, then we use the outermost containing form. If the
153 ;;; second subform is a list, then in some cases we return the CAR of
154 ;;; that form rather than the whole form (i.e. don't show DEFSTRUCT
156 (defun find-original-source (path)
157 (declare (list path))
158 (let* ((rpath (reverse (source-path-original-source path)))
160 (root (find-source-root tlf *source-info*)))
163 (current (rest rpath)))
166 (aver (null current))
168 (let ((head (first form)))
170 (let ((name (symbol-name head)))
171 (when (and (>= (length name) 3) (string= name "DEF" :end1 3))
172 (context (source-form-context form))))))
173 (when (null current) (return))
174 (setq form (nth (pop current) form)))
177 (values form (context)))
179 (let ((c (source-form-context root)))
180 (values form (if c (list c) nil))))
182 (values '(unable to locate source)
183 '((some strange place)))))))))
185 ;;; Convert a source form to a string, suitably formatted for use in
186 ;;; compiler warnings.
187 (defun stringify-form (form &optional (pretty t))
188 (with-standard-io-syntax
189 (let ((*print-readably* nil)
190 (*print-pretty* pretty)
191 (*print-level* *compiler-error-print-level*)
192 (*print-length* *compiler-error-print-length*)
193 (*print-lines* *compiler-error-print-lines*))
195 (format nil "~<~@; ~S~:>" (list form))
196 (prin1-to-string form)))))
198 ;;; shorthand for a repeated idiom in creating debug names
200 ;;; the problem, part I: We want to create debug names that look like
201 ;;; "&MORE processor for <something>" where <something> might be
202 ;;; either a source-name value (typically a symbol) or a non-symbol
203 ;;; debug-name value (typically a string). It's awkward to handle this
204 ;;; with FORMAT because we'd like to splice a source-name value using
205 ;;; "~S" (to get package qualifiers) but a debug-name value using "~A"
206 ;;; (to avoid irrelevant quotes at string splice boundaries).
208 ;;; the problem, part II: The <something> is represented as a pair
209 ;;; of values, SOURCE-NAME and DEBUG-NAME, where SOURCE-NAME is used
210 ;;; if it's not .ANONYMOUS. (This is parallel to the way that ordinarily
211 ;;; we don't use a value if it's NIL, instead defaulting it. But we
212 ;;; can't safely/comfortably use NIL for that in this context, since
213 ;;; the app programmer can use NIL as a name, so we use the private
214 ;;; symbol .ANONYMOUS. instead.)
216 ;;; the solution: Use this function to convert whatever it is to a
217 ;;; string, which FORMAT can then splice using "~A".
218 (defun as-debug-name (source-name debug-name)
219 (if (eql source-name '.anonymous.)
221 (debug-namify "~S" source-name)))
223 ;;; Return a COMPILER-ERROR-CONTEXT structure describing the current
224 ;;; error context, or NIL if we can't figure anything out. ARGS is a
225 ;;; list of things that are going to be printed out in the error
226 ;;; message, and can thus be blown off when they appear in the source
228 (defun find-error-context (args)
229 (let ((context *compiler-error-context*))
230 (if (compiler-error-context-p context)
232 (let ((path (or (and (boundp '*current-path*) *current-path*)
234 (node-source-path context)
236 (when (and *source-info* path)
237 (multiple-value-bind (form src-context) (find-original-source path)
238 (collect ((full nil cons)
240 (let ((forms (source-path-forms path))
242 (dolist (src (if (member (first forms) args)
245 (if (>= n *enclosing-source-cutoff*)
246 (short (stringify-form (if (consp src)
250 (full (stringify-form src)))
253 (let* ((tlf (source-path-tlf-number path))
254 (file-info (source-info-file-info *source-info*)))
255 (make-compiler-error-context
256 :enclosing-source (short)
258 :original-source (stringify-form form)
260 :file-name (file-info-name file-info)
262 (multiple-value-bind (ignore pos)
263 (find-source-root tlf *source-info*)
264 (declare (ignore ignore))
266 :original-source-path
267 (source-path-original-source path))))))))))
269 ;;;; printing error messages
271 ;;; We save the context information that we printed out most recently
272 ;;; so that we don't print it out redundantly.
274 ;;; The last COMPILER-ERROR-CONTEXT that we printed.
275 (defvar *last-error-context* nil)
276 (declaim (type (or compiler-error-context null) *last-error-context*))
278 ;;; The format string and args for the last error we printed.
279 (defvar *last-format-string* nil)
280 (defvar *last-format-args* nil)
281 (declaim (type (or string null) *last-format-string*))
282 (declaim (type list *last-format-args*))
284 ;;; The number of times that the last error message has been emitted,
285 ;;; so that we can compress duplicate error messages.
286 (defvar *last-message-count* 0)
287 (declaim (type index *last-message-count*))
289 ;;; If the last message was given more than once, then print out an
290 ;;; indication of how many times it was repeated. We reset the message
291 ;;; count when we are done.
292 (defun note-message-repeats (&optional (terpri t))
293 (cond ((= *last-message-count* 1)
294 (when terpri (terpri *error-output*)))
295 ((> *last-message-count* 1)
296 (format *error-output* "~&; [Last message occurs ~W times.]~2%"
297 *last-message-count*)))
298 (setq *last-message-count* 0))
300 ;;; Print out the message, with appropriate context if we can find it.
301 ;;; If the context is different from the context of the last message
302 ;;; we printed, then we print the context. If the original source is
303 ;;; different from the source we are working on, then we print the
304 ;;; current source in addition to the original source.
306 ;;; We suppress printing of messages identical to the previous, but
307 ;;; record the number of times that the message is repeated.
308 (defun print-compiler-message (format-string format-args)
310 (declare (type simple-string format-string))
311 (declare (type list format-args))
313 (let ((stream *error-output*)
314 (context (find-error-context format-args)))
317 (let ((file (compiler-error-context-file-name context))
318 (in (compiler-error-context-context context))
319 (form (compiler-error-context-original-source context))
320 (enclosing (compiler-error-context-enclosing-source context))
321 (source (compiler-error-context-source context))
322 (last *last-error-context*))
325 (equal file (compiler-error-context-file-name last)))
326 (when (pathnamep file)
327 (note-message-repeats)
329 (format stream "~2&; file: ~A~%" (namestring file))))
332 (equal in (compiler-error-context-context last)))
333 (note-message-repeats)
336 (pprint-logical-block (stream nil :per-line-prefix "; ")
337 (format stream "in:~{~<~% ~4:;~{ ~S~}~>~^ =>~}" in))
338 (format stream "~%"))
343 (compiler-error-context-original-source last)))
344 (note-message-repeats)
347 (pprint-logical-block (stream nil :per-line-prefix "; ")
348 (format stream " ~A" form))
349 (format stream "~&"))
353 (compiler-error-context-enclosing-source last)))
355 (note-message-repeats)
357 (format stream "~&; --> ~{~<~%; --> ~1:;~A~> ~}~%" enclosing)))
360 (equal source (compiler-error-context-source last)))
361 (setq *last-format-string* nil)
363 (note-message-repeats)
366 (write-string "; ==>" stream)
368 (pprint-logical-block (stream nil :per-line-prefix "; ")
369 (write-string src stream)))))))
372 (note-message-repeats)
373 (setq *last-format-string* nil)
374 (format stream "~&")))
376 (setq *last-error-context* context)
378 ;; FIXME: this testing for effective equality of compiler messages
379 ;; is ugly, and really ought to be done at a higher level.
380 (unless (and (equal format-string *last-format-string*)
381 (tree-equal format-args *last-format-args*))
382 (note-message-repeats nil)
383 (setq *last-format-string* format-string)
384 (setq *last-format-args* format-args)
385 (let ((*print-level* *compiler-error-print-level*)
386 (*print-length* *compiler-error-print-length*)
387 (*print-lines* *compiler-error-print-lines*))
389 (pprint-logical-block (stream nil :per-line-prefix "; ")
390 (format stream "~&~?" format-string format-args))
391 (format stream "~&"))))
393 (incf *last-message-count*)
396 (defun print-compiler-condition (condition)
397 (declare (type condition condition))
398 (let (;; These different classes of conditions have different
399 ;; effects on the return codes of COMPILE-FILE, so it's nice
400 ;; for users to be able to pick them out by lexical search
401 ;; through the output.
402 (what (etypecase condition
403 (style-warning 'style-warning)
405 ((or error compiler-error) 'error))))
406 (multiple-value-bind (format-string format-args)
407 (if (typep condition 'simple-condition)
408 (values (simple-condition-format-control condition)
409 (simple-condition-format-arguments condition))
411 (list (with-output-to-string (s)
412 (princ condition s)))))
413 (print-compiler-message
414 (format nil "caught ~S:~% ~A" what format-string)
418 ;;; The act of signalling one of these beasts must not cause WARNINGSP
419 ;;; (or FAILUREP) to be set from COMPILE or COMPILE-FILE, so we can't
420 ;;; inherit from WARNING or STYLE-WARNING.
422 ;;; FIXME: the handling of compiler-notes could be unified with
423 ;;; warnings and style-warnings (see the various handler functions
425 (define-condition compiler-note (condition) ()
427 "Root of the hierarchy of conditions representing information discovered
428 by the compiler that the user might wish to know, but which does not merit
429 a STYLE-WARNING (or any more serious condition)."))
430 (define-condition simple-compiler-note (simple-condition compiler-note) ())
431 (define-condition code-deletion-note (simple-compiler-note) ()
433 "A condition type signalled when the compiler deletes code that the user
434 has written, having proved that it is unreachable."))
436 (defun compiler-notify (datum &rest args)
437 (unless (if *compiler-error-context*
438 (policy *compiler-error-context* (= inhibit-warnings 3))
439 (policy *lexenv* (= inhibit-warnings 3)))
441 (coerce-to-condition datum args
442 'simple-compiler-note 'compiler-notify)))
446 (return-from compiler-notify (values))))
447 (incf *compiler-note-count*)
448 (multiple-value-bind (format-string format-args)
449 (if (typep condition 'simple-condition)
450 (values (simple-condition-format-control condition)
451 (simple-condition-format-arguments condition))
453 (list (with-output-to-string (s)
454 (princ condition s)))))
455 (print-compiler-message (format nil "note: ~A" format-string)
459 ;;; Issue a note when we might or might not be in the compiler.
460 (defun maybe-compiler-notify (&rest rest)
461 (if (boundp '*lexenv*) ; if we're in the compiler
462 (apply #'compiler-notify rest)
465 (coerce-to-condition (car rest) (cdr rest)
466 'simple-compiler-note
467 'maybe-compiler-notify)))
471 (return-from maybe-compiler-notify (values))))
472 (let ((stream *error-output*))
473 (pprint-logical-block (stream nil :per-line-prefix ";")
474 (format stream " note: ~3I~_")
475 (pprint-logical-block (stream nil)
476 (format stream "~A" condition)))
477 ;; (outside logical block, no per-line-prefix)
478 (fresh-line stream)))
481 ;;; The politically correct way to print out progress messages and
482 ;;; such like. We clear the current error context so that we know that
483 ;;; it needs to be reprinted, and we also FORCE-OUTPUT so that the
484 ;;; message gets seen right away.
485 (declaim (ftype (function (string &rest t) (values)) compiler-mumble))
486 (defun compiler-mumble (format-string &rest format-args)
487 (note-message-repeats)
488 (setq *last-error-context* nil)
489 (apply #'format *error-output* format-string format-args)
490 (force-output *error-output*)
493 ;;; Return a string that somehow names the code in COMPONENT. We use
494 ;;; the source path for the bind node for an arbitrary entry point to
495 ;;; find the source context, then return that as a string.
496 (declaim (ftype (function (component) simple-string) find-component-name))
497 (defun find-component-name (component)
498 (let ((ep (first (block-succ (component-head component)))))
499 (aver ep) ; else no entry points??
500 (multiple-value-bind (form context)
501 (find-original-source
502 (node-source-path (block-start-node ep)))
503 (declare (ignore form))
504 (let ((*print-level* 2)
505 (*print-pretty* nil))
506 (format nil "~{~{~S~^ ~}~^ => ~}" context)))))
508 ;;;; condition system interface
510 ;;; Keep track of how many times each kind of condition happens.
511 (defvar *compiler-error-count*)
512 (defvar *compiler-warning-count*)
513 (defvar *compiler-style-warning-count*)
514 (defvar *compiler-note-count*)
516 ;;; Keep track of whether any surrounding COMPILE or COMPILE-FILE call
517 ;;; should return WARNINGS-P or FAILURE-P.
519 (defvar *warnings-p*)
521 ;;; condition handlers established by the compiler. We re-signal the
522 ;;; condition, then if it isn't handled, we increment our warning
523 ;;; counter and print the error message.
524 (defun compiler-error-handler (condition)
526 (incf *compiler-error-count*)
529 (print-compiler-condition condition)
530 (continue condition))
531 (defun compiler-warning-handler (condition)
533 (incf *compiler-warning-count*)
536 (print-compiler-condition condition)
537 (muffle-warning condition))
538 (defun compiler-style-warning-handler (condition)
540 (incf *compiler-style-warning-count*)
541 (setf *warnings-p* t)
542 (print-compiler-condition condition)
543 (muffle-warning condition))
545 ;;;; undefined warnings
547 (defvar *undefined-warning-limit* 3
549 "If non-null, then an upper limit on the number of unknown function or type
550 warnings that the compiler will print for any given name in a single
551 compilation. This prevents excessive amounts of output when the real
552 problem is a missing definition (as opposed to a typo in the use.)")
554 ;;; Make an entry in the *UNDEFINED-WARNINGS* describing a reference
555 ;;; to NAME of the specified KIND. If we have exceeded the warning
556 ;;; limit, then just increment the count, otherwise note the current
559 ;;; Undefined types are noted by a condition handler in
560 ;;; WITH-COMPILATION-UNIT, which can potentially be invoked outside
561 ;;; the compiler, hence the BOUNDP check.
562 (defun note-undefined-reference (name kind)
564 ;; Check for boundness so we don't blow up if we're called
565 ;; when IR1 conversion isn't going on.
567 ;; FIXME: I'm pretty sure the INHIBIT-WARNINGS test below
568 ;; isn't a good idea; we should have INHIBIT-WARNINGS
569 ;; affect compiler notes, not STYLE-WARNINGs. And I'm not
570 ;; sure what the BOUNDP '*LEXENV* test above is for; it's
571 ;; likely a good idea, but it probably deserves an
572 ;; explanatory comment.
573 (policy *lexenv* (= inhibit-warnings 3)))
574 (let* ((found (dolist (warning *undefined-warnings* nil)
575 (when (and (equal (undefined-warning-name warning) name)
576 (eq (undefined-warning-kind warning) kind))
579 (make-undefined-warning :name name :kind kind))))
580 (unless found (push res *undefined-warnings*))
581 (when (or (not *undefined-warning-limit*)
582 (< (undefined-warning-count res) *undefined-warning-limit*))
583 (push (find-error-context (list name))
584 (undefined-warning-warnings res)))
585 (incf (undefined-warning-count res))))